HttpUtils { public static <T> T waitFor(Future<? extends T> future) throws IOException { try { return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } catch (ExecutionException e) { throw wrapInIOException(e.getCause()); } } private  HttpUtils(); static T waitFor(Future<? extends T> future); static Throwable unwrapCompletionException(Throwable ex); static boolean isSuccessStatus(int status); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor,
                                                         Predicate<Response> successPredicate,
                                                         String tag); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor,
                                                         Predicate<Response> successPredicate); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor, String tag); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor); static Predicate<Response> hasStatus(int status); static FailedRequestException createExceptionForResponse(Response response, String tag); static ResponseProcessor<T> jsonResult(ObjectMapper mapper, Class<T> resultClass); static ResponseProcessor<T> jsonResult(ObjectMapper mapper, TypeReference<T> typeReference); static Consumer<RequestBuilder> get(String uri); static ResponseProcessor<T> nullProcessor(); static String urlEncode(String src); static String addQueryParameters(String url, Map<String, ?> params); static String addQueryParameters(String url, Map<String, ?> params, boolean filterUndefined); static String addQueryParameter(String url, String key, Object value); static final Predicate<Response> SUCCESS_STATUS; }
@Test public void testWaitForSuccessfulFuture() throws IOException { Object result = new Object(); CompletableFuture<Object> future = CompletableFuture.completedFuture(result); assertThat(HttpUtils.waitFor(future)).isEqualTo(result); }
@Test public void testWaitForFailedFutureWrappedException() { Exception exception = new IllegalStateException("Something went wrong"); CompletableFuture<Object> future = failedFuture(exception); try { HttpUtils.waitFor(future); fail("No exception was thrown!"); } catch (IOException e) { assertThat(e.getCause()).isEqualTo(exception); } }
@Test public void testWaitForFailedFutureWithIOException() { Exception exception = new IOException("No connection"); CompletableFuture<Object> future = failedFuture(exception); try { HttpUtils.waitFor(future); fail("No exception was thrown!"); } catch (IOException e) { assertThat(e).isEqualTo(exception); } }
@Test public void testWaitForInterrupted() { CompletableFuture<Object> neverCompletingFuture = new CompletableFuture<>(); final Thread currentThread = Thread.currentThread(); Runnable interrupter = currentThread::interrupt; new Thread(interrupter).start(); try { HttpUtils.waitFor(neverCompletingFuture); fail("No exception was thrown!"); } catch (IOException e) { assertThat(e.getCause()).isInstanceOf(InterruptedException.class); assertThat(Thread.currentThread().isInterrupted()).isTrue(); } }