HttpResponses { public static String asResponseString(final DownloadResponse downloadResponse) { if (downloadResponse == null) { return null; } try { return new String(downloadResponse.getByteArray(), "UTF-8"); } catch (Exception e) { return null; } } private  HttpResponses(); static Bitmap asBitmap(final DownloadResponse downloadResponse); static JSONObject asJsonObject(final DownloadResponse downloadResponse); static String asResponseString(final DownloadResponse downloadResponse);  }
@Test public void asResponseString_withMultipleLines_shouldReturnResponseAndCloseStream() throws Exception { String responseData = "1\n2\n3\n4"; when(downloadResponse1.getByteArray()).thenReturn(responseData.getBytes()); final String responseString = HttpResponses.asResponseString(downloadResponse1); assertThat(responseString).isEqualTo(responseData); }
@Test public void asResponseString_shouldReturnResponseAndCloseStream() throws Exception { String responseData = "response_string"; when(downloadResponse1.getByteArray()).thenReturn(responseData.getBytes()); final String responseString = HttpResponses.asResponseString(downloadResponse1); assertThat(responseString).isEqualTo(responseData); }
@Test public void asResponseString_newStringThrowsException_shouldReturnNull() throws Exception { when(downloadResponse1.getByteArray()).thenReturn(null); assertThat(HttpResponses.asResponseString(downloadResponse1)).isNull(); }
@Test public void asResponseString_withNullResponse_shouldReturnNull() throws Exception { final String responseString = HttpResponses.asResponseString(null); assertThat(responseString).isNull(); }