mirror of
https://github.com/google/nomulus.git
synced 2025-08-06 01:35:17 +02:00
Refactor ICANN reporting and billing into common package
This moves the default yearMonth logic into a common ReportingModule, rather than the coarse-scoped BackendModule, which may not want the default parameter extraction logic, as well as moving the 'yearMonth' parameter constant to the common package it's used in. This also provides a basis for future consolidation of the ReportingEmailUtils and BillingEmailUtils classes, which have modest overlap. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=183130311
This commit is contained in:
parent
9d532cb507
commit
74ced1e907
71 changed files with 233 additions and 142 deletions
42
javatests/google/registry/reporting/billing/BUILD
Normal file
42
javatests/google/registry/reporting/billing/BUILD
Normal file
|
@ -0,0 +1,42 @@
|
|||
package(
|
||||
default_testonly = 1,
|
||||
default_visibility = ["//java/google/registry:registry_project"],
|
||||
)
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
load("//java/com/google/testing/builddefs:GenTestRules.bzl", "GenTestRules")
|
||||
|
||||
java_library(
|
||||
name = "billing",
|
||||
srcs = glob(["*.java"]),
|
||||
deps = [
|
||||
"//java/google/registry/gcs",
|
||||
"//java/google/registry/reporting/billing",
|
||||
"//java/google/registry/storage/drive",
|
||||
"//java/google/registry/util",
|
||||
"//javatests/google/registry/testing",
|
||||
"@com_google_apis_google_api_services_dataflow",
|
||||
"@com_google_appengine_api_1_0_sdk",
|
||||
"@com_google_appengine_tools_appengine_gcs_client",
|
||||
"@com_google_dagger",
|
||||
"@com_google_guava",
|
||||
"@com_google_truth",
|
||||
"@com_google_truth_extensions_truth_java8_extension",
|
||||
"@javax_servlet_api",
|
||||
"@joda_time",
|
||||
"@junit",
|
||||
"@org_apache_beam_runners_direct_java",
|
||||
"@org_apache_beam_runners_google_cloud_dataflow_java",
|
||||
"@org_apache_beam_sdks_java_core",
|
||||
"@org_apache_beam_sdks_java_io_google_cloud_platform",
|
||||
"@org_mockito_all",
|
||||
],
|
||||
)
|
||||
|
||||
GenTestRules(
|
||||
name = "GeneratedTestRules",
|
||||
default_test_size = "small",
|
||||
test_files = glob(["*Test.java"]),
|
||||
deps = [":billing"],
|
||||
)
|
|
@ -0,0 +1,172 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.reporting.billing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
import static google.registry.testing.JUnitBackports.expectThrows;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
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 google.registry.gcs.GcsUtils;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeSleeper;
|
||||
import google.registry.util.Retrier;
|
||||
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;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
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/CRR-INV-2017-10.csv")))
|
||||
.thenReturn(
|
||||
new ByteArrayInputStream("test,data\nhello,world".getBytes(StandardCharsets.UTF_8)));
|
||||
msgCaptor = ArgumentCaptor.forClass(Message.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"),
|
||||
"test-bucket",
|
||||
"results/",
|
||||
gcsUtils,
|
||||
new Retrier(new FakeSleeper(new FakeClock()), RETRY_COUNT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_emailOverallInvoice() throws MessagingException, IOException {
|
||||
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);
|
||||
// TODO(b/71631624): Fix content type in nomulus build to be "text/csv; charset=utf-8" once next
|
||||
// version of framework is released.
|
||||
assertThat(attachmentPart.getContentType())
|
||||
.isEqualTo("text/plain; name=CRR-INV-2017-10.csv");
|
||||
assertThat(attachmentPart.getContent().toString()).isEqualTo("test,data\nhello,world");
|
||||
}
|
||||
|
||||
@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) throws Throwable {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
RuntimeException thrown =
|
||||
expectThrows(RuntimeException.class, () -> emailUtils.emailOverallInvoice());
|
||||
assertThat(thrown).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");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_sendAlertEmail() throws MessagingException, IOException {
|
||||
emailUtils.sendAlertEmail("Alert!");
|
||||
verify(emailService).sendMessage(msgCaptor.capture());
|
||||
validateAlertMessage(msgCaptor.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()
|
||||
.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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,205 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.reporting.billing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.DatastoreHelper.loadRegistrar;
|
||||
import static google.registry.testing.DatastoreHelper.persistResource;
|
||||
import static google.registry.testing.GcsTestingUtils.writeGcsFile;
|
||||
import static google.registry.testing.JUnitBackports.expectThrows;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.storage.drive.DriveConnection;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.FakeSleeper;
|
||||
import google.registry.util.Retrier;
|
||||
import java.io.IOException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link google.registry.reporting.billing.CopyDetailReportsAction}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class CopyDetailReportsActionTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.withDatastore()
|
||||
.build();
|
||||
|
||||
|
||||
private final GcsService gcsService = GcsServiceFactory.createGcsService();
|
||||
private final GcsUtils gcsUtils = new GcsUtils(gcsService, 1024);
|
||||
|
||||
private FakeResponse response;
|
||||
private DriveConnection driveConnection;
|
||||
private BillingEmailUtils emailUtils;
|
||||
private CopyDetailReportsAction action;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
persistResource(loadRegistrar("TheRegistrar").asBuilder().setDriveFolderId("0B-12345").build());
|
||||
response = new FakeResponse();
|
||||
driveConnection = mock(DriveConnection.class);
|
||||
emailUtils = mock(BillingEmailUtils.class);
|
||||
action =
|
||||
new CopyDetailReportsAction(
|
||||
"test-bucket",
|
||||
"results/",
|
||||
driveConnection,
|
||||
gcsUtils,
|
||||
new Retrier(new FakeSleeper(new FakeClock()), 3),
|
||||
response,
|
||||
emailUtils);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess() throws IOException {
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/invoice_details_2017-10_TheRegistrar_test.csv"),
|
||||
"hello,world\n1,2".getBytes(UTF_8));
|
||||
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/invoice_details_2017-10_TheRegistrar_hello.csv"),
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
|
||||
action.run();
|
||||
verify(driveConnection)
|
||||
.createFile(
|
||||
"invoice_details_2017-10_TheRegistrar_test.csv",
|
||||
MediaType.CSV_UTF_8,
|
||||
"0B-12345",
|
||||
"hello,world\n1,2".getBytes(UTF_8));
|
||||
|
||||
verify(driveConnection)
|
||||
.createFile(
|
||||
"invoice_details_2017-10_TheRegistrar_hello.csv",
|
||||
MediaType.CSV_UTF_8,
|
||||
"0B-12345",
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
||||
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
|
||||
assertThat(response.getPayload()).isEqualTo("Copied detail reports.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_nonDetailReportFiles_notSent() throws IOException{
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/invoice_details_2017-10_TheRegistrar_hello.csv"),
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/not_a_detail_report_2017-10_TheRegistrar_test.csv"),
|
||||
"hello,world\n1,2".getBytes(UTF_8));
|
||||
action.run();
|
||||
verify(driveConnection)
|
||||
.createFile(
|
||||
"invoice_details_2017-10_TheRegistrar_hello.csv",
|
||||
MediaType.CSV_UTF_8,
|
||||
"0B-12345",
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
// Verify we didn't copy the non-detail report file.
|
||||
verifyNoMoreInteractions(driveConnection);
|
||||
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
||||
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
|
||||
assertThat(response.getPayload()).isEqualTo("Copied detail reports.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_transientIOException_retries() throws IOException {
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/invoice_details_2017-10_TheRegistrar_hello.csv"),
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
when(driveConnection.createFile(any(), any(), any(), any()))
|
||||
.thenThrow(new IOException("expected"))
|
||||
.thenReturn("success");
|
||||
|
||||
action.run();
|
||||
verify(driveConnection, times(2))
|
||||
.createFile(
|
||||
"invoice_details_2017-10_TheRegistrar_hello.csv",
|
||||
MediaType.CSV_UTF_8,
|
||||
"0B-12345",
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
||||
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
|
||||
assertThat(response.getPayload()).isEqualTo("Copied detail reports.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFail_tooManyFailures_sendsAlertEmail() throws IOException {
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/invoice_details_2017-10_TheRegistrar_hello.csv"),
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
when(driveConnection.createFile(any(), any(), any(), any()))
|
||||
.thenThrow(new IOException("expected"));
|
||||
|
||||
RuntimeException thrown = expectThrows(RuntimeException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("java.io.IOException: expected");
|
||||
verify(driveConnection, times(3))
|
||||
.createFile(
|
||||
"invoice_details_2017-10_TheRegistrar_hello.csv",
|
||||
MediaType.CSV_UTF_8,
|
||||
"0B-12345",
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
verify(emailUtils).sendAlertEmail("Warning: CopyDetailReportsAction failed.\nEncountered: "
|
||||
+ "expected on file: invoice_details_2017-10_TheRegistrar_hello.csv");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFail_registrarDoesntExist_doesntCopy() throws IOException {
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename("test-bucket", "results/invoice_details_2017-10_notExistent_hello.csv"),
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
action.run();
|
||||
verifyZeroInteractions(driveConnection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFail_noRegistrarFolderId_doesntCopy() throws IOException {
|
||||
persistResource(loadRegistrar("TheRegistrar").asBuilder().setDriveFolderId(null).build());
|
||||
writeGcsFile(
|
||||
gcsService,
|
||||
new GcsFilename(
|
||||
"test-bucket", "results/invoice_details_2017-10_TheRegistrar_hello.csv"),
|
||||
"hola,mundo\n3,4".getBytes(UTF_8));
|
||||
action.run();
|
||||
verifyZeroInteractions(driveConnection);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.reporting.billing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.services.dataflow.Dataflow;
|
||||
import com.google.api.services.dataflow.Dataflow.Projects;
|
||||
import com.google.api.services.dataflow.Dataflow.Projects.Templates;
|
||||
import com.google.api.services.dataflow.Dataflow.Projects.Templates.Launch;
|
||||
import com.google.api.services.dataflow.model.Job;
|
||||
import com.google.api.services.dataflow.model.LaunchTemplateParameters;
|
||||
import com.google.api.services.dataflow.model.LaunchTemplateResponse;
|
||||
import com.google.api.services.dataflow.model.RuntimeEnvironment;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
import java.io.IOException;
|
||||
import org.joda.time.YearMonth;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link google.registry.reporting.billing.GenerateInvoicesAction}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class GenerateInvoicesActionTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder().withTaskQueue().build();
|
||||
|
||||
private Dataflow dataflow;
|
||||
private Projects projects;
|
||||
private Templates templates;
|
||||
private Launch launch;
|
||||
private FakeResponse response;
|
||||
private BillingEmailUtils emailUtils;
|
||||
private GenerateInvoicesAction action;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
dataflow = mock(Dataflow.class);
|
||||
projects = mock(Projects.class);
|
||||
templates = mock(Templates.class);
|
||||
launch = mock(Launch.class);
|
||||
emailUtils = mock(BillingEmailUtils.class);
|
||||
when(dataflow.projects()).thenReturn(projects);
|
||||
when(projects.templates()).thenReturn(templates);
|
||||
when(templates.launch(any(String.class), any(LaunchTemplateParameters.class)))
|
||||
.thenReturn(launch);
|
||||
when(launch.setGcsPath(any(String.class))).thenReturn(launch);
|
||||
|
||||
response = new FakeResponse();
|
||||
Job job = new Job();
|
||||
job.setId("12345");
|
||||
when(launch.execute()).thenReturn(new LaunchTemplateResponse().setJob(job));
|
||||
|
||||
action =
|
||||
new GenerateInvoicesAction(
|
||||
"test-project",
|
||||
"gs://test-project-beam",
|
||||
"gs://test-project-beam/templates/invoicing",
|
||||
new YearMonth(2017, 10),
|
||||
dataflow,
|
||||
response,
|
||||
emailUtils);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLaunchTemplateJob() throws Exception {
|
||||
action.run();
|
||||
LaunchTemplateParameters expectedParams =
|
||||
new LaunchTemplateParameters()
|
||||
.setJobName("invoicing-2017-10")
|
||||
.setEnvironment(
|
||||
new RuntimeEnvironment()
|
||||
.setZone("us-east1-c")
|
||||
.setTempLocation("gs://test-project-beam/temporary"))
|
||||
.setParameters(ImmutableMap.of("yearMonth", "2017-10"));
|
||||
verify(templates).launch("test-project", expectedParams);
|
||||
verify(launch).setGcsPath("gs://test-project-beam/templates/invoicing");
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getPayload()).isEqualTo("Launched dataflow template.");
|
||||
|
||||
TaskMatcher matcher =
|
||||
new TaskMatcher()
|
||||
.url("/_dr/task/publishInvoices")
|
||||
.method("POST")
|
||||
.param("jobId", "12345")
|
||||
.param("yearMonth", "2017-10");
|
||||
assertTasksEnqueued("billing", matcher);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCaughtIOException() throws IOException {
|
||||
when(launch.execute()).thenThrow(new IOException("expected"));
|
||||
action.run();
|
||||
assertThat(response.getStatus()).isEqualTo(500);
|
||||
assertThat(response.getPayload()).isEqualTo("Template launch failed: expected");
|
||||
verify(emailUtils).sendAlertEmail("Template Launch failed due to expected");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.reporting.billing;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.services.dataflow.Dataflow;
|
||||
import com.google.api.services.dataflow.Dataflow.Projects;
|
||||
import com.google.api.services.dataflow.Dataflow.Projects.Jobs;
|
||||
import com.google.api.services.dataflow.Dataflow.Projects.Jobs.Get;
|
||||
import com.google.api.services.dataflow.model.Job;
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
import java.io.IOException;
|
||||
import org.joda.time.YearMonth;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class PublishInvoicesActionTest {
|
||||
|
||||
private Dataflow dataflow;
|
||||
private Projects projects;
|
||||
private Jobs jobs;
|
||||
private Get get;
|
||||
private BillingEmailUtils emailUtils;
|
||||
|
||||
private Job expectedJob;
|
||||
private FakeResponse response;
|
||||
private PublishInvoicesAction uploadAction;
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder().withTaskQueue().build();
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
dataflow = mock(Dataflow.class);
|
||||
projects = mock(Projects.class);
|
||||
jobs = mock(Jobs.class);
|
||||
get = mock(Get.class);
|
||||
when(dataflow.projects()).thenReturn(projects);
|
||||
when(projects.jobs()).thenReturn(jobs);
|
||||
when(jobs.get("test-project", "12345")).thenReturn(get);
|
||||
expectedJob = new Job();
|
||||
when(get.execute()).thenReturn(expectedJob);
|
||||
emailUtils = mock(BillingEmailUtils.class);
|
||||
response = new FakeResponse();
|
||||
uploadAction =
|
||||
new PublishInvoicesAction(
|
||||
"test-project", "12345", emailUtils, dataflow, response, new YearMonth(2017, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobDone_enqueuesCopyAction_emailsResults() throws Exception {
|
||||
expectedJob.setCurrentState("JOB_STATE_DONE");
|
||||
uploadAction.run();
|
||||
assertThat(response.getStatus()).isEqualTo(SC_OK);
|
||||
verify(emailUtils).emailOverallInvoice();
|
||||
TaskMatcher matcher =
|
||||
new TaskMatcher()
|
||||
.url("/_dr/task/copyDetailReports")
|
||||
.method("POST")
|
||||
.param("yearMonth", "2017-10");
|
||||
assertTasksEnqueued("retryable-cron-tasks", matcher);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobFailed_returnsNonRetriableResponse() {
|
||||
expectedJob.setCurrentState("JOB_STATE_FAILED");
|
||||
uploadAction.run();
|
||||
assertThat(response.getStatus()).isEqualTo(SC_NO_CONTENT);
|
||||
verify(emailUtils).sendAlertEmail("Dataflow job 12345 ended in status failure.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobIndeterminate_returnsRetriableResponse() {
|
||||
expectedJob.setCurrentState("JOB_STATE_RUNNING");
|
||||
uploadAction.run();
|
||||
assertThat(response.getStatus()).isEqualTo(SC_NOT_MODIFIED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIOException_returnsFailureMessage() throws IOException {
|
||||
when(get.execute()).thenThrow(new IOException("expected"));
|
||||
uploadAction.run();
|
||||
assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
|
||||
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
|
||||
assertThat(response.getPayload()).isEqualTo("Template launch failed: expected");
|
||||
verify(emailUtils).sendAlertEmail("Publish action failed due to expected");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue