Refactor EppToolVerifier to accept chaining verify commands

We're doing this to allow several new tests:
- xml files (that exist today)
- xml files with substitutions
- xml content (maybe? Currently private. Caching the files seems more readable)
- no data at all

Instead of having only one interface

eppToolVerifier.verifySent("file1.xml", "file2.xml");

we're refactoring to allow:
eppToolVerifier
  .verifySent("file1.xml")
  .verifySentAny() // we don't care about this epps
  .verifySent("file2.xml", substitutions)
  .verifyNoMoreSent();

In this case we're checking that "exactly 3 EPPs were sent, where the 1st one has content from file1.xml, and the 3rd one has the content from file2.xml, after the given substitutions were applied"

This also updates EppToolCommandTestCase to have only one EppToolVerifier, and
always finish by checking verifyNoMoreSent, meaning that in every test - all
sent epps must be accounted for (verified or skiped)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177353887
This commit is contained in:
guyben 2017-11-29 13:20:55 -08:00 committed by jianglai
parent 0e3d050dae
commit 68768a561f
20 changed files with 285 additions and 190 deletions

View file

@ -41,28 +41,31 @@ import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.eppcommon.Trid;
import google.registry.model.eppinput.EppInput;
import google.registry.model.reporting.HistoryEntry;
import google.registry.tools.ServerSideCommand.Connection;
import google.registry.tools.server.ToolsTestData;
import java.io.IOException;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
/** Unit tests for {@link AllocateDomainCommand}. */
public class AllocateDomainCommandTest extends CommandTestCase<AllocateDomainCommand> {
@Mock
Connection connection;
private EppToolVerifier eppVerifier;
@Before
public void init() throws IOException {
command.setConnection(connection);
eppVerifier = EppToolVerifier.create(command).expectClientId("TheRegistrar").expectSuperuser();
createTld("tld", QUIET_PERIOD);
createApplication("example-one.tld", "domain_create_sunrush.xml", "1-TLD");
createApplication("example-two.tld", "domain_create_sunrush2.xml", "2-TLD");
}
@After
public void cleanup() throws Exception {
eppVerifier.verifyNoMoreSent();
}
private void createApplication(String name, String xmlFile, String repoId) throws IOException {
DomainApplication application =
persistResource(newDomainApplication(name)
@ -105,30 +108,25 @@ public class AllocateDomainCommandTest extends CommandTestCase<AllocateDomainCom
.build());
}
private EppToolVerifier eppVerifier() {
return new EppToolVerifier()
.withConnection(connection)
.withClientId("TheRegistrar")
.asSuperuser();
}
@Test
public void testSuccess() throws Exception {
runCommand("--ids=1-TLD", "--force", "--superuser");
// NB: These commands are sent as the sponsoring registrar, in this case "TheRegistrar".
eppVerifier().verifySent("allocate_domain.xml");
eppVerifier.verifySent("allocate_domain.xml");
}
@Test
public void testSuccess_multiple() throws Exception {
runCommand("--ids=1-TLD,2-TLD", "--force", "--superuser");
eppVerifier().verifySent("allocate_domain.xml", "allocate_domain2.xml");
eppVerifier
.verifySent("allocate_domain.xml")
.verifySent("allocate_domain2.xml");
}
@Test
public void testSuccess_dryRun() throws Exception {
runCommand("--ids=1-TLD", "--dry_run", "--superuser");
eppVerifier().asDryRun().verifySent("allocate_domain.xml");
eppVerifier.expectDryRun().verifySent("allocate_domain.xml");
}
@Test

View file

@ -37,35 +37,35 @@ public class CreateAnchorTenantCommandTest
public void testSuccess() throws Exception {
runCommandForced("--client=NewRegistrar", "--superuser",
"--reason=anchor-tenant-test", "--contact=jd1234", "--domain_name=example.tld");
eppVerifier().asSuperuser().verifySent("domain_create_anchor_tenant.xml");
eppVerifier.expectSuperuser().verifySent("domain_create_anchor_tenant.xml");
}
@Test
public void testSuccess_suppliedPassword() throws Exception {
runCommandForced("--client=NewRegistrar", "--superuser", "--password=foo",
"--reason=anchor-tenant-test", "--contact=jd1234", "--domain_name=example.tld");
eppVerifier().asSuperuser().verifySent("domain_create_anchor_tenant_password.xml");
eppVerifier.expectSuperuser().verifySent("domain_create_anchor_tenant_password.xml");
}
@Test
public void testSuccess_multipleWordReason() throws Exception {
runCommandForced("--client=NewRegistrar", "--superuser",
"--reason=\"anchor tenant test\"", "--contact=jd1234", "--domain_name=example.tld");
eppVerifier().asSuperuser().verifySent("domain_create_anchor_tenant_multiple_word_reason.xml");
eppVerifier.expectSuperuser().verifySent("domain_create_anchor_tenant_multiple_word_reason.xml");
}
@Test
public void testSuccess_noReason() throws Exception {
runCommandForced("--client=NewRegistrar", "--superuser",
"--contact=jd1234", "--domain_name=example.tld");
eppVerifier().asSuperuser().verifySent("domain_create_anchor_tenant_no_reason.xml");
eppVerifier.expectSuperuser().verifySent("domain_create_anchor_tenant_no_reason.xml");
}
@Test
public void testSuccess_feeStandard() throws Exception {
runCommandForced("--client=NewRegistrar", "--superuser", "--fee",
"--reason=anchor-tenant-test", "--contact=jd1234", "--domain_name=example.tld");
eppVerifier().asSuperuser().verifySent("domain_create_anchor_tenant_fee_standard.xml");
eppVerifier.expectSuperuser().verifySent("domain_create_anchor_tenant_fee_standard.xml");
}
@Test
@ -78,7 +78,7 @@ public class CreateAnchorTenantCommandTest
.build());
runCommandForced("--client=NewRegistrar", "--superuser", "--fee",
"--reason=anchor-tenant-test", "--contact=jd1234", "--domain_name=premium.tld");
eppVerifier().asSuperuser().verifySent("domain_create_anchor_tenant_fee_premium.xml");
eppVerifier.expectSuperuser().verifySent("domain_create_anchor_tenant_fee_premium.xml");
}
@Test

View file

@ -45,7 +45,7 @@ public class CreateContactCommandTest extends EppToolCommandTestCase<CreateConta
"--fax=+1.7035555556",
"--email=jdoe@example.com",
"--password=2fooBAR");
eppVerifier().verifySent("contact_create_complete.xml");
eppVerifier.verifySent("contact_create_complete.xml");
}
@Test
@ -53,7 +53,7 @@ public class CreateContactCommandTest extends EppToolCommandTestCase<CreateConta
// Will never be the case, but tests that each field can be omitted.
// Also tests the auto-gen password.
runCommandForced("--client=NewRegistrar");
eppVerifier().verifySent("contact_create_minimal.xml");
eppVerifier.verifySent("contact_create_minimal.xml");
}
@Test

View file

@ -38,7 +38,7 @@ public class CreateDomainCommandTest extends EppToolCommandTestCase<CreateDomain
"--techs=crr-tech",
"--password=2fooBAR",
"example.tld");
eppVerifier().verifySent("domain_create_complete.xml");
eppVerifier.verifySent("domain_create_complete.xml");
}
@Test
@ -50,7 +50,7 @@ public class CreateDomainCommandTest extends EppToolCommandTestCase<CreateDomain
"--admins=crr-admin",
"--techs=crr-tech",
"example.tld");
eppVerifier().verifySent("domain_create_minimal.xml");
eppVerifier.verifySent("domain_create_minimal.xml");
}
@Test
@ -62,7 +62,9 @@ public class CreateDomainCommandTest extends EppToolCommandTestCase<CreateDomain
"--techs=crr-tech",
"example.tld",
"example.abc");
eppVerifier().verifySent("domain_create_minimal.xml", "domain_create_minimal_abc.xml");
eppVerifier
.verifySent("domain_create_minimal.xml")
.verifySent("domain_create_minimal_abc.xml");
}
@Test

View file

@ -29,7 +29,7 @@ public class CreateHostCommandTest extends EppToolCommandTestCase<CreateHostComm
"--client=NewRegistrar",
"--host=example.tld",
"--addresses=162.100.102.99,2001:0db8:85a3:0000:0000:8a2e:0370:7334,4.5.6.7");
eppVerifier().verifySent("host_create_complete.xml");
eppVerifier.verifySent("host_create_complete.xml");
}
@Test
@ -38,7 +38,7 @@ public class CreateHostCommandTest extends EppToolCommandTestCase<CreateHostComm
runCommandForced(
"--client=NewRegistrar",
"--host=notours.external");
eppVerifier().verifySent("host_create_minimal.xml");
eppVerifier.verifySent("host_create_minimal.xml");
}
@Test

View file

@ -23,14 +23,14 @@ public class DeleteDomainCommandTest extends EppToolCommandTestCase<DeleteDomain
@Test
public void testSuccess() throws Exception {
runCommand("--client=NewRegistrar", "--domain_name=example.tld", "--force", "--reason=Test");
eppVerifier().verifySent("domain_delete.xml");
eppVerifier.verifySent("domain_delete.xml");
}
@Test
public void testSuccess_multipleWordReason() throws Exception {
runCommand(
"--client=NewRegistrar", "--domain_name=example.tld", "--force", "--reason=\"Test test\"");
eppVerifier().verifySent("domain_delete_multiple_word_reason.xml");
eppVerifier.verifySent("domain_delete_multiple_word_reason.xml");
}
@Test
@ -41,7 +41,7 @@ public class DeleteDomainCommandTest extends EppToolCommandTestCase<DeleteDomain
"--force",
"--reason=Test",
"--registrar_request=false");
eppVerifier().verifySent("domain_delete.xml");
eppVerifier.verifySent("domain_delete.xml");
}
@Test
@ -52,7 +52,7 @@ public class DeleteDomainCommandTest extends EppToolCommandTestCase<DeleteDomain
"--force",
"--reason=Test",
"--registrar_request=true");
eppVerifier().verifySent("domain_delete_by_registrar.xml");
eppVerifier.verifySent("domain_delete_by_registrar.xml");
}
@Test

View file

