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