private static CompletableFuture<MessageContentResponse> toMessageContentResponseFuture( final Call<ResponseBody> callToWrap) { final ResponseBodyCallbackAdaptor future = new ResponseBodyCallbackAdaptor(); callToWrap.enqueue(future); return future; }
@Override public void onFailure(final Call<ResponseBody> call, final Throwable t) { completeExceptionally( new GeneralLineMessagingException(t.getMessage(), null, t)); }
@Test public void onFailureTest() throws Exception { IOException t = mock(IOException.class); when(t.getMessage()).thenReturn("ResponseBodyCallbackAdaptorTest Failed"); // Do target.onFailure(call, t); // Verify assertThat(target).isCompletedExceptionally(); assertThat(target.handle((ignored, e) -> e).get()) .isInstanceOf(GeneralLineMessagingException.class) .hasMessageContaining("ResponseBodyCallbackAdaptorTest Failed"); } }
@Test public void onResponseSuccessTest() throws Exception { Response<ResponseBody> response = Response.success(ResponseBody.create(MediaType.parse("image/jpeg"), "")); // Do target.onResponse(call, response); // Verify assertThat(target).isCompleted(); final MessageContentResponse messageContentResponse = target.get(); assertThat(messageContentResponse.getLength()).isEqualTo(0); assertThat(messageContentResponse.getStream()) .hasSameContentAs(new ByteArrayInputStream(new byte[] {})); assertThat(messageContentResponse.getAllHeaders()) .isEmpty(); }
@Override public void onResponse(final Call<ResponseBody> call, final Response<ResponseBody> response) { if (!response.isSuccessful()) { completeExceptionally(EXCEPTION_CONVERTER.apply(response)); return; } try { complete(convert(response)); } catch (RuntimeException exceptionInConvert) { completeExceptionally( new GeneralLineMessagingException(exceptionInConvert.getMessage(), null, exceptionInConvert)); } }
@Before public void setUp() throws Exception { target = new ResponseBodyCallbackAdaptor(); }
@Test public void onResponseFailTest() throws Exception { Response<ResponseBody> response = Response.error(401, ResponseBody.create(MediaType.parse("text/javascript"), "{}")); // Do target.onResponse(call, response); // Verify assertThat(target).isCompletedExceptionally(); final Throwable t = target.handle((ignored, e) -> e).get(); assertThat(t).isInstanceOf(UnauthorizedException.class); }