@Test public void shouldSendMessageWithFileSystemTemplate() throws EmailException, MessagingException, IOException { final File tempFile = File.createTempFile("dummy", "tmp"); final File tempDir = tempFile.getParentFile(); tempFile.deleteOnExit(); final File testTemplate = new File(tempDir, "testtemplate.vm"); testTemplate.deleteOnExit(); final FileWriter out = new FileWriter(testTemplate); out.write("<p>Now is the winter of our $item</p>\n"); out.close(); unit.setTemplateDirectory(tempDir.getAbsolutePath()); when(mailSender.createMimeMessage()).thenReturn(mimeMessage); final Map<String, Object> templateProperties = new HashMap<String, Object>(); templateProperties.put("item", "discontent"); unit.send( TO_EXAMPLE, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "testtemplate", templateProperties); verify(mailSender).send(mimeMessage); verifyExpectationsForMimeMessage( FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "<p>Now is the winter of our discontent</p>\n", TO_EXAMPLE); }
@Test public void shouldSendMessageWithNullProperties() throws EmailException, MessagingException { when(mailSender.createMimeMessage()).thenReturn(mimeMessage); unit.send(TO_EXAMPLE, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "demo", null); verify(mailSender).send(mimeMessage); verifyExpectationsForMimeMessage( FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "<b>Hello, $subject</b>\n", TO_EXAMPLE); }
@Test public void shouldSendMessageWithNoSubject() throws EmailException, MessagingException { when(mailSender.createMimeMessage()).thenReturn(mimeMessage); final Map<String, Object> templateProperties = new HashMap<String, Object>(); templateProperties.put("subject", "world"); unit.send(TO_EXAMPLE, FROM_ADDRESS_DEFAULT, null, "demo", templateProperties); verify(mailSender).send(mimeMessage); verifyExpectationsForMimeMessage(FROM_ADDRESS_DEFAULT, null, CONTENT_DEFAULT, TO_EXAMPLE); }
@Test public void shouldNotSendMessageWhenEmailEnabledPropertyIsPresentAndFalse() throws EmailException, MessagingException { reset(yazinoConfiguration); when(yazinoConfiguration.getBoolean(EmailService.PROPERTY_EMAIL_ENABLED, true)) .thenReturn(false); final Map<String, Object> templateProperties = new HashMap<String, Object>(); templateProperties.put("subject", "world"); unit.send(TO_EXAMPLE, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "demo", templateProperties); verifyZeroInteractions(mailSender); }
@Test(expected = EmailException.class) public void shouldThrowExceptionOnSendFailure() throws EmailException, MessagingException { when(mailSender.createMimeMessage()).thenReturn(mimeMessage); doThrow(new MailSendException("it's all gone bad")).when(mailSender).send(mimeMessage); final Map<String, Object> templateProperties = new HashMap<String, Object>(); templateProperties.put("subject", "world"); unit.send(TO_EXAMPLE, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "demo", templateProperties); verifyExpectationsForMimeMessage( FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, CONTENT_DEFAULT, TO_EXAMPLE); }
@Test public void shouldSendMessagesToMultipleRecipients() throws MessagingException, EmailException { when(mailSender.createMimeMessage()).thenReturn(mimeMessage); String otherTo = "*****@*****.**"; final Map<String, Object> templateProperties = new HashMap<String, Object>(); templateProperties.put("subject", "world"); unit.send( new String[] {TO_EXAMPLE, otherTo}, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "demo", templateProperties); verify(mailSender).send(mimeMessage); verifyExpectationsForMimeMessage( FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, CONTENT_DEFAULT, TO_EXAMPLE, otherTo); }
@Test(expected = EmailException.class) public void shouldThrowExceptionForInvalidTemplate() throws EmailException { unit.send(TO_EXAMPLE, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "demo-not-existing", null); }
@Test(expected = EmailException.class) public void shouldThrowExceptionForInvalidFSPath() throws EmailException { unit.setTemplateDirectory("/nowhere/possibly/I/hope"); unit.send(TO_EXAMPLE, FROM_ADDRESS_DEFAULT, SUBJECT_DEFAULT, "testtemplate", null); }