Search in sources :

Example 1 with CommunicationException

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

the class PaymentsClientTest method testCreateMethodNotAllowed.

/**
 * Tests that a 405 response with a non-JSON response will throw a {@link CommunicationException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateMethodNotAllowed() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("method_not_allowed.html");
    whenPost().thenReturn(new Response(405, responseBody, Arrays.asList(new ResponseHeader("content-type", "text/html"))));
    CreatePaymentRequest body = createRequest();
    try {
        client.merchant("merchantId").payments().create(body);
        Assert.fail("Expected CommunicationException");
    } catch (CommunicationException e) {
        Assert.assertNotNull(e.getCause());
        Assert.assertEquals(ResponseException.class, e.getCause().getClass());
        Assert.assertTrue(e.getCause().toString().contains(responseBody));
    }
}
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) CommunicationException(com.ingenico.connect.gateway.sdk.java.CommunicationException) ResponseException(com.ingenico.connect.gateway.sdk.java.ResponseException) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Example 2 with CommunicationException

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

the class DefaultConnectionLoggerTest method testLogErrorOnly.

@Test
public void testLogErrorOnly() throws Exception {
    // logging is enabled after the request is logged but before the error is logged
    serverBootstrap.registerHandler("/v1/1234/services/testconnection", requestHandler);
    HttpHost host = start();
    Client client = createClient(host, 1000, 10);
    TestLogger logger = new TestLogger();
    setupRequestHandler(enableLogging(delayedAnswer(setHtmlResponse("notFound.html", 404), 100), client, logger));
    try {
        client.merchant("1234").services().testconnection();
        Assert.fail("expected CommunicationException");
    } catch (CommunicationException e) {
    // expected
    } finally {
        client.close();
    }
    Assert.assertEquals(1, logger.entries.size());
    TestLoggerEntry errorEntry = logger.entries.get(0);
    Assert.assertNotNull(errorEntry.message);
    Assert.assertNotNull(errorEntry.thrown);
    assertError(errorEntry.message);
    Assert.assertEquals(SocketTimeoutException.class, errorEntry.thrown.getClass());
}
Also used : CommunicationException(com.ingenico.connect.gateway.sdk.java.CommunicationException) HttpHost(org.apache.http.HttpHost) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Example 3 with CommunicationException

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

the class DefaultConnectionLoggerTest method testReadTimeout.

@Test
public void testReadTimeout() throws Exception {
    // an exception is thrown before logging the response
    serverBootstrap.registerHandler("/v1/1234/services/testconnection", requestHandler);
    HttpHost host = start();
    Client client = createClient(host, 1000, 10);
    TestLogger logger = new TestLogger();
    client.enableLogging(logger);
    setupRequestHandler(delayedAnswer(setHtmlResponse("notFound.html", 404), 100));
    try {
        client.merchant("1234").services().testconnection();
        Assert.fail("expected CommunicationException");
    } catch (CommunicationException e) {
    // expected
    } finally {
        client.close();
    }
    Assert.assertEquals(2, logger.entries.size());
    TestLoggerEntry requestEntry = logger.entries.get(0);
    Assert.assertNotNull(requestEntry.message);
    Assert.assertNull(requestEntry.thrown);
    TestLoggerEntry errorEntry = logger.entries.get(1);
    Assert.assertNotNull(errorEntry.message);
    Assert.assertNotNull(errorEntry.thrown);
    assertRequestAndError(requestEntry.message, errorEntry.message, "testConnection");
    Assert.assertEquals(SocketTimeoutException.class, errorEntry.thrown.getClass());
}
Also used : CommunicationException(com.ingenico.connect.gateway.sdk.java.CommunicationException) HttpHost(org.apache.http.HttpHost) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Example 4 with CommunicationException

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

CommunicationException (com.ingenico.connect.gateway.sdk.java.CommunicationException)4 Client (com.ingenico.connect.gateway.sdk.java.Client)3 Test (org.junit.Test)3 Response (com.ingenico.connect.gateway.sdk.java.Response)2 ResponseHeader (com.ingenico.connect.gateway.sdk.java.ResponseHeader)2 HttpHost (org.apache.http.HttpHost)2 ResponseException (com.ingenico.connect.gateway.sdk.java.ResponseException)1 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)1 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)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 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1 HttpContext (org.apache.http.protocol.HttpContext)1