Search in sources :

Example 1 with ProductEntity

use of uk.gov.pay.products.persistence.entity.ProductEntity in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateAMotoPaymentWithCardAgentInitiatedMotoSource_whenProductIsAgentInitiatedMoto.

@Test
public void shouldCreateAMotoPaymentWithCardAgentInitiatedMotoSource_whenProductIsAgentInitiatedMoto() {
    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";
    String userDefinedReference = "user-defined-reference";
    Integer gatewayAccountId = 1;
    String paymentId = "payment-id";
    String paymentExternalId = "random-external-id";
    String paymentNextUrl = "http://next.url";
    SupportedLanguage language = SupportedLanguage.ENGLISH;
    Long priceOverride = 500L;
    ProductEntity productEntity = createProductEntity(productId, ProductType.AGENT_INITIATED_MOTO, productPrice, productExternalId, productName, "", productApiToken, gatewayAccountId, true, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(priceOverride, userDefinedReference, productName, productReturnUrl + "/" + paymentExternalId, language, true, Map.of(), CARD_AGENT_INITIATED_MOTO);
    PaymentResponse paymentResponse = createPaymentResponse(paymentId, priceOverride, paymentNextUrl, productReturnUrl);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    when(randomUuid()).thenReturn(paymentExternalId);
    when(productsConfiguration.getProductsUiConfirmUrl()).thenReturn(productReturnUrl);
    when(publicApiRestClient.createPayment(argThat(is(productApiToken)), argThat(PaymentRequestMatcher.isSame(expectedPaymentRequest)))).thenReturn(paymentResponse);
    Payment payment = paymentCreator.doCreate(productExternalId, priceOverride, userDefinedReference);
    assertNotNull(payment);
    assertNotNull(payment.getExternalId());
    assertThat(payment.getGovukPaymentId(), is(paymentResponse.getPaymentId()));
    assertThat(payment.getAmount(), is(500L));
    assertThat(payment.getReferenceNumber(), is(userDefinedReference));
    PaymentEntity expectedPaymentEntity = createPaymentEntity(paymentId, userDefinedReference, paymentNextUrl, productEntity, SUBMITTED, priceOverride);
    verify(paymentDao).merge(argThat(PaymentEntityMatcher.isSame(expectedPaymentEntity)));
}
Also used : Payment(uk.gov.pay.products.model.Payment) SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ProductEntity

use of uk.gov.pay.products.persistence.entity.ProductEntity in project pay-products by alphagov.

the class PaymentCreatorTest method createProductEntity.

private ProductEntity createProductEntity(int id, ProductType type, long price, String externalId, String name, String returnUrl, String apiToken, Integer gatewayAccountId, Boolean referenceEnabled, SupportedLanguage language) {
    ProductEntity productEntity = createProductEntity(id, price, externalId, name, returnUrl, apiToken, gatewayAccountId, referenceEnabled, language);
    productEntity.setType(type);
    return productEntity;
}
Also used : ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity)

Example 3 with ProductEntity

use of uk.gov.pay.products.persistence.entity.ProductEntity in project pay-products by alphagov.

the class PaymentCreatorTest method shouldThrowPaymentCreationException_whenReferenceEnabledAndNoReferencePresent.

@Test
public void shouldThrowPaymentCreationException_whenReferenceEnabledAndNoReferencePresent() {
    int productId = 1;
    String productExternalId = "product-external-id";
    long productPrice = 100L;
    String productName = "name";
    String productReturnUrl = "https://return.url";
    String productApiToken = "api-token";
    SupportedLanguage language = SupportedLanguage.WELSH;
    Integer gatewayAccountId = 1;
    ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, productReturnUrl, productApiToken, gatewayAccountId, true, language);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    thrown.expect(BadPaymentRequestException.class);
    thrown.expectMessage("User defined reference is enabled but missing");
    paymentCreator.doCreate(productExternalId, null, null);
}
Also used : SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ProductEntity

use of uk.gov.pay.products.persistence.entity.ProductEntity in project pay-products by alphagov.

the class ProductCreatorTest method shouldSuccess_whenProvidedAProductWithMinimumRequiredFields.

