ResponseHelper { public static <T> T readEntity(@NonNull final Response response, GenericType<T> entityType) { synchronized (response) { if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) { response.bufferEntity(); return response.readEntity(entityType); } if (response.getStatus() == Status.NOT_MODIFIED.getStatusCode()) { closeResponseSilently(response); return null; } } throw new IllegalStateException( "Attempted to read entity from unsuccessful response, should have called throwIfNotSuccessful first"); }  static void throwIfNotSuccessful(@NonNull Response response); static void readWithoutEntity(@NonNull final Response response); static T readEntity(@NonNull final Response response, GenericType<T> entityType); static T readEntity(@NonNull final Response response, Class<T> entityType); static void closeResponseSilently(final Response response); static void closeResponseSilentlyIfNotBuffered(@NonNull final Response response);  }
@Test public void testReadEntity_encodedJsonString() throws Exception { Response response = mock(Response.class); Response.StatusType statusInfo = mock(Response.StatusType.class); String jsonEncodedString = new ObjectMapper().writeValueAsString("foo \" bar"); assertEquals("\"foo \\\" bar\"", jsonEncodedString); Class<String> entityType = String.class; when(response.getStatusInfo()).thenReturn(statusInfo); when(statusInfo.getFamily()).thenReturn(Response.Status.Family.SUCCESSFUL); when(response.getHeaderString(HttpHeaders.CONTENT_TYPE)) .thenReturn(javax.ws.rs.core.MediaType.APPLICATION_JSON); when(response.readEntity(entityType)).thenReturn(jsonEncodedString); String responseString = ResponseHelper.readEntity(response, entityType); assertEquals("foo \" bar", responseString); verify(response).bufferEntity(); verify(statusInfo).getFamily(); verify(response).readEntity(entityType); verify(response).getHeaderString(HttpHeaders.CONTENT_TYPE); }
@Test public void testReadEntity_streamWithContentType() { Response response = mock(Response.class); Response.StatusType statusInfo = mock(Response.StatusType.class); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); List<Object> contentType = ImmutableList.<Object>of("text"); InputStream mockStream = mock(InputStream.class); Class<InputStream> entityType = InputStream.class; when(response.getStatusInfo()).thenReturn(statusInfo); when(statusInfo.getFamily()).thenReturn(Response.Status.Family.SUCCESSFUL); when(response.getHeaders()).thenReturn(headers); headers.add(HttpHeaders.CONTENT_TYPE, contentType); when(response.readEntity(entityType)).thenReturn(mockStream); InputStream inputStream = ResponseHelper.readEntity(response, entityType); assertThat(inputStream, instanceOf(WrappedResponseInputStream.class)); verify(response).getStatusInfo(); verify(statusInfo).getFamily(); verify(response, atLeastOnce()).getHeaders(); verify(response).readEntity(entityType); verify(response, never()).bufferEntity(); verify(response).getStringHeaders(); verifyNoMoreInteractions(response, statusInfo, mockStream); assertEquals(ImmutableList.of(contentType), headers.get(HttpHeaders.CONTENT_TYPE)); }
@Test public void testReadEntity_streamWithoutContentType() { Response response = mock(Response.class); Response.StatusType statusInfo = mock(Response.StatusType.class); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); InputStream mockStream = mock(InputStream.class); Class<InputStream> entityType = InputStream.class; when(response.getStatusInfo()).thenReturn(statusInfo); when(statusInfo.getFamily()).thenReturn(Response.Status.Family.SUCCESSFUL); when(response.getHeaders()).thenReturn(headers); when(response.readEntity(entityType)).thenReturn(mockStream); InputStream inputStream = ResponseHelper.readEntity(response, entityType); assertThat(inputStream, instanceOf(WrappedResponseInputStream.class)); verify(response).getStatusInfo(); verify(statusInfo).getFamily(); verify(response, atLeastOnce()).getHeaders(); verify(response).readEntity(entityType); verify(response, never()).bufferEntity(); verify(response).getStringHeaders(); verifyNoMoreInteractions(response, statusInfo, mockStream); }
@Test public void testReadEntity_streamWithContentLength() { Response response = mock(Response.class); Response.StatusType statusInfo = mock(Response.StatusType.class); MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); List<Object> contentType = ImmutableList.<Object>of("text"); InputStream mockStream = mock(InputStream.class); Class<InputStream> entityType = InputStream.class; when(response.getStatusInfo()).thenReturn(statusInfo); when(statusInfo.getFamily()).thenReturn(Response.Status.Family.SUCCESSFUL); when(response.getHeaders()).thenReturn(headers); headers.add(HttpHeaders.CONTENT_TYPE, contentType); headers.add(HttpHeaders.CONTENT_LENGTH, "100"); MultivaluedMap<String, String> stringHeaders = new MultivaluedHashMap<>(); stringHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, "100"); when(response.getStringHeaders()).thenReturn(stringHeaders); when(response.readEntity(entityType)).thenReturn(mockStream); InputStream inputStream = ResponseHelper.readEntity(response, entityType); assertThat(inputStream, instanceOf(ContentLengthVerifyingInputStream.class)); verify(response).getStatusInfo(); verify(statusInfo).getFamily(); verify(response, atLeastOnce()).getHeaders(); verify(response).readEntity(entityType); verify(response, never()).bufferEntity(); verify(response).getStringHeaders(); verifyNoMoreInteractions(response, statusInfo, mockStream); headers = response.getHeaders(); stringHeaders = response.getStringHeaders(); assertEquals(ImmutableList.of(contentType), headers.get(HttpHeaders.CONTENT_TYPE)); assertEquals(ImmutableList.of("100"), headers.get(HttpHeaders.CONTENT_LENGTH)); assertEquals(ImmutableList.of("100"), stringHeaders.get(HttpHeaders.CONTENT_LENGTH)); }