Add metric related unit tests to EppControllerTest and FlowRunnerTest

This is one of a series of CLs which will refactor EppMetrics into a value type
and Metrics into a stateless class which will have an export(EppMetrics requestDetails)
method to export EPP metrics in a stateless way. Once EppMetrics is a value
type, I will create a new StackdriverEppMetrics that will also accept the value
type via an incrementRequests(EppMetrics requestDetails), allowing us to
monitor EPP via BigQuery and Stackdriver with minimum code duplication.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131973288
This commit is contained in:
shikhman 2016-09-01 11:44:56 -07:00 committed by Ben McIlwain
parent 5098b03af4
commit 5ee78c505d
2 changed files with 110 additions and 4 deletions

View file

@ -15,26 +15,69 @@
package google.registry.flows; package google.registry.flows;
import static google.registry.flows.EppXmlTransformer.marshal; import static google.registry.flows.EppXmlTransformer.marshal;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.eppoutput.EppOutput;
import google.registry.model.eppoutput.EppResponse;
import google.registry.model.eppoutput.Result; import google.registry.model.eppoutput.Result;
import google.registry.model.eppoutput.Result.Code; import google.registry.model.eppoutput.Result.Code;
import google.registry.monitoring.whitebox.EppMetrics;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import google.registry.testing.ShardableTestCase; import google.registry.testing.ShardableTestCase;
import google.registry.util.SystemClock; import google.registry.util.SystemClock;
import google.registry.xml.ValidationMode; import google.registry.xml.ValidationMode;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/** Unit tests for {@link EppController}. */ /** Unit tests for {@link EppController}. */
@RunWith(JUnit4.class) @RunWith(MockitoJUnitRunner.class)
public class EppControllerTest extends ShardableTestCase { public class EppControllerTest extends ShardableTestCase {
@Rule @Rule
public AppEngineRule appEngineRule = new AppEngineRule.Builder().build(); public AppEngineRule appEngineRule = new AppEngineRule.Builder().build();
@Mock SessionMetadata sessionMetadata;
@Mock TransportCredentials transportCredentials;
@Mock EppMetrics eppMetrics;
@Mock FlowComponent.Builder flowComponentBuilder;
@Mock FlowComponent flowComponent;
@Mock FlowRunner flowRunner;
@Mock EppOutput eppOutput;
@Mock EppResponse eppResponse;
@Mock Result result;
private EppController eppController;
@Before
public void setUp() throws Exception {
when(sessionMetadata.getClientId()).thenReturn("foo");
when(flowComponentBuilder.flowModule(Matchers.<FlowModule>any()))
.thenReturn(flowComponentBuilder);
when(flowComponentBuilder.build()).thenReturn(flowComponent);
when(flowComponent.flowRunner()).thenReturn(flowRunner);
when(flowRunner.run()).thenReturn(eppOutput);
when(eppOutput.isResponse()).thenReturn(true);
when(eppOutput.getResponse()).thenReturn(eppResponse);
when(eppResponse.getResult()).thenReturn(result);
when(result.getCode()).thenReturn(Code.SuccessWithNoMessages);
eppController = new EppController();
eppController.metrics = eppMetrics;
eppController.clock = new FakeClock();
eppController.flowComponentBuilder = flowComponentBuilder;
}
@Test @Test
public void testMarshallingUnknownError() throws Exception { public void testMarshallingUnknownError() throws Exception {
marshal( marshal(
@ -42,4 +85,41 @@ public class EppControllerTest extends ShardableTestCase {
new SystemClock(), Result.create(Code.CommandFailed), Trid.create(null)), new SystemClock(), Result.create(Code.CommandFailed), Trid.create(null)),
ValidationMode.STRICT); ValidationMode.STRICT);
} }
@Test
public void testHandleEppCommand_unmarshallableData_exportsMetric() {
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
false,
new byte[0]);
verify(eppMetrics).setClientId("foo");
verify(eppMetrics).setPrivilegeLevel("NORMAL");
verify(eppMetrics).setEppStatus(Code.SyntaxError);
verify(eppMetrics).export();
}
@Test
public void testHandleEppCommand_regularEppCommand_exportsMetric() {
String domainCreateXml =
loadFileWithSubstitutions(
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of());
eppController.handleEppCommand(
sessionMetadata,
transportCredentials,
EppRequestSource.UNIT_TEST,
false,
true,
domainCreateXml.getBytes(UTF_8));
verify(eppMetrics).setClientId("foo");
verify(eppMetrics).setPrivilegeLevel("SUPERUSER");
verify(eppMetrics).setEppStatus(Code.SuccessWithNoMessages);
verify(eppMetrics).setCommandName("Create");
verify(eppMetrics).setEppTarget("example.tld");
verify(eppMetrics).export();
}
} }

View file

@ -19,6 +19,8 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.mock; 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; import com.google.appengine.api.users.User;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
@ -32,6 +34,7 @@ import com.google.common.testing.TestLogHandler;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.eppinput.EppInput; import google.registry.model.eppinput.EppInput;
import google.registry.model.eppoutput.EppOutput; import google.registry.model.eppoutput.EppOutput;
import google.registry.model.eppoutput.EppResponse;
import google.registry.monitoring.whitebox.EppMetrics; import google.registry.monitoring.whitebox.EppMetrics;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
@ -42,6 +45,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.joda.time.DateTime;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@ -64,6 +68,12 @@ public class FlowRunnerTest extends ShardableTestCase {
public void before() { public void before() {
Logger.getLogger(FlowRunner.class.getCanonicalName()).addHandler(handler); Logger.getLogger(FlowRunner.class.getCanonicalName()).addHandler(handler);
final EppOutput eppOutput = mock(EppOutput.class);
EppResponse eppResponse = mock(EppResponse.class);
when(eppResponse.getCreatedRepoId()).thenReturn("foo");
when(eppResponse.getExecutionTime()).thenReturn(new DateTime(1337));
when(eppOutput.getResponse()).thenReturn(eppResponse);
flowRunner.clientId = "TheRegistrar"; flowRunner.clientId = "TheRegistrar";
flowRunner.clock = new FakeClock(); flowRunner.clock = new FakeClock();
flowRunner.credentials = new PasswordOnlyTransportCredentials(); flowRunner.credentials = new PasswordOnlyTransportCredentials();
@ -74,8 +84,9 @@ public class FlowRunnerTest extends ShardableTestCase {
new Flow() { new Flow() {
@Override @Override
protected EppOutput run() { protected EppOutput run() {
return null; return eppOutput;
}}); }
});
flowRunner.inputXmlBytes = "<xml/>".getBytes(UTF_8); flowRunner.inputXmlBytes = "<xml/>".getBytes(UTF_8);
flowRunner.isDryRun = false; flowRunner.isDryRun = false;
flowRunner.isSuperuser = false; flowRunner.isSuperuser = false;
@ -98,6 +109,21 @@ public class FlowRunnerTest extends ShardableTestCase {
"xmlBytes", "PHhtbC8+"); "xmlBytes", "PHhtbC8+");
} }
@Test
public void testRun_notIsTransactional_callsMetricIncrementAttempts() throws Exception {
flowRunner.run();
verify(flowRunner.metrics).incrementAttempts();
}
@Test
public void testRun_isTransactional_callsMetricIncrementAttempts() throws Exception {
flowRunner.isTransactional = true;
flowRunner.run();
verify(flowRunner.metrics).incrementAttempts();
}
@Test @Test
public void testRun_reportingLogStatement_noClientId() throws Exception { public void testRun_reportingLogStatement_noClientId() throws Exception {
flowRunner.clientId = null; flowRunner.clientId = null;