Defaults { public static BiFunction<HttpRequestBase, HttpResponse, String> asString() { return (request, response) -> { try (InputStream content = response.getEntity().getContent()) { return IOUtils.toString(content, UTF_8); } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } finally { EntityUtils.consumeQuietly(response.getEntity()); 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 shouldReturnString() throws Exception { final BasicHttpResponse response = buildResponse(); response.setEntity(new StringEntity("My response")); final String value = Defaults.asString().apply(buildRequest(), response); assertEquals("My response", value); }
@Test public void shouldReturnString_handleIOError() throws Exception { final BasicHttpResponse response = buildResponse(); response.setEntity(new StringEntity("") { @Override public InputStream getContent() throws IOException { throw new IOException("Unexpected exception"); } }); try { Defaults.asString().apply(buildRequest(), response); } catch (TDPException e) { assertEquals(CommonErrorCodes.UNEXPECTED_EXCEPTION, e.getCode()); } }