Search in sources :

Example 1 with PaymentState

use of uk.gov.pay.products.client.publicapi.model.PaymentState in project pay-products by alphagov.

the class PaymentFinderTest method shouldReturnAList_whenFoundByProductExternalId.

@Test
public void shouldReturnAList_whenFoundByProductExternalId() {
    String productExternalId = randomUuid();
    String paymentExternalId_1 = randomUuid();
    String paymentExternalId_2 = randomUuid();
    PaymentEntity paymentEntity_1 = new PaymentEntity();
    PaymentEntity paymentEntity_2 = new PaymentEntity();
    ProductEntity productEntity = ProductEntityFixture.aProductEntity().build();
    paymentEntity_1.setExternalId(paymentExternalId_1);
    paymentEntity_1.setProductEntity(productEntity);
    paymentEntity_1.setStatus(PaymentStatus.SUBMITTED);
    paymentEntity_1.setGovukPaymentId(randomUuid());
    paymentEntity_2.setExternalId(paymentExternalId_2);
    paymentEntity_2.setProductEntity(productEntity);
    paymentEntity_2.setStatus(PaymentStatus.CREATED);
    paymentEntity_2.setGovukPaymentId(randomUuid());
    List<PaymentEntity> paymentList = Arrays.asList(paymentEntity_1, paymentEntity_2);
    PaymentResponse paymentResponse = new PaymentResponse();
    paymentResponse.setState(new PaymentState("success", true, "a message", "a code"));
    when(paymentDao.findByProductExternalId(productExternalId)).thenReturn(paymentList);
    when(publicApiRestClient.getPayment(productEntity.getPayApiToken(), paymentEntity_1.getGovukPaymentId())).thenReturn(Optional.of(paymentResponse));
    List<Payment> expectedPaymentList = paymentFinder.findByProductExternalId(productExternalId);
    assertThat(expectedPaymentList.isEmpty(), is(false));
    assertThat(expectedPaymentList.size(), is(2));
    Optional<Payment> payment_1 = expectedPaymentList.stream().filter(payment -> payment.getExternalId().equals(paymentExternalId_1)).findFirst();
    assertThat(payment_1.isPresent(), is(true));
    assertThat(payment_1.get().getGovUkStatus(), is("success"));
    Optional<Payment> payment_2 = expectedPaymentList.stream().filter(payment -> payment.getExternalId().equals(paymentExternalId_2)).findFirst();
    assertThat(payment_2.isPresent(), is(true));
    assertThat(payment_2.get().getGovUkStatus(), is(nullValue()));
}
Also used : PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) PublicApiRestClient(uk.gov.pay.products.client.publicapi.PublicApiRestClient) PaymentDao(uk.gov.pay.products.persistence.dao.PaymentDao) Arrays(java.util.Arrays) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ProductEntityFixture(uk.gov.pay.products.fixtures.ProductEntityFixture) RandomIdGenerator.randomUuid(uk.gov.pay.products.util.RandomIdGenerator.randomUuid) ArrayList(java.util.ArrayList) PaymentState(uk.gov.pay.products.client.publicapi.model.PaymentState) PaymentStatus(uk.gov.pay.products.util.PaymentStatus) List(java.util.List) IsNull.nullValue(org.hamcrest.core.IsNull.nullValue) Is.is(org.hamcrest.core.Is.is) Optional(java.util.Optional) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Before(org.junit.Before) Payment(uk.gov.pay.products.model.Payment) Payment(uk.gov.pay.products.model.Payment) PaymentState(uk.gov.pay.products.client.publicapi.model.PaymentState) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) Test(org.junit.Test)

Example 2 with PaymentState

use of uk.gov.pay.products.client.publicapi.model.PaymentState in project pay-products by alphagov.

the class PaymentFinderTest method shouldReturnPaymentWithExternalStatus_whenFoundByExternalIdAndStatusIsSubmitted.

