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:
nickfelt 2016-09-08 12:33:00 -07:00 committed by Ben McIlwain
parent e478fd09fb
commit 36c6d59fee
4 changed files with 26 additions and 29 deletions

View file

@ -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");
}
}