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:
gbrodman 2019-03-11 14:30:41 -07:00 committed by Ben McIlwain
parent 9823ee7fcf
commit 50e0a9b532
35 changed files with 773 additions and 614 deletions

View file

@ -20,6 +20,7 @@ import static google.registry.model.common.GaeUserIdConverter.convertEmailAddres
import static google.registry.model.registrar.Registrar.loadByClientId;
import static google.registry.testing.DatastoreHelper.persistPremiumList;
import static javax.servlet.http.HttpServletResponse.SC_MOVED_TEMPORARILY;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.appengine.api.users.User;
@ -41,12 +42,10 @@ import google.registry.testing.DeterministicStringGenerator;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse;
import google.registry.ui.server.SendEmailUtils;
import google.registry.util.EmailMessage;
import google.registry.util.SendEmailService;
import java.util.Optional;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import org.joda.money.CurrencyUnit;
import org.junit.Before;
@ -54,6 +53,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
@ -74,10 +74,9 @@ public final class ConsoleRegistrarCreatorActionTest {
@Mock HttpServletRequest request;
@Mock SendEmailService emailService;
Message message;
@Before
public void setUp() {
public void setUp() throws Exception {
persistPremiumList("default_sandbox_list", "sandbox,USD 1000");
action.req = request;
@ -92,7 +91,7 @@ public final class ConsoleRegistrarCreatorActionTest {
action.registryEnvironment = RegistryEnvironment.UNITTEST;
action.sendEmailUtils =
new SendEmailUtils(
"outgoing@registry.example",
new InternetAddress("outgoing@registry.example"),
"UnitTest Registry",
ImmutableList.of("notification@test.example", "notification2@test.example"),
emailService);
@ -120,9 +119,6 @@ public final class ConsoleRegistrarCreatorActionTest {
action.passwordGenerator = new DeterministicStringGenerator("abcdefghijklmnopqrstuvwxyz");
action.passcodeGenerator = new DeterministicStringGenerator("314159265");
message = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
when(emailService.createMessage()).thenReturn(message);
}
@Test
@ -156,7 +152,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_minimalAddress() throws Exception {
public void testPost_authorized_minimalAddress() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.billingAccount = Optional.of("USD=billing-account");
@ -176,8 +172,11 @@ public final class ConsoleRegistrarCreatorActionTest {
assertThat(response.getPayload())
.contains("<h1>Successfully created Registrar myclientid</h1>");
assertThat(message.getSubject()).isEqualTo("Registrar myclientid created in unittest");
assertThat(message.getContent())
ArgumentCaptor<EmailMessage> contentCaptor = ArgumentCaptor.forClass(EmailMessage.class);
verify(emailService).sendEmail(contentCaptor.capture());
EmailMessage emailMessage = contentCaptor.getValue();
assertThat(emailMessage.subject()).isEqualTo("Registrar myclientid created in unittest");
assertThat(emailMessage.body())
.isEqualTo(
""
+ "The following registrar was created in unittest by TestUserId:\n"
@ -221,7 +220,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_allAddress() throws Exception {
public void testPost_authorized_allAddress() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.billingAccount = Optional.of("USD=billing-account");
@ -257,7 +256,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_multipleBillingLines() throws Exception {
public void testPost_authorized_multipleBillingLines() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.ianaId = Optional.of(12321);
@ -294,7 +293,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_repeatingCurrency_fails() throws Exception {
public void testPost_authorized_repeatingCurrency_fails() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.ianaId = Optional.of(12321);
@ -322,7 +321,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_badCurrency_fails() throws Exception {
public void testPost_authorized_badCurrency_fails() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.ianaId = Optional.of(12321);
@ -349,7 +348,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_badBillingLine_fails() throws Exception {
public void testPost_authorized_badBillingLine_fails() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.ianaId = Optional.of(12321);
@ -378,7 +377,7 @@ public final class ConsoleRegistrarCreatorActionTest {
}
@Test
public void testPost_authorized_setPassword() throws Exception {
public void testPost_authorized_setPassword() {
action.clientId = Optional.of("myclientid");
action.name = Optional.of("registrar name");
action.billingAccount = Optional.of("USD=billing-account");