FullSmileResponseHandler implements ResponseHandler<SmileResponse<T>, RuntimeException> { @Override public SmileResponse<T> handle(Request request, Response response) { String contentType = response.getHeader(CONTENT_TYPE); if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) { return new SmileResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders()); } try { return new SmileResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, response.getInputStream()); } catch (IOException e) { throw new RuntimeException("Error reading SMILE response from server", e); } } private  FullSmileResponseHandler(JsonCodec<T> jsonCodec); static FullSmileResponseHandler<T> createFullSmileResponseHandler(JsonCodec<T> jsonCodec); @Override SmileResponse<T> handleException(Request request, Exception exception); @Override SmileResponse<T> handle(Request request, Response response);  }
@Test public void testValidSmile() { SmileResponse<User> response = handler.handle(null, createSmileResponse(OK, ImmutableMap.of( "name", "Joe", "age", 25, "extra", true ))); assertTrue(response.hasValue()); assertEquals(response.getValue().getName(), "Joe"); assertEquals(response.getValue().getAge(), 25); }
@Test public void testInvalidSmile() { SmileResponse<User> response = handler.handle(null, createSmileResponse(OK, ImmutableMap.of( "age", "foo" ))); assertFalse(response.hasValue()); assertEquals(response.getException().getMessage(), "Unable to create " + User.class + " from SMILE response"); assertInstanceOf(response.getException().getCause(), InvalidFormatException.class); assertContains(response.getException().getCause().getMessage(), "Cannot deserialize value of type `int` from String \"foo\": not a valid Integer value"); }
@Test public void testInvalidSmileGetValue() { SmileResponse<User> response = handler.handle(null, createSmileResponse(OK, ImmutableMap.of( "age", "foo" ))); try { response.getValue(); fail("expected exception"); } catch (IllegalStateException e) { assertEquals(e.getMessage(), "Response does not contain a SMILE value"); assertSame(e.getCause(), response.getException()); } }
@Test public void testNonSmileResponse() { SmileResponse<User> response = handler.handle(null, mockResponse().contentType(PLAIN_TEXT_UTF_8).body("hello").build()); assertFalse(response.hasValue()); assertNull(response.getException()); }
@Test public void testMissingContentType() { SmileResponse<User> response = handler.handle(null, mockResponse().body("hello").build()); assertFalse(response.hasValue()); assertNull(response.getException()); assertTrue(response.getHeaders().isEmpty()); }
@Test public void testSmileErrorResponse() { SmileResponse<User> response = handler.handle(null, createSmileResponse(INTERNAL_SERVER_ERROR, ImmutableMap.of( "error", true ))); assertTrue(response.hasValue()); assertNull(response.getValue().getName()); assertEquals(response.getValue().getAge(), 0); }
@Test public void testSmileReadException() throws IOException { InputStream inputStream = mock(InputStream.class); IOException expectedException = new IOException("test exception"); when(inputStream.read()).thenThrow(expectedException); when(inputStream.read(any(byte[].class))).thenThrow(expectedException); when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException); try { handler.handle(null, mockResponse() .contentType(MEDIA_TYPE_SMILE) .body(inputStream) .build()); fail("expected exception"); } catch (RuntimeException e) { assertEquals(e.getMessage(), "Error reading SMILE response from server"); assertSame(e.getCause(), expectedException); } }