mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 08:27:14 +02:00
Avoid mocking in tests for EppMetric
Followup to [] Mocking shouldn't be used when you can use the real implementation just as easily (and more robustly) - in particular, you almost never want to mock a value type. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132586826
This commit is contained in:
parent
e478fd09fb
commit
36c6d59fee
4 changed files with 26 additions and 29 deletions
|
@ -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.<FlowModule>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<EppMetric> 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<EppMetric> metricCaptor = ArgumentCaptor.forClass(EppMetric.class);
|
||||
String domainCreateXml =
|
||||
loadFileWithSubstitutions(
|
||||
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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.<String>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
|
||||
|
|
|
@ -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<String, Object> envAttr = new HashMap<>();
|
||||
final HashMap<String, Object> 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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue