mirror of
https://github.com/google/nomulus.git
synced 2025-07-24 03:30:46 +02:00
Refactor common email sending utility
The main thrust of this is to create a common POJO that contains email content in a simple way, then have one class that converts that to an email and sends it. Any class that uses email should only have to deal with creating that POJO. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237883643
This commit is contained in:
parent
792cd21da7
commit
058c12c343
35 changed files with 773 additions and 614 deletions
|
@ -15,132 +15,94 @@
|
|||
package google.registry.reporting.billing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeSleeper;
|
||||
import google.registry.util.Retrier;
|
||||
import google.registry.util.EmailMessage;
|
||||
import google.registry.util.EmailMessage.Attachment;
|
||||
import google.registry.util.SendEmailService;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Properties;
|
||||
import javax.mail.BodyPart;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import org.joda.time.YearMonth;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
/** Unit tests for {@link google.registry.reporting.billing.BillingEmailUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class BillingEmailUtilsTest {
|
||||
|
||||
private static final int RETRY_COUNT = 2;
|
||||
|
||||
private SendEmailService emailService;
|
||||
private BillingEmailUtils emailUtils;
|
||||
private GcsUtils gcsUtils;
|
||||
private ArgumentCaptor<Message> msgCaptor;
|
||||
private ArgumentCaptor<EmailMessage> contentCaptor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws Exception {
|
||||
emailService = mock(SendEmailService.class);
|
||||
when(emailService.createMessage())
|
||||
.thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties(), null)));
|
||||
gcsUtils = mock(GcsUtils.class);
|
||||
when(gcsUtils.openInputStream(new GcsFilename("test-bucket", "results/REG-INV-2017-10.csv")))
|
||||
.thenReturn(
|
||||
new ByteArrayInputStream("test,data\nhello,world".getBytes(StandardCharsets.UTF_8)));
|
||||
msgCaptor = ArgumentCaptor.forClass(Message.class);
|
||||
contentCaptor = ArgumentCaptor.forClass(EmailMessage.class);
|
||||
|
||||
emailUtils =
|
||||
new BillingEmailUtils(
|
||||
emailService,
|
||||
new YearMonth(2017, 10),
|
||||
"my-sender@test.com",
|
||||
"my-receiver@test.com",
|
||||
ImmutableList.of("hello@world.com", "hola@mundo.com"),
|
||||
new InternetAddress("my-sender@test.com"),
|
||||
new InternetAddress("my-receiver@test.com"),
|
||||
ImmutableList.of(
|
||||
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")),
|
||||
"test-bucket",
|
||||
"REG-INV",
|
||||
"results/",
|
||||
gcsUtils,
|
||||
new Retrier(new FakeSleeper(new FakeClock()), RETRY_COUNT));
|
||||
gcsUtils);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_emailOverallInvoice() throws MessagingException, IOException {
|
||||
public void testSuccess_emailOverallInvoice() throws MessagingException {
|
||||
emailUtils.emailOverallInvoice();
|
||||
// We inspect individual parameters because Message doesn't implement equals().
|
||||
verify(emailService).sendMessage(msgCaptor.capture());
|
||||
Message expectedMsg = msgCaptor.getValue();
|
||||
assertThat(expectedMsg.getFrom())
|
||||
.asList()
|
||||
.containsExactly(new InternetAddress("my-sender@test.com"));
|
||||
assertThat(expectedMsg.getAllRecipients())
|
||||
.asList()
|
||||
.containsExactly(
|
||||
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com"));
|
||||
assertThat(expectedMsg.getSubject()).isEqualTo("Domain Registry invoice data 2017-10");
|
||||
assertThat(expectedMsg.getContent()).isInstanceOf(Multipart.class);
|
||||
Multipart contents = (Multipart) expectedMsg.getContent();
|
||||
assertThat(contents.getCount()).isEqualTo(2);
|
||||
assertThat(contents.getBodyPart(0)).isInstanceOf(BodyPart.class);
|
||||
BodyPart textPart = contents.getBodyPart(0);
|
||||
assertThat(textPart.getContentType()).isEqualTo("text/plain; charset=us-ascii");
|
||||
assertThat(textPart.getContent().toString())
|
||||
.isEqualTo("Attached is the 2017-10 invoice for the domain registry.");
|
||||
assertThat(contents.getBodyPart(1)).isInstanceOf(BodyPart.class);
|
||||
BodyPart attachmentPart = contents.getBodyPart(1);
|
||||
assertThat(attachmentPart.getContentType()).endsWith("name=REG-INV-2017-10.csv");
|
||||
assertThat(attachmentPart.getContent().toString()).isEqualTo("test,data\nhello,world");
|
||||
|
||||
verify(emailService).sendEmail(contentCaptor.capture());
|
||||
EmailMessage emailMessage = contentCaptor.getValue();
|
||||
EmailMessage expectedContent =
|
||||
EmailMessage.newBuilder()
|
||||
.setFrom(new InternetAddress("my-sender@test.com"))
|
||||
.setRecipients(
|
||||
ImmutableList.of(
|
||||
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")))
|
||||
.setSubject("Domain Registry invoice data 2017-10")
|
||||
.setBody("Attached is the 2017-10 invoice for the domain registry.")
|
||||
.setAttachment(
|
||||
Attachment.newBuilder()
|
||||
.setContent("test,data\nhello,world")
|
||||
.setContentType(MediaType.CSV_UTF_8)
|
||||
.setFilename("REG-INV-2017-10.csv")
|
||||
.build())
|
||||
.build();
|
||||
assertThat(emailMessage).isEqualTo(expectedContent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_tooManyRetries_emailsAlert() throws MessagingException, IOException {
|
||||
// This message throws whenever it tries to set content, to force the overall invoice to fail.
|
||||
Message throwingMessage = mock(Message.class);
|
||||
doThrow(new MessagingException("expected"))
|
||||
.when(throwingMessage)
|
||||
.setContent(any(Multipart.class));
|
||||
when(emailService.createMessage()).thenAnswer(
|
||||
new Answer<Message>() {
|
||||
private int count = 0;
|
||||
|
||||
@Override
|
||||
public Message answer(InvocationOnMock invocation) {
|
||||
// Once we've failed the retry limit for the original invoice, return a normal message
|
||||
// so we can properly check its contents.
|
||||
if (count < RETRY_COUNT) {
|
||||
count++;
|
||||
return throwingMessage;
|
||||
} else if (count == RETRY_COUNT) {
|
||||
return new MimeMessage(Session.getDefaultInstance(new Properties(), null));
|
||||
} else {
|
||||
assertWithMessage("Attempted to generate too many messages!").fail();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
public void testFailure_emailsAlert() throws MessagingException {
|
||||
doThrow(new RuntimeException(new MessagingException("expected")))
|
||||
.doNothing()
|
||||
.when(emailService)
|
||||
.sendEmail(contentCaptor.capture());
|
||||
RuntimeException thrown =
|
||||
assertThrows(RuntimeException.class, () -> emailUtils.emailOverallInvoice());
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("Emailing invoice failed");
|
||||
|
@ -149,26 +111,24 @@ public class BillingEmailUtilsTest {
|
|||
.hasMessageThat()
|
||||
.isEqualTo("javax.mail.MessagingException: expected");
|
||||
// Verify we sent an e-mail alert
|
||||
verify(emailService).sendMessage(msgCaptor.capture());
|
||||
validateAlertMessage(msgCaptor.getValue(), "Emailing invoice failed due to expected");
|
||||
verify(emailService, times(2)).sendEmail(contentCaptor.capture());
|
||||
validateAlertMessage(contentCaptor.getValue(), "Emailing invoice failed due to expected");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_sendAlertEmail() throws MessagingException, IOException {
|
||||
public void testSuccess_sendAlertEmail() throws MessagingException {
|
||||
emailUtils.sendAlertEmail("Alert!");
|
||||
verify(emailService).sendMessage(msgCaptor.capture());
|
||||
validateAlertMessage(msgCaptor.getValue(), "Alert!");
|
||||
verify(emailService).sendEmail(contentCaptor.capture());
|
||||
validateAlertMessage(contentCaptor.getValue(), "Alert!");
|
||||
}
|
||||
|
||||
private void validateAlertMessage(Message msg, String body)
|
||||
throws MessagingException, IOException {
|
||||
assertThat(msg.getFrom()).hasLength(1);
|
||||
assertThat(msg.getFrom()[0]).isEqualTo(new InternetAddress("my-sender@test.com"));
|
||||
assertThat(msg.getAllRecipients())
|
||||
.asList()
|
||||
private void validateAlertMessage(EmailMessage emailMessage, String body)
|
||||
throws MessagingException {
|
||||
assertThat(emailMessage.from()).isEqualTo(new InternetAddress("my-sender@test.com"));
|
||||
assertThat(emailMessage.recipients())
|
||||
.containsExactly(new InternetAddress("my-receiver@test.com"));
|
||||
assertThat(msg.getSubject()).isEqualTo("Billing Pipeline Alert: 2017-10");
|
||||
assertThat(msg.getContentType()).isEqualTo("text/plain");
|
||||
assertThat(msg.getContent().toString()).isEqualTo(body);
|
||||
assertThat(emailMessage.subject()).isEqualTo("Billing Pipeline Alert: 2017-10");
|
||||
assertThat(emailMessage.contentType()).isEmpty();
|
||||
assertThat(emailMessage.body()).isEqualTo(body);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue