DefaultEmailService implements EmailService { @Override public MimeMessage send(final @NonNull Email email) { email.setSentAt(new Date()); final MimeMessage mimeMessage = toMimeMessage(email); javaMailSender.send(mimeMessage); emailLogRenderer.info("Sent email {}.", email); return mimeMessage; } @Autowired(required = false)  DefaultEmailService(final @NonNull JavaMailSender javaMailSender,
                               final TemplateService templateService,
                               final @NonNull EmailToMimeMessage emailToMimeMessage,
                               final @NonNull EmailLogRenderer emailLogRenderer); @Autowired(required = false)  DefaultEmailService(final @NonNull JavaMailSender javaMailSender,
                               final @NonNull EmailToMimeMessage emailToMimeMessage,
                               final @NonNull EmailLogRenderer emailLogRenderer); @Override MimeMessage send(final @NonNull Email email); MimeMessage send(final @NonNull Email email,
                            final @NonNull String template,
                            final Map<String, Object> modelObject,
                            final @NonNull InlinePicture... inlinePictures);  }
@Test public void sendMailWithoutTemplate() throws MessagingException, IOException { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); final MimeMessage sentMessage = mailService.send(email); assertThat(email.getSentAt(), not(is(nullValue()))); validateFrom(email, sentMessage); validateReplyTo(email, sentMessage); validateTo(email, sentMessage); validateCc(email, sentMessage); validateBcc(email, sentMessage); validateSubject(email, sentMessage); validateBody(email, sentMessage); verify(javaMailSender, times(1)).createMimeMessage(); verify(javaMailSender, times(1)).send(any(MimeMessage.class)); }
@Test public void sendMailWithTemplate() throws MessagingException, IOException, TemplateException, CannotSendEmailException { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); final String toBeOverriddenBody = email.getBody(); final String bodyToBeReturned = "Ciao Tito"; when(templateService.mergeTemplateIntoString(any(String.class), any(Map.class))).thenReturn(bodyToBeReturned); final MimeMessage sentMessage = mailService.send(email, "never_called.ftl", Maps.newHashMap()); assertThat(email.getSentAt(), not(is(nullValue()))); validateFrom(email, sentMessage); validateReplyTo(email, sentMessage); validateTo(email, sentMessage); validateCc(email, sentMessage); validateBcc(email, sentMessage); validateSubject(email, sentMessage); assertThat(((MimeMultipart) sentMessage.getContent()).getBodyPart(0).getContent(), allOf(not(is(toBeOverriddenBody)), is(bodyToBeReturned))); verify(templateService, times(1)).mergeTemplateIntoString(any(String.class), any(Map.class)); verify(javaMailSender, times(1)).createMimeMessage(); verify(javaMailSender, times(1)).send(any(MimeMessage.class)); }
@Test public void sendMailWithTemplateAndInlinePicture() throws MessagingException, IOException, TemplateException, CannotSendEmailException, URISyntaxException { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); final String toBeOverriddenBody = email.getBody(); final String bodyToBeReturned = "<img src=\"100_percent_free.jpg\" />"; final String imageName = "100_percent_free.jpg"; when(templateService.mergeTemplateIntoString(any(String.class), any(Map.class))).thenReturn(bodyToBeReturned); final File inlineImageFile = new File(getClass().getClassLoader() .getResource("images" + File.separator + imageName).toURI()); final MimeMessage sentMessage = mailService.send(email, "never_called.ftl", Maps.newHashMap(), getInlinePicture(inlineImageFile, imageName)); assertThat(email.getSentAt(), not(is(nullValue()))); validateFrom(email, sentMessage); validateReplyTo(email, sentMessage); validateTo(email, sentMessage); validateCc(email, sentMessage); validateBcc(email, sentMessage); validateSubject(email, sentMessage); final String imageId = ((MimeBodyPart) (((MimeMultipart) sentMessage.getContent()).getBodyPart(0))).getContentID(); assertThat(((MimeMultipart) sentMessage.getContent()).getBodyPart(1).getContent(), allOf(not(is(toBeOverriddenBody)), not(is(bodyToBeReturned)), is("<img src=\"cid:" + imageId.substring(1, imageId.length() - 1) + "\" />"))); verify(templateService, times(1)).mergeTemplateIntoString(any(String.class), any(Map.class)); }
@Test public void sendMailWithoutTemplateShouldThrowExceptionWhenEmailIsNull() { thrown.expect(NullPointerException.class); mailService.send(null); fail(); }
@Test public void sendMailWithTemplateShouldThrowExceptionWhenEmailIsNull() throws CannotSendEmailException { final String imageName = "100_percent_free.jpg"; final File inlineImageFile = new File(getClass().getClassLoader() .getResource("images" + File.separator + imageName).getFile()); thrown.expect(NullPointerException.class); mailService.send(null, "never_called.ftl", Maps.newHashMap(), getInlinePicture(inlineImageFile, imageName)); fail(); }
@Test public void sendMailWithTemplateShouldThrowExceptionWhenTemplateIsNull() throws CannotSendEmailException, UnsupportedEncodingException { final Email email = getSimpleMail(); final String imageName = "100_percent_free.jpg"; final File inlineImageFile = new File(getClass().getClassLoader() .getResource("images" + File.separator + imageName).getFile()); thrown.expect(NullPointerException.class); mailService.send(email, null, Maps.newHashMap(), getInlinePicture(inlineImageFile, imageName)); fail(); }
@Test public void sendMailWithTemplateAndInlinePictureThrowExceptionWhenPictureIsNull() throws IOException, CannotSendEmailException, TemplateException { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); when(templateService.mergeTemplateIntoString(any(String.class), any(Map.class))).thenReturn("doesn't matter"); thrown.expect(NullPointerException.class); mailService.send(email, "never_called.ftl", Maps.newHashMap(), null); fail(); }
@Test public void shouldSendMailWithTemplateCatchIOException() throws Exception { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); when(templateService.mergeTemplateIntoString(any(String.class), any(Map.class))).thenThrow(IOException.class); thrown.expect(CannotSendEmailException.class); thrown.expectMessage("Error while sending the email due to problems with the template file."); thrown.expectCause(instanceOf(IOException.class)); mailService.send(email, "never_called.ftl", Maps.newHashMap()); fail(); }
@Test public void shouldSendMailWithTemplateCatchTemplateException() throws Exception { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); when(templateService.mergeTemplateIntoString(any(String.class), any(Map.class))).thenThrow(TemplateException.class); thrown.expect(CannotSendEmailException.class); thrown.expectMessage("Error while processing the template file with the given model object."); thrown.expectCause(instanceOf(TemplateException.class)); mailService.send(email, "never_called.ftl", Maps.newHashMap()); fail(); }
@Test public void shouldSendMailWithTemplateCatchMessagingException() throws Exception { final Email email = getSimpleMail(); assertThat(email.getSentAt(), is(nullValue())); when(templateService.mergeTemplateIntoString(any(String.class), any(Map.class))).thenThrow(MessagingException.class); thrown.expect(CannotSendEmailException.class); thrown.expectMessage("Error while sending the email due to problems with the mime content."); thrown.expectCause(instanceOf(MessagingException.class)); mailService.send(email, "never_called.ftl", Maps.newHashMap()); fail(); }