@ -23,14 +23,14 @@ public class DeleteHostCommandTest extends EppToolCommandTestCase<DeleteHostComm
@Test
public void testSuccess() throws Exception {
runCommand("--client=NewRegistrar", "--host=ns1.example.tld", "--force", "--reason=Test");
eppVerifier().verifySent("host_delete.xml");
eppVerifier.verifySent("host_delete.xml");
}
@Test
public void testSuccess_multipleWordReason() throws Exception {
runCommand(
"--client=NewRegistrar", "--host=ns1.example.tld", "--force", "--reason=\"Test test\"");
eppVerifier().verifySent("host_delete_multiple_word_reason.xml");
eppVerifier.verifySent("host_delete_multiple_word_reason.xml");
}
@Test
@ -41,7 +41,7 @@ public class DeleteHostCommandTest extends EppToolCommandTestCase<DeleteHostComm
"--force",
"--reason=Test",
"--registrar_request=false");
eppVerifier().verifySent("host_delete.xml");
eppVerifier.verifySent("host_delete.xml");
}
@Test
@ -52,7 +52,7 @@ public class DeleteHostCommandTest extends EppToolCommandTestCase<DeleteHostComm
"--force",
"--reason=Test",
"--registrar_request=true");
eppVerifier().verifySent("host_delete_by_registrar.xml");
eppVerifier.verifySent("host_delete_by_registrar.xml");
}
@Test

View file

@ -25,7 +25,7 @@ public class DomainApplicationInfoCommandTest
public void testSuccess() throws Exception {
runCommandForced("--client=NewRegistrar", "--domain_name=example.tld",
"--phase=landrush", "--id=123");
eppVerifier().verifySent("domain_info_landrush.xml");
eppVerifier.verifySent("domain_info_landrush.xml");
}
@Test
@ -33,7 +33,7 @@ public class DomainApplicationInfoCommandTest
// Sunrush: phase=sunrise, subphase=landrush
runCommandForced("--client=NewRegistrar", "--domain_name=example.tld",
"--phase=sunrush", "--id=123");
eppVerifier().verifySent("domain_info_sunrush.xml");
eppVerifier.verifySent("domain_info_sunrush.xml");
}
@Test

View file

@ -23,15 +23,15 @@ public class DomainCheckClaimsCommandTest extends EppToolCommandTestCase<DomainC
@Test
public void testSuccess() throws Exception {
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifySent("domain_check_claims.xml");
eppVerifier.verifySent("domain_check_claims.xml");
}
@Test
public void testSuccess_multipleTlds() throws Exception {
runCommandForced("--client=NewRegistrar", "example.tld", "example.tld2");
eppVerifier().verifySent(
"domain_check_claims.xml",
"domain_check_claims_second_tld.xml");
eppVerifier
.verifySent("domain_check_claims.xml")
.verifySent("domain_check_claims_second_tld.xml");
}
@Test
@ -41,7 +41,7 @@ public class DomainCheckClaimsCommandTest extends EppToolCommandTestCase<DomainC
"example.tld",
"example2.tld",
"example3.tld");
eppVerifier().verifySent("domain_check_claims_multiple.xml");
eppVerifier.verifySent("domain_check_claims_multiple.xml");
}
@Test
@ -52,9 +52,9 @@ public class DomainCheckClaimsCommandTest extends EppToolCommandTestCase<DomainC
"example2.tld",
"example3.tld",
"example.tld2");
eppVerifier().verifySent(
"domain_check_claims_multiple.xml",
"domain_check_claims_second_tld.xml");
eppVerifier
.verifySent("domain_check_claims_multiple.xml")
.verifySent("domain_check_claims_second_tld.xml");
}
@Test

View file

@ -23,15 +23,15 @@ public class DomainCheckCommandTest extends EppToolCommandTestCase<DomainCheckCo
@Test
public void testSuccess() throws Exception {
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifySent("domain_check.xml");
eppVerifier.verifySent("domain_check.xml");
}
@Test
public void testSuccess_multipleTlds() throws Exception {
runCommandForced("--client=NewRegistrar", "example.tld", "example.tld2");
eppVerifier().verifySent(
"domain_check.xml",
"domain_check_second_tld.xml");
eppVerifier
.verifySent("domain_check.xml")
.verifySent("domain_check_second_tld.xml");
}
@Test
@ -41,7 +41,7 @@ public class DomainCheckCommandTest extends EppToolCommandTestCase<DomainCheckCo
"example.tld",
"example2.tld",
"example3.tld");
eppVerifier().verifySent("domain_check_multiple.xml");
eppVerifier.verifySent("domain_check_multiple.xml");
}
@Test
@ -52,9 +52,9 @@ public class DomainCheckCommandTest extends EppToolCommandTestCase<DomainCheckCo
"example2.tld",
"example3.tld",
"example.tld2");
eppVerifier().verifySent(
"domain_check_multiple.xml",
"domain_check_second_tld.xml");
eppVerifier
.verifySent("domain_check_multiple.xml")
.verifySent("domain_check_second_tld.xml");
}
@Test

View file

