Search in sources :

Example 1 with PaymentCreationException

use of uk.gov.pay.products.exception.PaymentCreationException in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateAnErrorPayment_whenPublicApiCallFails.

@Test
public void shouldCreateAnErrorPayment_whenPublicApiCallFails() {
    PowerMockito.mockStatic(RandomIdGenerator.class);
    int productId = 1;
    String productExternalId = "product-external-id";
    long productPrice = 100L;
    String productName = "name";
    String productReturnUrl = "https://return.url";
    String productApiToken = "api-token";
    Integer gatewayAccountId = 1;
    String paymentExternalId = "random-external-id";
    String referenceNumber = createRandomReferenceNumber();
    String productsUIConfirmUri = "https://products-ui/payment-complete";
    String paymentReturnUrl = format("%s/%s", productsUIConfirmUri, paymentExternalId);
    SupportedLanguage language = SupportedLanguage.WELSH;
    ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, productReturnUrl, productApiToken, gatewayAccountId, false, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(productPrice, referenceNumber, productName, paymentReturnUrl, language, false, Map.of(), CARD_PAYMENT_LINK);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    when(randomUuid()).thenReturn(paymentExternalId);
    when(randomUserFriendlyReference()).thenReturn(referenceNumber);
    when(productsConfiguration.getProductsUiConfirmUrl()).thenReturn(productsUIConfirmUri);
    when(publicApiRestClient.createPayment(argThat(is(productApiToken)), argThat(PaymentRequestMatcher.isSame(expectedPaymentRequest)))).thenThrow(PublicApiResponseErrorException.class);
    try {
        paymentCreator.doCreate(productExternalId, null, null);
        fail("Expected an PaymentCreationException to be thrown");
    } catch (PaymentCreationException e) {
        assertThat(e.getProductExternalId(), is(productExternalId));
        PaymentEntity expectedPaymentEntity = createPaymentEntity(null, null, productEntity, ERROR, null);
        verify(paymentDao).merge(argThat(PaymentEntityMatcher.isSame(expectedPaymentEntity)));
    }
}
Also used : SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentCreationException(uk.gov.pay.products.exception.PaymentCreationException) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)1 PaymentCreationException (uk.gov.pay.products.exception.PaymentCreationException)1 PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)1 ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)1 SupportedLanguage (uk.gov.service.payments.commons.model.SupportedLanguage)1