Search in sources :

Example 1 with ValidationException

use of com.ingenico.connect.gateway.sdk.java.ValidationException in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnectionLoggerTest method testCreatePaymentInvalidCardNumber.

@Test
public void testCreatePaymentInvalidCardNumber() throws Exception {
    // an exception is thrown after logging the response
    serverBootstrap.registerHandler("/v1/1234/payments", requestHandler);
    HttpHost host = start();
    Client client = createClient(host);
    TestLogger logger = new TestLogger();
    client.enableLogging(logger);
    setupRequestHandler(setJsonResponse("createPayment.failure.invalidCardNumber.json", 400));
    try {
        AmountOfMoney amountOfMoney = new AmountOfMoney();
        amountOfMoney.setCurrencyCode("CAD");
        amountOfMoney.setAmount(2345L);
        Address billingAddress = new Address();
        billingAddress.setCountryCode("CA");
        Customer customer = new Customer();
        customer.setBillingAddress(billingAddress);
        Order order = new Order();
        order.setAmountOfMoney(amountOfMoney);
        order.setCustomer(customer);
        Card card = new Card();
        card.setCvv("123");
        card.setCardNumber("1234567890123456");
        card.setExpiryDate("1220");
        CardPaymentMethodSpecificInput paymentMethodSpecificInput = new CardPaymentMethodSpecificInput();
        paymentMethodSpecificInput.setPaymentProductId(1);
        paymentMethodSpecificInput.setCard(card);
        CreatePaymentRequest request = new CreatePaymentRequest();
        request.setOrder(order);
        request.setCardPaymentMethodSpecificInput(paymentMethodSpecificInput);
        client.merchant("1234").payments().create(request);
        Assert.fail("expected ValidationException");
    } catch (ValidationException e) {
    // expected
    } finally {
        client.close();
    }
    Assert.assertEquals(2, logger.entries.size());
    TestLoggerEntry requestEntry = logger.entries.get(0);
    Assert.assertNotNull(requestEntry.message);
    Assert.assertNull(requestEntry.thrown);
    TestLoggerEntry responseEntry = logger.entries.get(1);
    Assert.assertNotNull(responseEntry.message);
    Assert.assertNull(responseEntry.thrown);
    assertRequestAndResponse(requestEntry.message, responseEntry.message, "createPayment.failure.invalidCardNumber");
}
Also used : Order(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order) ValidationException(com.ingenico.connect.gateway.sdk.java.ValidationException) Address(com.ingenico.connect.gateway.sdk.java.domain.definitions.Address) Customer(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CardPaymentMethodSpecificInput(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.CardPaymentMethodSpecificInput) AmountOfMoney(com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney) Card(com.ingenico.connect.gateway.sdk.java.domain.definitions.Card) Test(org.junit.Test)

Example 2 with ValidationException

use of com.ingenico.connect.gateway.sdk.java.ValidationException in project connect-sdk-java by Ingenico-ePayments.

the class PaymentsClientTest method testCreateInvalidRequest.

/**
 * Tests that a 400 failure response without a payment result will throw a {@link ValidationException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateInvalidRequest() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("invalid_request.json");
    whenPost().thenReturn(new Response(400, responseBody, null));
    CreatePaymentRequest body = createRequest();
    try {
        client.merchant("merchantId").payments().create(body);
        Assert.fail("Expected ValidationException");
    } catch (ValidationException e) {
        Assert.assertTrue(e.toString().contains(responseBody));
    }
}
Also used : Response(com.ingenico.connect.gateway.sdk.java.Response) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) ValidationException(com.ingenico.connect.gateway.sdk.java.ValidationException) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Aggregations

Client (com.ingenico.connect.gateway.sdk.java.Client)2 ValidationException (com.ingenico.connect.gateway.sdk.java.ValidationException)2 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)2 Test (org.junit.Test)2 Response (com.ingenico.connect.gateway.sdk.java.Response)1 Address (com.ingenico.connect.gateway.sdk.java.domain.definitions.Address)1 AmountOfMoney (com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney)1 Card (com.ingenico.connect.gateway.sdk.java.domain.definitions.Card)1 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)1 CardPaymentMethodSpecificInput (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.CardPaymentMethodSpecificInput)1 Customer (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer)1 Order (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order)1 HttpHost (org.apache.http.HttpHost)1