Change EppMetric.Builder to use @AutoValue.Builder

Getting rid of builder boilerplate makes my heart sing.  Since we can no
longer @Inject the Builder() constructor, this change adds a provider
in WhiteboxModule that calls a special builderForRequest() factory method,
which gets passed a request ID and Clock and preserves the existing
EppMetric magic that sets the start and end time for you.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132714432
This commit is contained in:
nickfelt 2016-09-09 14:09:11 -07:00 committed by Ben McIlwain
parent ceb5c2117e
commit 2537e95de5
10 changed files with 108 additions and 107 deletions

View file

@ -32,8 +32,10 @@ import google.registry.monitoring.whitebox.EppMetric;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import google.registry.testing.ShardableTestCase;
import google.registry.util.Clock;
import google.registry.util.SystemClock;
import google.registry.xml.ValidationMode;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -61,6 +63,10 @@ public class EppControllerTest extends ShardableTestCase {
@Mock EppResponse eppResponse;
@Mock Result result;
private static final DateTime startTime = DateTime.parse("2016-09-01T00:00:00Z");
private final Clock clock = new FakeClock(startTime);
private EppController eppController;
@Before
@ -77,9 +83,9 @@ public class EppControllerTest extends ShardableTestCase {
when(result.getCode()).thenReturn(Code.SuccessWithNoMessages);
eppController = new EppController();
eppController.metricBuilder = new EppMetric.Builder();
eppController.metricBuilder = EppMetric.builderForRequest("request-id-1", clock);
eppController.bigQueryMetricsEnqueuer = metricsEnqueuer;
eppController.clock = new FakeClock();
eppController.clock = clock;
eppController.flowComponentBuilder = flowComponentBuilder;
eppController.eppMetrics = eppMetrics;
}
@ -105,6 +111,9 @@ public class EppControllerTest extends ShardableTestCase {
verify(metricsEnqueuer).export(metricCaptor.capture());
EppMetric metric = metricCaptor.getValue();
assertThat(metric.getRequestId()).isEqualTo("request-id-1");
assertThat(metric.getStartTimestamp()).isEqualTo(startTime);
assertThat(metric.getEndTimestamp()).isEqualTo(clock.nowUtc());
assertThat(metric.getClientId()).hasValue("some-client");
assertThat(metric.getPrivilegeLevel()).hasValue("NORMAL");
assertThat(metric.getStatus()).hasValue(Code.SyntaxError);
@ -126,6 +135,9 @@ public class EppControllerTest extends ShardableTestCase {
verify(metricsEnqueuer).export(metricCaptor.capture());
EppMetric metric = metricCaptor.getValue();
assertThat(metric.getRequestId()).isEqualTo("request-id-1");
assertThat(metric.getStartTimestamp()).isEqualTo(startTime);
assertThat(metric.getEndTimestamp()).isEqualTo(clock.nowUtc());
assertThat(metric.getClientId()).hasValue("some-client");
assertThat(metric.getPrivilegeLevel()).hasValue("SUPERUSER");
assertThat(metric.getStatus()).hasValue(Code.SuccessWithNoMessages);

View file

@ -42,13 +42,13 @@ interface EppTestComponent {
@Module
static class FakesAndMocksModule {
final FakeClock clock;
final EppMetric.Builder metrics;
final EppMetric.Builder metricBuilder;
final BigQueryMetricsEnqueuer metricsEnqueuer;
final ModulesService modulesService;
FakesAndMocksModule(FakeClock clock) {
this.clock = clock;
this.metrics = new EppMetric.Builder();
this.metricBuilder = EppMetric.builderForRequest("request-id-1", clock);
this.modulesService = mock(ModulesService.class);
this.metricsEnqueuer = mock(BigQueryMetricsEnqueuer.class);
}
@ -60,7 +60,7 @@ interface EppTestComponent {
@Provides
EppMetric.Builder provideMetrics() {
return metrics;
return metricBuilder;
}
@Provides

View file

@ -90,7 +90,7 @@ public class FlowRunnerTest extends ShardableTestCase {
flowRunner.isDryRun = false;
flowRunner.isSuperuser = false;
flowRunner.isTransactional = false;
flowRunner.metric = new EppMetric.Builder();
flowRunner.metric = EppMetric.builderForRequest("request-id-1", flowRunner.clock);
flowRunner.sessionMetadata =
new StatelessRequestSessionMetadata("TheRegistrar", ImmutableSet.<String>of());
flowRunner.trid = Trid.create("client-123", "server-456");

View file

@ -36,21 +36,22 @@ public class EppMetricTest {
@Test
public void testGetBigQueryRowEncoding_encodesCorrectly() throws Exception {
EppMetric metric =
new EppMetric.Builder(new DateTime(1337))
.setEppTarget("target")
.setPrivilegeLevel("level")
EppMetric.builder()
.setRequestId("request-id-1")
.setStartTimestamp(new DateTime(1337))
.setEndTimestamp(new DateTime(1338))
.setCommandName("command")
.setClientId("client")
.setPrivilegeLevel("level")
.setEppTarget("target")
.setStatus(Code.CommandUseError)
.incrementAttempts()
.build(new DateTime(1338));
.build();
// The request_id is randomly generated and hard to mock without a lot of supporting code
// so we just use the tested metric's request_id verbatim.
assertThat(metric.getBigQueryRowEncoding())
.containsExactlyEntriesIn(
new ImmutableMap.Builder<String, String>()
.put("requestId", metric.getRequestId())
.put("requestId", "request-id-1")
.put("startTime", "1.337000")
.put("endTime", "1.338000")
.put("commandName", "command")
@ -65,14 +66,17 @@ public class EppMetricTest {
@Test
public void testGetBigQueryRowEncoding_hasAllSchemaFields() throws Exception {
EppMetric metric =
new EppMetric.Builder(new DateTime(1337))
.setEppTarget("target")
.setPrivilegeLevel("level")
EppMetric.builder()
.setRequestId("request-id-1")
.setStartTimestamp(new DateTime(1337))
.setEndTimestamp(new DateTime(1338))
.setCommandName("command")
.setClientId("client")
.setPrivilegeLevel("level")
.setEppTarget("target")
.setStatus(Code.CommandUseError)
.incrementAttempts()
.build(new DateTime(1338));
.build();
ImmutableSet.Builder<String> schemaFieldNames = new ImmutableSet.Builder<>();
for (TableFieldSchema schemaField : metric.getSchemaFields()) {
schemaFieldNames.add(schemaField.getName());