DefaultHttpJsonResponse implements HttpJsonResponse { @Override public <T> T as(Class<T> clazz, Type genericType) throws IOException { requireNonNull(clazz, "Required non-null class"); try { return JsonHelper.fromJson(responseBody, clazz, genericType); } catch (JsonParseException jsonEx) { throw new IOException(jsonEx.getLocalizedMessage(), jsonEx); } } DefaultHttpJsonResponse(String response, int responseCode); @Override String asString(); @Override T asDto(Class<T> dtoInterface); @Override List<T> asList(Class<T> dtoInterface); @Override @SuppressWarnings("unchecked") Map<String, String> asProperties(); @Override T as(Class<T> clazz, Type genericType); @Override int getResponseCode();  }
@Test public void shouldDeserializeResponseToGivenType() throws Exception { final String responseBody = DtoFactory.getInstance().toJson(new JsonArrayImpl<>(singletonList("element"))); final DefaultHttpJsonResponse response = new DefaultHttpJsonResponse(responseBody, 200); assertEquals(response.as(Set.class, new TypeToken<Set<String>>() { }.getType()), singleton("element")); }
@Test(expectedExceptions = NullPointerException.class) public void shouldThrowNullPointerExceptionWhenClazzIsNull() throws Exception { new DefaultHttpJsonResponse("{}", 200).as(null, null); }
@Test(expectedExceptions = IOException.class) public void shouldThrowServerExceptionWhenParsingNotValidJsonContent() throws Exception { new DefaultHttpJsonResponse("not valid json", 200).as(Set.class, null); }