@ -23,15 +23,15 @@ public class DomainCheckFeeCommandTest extends EppToolCommandTestCase<DomainChec
@Test
public void testSuccess() throws Exception {
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifySent("domain_check_fee.xml");
eppVerifier.verifySent("domain_check_fee.xml");
}
@Test
public void testSuccess_multipleTlds() throws Exception {
runCommandForced("--client=NewRegistrar", "example.tld", "example.tld2");
eppVerifier().verifySent(
"domain_check_fee.xml",
"domain_check_fee_second_tld.xml");
eppVerifier
.verifySent("domain_check_fee.xml")
.verifySent("domain_check_fee_second_tld.xml");
}
@Test
@ -41,7 +41,7 @@ public class DomainCheckFeeCommandTest extends EppToolCommandTestCase<DomainChec
"example.tld",
"example2.tld",
"example3.tld");
eppVerifier().verifySent("domain_check_fee_multiple.xml");
eppVerifier.verifySent("domain_check_fee_multiple.xml");
}
@Test
@ -52,9 +52,9 @@ public class DomainCheckFeeCommandTest extends EppToolCommandTestCase<DomainChec
"example2.tld",
"example3.tld",
"example.tld2");
eppVerifier().verifySent(
"domain_check_fee_multiple.xml",
"domain_check_fee_second_tld.xml");
eppVerifier
.verifySent("domain_check_fee_multiple.xml")
.verifySent("domain_check_fee_second_tld.xml");
}
@Test

View file

@ -52,7 +52,7 @@ public class EppToolCommandTest extends EppToolCommandTestCase<EppToolCommand> {
runCommandForced(
"--client=NewRegistrar",
ToolsTestData.loadUtf8("contact_create.xml"));
eppVerifier().verifySent("contact_create.xml");
eppVerifier.verifySent("contact_create.xml");
}
@Test
@ -63,7 +63,10 @@ public class EppToolCommandTest extends EppToolCommandTestCase<EppToolCommand> {
ToolsTestData.loadUtf8("contact_create.xml"),
ToolsTestData.loadUtf8("domain_check.xml"),
ToolsTestData.loadUtf8("domain_check_fee.xml"));
eppVerifier().verifySent("contact_create.xml", "domain_check.xml", "domain_check_fee.xml");
eppVerifier
.verifySent("contact_create.xml")
.verifySent("domain_check.xml")
.verifySent("domain_check_fee.xml");
}
@Test

View file

