Search in sources :

Example 6 with Response

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

the class PaymentsClientTest method testCreateInternalServerErrorWithoutBody.

/**
 * Tests that a 500 response with a JSON response with no body will throw a {@link GlobalCollectException} and not a {@link NullPointerException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateInternalServerErrorWithoutBody() {
    Client client = Factory.createClient(session);
    String responseBody = null;
    whenPost().thenReturn(new Response(500, responseBody, Arrays.asList(new ResponseHeader("content-type", "text/html"))));
    CreatePaymentRequest body = createRequest();
    try {
        client.merchant("merchantId").payments().create(body);
        Assert.fail("Expected GlobalCollectException");
    } catch (GlobalCollectException e) {
        Assert.assertEquals(responseBody, e.getResponseBody());
        Assert.assertNull(e.getErrorId());
        Assert.assertEquals(0, e.getErrors().size());
    }
}
Also used : Response(com.ingenico.connect.gateway.sdk.java.Response) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) ResponseHeader(com.ingenico.connect.gateway.sdk.java.ResponseHeader) GlobalCollectException(com.ingenico.connect.gateway.sdk.java.GlobalCollectException) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Example 7 with Response

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

the class PaymentsClientTest method testCreateReferenceError.

/**
 * Tests that a 409 failure response with a duplicate request code but without an idempotence key will throw a
 * {@link ReferenceException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateReferenceError() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("duplicate_request.json");
    whenPost().thenReturn(new Response(409, responseBody, null));
    CreatePaymentRequest body = createRequest();
    try {
        client.merchant("merchantId").payments().create(body);
        Assert.fail("Expected ApiException");
    } catch (ReferenceException 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) ReferenceException(com.ingenico.connect.gateway.sdk.java.ReferenceException) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Example 8 with Response

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

the class PaymentsClientTest method testCreateInvalidAuthorization.

/**
 * Tests that a 401 failure response without a payment result will throw a {@link ApiException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateInvalidAuthorization() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("invalid_authorization.json");
    whenPost().thenReturn(new Response(401, responseBody, null));
    CreatePaymentRequest body = createRequest();
    try {
        client.merchant("merchantId").payments().create(body);
        Assert.fail("Expected ApiException");
    } catch (ApiException 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) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) ApiException(com.ingenico.connect.gateway.sdk.java.ApiException) Test(org.junit.Test)

Example 9 with Response

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

Example 10 with Response

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

the class DefaultConnection method executeRequest.

@SuppressWarnings("resource")
protected Response executeRequest(HttpUriRequest request) {
    final String requestId = UUID.randomUUID().toString();
    final long startTime = System.currentTimeMillis();
    HttpContext context = new BasicHttpContext();
    context.setAttribute(REQUEST_ID_ATTRIBUTE, requestId);
    context.setAttribute(START_TIMME_ATTRIBUTE, startTime);
    try {
        CloseableHttpResponse httpResponse = httpClient.execute(request, context);
        HttpEntity entity = httpResponse.getEntity();
        try {
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            String body = entity == null ? null : EntityUtils.toString(entity, CHARSET);
            List<ResponseHeader> headers = getHeaders(httpResponse);
            return new Response(statusCode, body, headers);
        } finally {
            /*
				 * Ensure that the entity content is fully consumed and the
				 * content stream, if exists, is closed so the connection can be
				 * reused. Do not close the httpResponse because that will
				 * prevent the connection from being reused.
				 */
            EntityUtils.consume(entity);
        }
    } catch (ClientProtocolException e) {
        logError(requestId, e, startTime, communicatorLogger);
        throw new CommunicationException(e);
    } catch (IOException e) {
        logError(requestId, e, startTime, communicatorLogger);
        throw new CommunicationException(e);
    } catch (RuntimeException e) {
        logError(requestId, e, startTime, communicatorLogger);
        throw e;
    }
}
Also used : ResponseHeader(com.ingenico.connect.gateway.sdk.java.ResponseHeader) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) CommunicationException(com.ingenico.connect.gateway.sdk.java.CommunicationException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) Response(com.ingenico.connect.gateway.sdk.java.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

Response (com.ingenico.connect.gateway.sdk.java.Response)10 Client (com.ingenico.connect.gateway.sdk.java.Client)9 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)9 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)9 Test (org.junit.Test)9 ResponseHeader (com.ingenico.connect.gateway.sdk.java.ResponseHeader)4 CommunicationException (com.ingenico.connect.gateway.sdk.java.CommunicationException)2 ResponseException (com.ingenico.connect.gateway.sdk.java.ResponseException)2 ApiException (com.ingenico.connect.gateway.sdk.java.ApiException)1 CallContext (com.ingenico.connect.gateway.sdk.java.CallContext)1 DeclinedPaymentException (com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException)1 GlobalCollectException (com.ingenico.connect.gateway.sdk.java.GlobalCollectException)1 IdempotenceException (com.ingenico.connect.gateway.sdk.java.IdempotenceException)1 NotFoundException (com.ingenico.connect.gateway.sdk.java.NotFoundException)1 ReferenceException (com.ingenico.connect.gateway.sdk.java.ReferenceException)1 ValidationException (com.ingenico.connect.gateway.sdk.java.ValidationException)1 IOException (java.io.IOException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1