Search in sources :

Example 1 with IdempotenceException

use of com.ingenico.connect.gateway.sdk.java.IdempotenceException 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 IdempotenceException

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

the class PaymentsClientTest method testCreateIdempotenceError.

/**
 * Tests that a 409 failure response with a duplicate request code and an idempotence key will throw an {@link IdempotenceException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateIdempotenceError() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("duplicate_request.json");
    whenPost().thenReturn(new Response(409, responseBody, null));
    CreatePaymentRequest body = createRequest();
    CallContext context = new CallContext().withIdempotenceKey("key");
    try {
        client.merchant("merchantId").payments().create(body, context);
        Assert.fail("Expected ApiException");
    } catch (IdempotenceException e) {
        Assert.assertTrue(e.toString().contains(responseBody));
        Assert.assertEquals(context.getIdempotenceKey(), e.getIdempotenceKey());
    }
}
Also used : Response(com.ingenico.connect.gateway.sdk.java.Response) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) 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)

Aggregations

CallContext (com.ingenico.connect.gateway.sdk.java.CallContext)2 Client (com.ingenico.connect.gateway.sdk.java.Client)2 IdempotenceException (com.ingenico.connect.gateway.sdk.java.IdempotenceException)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 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)1 HashMap (java.util.HashMap)1 HttpHost (org.apache.http.HttpHost)1