FileReader { public InputStream readFile(final String filename, final Path filePath) { File requestedFile = getFile(filename, filePath); InputStream fileInputStream = null; if (requestedFile.isFile()) { if (requestedFile.exists()) { try { fileInputStream = fileInputStreamFactory.createFileInputStream(requestedFile); } catch (FileNotFoundException e) { logger.warn("Message file not found!", e); } } } return fileInputStream; }  InputStream readFile(final String filename, final Path filePath);  }
@Test public final void testReadFileShouldReturnNullWhenRequestedFileDoesNotExist() { given(path.resolve(FILE_NAME)).willReturn(messageFilePath); given(messageFilePath.toFile()).willReturn(messageFile); given(messageFile.isFile()).willReturn(true); given(messageFile.exists()).willReturn(false); InputStream actual = underTest.readFile(FILE_NAME, path); assertNull(actual); }
@Test public final void testReadFileShouldReturnNullWhenRequestedFileIsNotAFile() { given(path.resolve(FILE_NAME)).willReturn(messageFilePath); given(messageFilePath.toFile()).willReturn(messageFile); given(messageFile.isFile()).willReturn(false); given(messageFile.exists()).willReturn(false); InputStream actual = underTest.readFile(FILE_NAME, path); assertNull(actual); }
@Test public final void testReadFileShouldReturnFileInputStreamIfFileExists() throws FileNotFoundException { given(path.resolve(FILE_NAME)).willReturn(messageFilePath); given(messageFilePath.toFile()).willReturn(messageFile); given(messageFile.exists()).willReturn(true); given(messageFile.isFile()).willReturn(true); given(fileInputStreamFactory.createFileInputStream(messageFile)).willReturn(fileInputStream); InputStream actual = underTest.readFile(FILE_NAME, path); assertEquals(actual, fileInputStream); }
@Test public final void testReadFileShouldLogWarningWhenFileWasNotFoundAndReturnNull() throws FileNotFoundException { given(path.resolve(FILE_NAME)).willReturn(messageFilePath); given(messageFilePath.toFile()).willReturn(messageFile); given(messageFile.exists()).willReturn(true); given(messageFile.isFile()).willReturn(true); given(fileInputStreamFactory.createFileInputStream(messageFile)).willThrow(new FileNotFoundException()); InputStream actual = underTest.readFile(FILE_NAME, path); assertNull(actual); verify(logger).warn(anyString(), any(FileNotFoundException.class)); }