ResourceFileNameHandler { public void writeResourceFile(final HttpServletResponse resp, final String path, final Path filePath) throws IOException { String requestedFilename = path.substring(1); InputStream messageFileInputStream = fileReader.readFile(requestedFilename, filePath); if (messageFileExists(messageFileInputStream)) { writeResourceFileInResponse(resp, requestedFilename, messageFileInputStream); } else { writeErrorToResponse(resp); } }  void writeResourceFile(final HttpServletResponse resp, final String path, final Path filePath);  }
@Test public void testWriteResourceFileShouldWriteInputStreamToOutputStream() throws IOException { given(fileReader.readFile(FILE_NAME, path)).willReturn(inputStream); given(response.getOutputStream()).willReturn(outputStream); given(inputStream.read(Mockito.any(byte[].class))).willReturn(1, -1); underTest.writeResourceFile(response, PATH_INFO, path); verify(outputStream).write(Mockito.any(byte[].class), Mockito.eq(0), Mockito.eq(1)); }
@Test public void testWriteResourceFileShouldSetHeaders() throws IOException { given(fileReader.readFile(FILE_NAME, path)).willReturn(inputStream); given(response.getOutputStream()).willReturn(outputStream); given(inputStream.read(Mockito.any(byte[].class))).willReturn(1, -1); underTest.writeResourceFile(response, PATH_INFO, path); verify(response).setContentType(TEXT); verify(response).setHeader(CONTENT_DISPOSITION, "attachment;filename=" + FILE_NAME); }
@Test public void testWriteResourceFileShouldCloseOutputStream() throws IOException { given(fileReader.readFile(FILE_NAME, path)).willReturn(inputStream); given(response.getOutputStream()).willReturn(outputStream); given(inputStream.read(Mockito.any(byte[].class))).willReturn(1, -1); underTest.writeResourceFile(response, PATH_INFO, path); verify(outputStream).flush(); verify(outputStream).close(); }
@Test public void testWriteResourceFileWhenFileDoesNotExistShouldWriteErrorResponse() throws IOException { given(fileReader.readFile(FILE_NAME, path)).willReturn(null); given(response.getWriter()).willReturn(writer); underTest.writeResourceFile(response, PATH_INFO, path); verify(response).setContentType(HTML); verify(writer).write(FILE_NOT_FOUND_MSG); verify(writer).flush(); verify(writer).close(); }