Improve ListDomainsCommand to list domains on multiple TLDs

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=140053423
This commit is contained in:
mcilwain 2016-11-23 11:23:14 -08:00 committed by Ben McIlwain
parent 35d88a9f8c
commit 5eb9702f05
11 changed files with 120 additions and 88 deletions

View file

@ -14,7 +14,11 @@
package google.registry.tools;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import google.registry.tools.server.ListDomainsAction;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
@ -32,7 +36,15 @@ public class ListDomainsCommandTest extends ListObjectsCommandTestCase<ListDomai
}
@Override
final String getTld() {
return "foo";
protected List<String> getTlds() {
return ImmutableList.of("foo");
}
@Test
public void test_tldsParamTooLong() throws Exception {
String tldsParam = "--tld=foo,bar" + Strings.repeat(",baz", 300);
thrown.expect(
IllegalArgumentException.class, "Total length of TLDs is too long for URL parameter");
runCommand(tldsParam);
}
}

View file

@ -27,9 +27,4 @@ public class ListHostsCommandTest extends ListObjectsCommandTestCase<ListHostsCo
final String getTaskPath() {
return ListHostsAction.PATH;
}
@Override
final String getTld() {
return null;
}
}

View file

@ -15,7 +15,6 @@
package google.registry.tools;
import static google.registry.request.JsonResponse.JSON_SAFETY_PREFIX;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.tools.server.ListObjectsAction.FIELDS_PARAM;
import static google.registry.tools.server.ListObjectsAction.FULL_FIELD_NAMES_PARAM;
import static google.registry.tools.server.ListObjectsAction.PRINT_HEADER_ROW_PARAM;
@ -25,10 +24,14 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
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 java.util.List;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@ -46,24 +49,20 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
abstract String getTaskPath();
/**
* The TLD to be used (for those subclasses that use TLDs; may be null).
* The TLD to be used (for those subclasses that use TLDs; defaults to empty).
*/
abstract String getTld();
protected List<String> getTlds() {
return ImmutableList.<String>of();
}
/**
* The TLD argument to be passed on the command line; null if not needed.
* The TLDs argument to be passed on the command line; null if not needed.
*/
String tldArgumentString;
@Nullable String tldsParameter;
@Before
public void init() throws Exception {
String tld = getTld();
if (tld == null) {
tldArgumentString = null;
} else {
createTld(tld);
tldArgumentString = "--tld=" + tld;
}
tldsParameter = getTlds().isEmpty() ? null : ("--tld=" + Joiner.on(',').join(getTlds()));
command.setConnection(connection);
when(
connection.send(
@ -89,9 +88,8 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
if (fullFieldNames.isPresent()) {
params.put(FULL_FIELD_NAMES_PARAM, fullFieldNames.get());
}
String tld = getTld();
if (tld != null) {
params.put("tld", tld);
if (!getTlds().isEmpty()) {
params.put("tlds", Joiner.on(',').join(getTlds()));
}
verify(connection).send(
eq(getTaskPath()),
@ -102,74 +100,74 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
@Test
public void testRun_noFields() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand();
} else {
runCommand(tldArgumentString);
runCommand(tldsParameter);
}
verifySent(null, Optional.<Boolean>absent(), Optional.<Boolean>absent());
}
@Test
public void testRun_oneField() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand("--fields=fieldName");
} else {
runCommand("--fields=fieldName", tldArgumentString);
runCommand("--fields=fieldName", tldsParameter);
}
verifySent("fieldName", Optional.<Boolean>absent(), Optional.<Boolean>absent());
}
@Test
public void testRun_wildcardField() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand("--fields=*");
} else {
runCommand("--fields=*", tldArgumentString);
runCommand("--fields=*", tldsParameter);
}
verifySent("*", Optional.<Boolean>absent(), Optional.<Boolean>absent());
}
@Test
public void testRun_header() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand("--fields=fieldName", "--header=true");
} else {
runCommand("--fields=fieldName", "--header=true", tldArgumentString);
runCommand("--fields=fieldName", "--header=true", tldsParameter);
}
verifySent("fieldName", Optional.of(Boolean.TRUE), Optional.<Boolean>absent());
}
@Test
public void testRun_noHeader() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand("--fields=fieldName", "--header=false");
} else {
runCommand("--fields=fieldName", "--header=false", tldArgumentString);
runCommand("--fields=fieldName", "--header=false", tldsParameter);
}
verifySent("fieldName", Optional.of(Boolean.FALSE), Optional.<Boolean>absent());
}
@Test
public void testRun_fullFieldNames() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand("--fields=fieldName", "--full_field_names");
} else {
runCommand("--fields=fieldName", "--full_field_names", tldArgumentString);
runCommand("--fields=fieldName", "--full_field_names", tldsParameter);
}
verifySent("fieldName", Optional.<Boolean>absent(), Optional.of(Boolean.TRUE));
}
@Test
public void testRun_allParameters() throws Exception {
if (tldArgumentString == null) {
if (tldsParameter == null) {
runCommand("--fields=fieldName,otherFieldName,*", "--header=true", "--full_field_names");
} else {
runCommand(
"--fields=fieldName,otherFieldName,*",
"--header=true",
"--full_field_names",
tldArgumentString);
tldsParameter);
}
verifySent(
"fieldName,otherFieldName,*", Optional.of(Boolean.TRUE), Optional.of(Boolean.TRUE));

View file

@ -28,9 +28,4 @@ public class ListPremiumListsCommandTest
final String getTaskPath() {
return ListPremiumListsAction.PATH;
}
@Override
final String getTld() {
return null;
}
}

