RestFunctions { protected static Optional<Object> parseResponse(RestConfig restConfig, HttpUriRequest httpUriRequest, HttpEntity httpEntity) throws IOException { Optional<Object> parsedResponse = Optional.empty(); if (httpEntity != null) { int actualContentLength = 0; String json = EntityUtils.toString(httpEntity); if (json != null && !json.isEmpty()) { actualContentLength = json.length(); parsedResponse = Optional.of(JSONUtils.INSTANCE.load(json, JSONUtils.MAP_SUPPLIER)); } if (restConfig.verifyContentLength() && actualContentLength != httpEntity.getContentLength()) { throw new IOException(String.format("Stellar REST request to %s returned incorrect or missing content length. " + "Content length in the response was %d but the actual body content length was %d.", httpUriRequest.getURI().toString(), httpEntity.getContentLength(), actualContentLength)); } } return parsedResponse; }  static T getArg(int index, Class<T> clazz, List<Object> args);  }
@Test public void restGetShouldParseResponse() throws Exception { RestConfig restConfig = new RestConfig(); HttpGet httpGet = mock(HttpGet.class); HttpEntity httpEntity = mock(HttpEntity.class); when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("{\"get\":\"success\"}".getBytes( StandardCharsets.UTF_8 ))); Optional<Object> actual = RestFunctions.parseResponse(restConfig, httpGet, httpEntity); assertTrue(actual.isPresent()); assertEquals("success", ((Map<String, Object>) actual.get()).get("get")); }
@Test public void restGetShouldParseResponseOnNullHttpEntity() throws Exception { RestConfig restConfig = new RestConfig(); HttpGet httpGet = mock(HttpGet.class); assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, null)); }
@Test public void restGetShouldParseResponseOnNullContent() throws Exception { RestConfig restConfig = new RestConfig(); HttpGet httpGet = mock(HttpGet.class); HttpEntity httpEntity = mock(HttpEntity.class); when(httpEntity.getContent()).thenReturn(null); assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, httpEntity)); }
@Test public void restGetShouldParseResponseOnEmptyInputStream() throws Exception { RestConfig restConfig = new RestConfig(); HttpGet httpGet = mock(HttpGet.class); HttpEntity httpEntity = mock(HttpEntity.class); when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8))); assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, httpEntity)); }