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());
}
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());
}
}
Aggregations