diff --git a/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java b/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java index 9ad2bb20b..50a09da79 100644 --- a/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java +++ b/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java @@ -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"); - verifySent("testdata/domain_create_anchor_tenant.xml", false, true); + eppVerifier().asSuperuser().verifySent("testdata/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"); - verifySent("testdata/domain_create_anchor_tenant_password.xml", false, true); + eppVerifier().asSuperuser().verifySent("testdata/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"); - verifySent("testdata/domain_create_anchor_tenant_multiple_word_reason.xml", false, true); + eppVerifier().asSuperuser().verifySent("testdata/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"); - verifySent("testdata/domain_create_anchor_tenant_no_reason.xml", false, true); + eppVerifier().asSuperuser().verifySent("testdata/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"); - verifySent("testdata/domain_create_anchor_tenant_fee_standard.xml", false, true); + eppVerifier().asSuperuser().verifySent("testdata/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"); - verifySent("testdata/domain_create_anchor_tenant_fee_premium.xml", false, true); + eppVerifier().asSuperuser().verifySent("testdata/domain_create_anchor_tenant_fee_premium.xml"); } @Test diff --git a/javatests/google/registry/tools/CreateContactCommandTest.java b/javatests/google/registry/tools/CreateContactCommandTest.java index 1a6798672..bb629e232 100644 --- a/javatests/google/registry/tools/CreateContactCommandTest.java +++ b/javatests/google/registry/tools/CreateContactCommandTest.java @@ -45,7 +45,7 @@ public class CreateContactCommandTest "--fax=+1.7035555556", "--email=jdoe@example.com", "--password=2fooBAR"); - verifySent("testdata/contact_create_complete.xml", false, false); + eppVerifier().verifySent("testdata/contact_create_complete.xml"); } @Test @@ -53,7 +53,7 @@ public class CreateContactCommandTest // Will never be the case, but tests that each field can be omitted. // Also tests the auto-gen password. runCommandForced("--client=NewRegistrar"); - verifySent("testdata/contact_create_minimal.xml", false, false); + eppVerifier().verifySent("testdata/contact_create_minimal.xml"); } @Test diff --git a/javatests/google/registry/tools/DeleteDomainCommandTest.java b/javatests/google/registry/tools/DeleteDomainCommandTest.java index aa773f559..81f8e8d1c 100644 --- a/javatests/google/registry/tools/DeleteDomainCommandTest.java +++ b/javatests/google/registry/tools/DeleteDomainCommandTest.java @@ -25,28 +25,28 @@ public class DeleteDomainCommandTest extends EppToolCommandTestCase extends C /** Subclasses can override this to perform additional initialization. */ void initEppToolCommandTestCase() throws Exception {} - void verifySent(String fileToMatch, boolean dryRun, boolean superuser) throws Exception { - ImmutableMap params = ImmutableMap.of( - "clientIdentifier", "NewRegistrar", - "superuser", superuser, - "dryRun", dryRun); - verify(connection) - .send(eq("/_dr/epptool"), eq(params), eq(APPLICATION_EPP_XML_UTF8), xml.capture()); - assertXmlEquals(readResourceUtf8(getClass(), fileToMatch), new String(xml.getValue(), UTF_8)); + /** Helper to get a new {@link EppVerifier} instance. */ + EppVerifier eppVerifier() { + return new EppVerifier(); } - void verifySent(List filesToMatch, boolean dryRun, boolean superuser) throws Exception { - ImmutableMap params = ImmutableMap.of( - "clientIdentifier", "NewRegistrar", - "superuser", superuser, - "dryRun", dryRun); - verify(connection, times(filesToMatch.size())) - .send(eq("/_dr/epptool"), eq(params), eq(APPLICATION_EPP_XML_UTF8), xml.capture()); - List capturedXml = xml.getAllValues(); - assertThat(filesToMatch).hasSize(capturedXml.size()); - for (String fileToMatch : filesToMatch) { - assertXmlEquals( - readResourceUtf8(getClass(), fileToMatch), - new String(capturedXml.get(filesToMatch.indexOf(fileToMatch)), UTF_8)); + /** Builder pattern class for verifying EPP commands sent to the server. */ + class EppVerifier { + + String clientIdentifier = "NewRegistrar"; + boolean superuser = false; + boolean dryRun = false; + + EppVerifier setClientIdentifier(String clientIdentifier) { + this.clientIdentifier = clientIdentifier; + return this; + } + + EppVerifier asSuperuser() { + this.superuser = true; + return this; + } + + EppVerifier asDryRun() { + this.dryRun = true; + return this; + } + + void verifySent(String... filesToMatch) throws Exception { + ImmutableMap params = ImmutableMap.of( + "clientIdentifier", clientIdentifier, + "superuser", superuser, + "dryRun", dryRun); + verify(connection, times(filesToMatch.length)) + .send(eq("/_dr/epptool"), eq(params), eq(APPLICATION_EPP_XML_UTF8), xml.capture()); + List capturedXml = xml.getAllValues(); + assertThat(filesToMatch).hasLength(capturedXml.size()); + for (String fileToMatch : filesToMatch) { + assertXmlEquals( + readResourceUtf8(getClass(), fileToMatch), + new String(capturedXml.get(binarySearch(filesToMatch, fileToMatch)), UTF_8)); + } } } } diff --git a/javatests/google/registry/tools/ExecuteEppCommandTest.java b/javatests/google/registry/tools/ExecuteEppCommandTest.java index 04585198f..9ba490db0 100644 --- a/javatests/google/registry/tools/ExecuteEppCommandTest.java +++ b/javatests/google/registry/tools/ExecuteEppCommandTest.java @@ -17,8 +17,6 @@ package google.registry.tools; import static google.registry.util.ResourceUtils.readResourceUtf8; import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.common.collect.ImmutableList; - import com.beust.jcommander.ParameterException; import org.junit.Test; @@ -40,19 +38,19 @@ public class ExecuteEppCommandTest extends EppToolCommandTestCase