diff --git a/javatests/google/registry/flows/EppControllerTest.java b/javatests/google/registry/flows/EppControllerTest.java index cf8a8ea05..b99a4c43a 100644 --- a/javatests/google/registry/flows/EppControllerTest.java +++ b/javatests/google/registry/flows/EppControllerTest.java @@ -14,6 +14,7 @@ package google.registry.flows; +import static com.google.common.truth.Truth.assertThat; import static google.registry.flows.EppXmlTransformer.marshal; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static java.nio.charset.StandardCharsets.UTF_8; @@ -37,6 +38,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @@ -50,8 +52,6 @@ public class EppControllerTest extends ShardableTestCase { @Mock SessionMetadata sessionMetadata; @Mock TransportCredentials transportCredentials; - @Mock EppMetric.Builder eppMetricBuilder; - @Mock EppMetric eppMetric; @Mock BigQueryMetricsEnqueuer metricsEnqueuer; @Mock FlowComponent.Builder flowComponentBuilder; @Mock FlowComponent flowComponent; @@ -64,7 +64,7 @@ public class EppControllerTest extends ShardableTestCase { @Before public void setUp() throws Exception { - when(sessionMetadata.getClientId()).thenReturn("foo"); + when(sessionMetadata.getClientId()).thenReturn("some-client"); when(flowComponentBuilder.flowModule(Matchers.any())) .thenReturn(flowComponentBuilder); when(flowComponentBuilder.build()).thenReturn(flowComponent); @@ -74,10 +74,9 @@ public class EppControllerTest extends ShardableTestCase { when(eppOutput.getResponse()).thenReturn(eppResponse); when(eppResponse.getResult()).thenReturn(result); when(result.getCode()).thenReturn(Code.SuccessWithNoMessages); - when(eppMetricBuilder.build()).thenReturn(eppMetric); eppController = new EppController(); - eppController.metric = eppMetricBuilder; + eppController.metric = new EppMetric.Builder(); eppController.bigQueryMetricsEnqueuer = metricsEnqueuer; eppController.clock = new FakeClock(); eppController.flowComponentBuilder = flowComponentBuilder; @@ -93,6 +92,7 @@ public class EppControllerTest extends ShardableTestCase { @Test public void testHandleEppCommand_unmarshallableData_exportsMetric() { + ArgumentCaptor metricCaptor = ArgumentCaptor.forClass(EppMetric.class); eppController.handleEppCommand( sessionMetadata, transportCredentials, @@ -101,15 +101,16 @@ public class EppControllerTest extends ShardableTestCase { false, new byte[0]); - verify(eppMetricBuilder).setClientId("foo"); - verify(eppMetricBuilder).setPrivilegeLevel("NORMAL"); - verify(eppMetricBuilder).setStatus(Code.SyntaxError); - verify(eppMetricBuilder).build(); - verify(metricsEnqueuer).export(eppMetric); + verify(metricsEnqueuer).export(metricCaptor.capture()); + EppMetric metric = metricCaptor.getValue(); + assertThat(metric.getClientId()).hasValue("some-client"); + assertThat(metric.getPrivilegeLevel()).hasValue("NORMAL"); + assertThat(metric.getStatus()).hasValue(Code.SyntaxError); } @Test public void testHandleEppCommand_regularEppCommand_exportsMetric() { + ArgumentCaptor metricCaptor = ArgumentCaptor.forClass(EppMetric.class); String domainCreateXml = loadFileWithSubstitutions( getClass(), "domain_create_prettyprinted.xml", ImmutableMap.of()); @@ -121,12 +122,12 @@ public class EppControllerTest extends ShardableTestCase { true, domainCreateXml.getBytes(UTF_8)); - verify(eppMetricBuilder).setClientId("foo"); - verify(eppMetricBuilder).setPrivilegeLevel("SUPERUSER"); - verify(eppMetricBuilder).setStatus(Code.SuccessWithNoMessages); - verify(eppMetricBuilder).setCommandName("Create"); - verify(eppMetricBuilder).setEppTarget("example.tld"); - verify(eppMetricBuilder).build(); - verify(metricsEnqueuer).export(eppMetric); + verify(metricsEnqueuer).export(metricCaptor.capture()); + EppMetric metric = metricCaptor.getValue(); + assertThat(metric.getClientId()).hasValue("some-client"); + assertThat(metric.getPrivilegeLevel()).hasValue("SUPERUSER"); + assertThat(metric.getStatus()).hasValue(Code.SuccessWithNoMessages); + assertThat(metric.getCommandName()).hasValue("Create"); + assertThat(metric.getEppTarget()).hasValue("example.tld"); } } diff --git a/javatests/google/registry/flows/EppTestComponent.java b/javatests/google/registry/flows/EppTestComponent.java index 5b8cd1698..af3cf6398 100644 --- a/javatests/google/registry/flows/EppTestComponent.java +++ b/javatests/google/registry/flows/EppTestComponent.java @@ -48,7 +48,7 @@ interface EppTestComponent { FakesAndMocksModule(FakeClock clock) { this.clock = clock; - this.metrics = mock(EppMetric.Builder.class); + this.metrics = new EppMetric.Builder(); this.modulesService = mock(ModulesService.class); this.metricsEnqueuer = mock(BigQueryMetricsEnqueuer.class); } diff --git a/javatests/google/registry/flows/FlowRunnerTest.java b/javatests/google/registry/flows/FlowRunnerTest.java index eeadabc7c..7141ea34b 100644 --- a/javatests/google/registry/flows/FlowRunnerTest.java +++ b/javatests/google/registry/flows/FlowRunnerTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static java.nio.charset.StandardCharsets.UTF_8; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.appengine.api.users.User; @@ -91,7 +90,7 @@ public class FlowRunnerTest extends ShardableTestCase { flowRunner.isDryRun = false; flowRunner.isSuperuser = false; flowRunner.isTransactional = false; - flowRunner.metric = mock(EppMetric.Builder.class); + flowRunner.metric = new EppMetric.Builder(); flowRunner.sessionMetadata = new StatelessRequestSessionMetadata("TheRegistrar", ImmutableSet.of()); flowRunner.trid = Trid.create("client-123", "server-456"); @@ -110,18 +109,16 @@ public class FlowRunnerTest extends ShardableTestCase { } @Test - public void testRun_notIsTransactional_callsMetricIncrementAttempts() throws Exception { + public void testRun_notIsTransactional_incrementsMetricAttempts() throws Exception { flowRunner.run(); - - verify(flowRunner.metric).incrementAttempts(); + assertThat(flowRunner.metric.build().getAttempts()).isEqualTo(1); } @Test - public void testRun_isTransactional_callsMetricIncrementAttempts() throws Exception { + public void testRun_isTransactional_incrementsMetricAttempts() throws Exception { flowRunner.isTransactional = true; flowRunner.run(); - - verify(flowRunner.metric).incrementAttempts(); + assertThat(flowRunner.metric.build().getAttempts()).isEqualTo(1); } @Test diff --git a/javatests/google/registry/flows/session/LoginFlowViaConsoleTest.java b/javatests/google/registry/flows/session/LoginFlowViaConsoleTest.java index b2985e642..8db4726d3 100644 --- a/javatests/google/registry/flows/session/LoginFlowViaConsoleTest.java +++ b/javatests/google/registry/flows/session/LoginFlowViaConsoleTest.java @@ -87,13 +87,12 @@ public class LoginFlowViaConsoleTest extends LoginFlowTestCase { } Environment login(final String name, final String authDomain, final String gaeUserId) { + oldEnv = ApiProxy.getCurrentEnvironment(); // This envAttr thing is the only way to set userId. // see https://code.google.com/p/googleappengine/issues/detail?id=3579 - final HashMap envAttr = new HashMap<>(); + final HashMap envAttr = new HashMap<>(oldEnv.getAttributes()); envAttr.put("com.google.appengine.api.users.UserService.user_id_key", gaeUserId); - // And then.. this. - oldEnv = ApiProxy.getCurrentEnvironment(); final Environment e = oldEnv; ApiProxy.setEnvironmentForCurrentThread(new Environment() { @Override