@Test
public void shouldReturnPaymentWithExternalStatus_whenFoundByExternalIdAndStatusIsSubmitted() {
    String externalId = randomUuid();
    PaymentEntity paymentEntity = new PaymentEntity();
    ProductEntity productEntity = ProductEntityFixture.aProductEntity().build();
    paymentEntity.setExternalId(externalId);
    paymentEntity.setProductEntity(productEntity);
    paymentEntity.setStatus(PaymentStatus.SUBMITTED);
    paymentEntity.setGovukPaymentId(externalId);
    PaymentResponse paymentResponse = new PaymentResponse();
    paymentResponse.setState(new PaymentState("submitted", true, "a message", "a code"));
    when(paymentDao.findByExternalId(externalId)).thenReturn(Optional.of(paymentEntity));
    when(publicApiRestClient.getPayment(productEntity.getPayApiToken(), paymentEntity.getGovukPaymentId())).thenReturn(Optional.of(paymentResponse));
    Optional<Payment> optionalPayment = paymentFinder.findByExternalId(externalId);
    assertThat(optionalPayment.isPresent(), is(true));
    Payment payment = optionalPayment.get();
    assertThat(payment.getGovUkStatus(), is("submitted"));
}
Also used : Payment(uk.gov.pay.products.model.Payment) PaymentState(uk.gov.pay.products.client.publicapi.model.PaymentState) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) Test(org.junit.Test)

Example 3 with PaymentState

use of uk.gov.pay.products.client.publicapi.model.PaymentState in project pay-products by alphagov.

the class PaymentResponseMatcher method hasAllPaymentProperties.

public static Matcher<PaymentResponse> hasAllPaymentProperties(final JsonObject expectedPaymentResponse) {
    return new BaseMatcher<>() {

        @Override
        public boolean matches(final Object obj) {
            final PaymentResponse actualPaymentResponse = (PaymentResponse) obj;
            boolean matched = true;
            matched = matched && StringUtils.equals(actualPaymentResponse.getPaymentId(), expectedPaymentResponse.getString("payment_id"));
            matched = matched && (actualPaymentResponse.getAmount() == (long) expectedPaymentResponse.getInt("amount"));
            JsonObject expectedState = expectedPaymentResponse.getJsonObject("state");
            PaymentState actualState = actualPaymentResponse.getState();
            if (actualState == null)
                return false;
            matched = matched && StringUtils.equals(actualState.getStatus(), expectedState.getString("status"));
            matched = matched && (actualState.isFinished() == expectedState.getBoolean("finished"));
            matched = matched && StringUtils.equals(actualState.getMessage(), expectedState.getString("message"));
            matched = matched && StringUtils.equals(actualState.getCode(), expectedState.getString("code"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getDescription(), expectedPaymentResponse.getString("description"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getReference(), expectedPaymentResponse.getString("reference"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getEmail(), expectedPaymentResponse.getString("email"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getEmail(), expectedPaymentResponse.getString("email"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getPaymentProvider(), expectedPaymentResponse.getString("payment_provider"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getReturnUrl(), expectedPaymentResponse.getString("return_url"));
            matched = matched && StringUtils.equals(actualPaymentResponse.getCreatedDate(), expectedPaymentResponse.getString("created_date"));
            JsonObject expectedRefundSummary = expectedPaymentResponse.getJsonObject("refund_summary");
            RefundSummary actualRefundSummary = actualPaymentResponse.getRefundSummary();
            if (actualRefundSummary == null)
                return false;
            matched = matched && StringUtils.equals(actualRefundSummary.getStatus(), expectedRefundSummary.getString("status"));
            matched = matched && (actualRefundSummary.getAmountAvailable() == (long) expectedRefundSummary.getInt("amount_available"));
            matched = matched && (actualRefundSummary.getAmountSubmitted() == (long) expectedRefundSummary.getInt("amount_submitted"));
            JsonObject expectedSettlementSummary = expectedPaymentResponse.getJsonObject("settlement_summary");
            SettlementSummary actualSettlementSummary = actualPaymentResponse.getSettlementSummary();
            if (actualSettlementSummary == null)
                return false;
            matched = matched && StringUtils.equals(actualSettlementSummary.getCaptureSubmitTime(), expectedSettlementSummary.getString("capture_submit_time"));
            matched = matched && StringUtils.equals(actualSettlementSummary.getCapturedDate(), expectedSettlementSummary.getString("captured_date"));
            JsonObject expectedLinks = expectedPaymentResponse.getJsonObject("_links");
            Links actualLinks = actualPaymentResponse.getLinks();
            if (actualLinks == null)
                return false;
            JsonObject expectedSelfLink = expectedLinks.getJsonObject("self");
            Link actualSelfLink = actualLinks.getSelf();
            if (actualSelfLink == null)
                return false;
            matched = matched && StringUtils.equals(actualSelfLink.getHref(), expectedSelfLink.getString("href"));
            matched = matched && StringUtils.equals(actualSelfLink.getMethod(), expectedSelfLink.getString("method"));
            JsonObject expectedNextUrlLink = expectedLinks.getJsonObject("next_url");
            Link actualNextUrlLink = actualLinks.getNextUrl();
            if (actualNextUrlLink == null)
                return false;
            matched = matched && StringUtils.equals(actualNextUrlLink.getHref(), expectedNextUrlLink.getString("href"));
            matched = matched && StringUtils.equals(actualNextUrlLink.getMethod(), expectedNextUrlLink.getString("method"));
            JsonObject expectedNextUrlPostLink = expectedLinks.getJsonObject("next_url_post");
            Link actualNextUrlPostLink = actualLinks.getNextUrlPost();
            if (actualNextUrlPostLink == null)
                return false;
            matched = matched && StringUtils.equals(actualNextUrlPostLink.getHref(), expectedNextUrlPostLink.getString("href"));
            matched = matched && StringUtils.equals(actualNextUrlPostLink.getMethod(), expectedNextUrlPostLink.getString("method"));
            matched = matched && StringUtils.equals(actualNextUrlPostLink.getType(), expectedNextUrlPostLink.getString("type"));
            JsonObject expectedEventsLink = expectedLinks.getJsonObject("events");
            Link actualEventsLink = actualLinks.getEvents();
            if (actualEventsLink == null)
                return false;
            matched = matched && StringUtils.equals(actualEventsLink.getHref(), expectedEventsLink.getString("href"));
            matched = matched && StringUtils.equals(actualEventsLink.getMethod(), expectedEventsLink.getString("method"));
            JsonObject expectedRefundsLink = expectedLinks.getJsonObject("refunds");
            Link actualRefundsLink = actualLinks.getRefunds();
            if (actualRefundsLink == null)
                return false;
            matched = matched && StringUtils.equals(actualRefundsLink.getHref(), expectedRefundsLink.getString("href"));
            matched = matched && StringUtils.equals(actualRefundsLink.getMethod(), expectedRefundsLink.getString("method"));
            JsonObject expectedCancelsLink = expectedLinks.getJsonObject("cancel");
            Link actualCancelLink = actualLinks.getCancel();
            if (actualCancelLink == null)
                return false;
            matched = matched && StringUtils.equals(actualCancelLink.getHref(), expectedCancelsLink.getString("href"));
            matched = matched && StringUtils.equals(actualCancelLink.getMethod(), expectedCancelsLink.getString("method"));
            return matched;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("PaymentResponse ").appendValue(expectedPaymentResponse);
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) RefundSummary(uk.gov.pay.products.client.publicapi.model.RefundSummary) PaymentState(uk.gov.pay.products.client.publicapi.model.PaymentState) JsonObject(javax.json.JsonObject) Links(uk.gov.pay.products.client.publicapi.model.Links) JsonObject(javax.json.JsonObject) SettlementSummary(uk.gov.pay.products.client.publicapi.model.SettlementSummary) Link(uk.gov.pay.products.client.publicapi.model.Link) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse)

Aggregations

PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)3 PaymentState (uk.gov.pay.products.client.publicapi.model.PaymentState)3 Test (org.junit.Test)2 Payment (uk.gov.pay.products.model.Payment)2 PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)2 ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 JsonObject (javax.json.JsonObject)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Is.is (org.hamcrest.core.Is.is)1 IsNull.nullValue (org.hamcrest.core.IsNull.nullValue)1 Before (org.junit.Before)1 RunWith (org.junit.runner.RunWith)1 Mock (org.mockito.Mock)1 Mockito.when (org.mockito.Mockito.when)1