@Test
public void shouldSuccess_whenProvidedAProductWithMinimumRequiredFields() {
    Product basicProduct = new Product(null, PRODUCT_NAME, null, payApiToken, PRICE, null, gatewayAccountId, ProductType.DEMO, null, null, null, SupportedLanguage.ENGLISH, null);
    Product product = productCreator.doCreate(basicProduct);
    assertThat(product.getName(), is("Test product name"));
    assertThat(product.getPrice(), is(1050L));
    assertThat(product.getPayApiToken(), is(payApiToken));
    assertThat(product.getGatewayAccountId(), is(gatewayAccountId));
    verify(productDao).persist(persistedProductEntity.capture());
    ProductEntity productEntity = persistedProductEntity.getValue();
    assertThat(productEntity.getName(), is("Test product name"));
    assertThat(productEntity.getPrice(), is(1050L));
    assertThat(productEntity.getPayApiToken(), is(payApiToken));
    assertThat(productEntity.getExternalId(), is(not(emptyOrNullString())));
    assertThat(productEntity.getDateCreated(), is(notNullValue()));
    assertThat(productEntity.getGatewayAccountId(), is(notNullValue()));
    assertThat(productEntity.getGatewayAccountId(), is(gatewayAccountId));
    assertThat(productEntity.getType(), is(notNullValue()));
    assertThat(productEntity.getType(), is(ProductType.DEMO));
    assertThat(productEntity.getLanguage(), is(SupportedLanguage.ENGLISH));
}
Also used : Product(uk.gov.pay.products.model.Product) ProductEntityFixture.aProductEntity(uk.gov.pay.products.fixtures.ProductEntityFixture.aProductEntity) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) Test(org.junit.Test)

Example 5 with ProductEntity

use of uk.gov.pay.products.persistence.entity.ProductEntity in project pay-products by alphagov.

the class ProductCreatorTest method doUpdateByGatewayAccountId_referenceEnabled_shouldUpdateProduct.

@Test
public void doUpdateByGatewayAccountId_referenceEnabled_shouldUpdateProduct() {
    String updatedName = "updated-name";
    String updatedDescription = "updated-description";
    Long updatedPrice = 500L;
    String updatedReferenceLabel = "updated-reference-label";
    String updatedReferenceHint = "updated-reference-hint";
    ProductMetadata metadata = new ProductMetadata("key-1", "value-1");
    ProductUpdateRequest productUpdateRequest = new ProductUpdateRequest(updatedName, updatedDescription, updatedPrice, true, updatedReferenceLabel, updatedReferenceHint, List.of(metadata));
    ProductEntity productEntity = aProductEntity().build();
    when(productDao.findByGatewayAccountIdAndExternalId(gatewayAccountId, externalId)).thenReturn(Optional.of(productEntity));
    Optional<Product> maybeUpdatedProduct = productCreator.doUpdateByGatewayAccountId(gatewayAccountId, externalId, productUpdateRequest);
    assertTrue(maybeUpdatedProduct.isPresent());
    Product updatedProduct = maybeUpdatedProduct.get();
    assertThat(updatedProduct.getName(), is(updatedName));
    assertThat(updatedProduct.getDescription(), is(updatedDescription));
    assertThat(updatedProduct.getPrice(), is(updatedPrice));
    assertThat(updatedProduct.getReferenceEnabled(), is(productUpdateRequest.getReferenceEnabled()));
    assertThat(updatedProduct.getReferenceLabel(), is(updatedReferenceLabel));
    assertThat(updatedProduct.getReferenceHint(), is(updatedReferenceHint));
    assertThat(updatedProduct.getMetadata(), contains(metadata));
    verify(productMetadataDao).deleteForProductExternalId("external-id");
}
Also used : ProductUpdateRequest(uk.gov.pay.products.model.ProductUpdateRequest) Product(uk.gov.pay.products.model.Product) ProductEntityFixture.aProductEntity(uk.gov.pay.products.fixtures.ProductEntityFixture.aProductEntity) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) ProductMetadata(uk.gov.pay.products.model.ProductMetadata) Test(org.junit.Test)

Aggregations

ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)52 Test (org.junit.Test)47 Product (uk.gov.pay.products.model.Product)30 PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)18 ProductEntityFixture.aProductEntity (uk.gov.pay.products.fixtures.ProductEntityFixture.aProductEntity)10 Payment (uk.gov.pay.products.model.Payment)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)8 SupportedLanguage (uk.gov.service.payments.commons.model.SupportedLanguage)8 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)7 PaymentEntityFixture.aPaymentEntity (uk.gov.pay.products.fixtures.PaymentEntityFixture.aPaymentEntity)5 ValidatableResponse (io.restassured.response.ValidatableResponse)4 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)3 PublicApiRestClient (uk.gov.pay.products.client.publicapi.PublicApiRestClient)2 PaymentState (uk.gov.pay.products.client.publicapi.model.PaymentState)2 PaymentCreationException (uk.gov.pay.products.exception.PaymentCreationException)2 ProductUpdateRequest (uk.gov.pay.products.model.ProductUpdateRequest)2 PaymentDao (uk.gov.pay.products.persistence.dao.PaymentDao)2 ProductMetadataEntity (uk.gov.pay.products.persistence.entity.ProductMetadataEntity)2 PaymentStatus (uk.gov.pay.products.util.PaymentStatus)2