### Question:
ParseRESTCommand extends ParseRequest<JSONObject> { @Override protected Task<JSONObject> onResponseAsync(ParseHttpResponse response, ProgressCallback downloadProgressCallback) { String content; InputStream responseStream = null; try { responseStream = response.getContent(); content = new String(ParseIOUtils.toByteArray(responseStream)); } catch (IOException e) { return Task.forError(e); } finally { ParseIOUtils.closeQuietly(responseStream); } int statusCode = response.getStatusCode(); if (statusCode >= 200 && statusCode < 600) { JSONObject json; try { json = new JSONObject(content); if (statusCode >= 400 && statusCode < 500) { return Task.forError(newPermanentException(json.optInt("code"), json.optString("error"))); } else if (statusCode >= 500) { return Task.forError(newTemporaryException(json.optInt("code"), json.optString("error"))); } return Task.forResult(json); } catch (JSONException e) { return Task.forError(newTemporaryException("bad json response", e)); } } return Task.forError(newPermanentException(ParseException.OTHER_CAUSE, content)); } ParseRESTCommand(
      String httpPath,
      ParseHttpRequest.Method httpMethod,
      Map<String, ?> parameters,
      String sessionToken);  ParseRESTCommand(
      String httpPath,
      ParseHttpRequest.Method httpMethod,
      JSONObject jsonParameters,
      String sessionToken); private  ParseRESTCommand(
      String httpPath,
      ParseHttpRequest.Method httpMethod,
      JSONObject jsonParameters,
      String localId, String sessionToken);   ParseRESTCommand(Init<?> builder); static ParseRESTCommand fromJSONObject(JSONObject jsonObject); @Override Task<JSONObject> executeAsync(
      final ParseHttpClient client,
      final ProgressCallback uploadProgressCallback,
      final ProgressCallback downloadProgressCallback,
      final Task<Void> cancellationToken); String getCacheKey(); JSONObject toJSONObject(); String getSessionToken(); String getOperationSetUUID(); void setLocalId(String localId); String getLocalId(); void resolveLocalIds(); void retainLocalIds(); void releaseLocalIds(); public String masterKey; }### Answer:
@Test public void testOnResponseCloseNetworkStreamWithNormalResponse() throws Exception { int statusCode = 200; JSONObject bodyJson = new JSONObject(); bodyJson.put("key", "value"); String bodyStr = bodyJson.toString(); ByteArrayInputStream bodyStream = new ByteArrayInputStream(bodyStr.getBytes()); InputStream mockResponseStream = spy(bodyStream); doNothing() .when(mockResponseStream) .close(); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(statusCode) .setTotalSize((long) bodyStr.length()) .setContent(mockResponseStream) .build(); ParseRESTCommand command = new ParseRESTCommand.Builder().build(); JSONObject json = ParseTaskUtils.wait(command.onResponseAsync(mockResponse, null)); verify(mockResponseStream, times(1)).close(); assertEquals(bodyJson, json, JSONCompareMode.NON_EXTENSIBLE); }
@Test public void testOnResposneCloseNetworkStreamWithIOException() throws Exception { int statusCode = 200; InputStream mockResponseStream = mock(InputStream.class); doNothing() .when(mockResponseStream) .close(); IOException readException = new IOException("Error"); doThrow(readException) .when(mockResponseStream) .read(); doThrow(readException) .when(mockResponseStream) .read(any(byte[].class)); ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() .setStatusCode(statusCode) .setContent(mockResponseStream) .build(); ParseRESTCommand command = new ParseRESTCommand.Builder().build(); Task<JSONObject> responseTask = command.onResponseAsync(mockResponse, null); responseTask.waitForCompletion(); assertTrue(responseTask.isFaulted()); assertTrue(responseTask.getError() instanceof IOException); assertEquals("Error", responseTask.getError().getMessage()); verify(mockResponseStream, times(1)).close(); }