AdminJsonService { @POST(path = "/backend/admin/send-test-email", permission = "admin:edit:smtp") String sendTestEmail(@BindRequest SmtpConfigDto configDto) throws IOException { SmtpConfigDto configDtoWithoutNewPassword; String passwordOverride; String newPassword = configDto.newPassword(); if (newPassword.isEmpty()) { configDtoWithoutNewPassword = configDto; passwordOverride = null; } else { configDtoWithoutNewPassword = ImmutableSmtpConfigDto.builder() .copyFrom(configDto) .newPassword("") .build(); passwordOverride = newPassword; } String testEmailRecipient = configDtoWithoutNewPassword.testEmailRecipient(); checkNotNull(testEmailRecipient); List<String> emailAddresses = Splitter.on(',').trimResults().splitToList(testEmailRecipient); try { String centralDisplay; String agentDisplay; if (central) { centralDisplay = configRepository.getCentralAdminGeneralConfig().centralDisplayName(); agentDisplay = ""; } else { centralDisplay = ""; agentDisplay = configRepository.getEmbeddedAdminGeneralConfig() .agentDisplayNameOrDefault(); } String subject = "Test email"; AlertingService.sendEmail(centralDisplay, agentDisplay, subject, emailAddresses, "", configDtoWithoutNewPassword.convert(configRepository), passwordOverride, configRepository.getLazySecretKey(), mailService); } catch (Exception e) { logger.debug(e.getMessage(), e); return createErrorResponse(e); } return "{}"; } AdminJsonService(boolean central, boolean offlineViewer, boolean webPortReadOnly,
            List<File> confDirs, ConfigRepository configRepository,
            RepoAdmin repoAdmin, LiveAggregateRepository liveAggregateRepository,
            MailService mailService, HttpClient httpClient);   }
@Test public void testWithoutDefaults() throws Exception { SmtpConfigDto configDto = ImmutableSmtpConfigDto.builder() .host("localhost") .username("") .passwordExists(false) .fromEmailAddress("from@example.org") .fromDisplayName("From Example") .version("1234") .testEmailRecipient("to@example.org") .build(); adminJsonService.sendTestEmail(configDto); Message message = mailService.getMessage(); assertThat(message.getFrom()[0].toString()).isEqualTo("From Example <from@example.org>"); assertThat(message.getRecipients(Message.RecipientType.TO)[0].toString()) .isEqualTo("to@example.org"); assertThat(message.getSubject()).isEqualTo("[Glowroot] Test email"); assertThat(message.getContent()).isEqualTo(""); }
@Test public void testWithDefaultFrom() throws Exception { SmtpConfigDto configDto = ImmutableSmtpConfigDto.builder() .host("localhost") .username("") .passwordExists(false) .fromEmailAddress("") .fromDisplayName("From Example") .version("1234") .testEmailRecipient("to@example.org") .build(); adminJsonService.sendTestEmail(configDto); Message message = mailService.getMessage(); String localHostname = InetAddress.getLocalHost().getHostName(); assertThat(message.getFrom()[0].toString()) .isEqualTo("From Example <glowroot@" + localHostname + ">"); assertThat(message.getRecipients(Message.RecipientType.TO)[0].toString()) .isEqualTo("to@example.org"); assertThat(message.getSubject()).isEqualTo("[Glowroot] Test email"); assertThat(message.getContent()).isEqualTo(""); }
@Test public void testWithDefaultFromAndDefaultDisplayName() throws Exception { SmtpConfigDto configDto = ImmutableSmtpConfigDto.builder() .host("localhost") .username("") .passwordExists(false) .fromEmailAddress("") .fromDisplayName("") .version("1234") .testEmailRecipient("to@example.org") .build(); adminJsonService.sendTestEmail(configDto); Message message = mailService.getMessage(); String localHostname = InetAddress.getLocalHost().getHostName(); assertThat(message.getFrom()[0].toString()) .isEqualTo("Glowroot <glowroot@" + localHostname + ">"); assertThat(message.getRecipients(Message.RecipientType.TO)[0].toString()) .isEqualTo("to@example.org"); assertThat(message.getSubject()).isEqualTo("[Glowroot] Test email"); assertThat(message.getContent()).isEqualTo(""); }