SimpleEmailService implements EmailService { @Override public CompletableFuture<Set<String>> sendSimpleEmail(String subject, String message, String... userEmails) { String htmlBody = emailTemplate.substring(emailTemplate.indexOf(BODY_BINDING) + BODY_BINDING.length(), emailTemplate.lastIndexOf(BODY_BINDING)); htmlBody = htmlBody.replace(MESSAGE_BINDING, message); return sendEmail(subject, htmlBody, userEmails); }  @Override CompletableFuture<Set<String>> sendSimpleEmail(String subject, String message, String... userEmails); @Override CompletableFuture<Set<String>> sendEmail(String subject, String htmlMessage, String... userEmails);  }
@Test public void sendSimpleEmailTest() throws Exception { CompletableFuture<Set<String>> cf = es.sendSimpleEmail(SUBJECT_LINE, TEXT_MESSAGE, TO_EMAIL_ADDRESS); Set<String> failedEmails = cf.get(); assertEquals(0, failedEmails.size()); List<SmtpMessage> emails = smtpServer.getReceivedEmails(); assertEquals(1, emails.size()); SmtpMessage email = emails.get(0); assertEquals(SUBJECT_LINE, email.getHeaderValue("Subject")); assertEquals(TO_EMAIL_ADDRESS, email.getHeaderValue("From")); assertTrue(email.getBody().contains(TEXT_MESSAGE)); }
@Test public void sendSimpleEmailInvalidAddressTest() throws Exception { CompletableFuture<Set<String>> cf = es.sendSimpleEmail(SUBJECT_LINE, TEXT_MESSAGE, "badAddress"); Set<String> failedEmails = cf.get(); assertEquals(1, failedEmails.size()); assertEquals("badAddress", failedEmails.iterator().next()); }
@Test public void sendSimpleEmailInvalidAndValidAddressTest() throws Exception { CompletableFuture<Set<String>> cf = es.sendSimpleEmail(SUBJECT_LINE, TEXT_MESSAGE, "badAddress", TO_EMAIL_ADDRESS); Set<String> failedEmails = cf.get(); assertEquals(1, failedEmails.size()); assertEquals("badAddress", failedEmails.iterator().next()); }
@Test(expected = ExecutionException.class) public void sendSimpleEmailInvalidPortTest() throws Exception { config.replace("port", 1); Method m = es.getClass().getDeclaredMethod("modified", BundleContext.class, Map.class); m.setAccessible(true); m.invoke(es, bundleContext, config); CompletableFuture<Set<String>> cf = es.sendSimpleEmail(SUBJECT_LINE, TEXT_MESSAGE, TO_EMAIL_ADDRESS); cf.get(); }
@Test public void sendSimpleEmailAbsoluteTemplateTest() throws Exception { config.replace("emailTemplate", URLDecoder.decode(templatePath.getPath(), "UTF-8")); Method m = es.getClass().getDeclaredMethod("modified", BundleContext.class, Map.class); m.setAccessible(true); m.invoke(es, bundleContext, config); CompletableFuture<Set<String>> cf = es.sendSimpleEmail(SUBJECT_LINE, TEXT_MESSAGE, TO_EMAIL_ADDRESS); Set<String> failedEmails = cf.get(); assertEquals(0, failedEmails.size()); List<SmtpMessage> emails = smtpServer.getReceivedEmails(); assertEquals(1, emails.size()); SmtpMessage email = emails.get(0); assertEquals(SUBJECT_LINE, email.getHeaderValue("Subject")); assertEquals(TO_EMAIL_ADDRESS, email.getHeaderValue("From")); assertTrue(email.getBody().contains(TEXT_MESSAGE)); }