Search in sources :

Example 1 with CallContext

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

the class DefaultConnectionIdempotenceTest method testIdempotenceDuplicateRequest.

@Test
public void testIdempotenceDuplicateRequest() throws Exception {
    String body = getResource("idempotence_duplicate_failure.json");
    final Long idempotenceRequestTimestamp = System.currentTimeMillis();
    Map<String, String> responseHeaders = new HashMap<String, String>(2);
    responseHeaders.put("Location", "http://localhost/v1/20000/payments/000002000020142549460000100001");
    responseHeaders.put("X-GCS-Idempotence-Request-Timestamp", idempotenceRequestTimestamp.toString());
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 409, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    final String idempotenceKey = UUID.randomUUID().toString();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        client.merchant("20000").payments().create(request, context);
        Assert.fail("Expected IdempotenceException");
    } catch (IdempotenceException e) {
        Assert.assertEquals(409, e.getStatusCode());
        Assert.assertEquals(body, e.getResponseBody());
        Assert.assertEquals(idempotenceKey, e.getIdempotenceKey());
        Assert.assertEquals(idempotenceRequestTimestamp, e.getIdempotenceRequestTimestamp());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertEquals(idempotenceRequestTimestamp, context.getIdempotenceRequestTimestamp());
}
Also used : HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) IdempotenceException(com.ingenico.connect.gateway.sdk.java.IdempotenceException) Test(org.junit.Test)

Example 2 with CallContext

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

the class DefaultConnectionIdempotenceTest method testIdempotenceFirstRequest.

@Test
public void testIdempotenceFirstRequest() throws Exception {
    final String body = getResource("idempotence_success.json");
    final String idempotenceKey = UUID.randomUUID().toString();
    Map<String, String> responseHeaders = new HashMap<String, String>(1);
    responseHeaders.put("Location", "http://localhost/v1/20000/payments/000002000020142549460000100001");
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 201, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        CreatePaymentResponse response = client.merchant("20000").payments().create(request, context);
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getPayment());
        Assert.assertNotNull(response.getPayment().getId());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertNull(context.getIdempotenceRequestTimestamp());
}
Also used : HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Example 3 with CallContext

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

the class DefaultConnectionIdempotenceTest method testIdempotenceSecondRequest.

@Test
public void testIdempotenceSecondRequest() throws Exception {
    String body = getResource("idempotence_success.json");
    final Long idempotenceRequestTimestamp = System.currentTimeMillis();
    Map<String, String> responseHeaders = new HashMap<String, String>(2);
    responseHeaders.put("Location", "http://localhost/v1/20000/payments/000002000020142549460000100001");
    responseHeaders.put("X-GCS-Idempotence-Request-Timestamp", idempotenceRequestTimestamp.toString());
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 201, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    final String idempotenceKey = UUID.randomUUID().toString();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        CreatePaymentResponse response = client.merchant("20000").payments().create(request, context);
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getPayment());
        Assert.assertNotNull(response.getPayment().getId());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertEquals(idempotenceRequestTimestamp, context.getIdempotenceRequestTimestamp());
}
Also used : HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Example 4 with CallContext

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

the class DefaultConnectionIdempotenceTest method testIdempotenceSecondFailure.

@Test
public void testIdempotenceSecondFailure() throws Exception {
    String body = getResource("idempotence_rejected.json");
    final Long idempotenceRequestTimestamp = System.currentTimeMillis();
    Map<String, String> responseHeaders = new HashMap<String, String>(2);
    responseHeaders.put("X-GCS-Idempotence-Request-Timestamp", idempotenceRequestTimestamp.toString());
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 402, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    final String idempotenceKey = UUID.randomUUID().toString();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        client.merchant("20000").payments().create(request, context);
        Assert.fail("Expected DeclinedPaymentException");
    } catch (DeclinedPaymentException e) {
        Assert.assertEquals(402, e.getStatusCode());
        Assert.assertEquals(body, e.getResponseBody());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertEquals(idempotenceRequestTimestamp, context.getIdempotenceRequestTimestamp());
}
Also used : DeclinedPaymentException(com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Example 5 with CallContext

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

the class DefaultConnectionIdempotenceTest method testIdempotenceFirstFailure.

@Test
public void testIdempotenceFirstFailure() throws Exception {
    String body = getResource("idempotence_rejected.json");
    Map<String, String> responseHeaders = new HashMap<String, String>();
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 402, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    final String idempotenceKey = UUID.randomUUID().toString();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        client.merchant("20000").payments().create(request, context);
        Assert.fail("Expected DeclinedPaymentException");
    } catch (DeclinedPaymentException e) {
        Assert.assertEquals(402, e.getStatusCode());
        Assert.assertEquals(body, e.getResponseBody());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertNull(context.getIdempotenceRequestTimestamp());
}
Also used : DeclinedPaymentException(com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Aggregations

CallContext (com.ingenico.connect.gateway.sdk.java.CallContext)7 Client (com.ingenico.connect.gateway.sdk.java.Client)7 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 HttpHost (org.apache.http.HttpHost)5 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)4 DeclinedPaymentException (com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException)2 IdempotenceException (com.ingenico.connect.gateway.sdk.java.IdempotenceException)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 Customer (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer)1 Order (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order)1 RedirectPaymentMethodSpecificInput (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.RedirectPaymentMethodSpecificInput)1 RedirectPaymentProduct809SpecificInput (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.RedirectPaymentProduct809SpecificInput)1