View file

@ -28,9 +28,4 @@ public class ListRegistrarsCommandTest
final String getTaskPath() {
return ListRegistrarsAction.PATH;
}
@Override
final String getTld() {
return null;
}
}

View file

@ -28,9 +28,4 @@ public class ListReservedListsCommandTest
final String getTaskPath() {
return ListReservedListsAction.PATH;
}
@Override
final String getTld() {
return null;
}
}

View file

@ -21,16 +21,10 @@ import google.registry.tools.server.ListTldsAction;
*
* @see ListObjectsCommandTestCase
*/
public class ListTldsCommandTest
extends ListObjectsCommandTestCase<ListTldsCommand> {
public class ListTldsCommandTest extends ListObjectsCommandTestCase<ListTldsCommand> {
@Override
final String getTaskPath() {
return ListTldsAction.PATH;
}
@Override
final String getTld() {
return null;
}
}

View file

@ -19,6 +19,7 @@ import static google.registry.testing.DatastoreHelper.createTlds;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import google.registry.testing.FakeClock;
import org.joda.time.DateTime;
import org.junit.Before;
@ -42,19 +43,19 @@ public class ListDomainsActionTest extends ListActionTestCase {
}
@Test
public void testRun_invalidRequest_missingTld() throws Exception {
action.tld = null;
public void testRun_invalidRequest_missingTlds() throws Exception {
action.tlds = ImmutableSet.of();
testRunError(
action,
null,
null,
null,
"^Null or empty TLD specified$");
"^Must specify TLDs to query$");
}
@Test
public void testRun_invalidRequest_invalidTld() throws Exception {
action.tld = "%%%badtld%%%";
action.tlds = ImmutableSet.of("%%%badtld%%%");
testRunError(
action,
null,
@ -65,7 +66,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_noParameters() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
testRunSuccess(
action,
null,
@ -75,7 +76,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithIdOnly() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
createTlds("bar", "sim");
persistActiveDomain("dontlist.bar");
persistActiveDomain("example1.foo");
@ -91,9 +92,28 @@ public class ListDomainsActionTest extends ListActionTestCase {
"^example2.foo$");
}
@Test
public void testRun_multipleTlds() throws Exception {
action.tlds = ImmutableSet.of("bar", "foo");
createTlds("bar", "sim");
persistActiveDomain("dolist.bar");
persistActiveDomain("example1.foo");
persistActiveDomain("example2.foo");
persistActiveDomain("notlistedaswell.sim");
testRunSuccess(
action,
null,
null,
null,
"^dolist.bar",
"^example1.foo$",
"^example2.foo$");
}
@Test
public void testRun_twoLinesWithIdOnlyNoHeader() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example2.foo");
testRunSuccess(
@ -107,7 +127,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithIdOnlyExplicitHeader() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example2.foo");
testRunSuccess(
@ -123,7 +143,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithRepoId() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example3.foo");
testRunSuccess(
@ -139,7 +159,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithRepoIdNoHeader() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example3.foo");
testRunSuccess(
@ -153,7 +173,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithRepoIdExplicitHeader() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example3.foo");
testRunSuccess(
@ -169,7 +189,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithWildcard() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example3.foo");
testRunSuccess(
@ -185,7 +205,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_twoLinesWithWildcardAndAnotherField() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example1.foo");
persistActiveDomain("example3.foo");
testRunSuccess(
@ -201,7 +221,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
@Test
public void testRun_withBadField_returnsError() throws Exception {
action.tld = "foo";
action.tlds = ImmutableSet.of("foo");
persistActiveDomain("example2.foo");
persistActiveDomain("example1.foo");
testRunError(