@Test public void sendsTheAccountNotification() throws Exception { ArgumentCaptor<EmailMessage> emailContentCaptor = ArgumentCaptor.forClass(EmailMessage.class); given(emailClient.sendEmail(emailContentCaptor.capture())).willReturn(success(ImmutableEmailResponse.builder().withMessage("message").build())); User user = user() .withUsername("username@me.com") .withFirstName("firstName") .withLastName("lastName") .build(); underTest.sendAccountCreationInformation(user, "password"); EmailMessage emailMessage = emailContentCaptor.getValue(); assertThat(emailMessage.to(), is("username@me.com")); assertThat(emailMessage.name(), is("firstName lastName")); assertThat(emailMessage.from(), is(appProperties.getSupportEmail())); assertThat(emailMessage.subject(), containsString("subject")); }
@Async public void sendAccountCreationInformation(User user, String password) { if (messageTemplate != null) { String emailContent = templateRenderer.render(messageTemplate, user.getUsername(), password); EmailMessage message = ImmutableEmailMessage.builder() .withName(user.getFirstName() + " " + user.getLastName()) .withTo(user.getUsername()) .withBody(emailContent) .withSubject(newAccountEmailSubject) .withFrom(supportEmail) .withCategory(EMAIL_CATEGORY) .build(); emailClient .sendEmail(message) .accept(failure -> log.error("Failed to send new account notification to {}", user), success -> log.info("Sent new account notification to {}", user)); } else { log.error("Cannot notify user that a new account has been created. Invalid email template configuration."); } }
NewAccountNotifier { @Async public void sendAccountCreationInformation(User user, String password) { if (messageTemplate != null) { String emailContent = templateRenderer.render(messageTemplate, user.getUsername(), password); EmailMessage message = ImmutableEmailMessage.builder() .withName(user.getFirstName() + " " + user.getLastName()) .withTo(user.getUsername()) .withBody(emailContent) .withSubject(newAccountEmailSubject) .withFrom(supportEmail) .withCategory(EMAIL_CATEGORY) .build(); emailClient .sendEmail(message) .accept(failure -> log.error("Failed to send new account notification to {}", user), success -> log.info("Sent new account notification to {}", user)); } else { log.error("Cannot notify user that a new account has been created. Invalid email template configuration."); } } }
NewAccountNotifier { @Async public void sendAccountCreationInformation(User user, String password) { if (messageTemplate != null) { String emailContent = templateRenderer.render(messageTemplate, user.getUsername(), password); EmailMessage message = ImmutableEmailMessage.builder() .withName(user.getFirstName() + " " + user.getLastName()) .withTo(user.getUsername()) .withBody(emailContent) .withSubject(newAccountEmailSubject) .withFrom(supportEmail) .withCategory(EMAIL_CATEGORY) .build(); emailClient .sendEmail(message) .accept(failure -> log.error("Failed to send new account notification to {}", user), success -> log.info("Sent new account notification to {}", user)); } else { log.error("Cannot notify user that a new account has been created. Invalid email template configuration."); } } @Autowired  NewAccountNotifier(EmailClient emailClient, TemplateRenderer templateRenderer, AppProperties appProperties); }
NewAccountNotifier { @Async public void sendAccountCreationInformation(User user, String password) { if (messageTemplate != null) { String emailContent = templateRenderer.render(messageTemplate, user.getUsername(), password); EmailMessage message = ImmutableEmailMessage.builder() .withName(user.getFirstName() + " " + user.getLastName()) .withTo(user.getUsername()) .withBody(emailContent) .withSubject(newAccountEmailSubject) .withFrom(supportEmail) .withCategory(EMAIL_CATEGORY) .build(); emailClient .sendEmail(message) .accept(failure -> log.error("Failed to send new account notification to {}", user), success -> log.info("Sent new account notification to {}", user)); } else { log.error("Cannot notify user that a new account has been created. Invalid email template configuration."); } } @Autowired  NewAccountNotifier(EmailClient emailClient, TemplateRenderer templateRenderer, AppProperties appProperties); @Async void sendAccountCreationInformation(User user, String password); }
NewAccountNotifier { @Async public void sendAccountCreationInformation(User user, String password) { if (messageTemplate != null) { String emailContent = templateRenderer.render(messageTemplate, user.getUsername(), password); EmailMessage message = ImmutableEmailMessage.builder() .withName(user.getFirstName() + " " + user.getLastName()) .withTo(user.getUsername()) .withBody(emailContent) .withSubject(newAccountEmailSubject) .withFrom(supportEmail) .withCategory(EMAIL_CATEGORY) .build(); emailClient .sendEmail(message) .accept(failure -> log.error("Failed to send new account notification to {}", user), success -> log.info("Sent new account notification to {}", user)); } else { log.error("Cannot notify user that a new account has been created. Invalid email template configuration."); } } @Autowired  NewAccountNotifier(EmailClient emailClient, TemplateRenderer templateRenderer, AppProperties appProperties); @Async void sendAccountCreationInformation(User user, String password);  }