static <T> CompletableFuture<T> toFuture(Call<T> callToWrap) { final CallbackAdaptor<T> completableFuture = new CallbackAdaptor<>(); callToWrap.enqueue(completableFuture); return completableFuture; }
@Override public void onFailure(final Call<T> call, final Throwable t) { completeExceptionally( new GeneralLineMessagingException(t.getMessage(), null, t)); } }
@Test public void onFailureTest() throws Exception { Throwable t = mock(Throwable.class); when(t.getMessage()).thenReturn("Message"); // Do target.onFailure(call, t); // Verify assertThat(target).isCompletedExceptionally(); assertThat(target.handle((ignored, e) -> e).get()) .isInstanceOf(GeneralLineMessagingException.class) .hasMessageContaining("Message"); } }
@Test public void onResponseSuccessfullyTest() throws Exception { final Object value = new Object(); Response<Object> response = Response.success(value); // Do target.onResponse(call, response); // Verify assertThat(target).isCompletedWithValue(value); }
@Before public void setUp() throws Exception { target = new CallbackAdaptor<>(); }
@Override public void onResponse(final Call<T> call, final Response<T> response) { if (response.isSuccessful()) { complete(response.body()); } else { completeExceptionally(EXCEPTION_CONVERTER.apply(response)); } }
@Test public void onResponseWithErrorTest() throws Exception { Response<Object> response = Response.error(400, ResponseBody.create(parse("application/json"), "{}")); // Do target.onResponse(call, response); // Verify assertThat(target).isCompletedExceptionally(); }
private static CompletableFuture<BotApiResponse> toBotApiFuture(Call<Void> callToWrap) { final CallbackAdaptor<Void> completableFuture = new CallbackAdaptor<>(); callToWrap.enqueue(completableFuture); return completableFuture.thenApply(VOID_TO_BOT_API_SUCCESS_RESPONSE); }