diff --git a/src/main/java/com/checkout/ApacheHttpClientTransport.java b/src/main/java/com/checkout/ApacheHttpClientTransport.java index 7e383e42..0bd67e8e 100644 --- a/src/main/java/com/checkout/ApacheHttpClientTransport.java +++ b/src/main/java/com/checkout/ApacheHttpClientTransport.java @@ -86,14 +86,18 @@ public CompletableFuture invoke(String httpMethod, String path, ApiCre .map(Header::getValue).orElse("NO_REQUEST_ID_SUPPLIED"); if (statusCode != 404) { - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { - StringBuilder stringBuilder = new StringBuilder(); - String line; - while ((line = bufferedReader.readLine()) != null) { - stringBuilder.append(line).append("\n"); + if (response.getEntity() != null && response.getEntity().getContent() != null) { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { + StringBuilder stringBuilder = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) { + stringBuilder.append(line).append("\n"); + } + String jsonBody = stringBuilder.toString(); + return new Response(statusCode, jsonBody, requestId); } - String jsonBody = stringBuilder.toString(); - return new Response(statusCode, jsonBody, requestId); + } else { + return new Response(statusCode, null, requestId); } } else { return new Response(statusCode, null, requestId); diff --git a/src/test/java/com/checkout/events/EventsTests.java b/src/test/java/com/checkout/events/EventsTests.java index d47bc36b..17403538 100644 --- a/src/test/java/com/checkout/events/EventsTests.java +++ b/src/test/java/com/checkout/events/EventsTests.java @@ -6,6 +6,7 @@ import com.checkout.webhooks.WebhookRequest; import com.checkout.webhooks.WebhookResponse; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.time.Instant; @@ -14,6 +15,11 @@ public class EventsTests extends ApiTestFixture { + @Before + public void before() throws Exception { + Thread.sleep(5000); + } + @Test public void retrieve_all_event_types() throws Exception { List allEventTypesResponses = getApi().eventsClient().retrieveAllEventTypes(null).get(); @@ -101,7 +107,7 @@ public void retrieve_event_notification() throws Exception { final String paymentId = payment.getPayment().getId(); // Sometimes the events aren't available immediately - Thread.sleep(5000); + Thread.sleep(10000); final String eventId = getApi().eventsClient().retrieveEvents(null, null, null, null, paymentId).get().getData().get(0).getId(); EventResponse event = getApi().eventsClient().retrieveEvent(eventId).get(); Assert.assertNotNull(event.getNotifications()); @@ -138,7 +144,7 @@ public void test_retry_specific_webhook() throws Exception { final String paymentId = payment.getPayment().getId(); // Sometimes the events aren't available immediately - Thread.sleep(5000); + Thread.sleep(10000); final String eventId = getApi().eventsClient().retrieveEvents(null, null, null, null, paymentId).get().getData().get(0).getId(); EventResponse originalEvent = getApi().eventsClient().retrieveEvent(eventId).get(); long initialAttempts = originalEvent.getNotifications().stream() @@ -147,7 +153,7 @@ public void test_retry_specific_webhook() throws Exception { getApi().eventsClient().retryWebhook(eventId, webhook.getId()).get(); - Thread.sleep(5000); + Thread.sleep(10000); EventResponse retriedEvent = getApi().eventsClient().retrieveEvent(eventId).get(); long retriedAttempts = retriedEvent.getNotifications().stream() .filter(it -> webhookUrl.equals(it.getUrl())) @@ -170,7 +176,7 @@ public void test_retry_all_webhooks() throws Exception { final String paymentId = payment.getPayment().getId(); // Sometimes the events aren't available immediately - Thread.sleep(5000); + Thread.sleep(10000); final String eventId = getApi().eventsClient().retrieveEvents(null, null, null, null, paymentId).get().getData().get(0).getId(); EventResponse originalEvent = getApi().eventsClient().retrieveEvent(eventId).get(); long initialAttempts = originalEvent.getNotifications().stream() @@ -179,7 +185,7 @@ public void test_retry_all_webhooks() throws Exception { getApi().eventsClient().retryAllWebhooks(eventId).get(); - Thread.sleep(5000); + Thread.sleep(10000); EventResponse retriedEvent = getApi().eventsClient().retrieveEvent(eventId).get(); long retriedAttempts = retriedEvent.getNotifications().stream() .filter(it -> webhookUrl.equals(it.getUrl()))