@ -16,29 +16,30 @@ package google.registry.tools;
import static google.registry.testing.DatastoreHelper.createTlds;
import google.registry.tools.ServerSideCommand.Connection;
import org.junit.After;
import org.junit.Before;
import org.mockito.Mock;
/**
* Abstract class for commands that construct + send EPP commands.
*
* Has an EppToolVerifier member that needs to have all epp messages accounted for before the test
* has ended.
*
* @param <C> the command type
*/
public abstract class EppToolCommandTestCase<C extends EppToolCommand> extends CommandTestCase<C> {
@Mock
Connection connection;
EppToolVerifier eppVerifier;
@Before
public void init() throws Exception {
// Create two TLDs for commands that allow multiple TLDs at once.
createTlds("tld", "tld2");
command.setConnection(connection);
eppVerifier = EppToolVerifier.create(command).expectClientId("NewRegistrar");
}
/** Helper to get a new {@link EppToolVerifier} instance. */
EppToolVerifier eppVerifier() {
return new EppToolVerifier().withConnection(connection).withClientId("NewRegistrar");
@After
public void cleanup() throws Exception {
eppVerifier.verifyNoMoreSent();
}
}

View file

@ -14,90 +14,192 @@
package google.registry.tools;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.xml.XmlTestUtils.assertXmlEquals;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.ServerSideCommand.Connection;
import google.registry.tools.server.ToolsTestData;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.mockito.ArgumentCaptor;
/** Class for verifying EPP commands sent to the server via the tool endpoint. */
/**
* Class for verifying EPP commands sent to the server via the tool endpoint.
*
* <p>Provides its own (mock) {@link Connection} that will be monitored for EPP transmission. This
* Connection needs to be registered with the tool endpoint - something like this:
*
* <pre> {@code
* SomeToolCommand command = ...;
* EppToolVerifier eppToolVerifier = EppToolVerifier.create(command);
* // run command...
* eppToolVerifier.expectClientId("SomeClientId").verifySent("some_epp_file.xml");
* }</pre>
*/
public class EppToolVerifier {
private final Connection connection;
private final String clientId;
private final boolean superuser;
private final boolean dryRun;
private final Connection connection = mock(Connection.class);
public EppToolVerifier() {
this(null, null, false, false);
private String clientId;
private boolean superuser;
private boolean dryRun;
private ImmutableList<byte[]> capturedParams;
private int paramIndex;
private EppToolVerifier() {}
/** Creates an EppToolVerifier that monitors EPPs sent by the given command. */
public static EppToolVerifier create(EppToolCommand command) {
EppToolVerifier eppToolVerifier = new EppToolVerifier();
command.setConnection(eppToolVerifier.getConnection());
return eppToolVerifier;
}
private EppToolVerifier(
Connection connection, String clientId, boolean superuser, boolean dryRun) {
this.connection = connection;
/**
* Sets the expected clientId for any following verifySent command.
*
* <p>Must be called at least once before any {@link verifySent} calls.
*/
public EppToolVerifier expectClientId(String clientId) {
this.clientId = clientId;
this.superuser = superuser;
this.dryRun = dryRun;
return this;
}
EppToolVerifier withConnection(Connection connection) {
return new EppToolVerifier(connection, clientId, superuser, dryRun);
/**
* Declares that any following verifySent command expects the "superuser" flag to be set.
*
* <p>If not called, {@link verifySent} will expect the "superuser" flag to be false.
*/
public EppToolVerifier expectSuperuser() {
this.superuser = true;
return this;
}
EppToolVerifier withClientId(String clientId) {
return new EppToolVerifier(connection, clientId, superuser, dryRun);
/**
* Declares that any following verifySent command expects the "dryRun" flag to be set.
*
* <p>If not called, {@link verifySent} will expect the "dryRun" flag to be false.
*/
public EppToolVerifier expectDryRun() {
this.dryRun = true;
return this;
}
EppToolVerifier asSuperuser() {
return new EppToolVerifier(connection, clientId, true, dryRun);
/**
* Tests that the expected EPP was sent.
*
* <p>The expected EPP must have the correct "clientId", "dryRun" and "superuser" flags as set by
* the various "expect*" calls.
*
* <p>The expected EPP's content is checked against the given file.
*
* <p>If multiple EPPs are expected, the verifySent* call order must match the actual EPP order.
*
* @param expectedXmlFile the name of the file holding the expected content of the EPP. The file
* resides in the tools/server/testdata directory.
*/
public EppToolVerifier verifySent(String expectedXmlFile) throws Exception {
return verifySentContents(ToolsTestData.loadUtf8(expectedXmlFile));
}
EppToolVerifier asDryRun() {
return new EppToolVerifier(connection, clientId, superuser, true);
/**
* Tests that the expected EPP was sent, with the given substitutions.
*
* <p>The expected EPP must have the correct "clientId", "dryRun" and "superuser" flags as set by
* the various "expect*" calls.
*
* <p>The expected EPP's content is checked against the given file after the given substitutions
* have been applied.
*
* <p>If multiple EPPs are expected, the verifySent* call order must match the EPP order.
*
* @param expectedXmlFile the name of the file holding the expected content of the EPP. The file
* resides in the tools/server/testdata directory.
* @param substitutions a list of substitutions to apply on the expectedXmlFile
*/
public EppToolVerifier verifySent(String expectedXmlFile, Map<String, String> substitutions)
throws Exception {
return verifySentContents(ToolsTestData.loadUtf8(expectedXmlFile, substitutions));
}
void verifySent(String... expectedXmlFiles) throws Exception {
verifySentContents(
Arrays.stream(expectedXmlFiles).map(ToolsTestData::loadUtf8).collect(toImmutableList()));
/**
* Tests an EPP was sent, without checking the contents.
*
* <p>The expected EPP are not check for its content or any of the "contentId" / "superuser" /
* "dryRun" flags - only that it exists.
*
* <p>If multiple EPPs are expected, the verifySent* call order must match the EPP order.
*/
public EppToolVerifier verifySentAny() throws Exception {
setArgumentsIfNeeded();
paramIndex++;
assertThat(capturedParams.size()).isAtLeast(paramIndex);
return this;
}
void verifySentContents(List<String> expectedXmlContents) throws Exception {
/**
* Test that no more EPPs were sent, after any that were expected in previous "verifySent" calls.
*/
public void verifyNoMoreSent() throws Exception {
setArgumentsIfNeeded();
assertThat(
capturedParams
.stream()
.skip(paramIndex)
.map(bytes -> new String(bytes, UTF_8))
.toArray())
.isEmpty();
}
private void setArgumentsIfNeeded() throws Exception {
if (capturedParams != null) {
return;
}
ArgumentCaptor<byte[]> params = ArgumentCaptor.forClass(byte[].class);
verify(connection, times(expectedXmlContents.size())).send(
verify(connection, atLeast(0)).send(
eq("/_dr/epptool"),
eq(ImmutableMap.<String, Object>of()),
eq(MediaType.FORM_DATA),
params.capture());
List<byte[]> capturedParams = params.getAllValues();
assertThat(capturedParams).hasSize(expectedXmlContents.size());
for (int i = 0; i < expectedXmlContents.size(); i++) {
byte[] capturedParam = capturedParams.get(i);
capturedParams = ImmutableList.copyOf(params.getAllValues());
paramIndex = 0;
}
private String bytesToXml(byte[] bytes) throws Exception {
checkState(clientId != null, "expectClientId must be called before any verifySent command");
Map<String, String> map =
Splitter.on('&').withKeyValueSeparator('=').split(new String(capturedParam, UTF_8));
Splitter.on('&')
.withKeyValueSeparator('=')
.split(new String(bytes, UTF_8));
assertThat(map).hasSize(4);
assertXmlEquals(
expectedXmlContents.get(i), URLDecoder.decode(map.get("xml"), UTF_8.toString()));
assertThat(map).containsEntry("dryRun", Boolean.toString(dryRun));
assertThat(map).containsEntry("clientId", clientId);
assertThat(map).containsEntry("superuser", Boolean.toString(superuser));
}
return URLDecoder.decode(map.get("xml"), UTF_8.toString());
}
void verifyNothingSent() {
verifyZeroInteractions(connection);
private EppToolVerifier verifySentContents(String expectedXmlContent) throws Exception {
setArgumentsIfNeeded();
assertThat(capturedParams.size()).isGreaterThan(paramIndex);
assertXmlEquals(
expectedXmlContent,
bytesToXml(capturedParams.get(paramIndex)));
paramIndex++;
return this;
}
/** Returns the (mock) Connection that is being monitored by this verifier. */
private Connection getConnection() {
return connection;
}
}

View file

@ -42,19 +42,19 @@ public class ExecuteEppCommandTest extends EppToolCommandTestCase<ExecuteEppComm
@Test
public void testSuccess() throws Exception {
runCommand("--client=NewRegistrar", "--force", eppFile);
eppVerifier().verifySent("contact_create.xml");
eppVerifier.verifySent("contact_create.xml");
}
@Test
public void testSuccess_dryRun() throws Exception {
runCommand("--client=NewRegistrar", "--dry_run", eppFile);
eppVerifier().asDryRun().verifySent("contact_create.xml");
eppVerifier.expectDryRun().verifySent("contact_create.xml");
}
@Test
public void testSuccess_withSuperuser() throws Exception {
runCommand("--client=NewRegistrar", "--superuser", "--force", eppFile);
eppVerifier().asSuperuser().verifySent("contact_create.xml");
eppVerifier.expectSuperuser().verifySent("contact_create.xml");
}
@Test
@ -62,7 +62,7 @@ public class ExecuteEppCommandTest extends EppToolCommandTestCase<ExecuteEppComm
inject.setStaticField(
ExecuteEppCommand.class, "stdin", new ByteArrayInputStream(xmlInput.getBytes(UTF_8)));
runCommand("--client=NewRegistrar", "--force");
eppVerifier().verifySent("contact_create.xml");
eppVerifier.verifySent("contact_create.xml");
}
@Test
@ -70,7 +70,9 @@ public class ExecuteEppCommandTest extends EppToolCommandTestCase<ExecuteEppComm
String xmlInput2 = ToolsTestData.loadUtf8("domain_check.xml");
String eppFile2 = writeToNamedTmpFile("eppFile2", xmlInput2);
runCommand("--client=NewRegistrar", "--force", eppFile, eppFile2);
eppVerifier().verifySent("contact_create.xml", "domain_check.xml");
eppVerifier
.verifySent("contact_create.xml")
.verifySent("domain_check.xml");
}
@Test

View file

@ -21,34 +21,27 @@ import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.tools.LockOrUnlockDomainCommand.REGISTRY_LOCK_STATUSES;
import static google.registry.tools.server.ToolsTestData.loadUtf8;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
/** Unit tests for {@link LockDomainCommand}. */
public class LockDomainCommandTest extends EppToolCommandTestCase<LockDomainCommand> {
/** Gets an overridden eppVerifier that has superuser set to true on it. */
@Override
EppToolVerifier eppVerifier() {
return new EppToolVerifier()
.withConnection(connection)
.withClientId("NewRegistrar")
.asSuperuser();
@Before
public void before() {
eppVerifier.expectSuperuser();
}
@Test
public void testSuccess_sendsCorrectEppXml() throws Exception {
persistActiveDomain("example.tld");
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier()
.verifySentContents(
ImmutableList.of(
loadUtf8("domain_lock.xml", ImmutableMap.of("DOMAIN", "example.tld"))));
eppVerifier.verifySent("domain_lock.xml", ImmutableMap.of("DOMAIN", "example.tld"));
}
@Test
@ -59,24 +52,24 @@ public class LockDomainCommandTest extends EppToolCommandTestCase<LockDomainComm
.addStatusValue(SERVER_TRANSFER_PROHIBITED)
.build());
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifySent("domain_lock_partial_statuses.xml");
eppVerifier.verifySent("domain_lock_partial_statuses.xml");
}
@Test
public void testSuccess_manyDomains() throws Exception {
List<String> params = new ArrayList<>();
List<String> expectedXmls = new ArrayList<>();
params.add("--client=NewRegistrar");
// Create 26 domains -- one more than the number of entity groups allowed in a transaction (in
// case that was going to be the failure point).
List<String> domains = new ArrayList<>();
for (int n = 0; n < 26; n++) {
String domain = String.format("domain%d.tld", n);
persistActiveDomain(domain);
params.add(domain);
expectedXmls.add(loadUtf8("domain_lock.xml", ImmutableMap.of("DOMAIN", domain)));
domains.add(domain);
}
runCommandForced(
ImmutableList.<String>builder().add("--client=NewRegistrar").addAll(domains).build());
for (String domain : domains) {
eppVerifier.verifySent("domain_lock.xml", ImmutableMap.of("DOMAIN", domain));
}
runCommandForced(params);
eppVerifier().verifySentContents(expectedXmls);
}
@Test
@ -96,7 +89,6 @@ public class LockDomainCommandTest extends EppToolCommandTestCase<LockDomainComm
.addStatusValues(REGISTRY_LOCK_STATUSES)
.build());
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifyNothingSent();
}
@Test

View file

@ -70,9 +70,9 @@ public class UniformRapidSuspensionCommandTest
"--domain_name=evil.tld",
"--hosts=urs1.example.com,urs2.example.com",
"--dsdata={\"keyTag\":1,\"alg\":1,\"digestType\":1,\"digest\":\"abc\"}");
eppVerifier()
.withClientId("CharlestonRoad")
.asSuperuser()
eppVerifier
.expectClientId("CharlestonRoad")
.expectSuperuser()
.verifySent("uniform_rapid_suspension.xml");
assertInStdout("uniform_rapid_suspension --undo");
assertInStdout("--domain_name evil.tld");
@ -87,9 +87,9 @@ public class UniformRapidSuspensionCommandTest
public void testCommand_respectsExistingHost() throws Exception {
persistDomainWithHosts(urs2, ns1);
runCommandForced("--domain_name=evil.tld", "--hosts=urs1.example.com,urs2.example.com");
eppVerifier()
.withClientId("CharlestonRoad")
.asSuperuser()
eppVerifier
.expectClientId("CharlestonRoad")
.expectSuperuser()
.verifySent("uniform_rapid_suspension_existing_host.xml");
assertInStdout("uniform_rapid_suspension --undo ");
assertInStdout("--domain_name evil.tld");
@ -101,6 +101,7 @@ public class UniformRapidSuspensionCommandTest
public void testCommand_generatesUndoForUndelegatedDomain() throws Exception {
persistActiveDomain("evil.tld");
runCommandForced("--domain_name=evil.tld", "--hosts=urs1.example.com,urs2.example.com");
eppVerifier.verifySentAny();
assertInStdout("uniform_rapid_suspension --undo");
assertInStdout("--domain_name evil.tld");
assertNotInStdout("--locks_to_preserve");
@ -113,6 +114,7 @@ public class UniformRapidSuspensionCommandTest
.addStatusValue(StatusValue.SERVER_DELETE_PROHIBITED)
.build());
runCommandForced("--domain_name=evil.tld");
eppVerifier.verifySentAny();
assertInStdout("uniform_rapid_suspension --undo");
assertInStdout("--domain_name evil.tld");
assertInStdout("--locks_to_preserve serverDeleteProhibited");
@ -123,9 +125,9 @@ public class UniformRapidSuspensionCommandTest
persistDomainWithHosts(urs1, urs2);
runCommandForced(
"--domain_name=evil.tld", "--undo", "--hosts=ns1.example.com,ns2.example.com");
eppVerifier()
.withClientId("CharlestonRoad")
.asSuperuser()
eppVerifier
.expectClientId("CharlestonRoad")
.expectSuperuser()
.verifySent("uniform_rapid_suspension_undo.xml");
assertNotInStdout("--undo"); // Undo shouldn't print a new undo command.
}
@ -138,9 +140,9 @@ public class UniformRapidSuspensionCommandTest
"--undo",
"--locks_to_preserve=serverDeleteProhibited",
"--hosts=ns1.example.com,ns2.example.com");
eppVerifier()
.withClientId("CharlestonRoad")
.asSuperuser()
eppVerifier
.expectClientId("CharlestonRoad")
.expectSuperuser()
.verifySent("uniform_rapid_suspension_undo_preserve.xml");
assertNotInStdout("--undo"); // Undo shouldn't print a new undo command.
}

View file

@ -22,25 +22,21 @@ import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.tools.server.ToolsTestData.loadUtf8;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
/** Unit tests for {@link UnlockDomainCommand}. */
public class UnlockDomainCommandTest extends EppToolCommandTestCase<UnlockDomainCommand> {
/** Gets an overridden eppVerifier that has superuser set to true on it. */
@Override
EppToolVerifier eppVerifier() {
return new EppToolVerifier()
.withConnection(connection)
.withClientId("NewRegistrar")
.asSuperuser();
@Before
public void before() {
eppVerifier.expectSuperuser();
}
private static void persistLockedDomain(String domainName) {
@ -57,10 +53,7 @@ public class UnlockDomainCommandTest extends EppToolCommandTestCase<UnlockDomain
public void testSuccess_sendsCorrectEppXml() throws Exception {
persistLockedDomain("example.tld");
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier()
.verifySentContents(
ImmutableList.of(
loadUtf8("domain_unlock.xml", ImmutableMap.of("DOMAIN", "example.tld"))));
eppVerifier.verifySent("domain_unlock.xml", ImmutableMap.of("DOMAIN", "example.tld"));
}
@Test
@ -71,24 +64,24 @@ public class UnlockDomainCommandTest extends EppToolCommandTestCase<UnlockDomain
.addStatusValues(ImmutableSet.of(SERVER_DELETE_PROHIBITED, SERVER_UPDATE_PROHIBITED))
.build());
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifySent("domain_unlock_partial_statuses.xml");
eppVerifier.verifySent("domain_unlock_partial_statuses.xml");
}
@Test
public void testSuccess_manyDomains() throws Exception {
List<String> params = new ArrayList<>();
List<String> expectedXmls = new ArrayList<>();
params.add("--client=NewRegistrar");
// Create 26 domains -- one more than the number of entity groups allowed in a transaction (in
// case that was going to be the failure point).
List<String> domains = new ArrayList<>();
for (int n = 0; n < 26; n++) {
String domain = String.format("domain%d.tld", n);
persistLockedDomain(domain);
params.add(domain);
expectedXmls.add(loadUtf8("domain_unlock.xml", ImmutableMap.of("DOMAIN", domain)));
domains.add(domain);
}
runCommandForced(
ImmutableList.<String>builder().add("--client=NewRegistrar").addAll(domains).build());
for (String domain : domains) {
eppVerifier.verifySent("domain_unlock.xml", ImmutableMap.of("DOMAIN", domain));
}
runCommandForced(params);
eppVerifier().verifySentContents(expectedXmls);
}
@Test
@ -104,7 +97,6 @@ public class UnlockDomainCommandTest extends EppToolCommandTestCase<UnlockDomain
public void testSuccess_alreadyUnlockedDomain_performsNoAction() throws Exception {
persistActiveDomain("example.tld");
runCommandForced("--client=NewRegistrar", "example.tld");
eppVerifier().verifyNothingSent();
}
@Test

View file

@ -49,7 +49,7 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
"--registrant=crr-admin",
"--password=2fooBAR",
"example.tld");
eppVerifier().verifySent("domain_update_complete.xml");
eppVerifier.verifySent("domain_update_complete.xml");
}
@Test
@ -68,7 +68,9 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
"--password=2fooBAR",
"example.tld",
"example.abc");
eppVerifier().verifySent("domain_update_complete.xml", "domain_update_complete_abc.xml");
eppVerifier
.verifySent("domain_update_complete.xml")
.verifySent("domain_update_complete_abc.xml");
}
@Test
@ -80,7 +82,7 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
"--add_techs=crr-tech2",
"--add_statuses=serverDeleteProhibited",
"example.tld");
eppVerifier().verifySent("domain_update_add.xml");
eppVerifier.verifySent("domain_update_add.xml");
}
@Test
@ -92,14 +94,14 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
"--remove_techs=crr-tech1",
"--remove_statuses=serverHold",
"example.tld");
eppVerifier().verifySent("domain_update_remove.xml");
eppVerifier.verifySent("domain_update_remove.xml");
}
@Test
public void testSuccess_change() throws Exception {
runCommandForced(
"--client=NewRegistrar", "--registrant=crr-admin", "--password=2fooBAR", "example.tld");
eppVerifier().verifySent("domain_update_change.xml");
eppVerifier.verifySent("domain_update_change.xml");
}
@Test
@ -112,7 +114,7 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
newDomainResource("example.tld").asBuilder().setNameservers(nameservers).build());
runCommandForced(
"--client=NewRegistrar", "--nameservers=ns2.zdns.google,ns3.zdns.google", "example.tld");
eppVerifier().verifySent("domain_update_set_nameservers.xml");
eppVerifier.verifySent("domain_update_set_nameservers.xml");
}
@Test
@ -142,7 +144,7 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
"--admins=crr-admin2,crr-admin3",
"--techs=crr-tech2,crr-tech3",
"example.tld");
eppVerifier().verifySent("domain_update_set_contacts.xml");
eppVerifier.verifySent("domain_update_set_contacts.xml");
}
@Test
@ -160,7 +162,7 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
runCommandForced(
"--client=NewRegistrar", "--statuses=clientRenewProhibited,serverHold", "example.tld");
eppVerifier().verifySent("domain_update_set_statuses.xml");
eppVerifier.verifySent("domain_update_set_statuses.xml");
}
@Test
@ -186,7 +188,6 @@ public class UpdateDomainCommandTest extends EppToolCommandTestCase<UpdateDomain
assertThat(e)
.hasMessageThat()
.containsMatch("The domain 'example.tld' has status SERVER_UPDATE_PROHIBITED");
eppVerifier().verifyNothingSent();
}
@Test

