Move two enums from FlowRunner to FlowTestCase.

They are only needed in test code.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125126550
This commit is contained in:
cgoldfeder 2016-06-16 18:54:40 -07:00 committed by Ben McIlwain
parent 038d3d5031
commit 31ddced437
23 changed files with 37 additions and 86 deletions

View file

@ -22,8 +22,6 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.model.eppcommon.Trid;
import google.registry.model.eppinput.EppInput;
import google.registry.model.eppoutput.EppOutput;
@ -61,10 +59,7 @@ public final class EppController {
ImmutableList<String> targetIds = eppInput.getTargetIds();
metrics.setCommandName(eppInput.getCommandName());
metrics.setClientId(sessionMetadata.getClientId());
metrics.setPrivilegeLevel(
sessionMetadata.isSuperuser()
? UserPrivileges.SUPERUSER.toString()
: UserPrivileges.NORMAL.toString());
metrics.setPrivilegeLevel(sessionMetadata.isSuperuser() ? "SUPERUSER" : "NORMAL");
if (!targetIds.isEmpty()) {
metrics.setEppTarget(Joiner.on(",").join(targetIds));
}
@ -76,9 +71,7 @@ public final class EppController {
inputXmlBytes,
metrics,
clock);
EppOutput eppOutput = flowRunner.run(
sessionMetadata.isDryRun() ? CommitMode.DRY_RUN : CommitMode.LIVE,
sessionMetadata.isSuperuser() ? UserPrivileges.SUPERUSER : UserPrivileges.NORMAL);
EppOutput eppOutput = flowRunner.run();
if (eppOutput.isResponse()) {
metrics.setEppStatus(eppOutput.getResponse().getResult().getCode());
}

View file

@ -101,14 +101,13 @@ public abstract class Flow {
EppInput eppInput,
Trid trid,
SessionMetadata sessionMetadata,
boolean superuser,
DateTime now,
byte[] inputXmlBytes) throws EppException {
this.eppInput = eppInput;
this.trid = trid;
this.sessionMetadata = sessionMetadata;
this.now = now;
this.superuser = superuser;
this.superuser = sessionMetadata.isSuperuser();
this.inputXmlBytes = inputXmlBytes;
initFlow();
validExtensions = ImmutableSet.copyOf(validExtensions);

View file

@ -38,12 +38,6 @@ public class FlowRunner {
private static final String COMMAND_LOG_FORMAT = "EPP Command" + Strings.repeat("\n\t%s", 4);
/** Whether to actually write to the datastore or just simulate. */
public enum CommitMode { LIVE, DRY_RUN }
/** Whether to run in normal or superuser mode. */
public enum UserPrivileges { NORMAL, SUPERUSER }
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private final Class<? extends Flow> flowClass;
@ -71,10 +65,8 @@ public class FlowRunner {
this.clock = clock;
}
public EppOutput run(
final CommitMode commitMode, final UserPrivileges userPrivileges) throws EppException {
public EppOutput run() throws EppException {
String clientId = sessionMetadata.getClientId();
final boolean isSuperuser = UserPrivileges.SUPERUSER.equals(userPrivileges);
logger.infofmt(
COMMAND_LOG_FORMAT,
trid.getServerTransactionId(),
@ -85,14 +77,14 @@ public class FlowRunner {
if (metrics != null) {
metrics.incrementAttempts();
}
return createAndInitFlow(isSuperuser, clock.nowUtc()).run();
return createAndInitFlow(clock.nowUtc()).run();
}
// We log the command in a structured format. Note that we do this before the transaction;
// if we did it after, we might miss a transaction that committed successfully but then crashed
// before it could log.
logger.info("EPP_Mutation " + new JsonLogStatement(trid)
.add("client", clientId)
.add("privileges", userPrivileges.toString())
.add("privileges", sessionMetadata.isSuperuser() ? "SUPERUSER" : "NORMAL")
.add("xmlBytes", base64().encode(inputXmlBytes)));
try {
EppOutput flowResult = ofy().transact(new Work<EppOutput>() {
@ -102,8 +94,8 @@ public class FlowRunner {
metrics.incrementAttempts();
}
try {
EppOutput output = createAndInitFlow(isSuperuser, ofy().getTransactionTime()).run();
if (CommitMode.DRY_RUN.equals(commitMode)) {
EppOutput output = createAndInitFlow(ofy().getTransactionTime()).run();
if (sessionMetadata.isDryRun()) {
throw new DryRunException(output);
}
return output;
@ -127,12 +119,11 @@ public class FlowRunner {
}
}
private Flow createAndInitFlow(boolean superuser, DateTime now) throws EppException {
private Flow createAndInitFlow(DateTime now) throws EppException {
return TypeUtils.<Flow>instantiate(flowClass).init(
eppInput,
trid,
sessionMetadata,
superuser,
now,
inputXmlBytes);
}

View file

@ -14,16 +14,8 @@
package google.registry.tools;
import static google.registry.flows.EppXmlTransformer.marshalWithLenientRetry;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Strings;
import google.registry.flows.EppException;
import google.registry.flows.FlowRunner;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
/** Container class for static utility methods. */
class CommandUtilities {
@ -60,10 +52,4 @@ class CommandUtilities {
System.out.println(text);
}
}
static String runFlow(
FlowRunner flowRunner, CommitMode commitMode, UserPrivileges userPrivileges)
throws EppException {
return new String(marshalWithLenientRetry(flowRunner.run(commitMode, userPrivileges)), UTF_8);
}
}

View file

@ -17,8 +17,8 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.io.Resources.getResource;
import static google.registry.flows.EppXmlTransformer.marshalWithLenientRetry;
import static google.registry.flows.EppXmlTransformer.unmarshal;
import static google.registry.tools.CommandUtilities.runFlow;
import static google.registry.util.X509Utils.getCertificateHash;
import static google.registry.util.X509Utils.loadCertificate;
import static java.nio.charset.StandardCharsets.US_ASCII;
@ -32,8 +32,6 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import google.registry.flows.FlowRunner;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.HttpSessionMetadata;
import google.registry.flows.TlsCredentials;
import google.registry.flows.session.LoginFlow;
@ -101,7 +99,7 @@ final class ValidateLoginCredentialsCommand implements RemoteApiCommand, GtechCo
.setData(new SoyMapData("clientIdentifier", clientIdentifier, "password", password))
.render()
.getBytes(UTF_8);
System.out.println(runFlow(
System.out.println(new String(marshalWithLenientRetry(
new FlowRunner(
LoginFlow.class,
unmarshal(EppInput.class, inputXmlBytes),
@ -114,8 +112,6 @@ final class ValidateLoginCredentialsCommand implements RemoteApiCommand, GtechCo
new BasicHttpSession()),
inputXmlBytes,
null,
new SystemClock()),
CommitMode.DRY_RUN,
UserPrivileges.NORMAL));
new SystemClock()).run()), UTF_8));
}
}

View file

@ -38,8 +38,6 @@ import dagger.Provides;
import google.registry.config.RegistryEnvironment;
import google.registry.flows.EppException;
import google.registry.flows.FlowRunner;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.SessionMetadata.SessionSource;
import google.registry.flows.StatelessRequestSessionMetadata;
import google.registry.flows.domain.DomainCheckFlow;
@ -124,7 +122,7 @@ public class CheckApiAction implements Runnable {
inputXmlBytes,
null,
clock)
.run(CommitMode.LIVE, UserPrivileges.NORMAL)
.run()
.getResponse();
DomainCheckData checkData = (DomainCheckData) response.getResponseData().get(0);
DomainCheck check = (DomainCheck) checkData.getChecks().get(0);

View file

@ -34,8 +34,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.SessionMetadata.SessionSource;
import google.registry.flows.picker.FlowPicker;
import google.registry.model.billing.BillingEvent;
@ -74,6 +72,12 @@ import java.util.Map;
@RunWith(MockitoJUnitRunner.class)
public abstract class FlowTestCase<F extends Flow> {
/** Whether to actually write to the datastore or just simulate. */
public enum CommitMode { LIVE, DRY_RUN }
/** Whether to run in normal or superuser mode. */
public enum UserPrivileges { NORMAL, SUPERUSER }
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
@ -87,7 +91,7 @@ public abstract class FlowTestCase<F extends Flow> {
protected EppLoader eppLoader;
protected Class<? extends Flow> flowClass;
protected SessionMetadata sessionMetadata;
protected TestSessionMetadata sessionMetadata;
protected FakeClock clock = new FakeClock(DateTime.now(UTC));
@Before
@ -275,7 +279,9 @@ public abstract class FlowTestCase<F extends Flow> {
/** Run a flow, and attempt to marshal the result to EPP or throw if it doesn't validate. */
public EppOutput runFlow(CommitMode commitMode, UserPrivileges userPrivileges) throws Exception {
EppOutput output = getFlowRunner().run(commitMode, userPrivileges);
sessionMetadata.setSuperuser(userPrivileges.equals(UserPrivileges.SUPERUSER));
sessionMetadata.setIsDryRun(commitMode.equals(CommitMode.DRY_RUN));
EppOutput output = getFlowRunner().run();
marshal(output, ValidationMode.STRICT);
return output;
}
@ -287,7 +293,9 @@ public abstract class FlowTestCase<F extends Flow> {
public void runFlowAssertResponse(
CommitMode commitMode, UserPrivileges userPrivileges, String xml, String... ignoredPaths)
throws Exception {
EppOutput eppOutput = getFlowRunner().run(commitMode, userPrivileges);
sessionMetadata.setSuperuser(userPrivileges.equals(UserPrivileges.SUPERUSER));
sessionMetadata.setIsDryRun(commitMode.equals(CommitMode.DRY_RUN));
EppOutput eppOutput = getFlowRunner().run();
if (eppOutput.isResponse()) {
assertThat(eppOutput.isSuccess()).isTrue();
}

View file

@ -30,8 +30,6 @@ import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceAsyncDeleteFlow.ResourceToDeleteIsReferencedException;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;

View file

@ -24,8 +24,6 @@ import static google.registry.testing.DatastoreHelper.persistResource;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;
import google.registry.flows.ResourceMutateFlow.ResourceToMutateDoesNotExistException;

View file

@ -43,8 +43,6 @@ import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Ref;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceCreateFlow.ResourceAlreadyExistsException;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.domain.DomainAllocateFlow.HasFinalStatusException;

View file

@ -41,8 +41,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceCreateFlow.ResourceAlreadyExistsException;
import google.registry.flows.ResourceFlow.BadCommandForRegistryPhaseException;
import google.registry.flows.ResourceFlowTestCase;

View file

@ -30,8 +30,6 @@ import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Ref;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlow.BadCommandForRegistryPhaseException;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;

View file

@ -33,8 +33,6 @@ import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Ref;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;
import google.registry.flows.ResourceMutateFlow.ResourceToMutateDoesNotExistException;

View file

@ -47,8 +47,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.LoggedInFlow.UndeclaredServiceExtensionException;
import google.registry.flows.ResourceCreateFlow.ResourceAlreadyExistsException;
import google.registry.flows.ResourceCreateOrMutateFlow.OnlyToolCanPassMetadataException;

View file

@ -44,8 +44,6 @@ import com.google.common.collect.Iterables;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceCreateOrMutateFlow.OnlyToolCanPassMetadataException;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;

View file

@ -35,8 +35,6 @@ import com.google.common.collect.ImmutableSortedMap;
import com.googlecode.objectify.Ref;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;
import google.registry.flows.ResourceMutateFlow.ResourceToMutateDoesNotExistException;

View file

@ -36,8 +36,6 @@ import com.google.common.collect.ImmutableSortedMap;
import com.googlecode.objectify.Key;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;
import google.registry.flows.ResourceMutateFlow.ResourceToMutateDoesNotExistException;

View file

@ -38,8 +38,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlowUtils.BadAuthInfoForResourceException;
import google.registry.flows.ResourceMutateFlow.ResourceToMutateDoesNotExistException;
import google.registry.flows.ResourceTransferRequestFlow.AlreadyPendingTransferException;

View file

@ -42,8 +42,6 @@ import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import google.registry.flows.EppException.UnimplementedExtensionException;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceCreateOrMutateFlow.OnlyToolCanPassMetadataException;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;

View file

@ -33,8 +33,6 @@ import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceAsyncDeleteFlow.ResourceToDeleteIsReferencedException;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;

View file

@ -42,8 +42,6 @@ import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.ResourceFlowTestCase;
import google.registry.flows.ResourceFlowUtils.ResourceNotOwnedException;
import google.registry.flows.ResourceMutateFlow.ResourceToMutateDoesNotExistException;

View file

@ -31,8 +31,6 @@ import static org.joda.time.Duration.standardDays;
import com.googlecode.objectify.Key;
import google.registry.flows.FlowRunner;
import google.registry.flows.FlowRunner.CommitMode;
import google.registry.flows.FlowRunner.UserPrivileges;
import google.registry.flows.SessionMetadata;
import google.registry.model.domain.DomainResource;
import google.registry.model.eppcommon.Trid;
@ -89,7 +87,7 @@ public class EppResourceUtilsTest {
"<xml></xml>".getBytes(),
null,
clock)
.run(CommitMode.LIVE, UserPrivileges.NORMAL);
.run();
}
/** Test that update flow creates commit logs needed to reload at any arbitrary time. */

View file

@ -25,6 +25,7 @@ public class TestSessionMetadata extends SessionMetadata {
private final Map<String, Object> properties = new HashMap<>();
private boolean isValid = true;
private boolean isDryRun = false;
private SessionSource sessionSource = SessionSource.NONE;
@Override
@ -57,6 +58,15 @@ public class TestSessionMetadata extends SessionMetadata {
sessionSource = source;
}
public void setIsDryRun(boolean isDryRun) {
this.isDryRun = isDryRun;
}
@Override
public boolean isDryRun() {
return isDryRun;
}
public boolean isValid() {
return isValid;
}