|
21 | 21 | import org.skyscreamer.jsonassert.JSONCompareMode;
|
22 | 22 |
|
23 | 23 | import java.io.ByteArrayInputStream;
|
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
24 | 26 |
|
25 | 27 | import bolts.Task;
|
26 | 28 |
|
|
29 | 31 | import static org.junit.Assert.assertNull;
|
30 | 32 | import static org.junit.Assert.assertTrue;
|
31 | 33 | import static org.mockito.Matchers.any;
|
| 34 | +import static org.mockito.Mockito.doNothing; |
| 35 | +import static org.mockito.Mockito.doThrow; |
32 | 36 | import static org.mockito.Mockito.mock;
|
| 37 | +import static org.mockito.Mockito.spy; |
33 | 38 | import static org.mockito.Mockito.times;
|
34 | 39 | import static org.mockito.Mockito.verify;
|
35 | 40 | import static org.mockito.Mockito.when;
|
@@ -449,4 +454,62 @@ public void testFromJSONObject() throws Exception {
|
449 | 454 | assertEquals(localId, command.getLocalId());
|
450 | 455 | assertEquals(jsonParameters, command.jsonParameters, JSONCompareMode.NON_EXTENSIBLE);
|
451 | 456 | }
|
| 457 | + |
| 458 | + @Test |
| 459 | + public void testOnResponseCloseNetworkStreamWithNormalResponse() throws Exception { |
| 460 | + // Mock response stream |
| 461 | + int statusCode = 200; |
| 462 | + JSONObject bodyJson = new JSONObject(); |
| 463 | + bodyJson.put("key", "value"); |
| 464 | + String bodyStr = bodyJson.toString(); |
| 465 | + ByteArrayInputStream bodyStream = new ByteArrayInputStream(bodyStr.getBytes()); |
| 466 | + InputStream mockResponseStream = spy(bodyStream); |
| 467 | + doNothing() |
| 468 | + .when(mockResponseStream) |
| 469 | + .close(); |
| 470 | + // Mock response |
| 471 | + ParseHttpResponse response = mock(ParseHttpResponse.class); |
| 472 | + when(response.getStatusCode()).thenReturn(statusCode); |
| 473 | + when(response.getContent()).thenReturn(mockResponseStream); |
| 474 | + when(response.getTotalSize()).thenReturn(bodyStr.length()); |
| 475 | + |
| 476 | + |
| 477 | + ParseRESTCommand command = new ParseRESTCommand.Builder().build(); |
| 478 | + JSONObject json = ParseTaskUtils.wait(command.onResponseAsync(response, null)); |
| 479 | + |
| 480 | + verify(mockResponseStream, times(1)).close(); |
| 481 | + assertEquals(bodyJson, json, JSONCompareMode.NON_EXTENSIBLE); |
| 482 | + } |
| 483 | + |
| 484 | + @Test |
| 485 | + public void testOnResposneCloseNetworkStreamWithIOException() throws Exception { |
| 486 | + // Mock response stream |
| 487 | + int statusCode = 200; |
| 488 | + InputStream mockResponseStream = mock(InputStream.class); |
| 489 | + doNothing() |
| 490 | + .when(mockResponseStream) |
| 491 | + .close(); |
| 492 | + IOException readException = new IOException("Error"); |
| 493 | + doThrow(readException) |
| 494 | + .when(mockResponseStream) |
| 495 | + .read(); |
| 496 | + doThrow(readException) |
| 497 | + .when(mockResponseStream) |
| 498 | + .read(any(byte[].class)); |
| 499 | + // Mock response |
| 500 | + ParseHttpResponse response = mock(ParseHttpResponse.class); |
| 501 | + when(response.getStatusCode()).thenReturn(statusCode); |
| 502 | + when(response.getContent()).thenReturn(mockResponseStream); |
| 503 | + |
| 504 | + ParseRESTCommand command = new ParseRESTCommand.Builder().build(); |
| 505 | + // We can not use ParseTaskUtils here since it will replace the original exception with runtime |
| 506 | + // exception |
| 507 | + Task<JSONObject> responseTask = command.onResponseAsync(response, null); |
| 508 | + responseTask.waitForCompletion(); |
| 509 | + |
| 510 | + assertTrue(responseTask.isFaulted()); |
| 511 | + assertTrue(responseTask.getError() instanceof IOException); |
| 512 | + assertEquals("Error", responseTask.getError().getMessage()); |
| 513 | + verify(mockResponseStream, times(1)).close(); |
| 514 | + } |
452 | 515 | }
|
0 commit comments