Defaults { public static <T> BiFunction<HttpRequestBase, HttpResponse, T> convertResponse(ObjectMapper mapper, Class<T> clazz) { return (request, response) -> { try (final InputStream content = response.getEntity().getContent()) { final String contentAsString = IOUtils.toString(content, UTF_8); if (StringUtils.isEmpty(contentAsString)) { return null; } else { return mapper.readerFor(clazz).readValue(contentAsString); } } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } finally { request.releaseConnection(); } }; } private  Defaults(); static Function<Exception, RuntimeException> passthrough(); static BiFunction<HttpRequestBase, HttpResponse, T> asNull(); static BiFunction<HttpRequestBase, HttpResponse, String> asString(); static BiFunction<HttpRequestBase, HttpResponse, String> emptyString(); static BiFunction<HttpRequestBase, HttpResponse, InputStream> emptyStream(); static BiFunction<HttpRequestBase, HttpResponse, InputStream> pipeStream(); static BiFunction<HttpRequestBase, HttpResponse, T> convertResponse(ObjectMapper mapper,
            Class<T> clazz); static BiFunction<HttpRequestBase, HttpResponse, T> convertResponse(ObjectMapper mapper,
            TypeReference<T> typeReference); static BiFunction<HttpRequestBase, HttpResponse, T> convertResponse(ObjectMapper mapper,
            TypeReference<T> typeReference, Function<Exception, T> errorHandler); static BiFunction<HttpRequestBase, HttpResponse, JsonNode> toJson(ObjectMapper mapper); static BiFunction<HttpRequestBase, HttpResponse, S> iterate(Class<T> clazz, ObjectMapper mapper,
            Function<Iterator<T>, S> convert); static ResponseEntity<String> getResponseEntity(HttpStatus status, HttpResponse response);  }
@Test public void shouldReadJsonResponse() throws Exception { final Response response = new Response(); response.value = "My Value"; final ObjectMapper mapper = new ObjectMapper(); final BasicHttpResponse httpResponse = buildResponse(); httpResponse.setEntity(new StringEntity(mapper.writeValueAsString(response))); final Response value = Defaults.convertResponse(mapper, Response.class).apply(buildRequest(), httpResponse); assertEquals("My Value", value.value); }
@Test public void shouldReadEmptyJsonResponse() throws Exception { final ObjectMapper mapper = new ObjectMapper(); final BasicHttpResponse httpResponse = buildResponse(); httpResponse.setEntity(new StringEntity("")); final Response value = Defaults.convertResponse(mapper, Response.class).apply(buildRequest(), httpResponse); assertNull(value); }
@Test public void shouldReadJsonResponse_ioException() throws Exception { final ObjectMapper mapper = new ObjectMapper(); final BasicHttpResponse httpResponse = buildResponse(); httpResponse.setEntity(new StringEntity("") { @Override public InputStream getContent() throws IOException { throw new IOException(); } }); try { Defaults.convertResponse(mapper, Response.class).apply(buildRequest(), httpResponse); } catch (TDPException e) { assertEquals(CommonErrorCodes.UNEXPECTED_EXCEPTION, e.getCode()); } }