AWSEmailProvider implements EmailProvider { @Override public boolean sendEmail(Collection<String> recipients, String from, MimeMessage mimeMessage) { try { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { mimeMessage.writeTo(outputStream); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray())); SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage); SendRawEmailResult result = ses.sendRawEmail(rawEmailRequest); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Email with messageId={} sent successfully.", result.getMessageId()); } return result != null; } } catch (Exception ex) { LOGGER.error("The email could not be sent. Error message: {}", ex.getMessage()); return false; } } AWSEmailProvider();   AWSEmailProvider(AmazonSimpleEmailService ses); @Override boolean sendEmail(Collection<String> recipients, String from, MimeMessage mimeMessage);  }
@Test public void testSendEmail() { Set<String> recipients = new HashSet<>(Arrays.asList("user.user1", "user.user2", "user.user3")); String subject = "test email subject"; String body = "test email body"; System.setProperty("athenz.notification_email_domain_from", "from.example.com"); System.setProperty("athenz.notification_email_domain_to", "example.com"); System.setProperty("athenz.notification_email_from", "no-reply-athenz"); AmazonSimpleEmailService ses = mock(AmazonSimpleEmailService.class); SendRawEmailResult result = mock(SendRawEmailResult.class); Mockito.when(ses.sendRawEmail(any(SendRawEmailRequest.class))).thenReturn(result); AWSEmailProvider awsEmailProvider = new AWSEmailProvider(ses); ArgumentCaptor<SendRawEmailRequest> captor = ArgumentCaptor.forClass(SendRawEmailRequest.class); EmailNotificationService svc = new EmailNotificationService(awsEmailProvider); svc.sendEmail(recipients, subject, body); Mockito.verify(ses, atLeastOnce()).sendRawEmail(captor.capture()); System.clearProperty("athenz.notification_email_domain_from"); System.clearProperty("athenz.notification_email_domain_to"); System.clearProperty("athenz.notification_email_from"); }
@Test public void testSendEmailBatch() { Set<String> recipients = new HashSet<>(); for (int i =0; i<60; i++) { recipients.add("user.user" + i); } String subject = "test email subject"; String body = "test email body"; System.setProperty("athenz.notification_email_domain_from", "example.com"); System.setProperty("athenz.notification_email_domain_to", "example.com"); System.setProperty("athenz.notification_email_from", "no-reply-athenz"); AmazonSimpleEmailService ses = mock(AmazonSimpleEmailService.class); SendRawEmailResult result = mock(SendRawEmailResult.class); Mockito.when(ses.sendRawEmail(any(SendRawEmailRequest.class))).thenReturn(result); ArgumentCaptor<SendRawEmailRequest> captor = ArgumentCaptor.forClass(SendRawEmailRequest.class); AWSEmailProvider emailProvider = new AWSEmailProvider(ses); EmailNotificationService svc = new EmailNotificationService(emailProvider); boolean emailResult = svc.sendEmail(recipients, subject, body); assertTrue(emailResult); Mockito.verify(ses, times(2)).sendRawEmail(captor.capture()); System.clearProperty("athenz.notification_email_domain_from"); System.clearProperty("athenz.notification_email_domain_to"); System.clearProperty("athenz.notification_email_from"); }
@Test public void testSendEmailBatchError() { Set<String> recipients = new HashSet<>(); for (int i =0; i<60; i++) { recipients.add("user.user" + i); } String subject = "test email subject"; String body = "test email body"; System.setProperty("athenz.notification_email_domain_from", "example.com"); System.setProperty("athenz.notification_email_domain_to", "example.com"); System.setProperty("athenz.notification_email_from", "no-reply-athenz"); AmazonSimpleEmailService ses = mock(AmazonSimpleEmailService.class); SendRawEmailResult result = mock(SendRawEmailResult.class); Mockito.when(ses.sendRawEmail(any(SendRawEmailRequest.class))) .thenReturn(null) .thenReturn(result); ArgumentCaptor<SendRawEmailRequest> captor = ArgumentCaptor.forClass(SendRawEmailRequest.class); AWSEmailProvider emailProvider = new AWSEmailProvider(ses); EmailNotificationService svc = new EmailNotificationService(emailProvider); boolean emailResult = svc.sendEmail(recipients, subject, body); assertFalse(emailResult); Mockito.verify(ses, times(2)).sendRawEmail(captor.capture()); System.clearProperty("athenz.notification_email_domain_from"); System.clearProperty("athenz.notification_email_domain_to"); System.clearProperty("athenz.notification_email_from"); }
@Test public void testSendEmailError() { Set<String> recipients = new HashSet<>(Arrays.asList("user.user1", "user.user2", "user.user3")); String subject = "test email subject"; String body = "test email body"; System.setProperty("athenz.notification_email_domain_to", "example.com"); System.setProperty("athenz.notification_email_domain_from", "from.example.com"); System.setProperty("athenz.notification_email_from", "no-reply-athenz"); AmazonSimpleEmailService ses = mock(AmazonSimpleEmailService.class); Mockito.when(ses.sendEmail(any(SendEmailRequest.class))).thenThrow(new RuntimeException()); AWSEmailProvider awsEmailProvider = new AWSEmailProvider(ses); EmailNotificationService svc = new EmailNotificationService(awsEmailProvider); boolean result = svc.sendEmail(recipients, subject, body); assertFalse(result); System.clearProperty("athenz.notification_email_domain_from"); System.clearProperty("athenz.notification_email_domain_to"); System.clearProperty("athenz.notification_email_from"); }