View file

@ -24,35 +24,35 @@ public class UpdateServerLocksCommandTest extends EppToolCommandTestCase<UpdateS
public void testSuccess_applyOne() throws Exception {
runCommandForced("--client=NewRegistrar", "--registrar_request=true", "--reason=Test",
"--domain_name=example.tld", "--apply=serverRenewProhibited");
eppVerifier().verifySent("update_server_locks_apply_one.xml");
eppVerifier.verifySent("update_server_locks_apply_one.xml");
}
@Test
public void testSuccess_multipleWordReason() throws Exception {
runCommandForced("--client=NewRegistrar", "--registrar_request=false",
"--reason=\"Test this\"", "--domain_name=example.tld", "--apply=serverRenewProhibited");
eppVerifier().verifySent("update_server_locks_multiple_word_reason.xml");
eppVerifier.verifySent("update_server_locks_multiple_word_reason.xml");
}
@Test
public void testSuccess_removeOne() throws Exception {
runCommandForced("--client=NewRegistrar", "--registrar_request=true", "--reason=Test",
"--domain_name=example.tld", "--remove=serverRenewProhibited");
eppVerifier().verifySent("update_server_locks_remove_one.xml");
eppVerifier.verifySent("update_server_locks_remove_one.xml");
}
@Test
public void testSuccess_applyAll() throws Exception {
runCommandForced("--client=NewRegistrar", "--registrar_request=true", "--reason=Test",
"--domain_name=example.tld", "--apply=all");
eppVerifier().verifySent("update_server_locks_apply_all.xml");
eppVerifier.verifySent("update_server_locks_apply_all.xml");
}
@Test
public void testSuccess_removeAll() throws Exception {
runCommandForced("--client=NewRegistrar", "--registrar_request=true", "--reason=Test",
"--domain_name=example.tld", "--remove=all");
eppVerifier().verifySent("update_server_locks_remove_all.xml");
eppVerifier.verifySent("update_server_locks_remove_all.xml");
}
@Test