EmailServiceFactory implements Discoverable { String getFileContents(@Nullable String filePath, String defaultFilePath) { if (filePath != null) { return readFileFromPath(filePath); } return readFileAsResources(defaultFilePath); }  @JsonGetter("messageOptions") MessageOptionsConfiguration getMessageOptionsConfiguration(); String getFromAddress(); Boolean isEnabled(); abstract EmailService createEmailService(MetricRegistry metrics); abstract EmailHealthCheck createHealthCheck(); MessageOptions getMessageOptions();  }
@Test void testGetFileContentsFromPath() throws Exception { String expected = Resources.toString( Resources.getResource(CUSTOM_BODY_TEXT_RESOURCE_FILE), StandardCharsets.UTF_8); assertEquals(expected, DEFAULT_FACTORY.getFileContents(CUSTOM_BODY_TEXT_FILE_PATH, CUSTOM_BODY_HTML_FILE_PATH)); }
@Test void testGetFileContentsFromInvalidPath() { EmailException invalidPath = assertThrows(EmailException.class, () -> DEFAULT_FACTORY.getFileContents("bad-path%name\0", CUSTOM_BODY_TEXT_FILE_PATH)); EmailException readError = assertThrows(EmailException.class, () -> DEFAULT_FACTORY.getFileContents("bad-path%name", CUSTOM_BODY_TEXT_FILE_PATH)); assertTrue(invalidPath.getCause() instanceof InvalidPathException); assertTrue(readError.getCause() instanceof IOException); try (MockedStatic<Files> filesMock = mockStatic(Files.class)) { filesMock.when(() -> Files.readString(any(Path.class), any(Charset.class))) .thenThrow(SecurityException.class); EmailException exception = assertThrows(EmailException.class, () -> DEFAULT_FACTORY.getFileContents("mytestpath", CUSTOM_BODY_TEXT_FILE_PATH)); assertTrue(exception.getCause() instanceof SecurityException); } }
@Test void testGetFileContentsFromInvalidResource() { EmailException nonexistent = assertThrows(EmailException.class, () -> DEFAULT_FACTORY.getFileContents(null, "not-exist")); assertTrue(nonexistent.getCause() instanceof IllegalArgumentException); try (MockedStatic<Resources> resourcesMock = mockStatic(Resources.class)) { resourcesMock.when(() -> Resources.toString(any(), any())).thenThrow(IOException.class); EmailException exception = assertThrows(EmailException.class, () -> DEFAULT_FACTORY.getFileContents(null, "not-exist\0")); assertTrue(exception.getCause() instanceof IOException); } }