Add limit to list_domains command

This allows list_domains to continue working for large TLDs.

TESTED=Deploys to alpha and it works to list the most recently created domains even
on a TLD with a huge number of domains on it (much more than .app has currently).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196717389
This commit is contained in:
mcilwain 2018-05-15 13:12:34 -07:00 committed by jianglai
parent e4f25c08e8
commit 9c0d3b6db3
11 changed files with 243 additions and 198 deletions

View file

@ -1,99 +1,105 @@
<datastore-indexes autoGenerate="false"> <datastore-indexes autoGenerate="false">
<!-- For finding contact resources by registrar. --> <!-- For finding contact resources by registrar. -->
<datastore-index kind="ContactResource" ancestor="false" source="manual"> <datastore-index kind="ContactResource" ancestor="false" source="manual">
<property name="currentSponsorClientId" direction="asc"/> <property name="currentSponsorClientId" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="deletionTime" direction="asc"/>
<property name="searchName" direction="asc"/> <property name="searchName" direction="asc"/>
</datastore-index> </datastore-index>
<!-- For finding domain resources by registrar. --> <!-- For finding domain resources by registrar. -->
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="^i" direction="asc"/> <property name="^i" direction="asc"/>
<property name="currentSponsorClientId" direction="asc"/> <property name="currentSponsorClientId" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="deletionTime" direction="asc"/>
</datastore-index> </datastore-index>
<!-- For finding domain resources by TLD. --> <!-- For finding domain resources by TLD. -->
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="^i" direction="asc"/> <property name="^i" direction="asc"/>
<property name="tld" direction="asc"/> <property name="tld" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="deletionTime" direction="asc"/>
</datastore-index> </datastore-index>
<!-- For finding domain resources by registrar. --> <!-- For finding domain resources by registrar. -->
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="currentSponsorClientId" direction="asc"/> <property name="currentSponsorClientId" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="deletionTime" direction="asc"/>
</datastore-index> </datastore-index>
<!-- For finding host resources by registrar. --> <!-- For finding the most recently created domain resources. -->
<datastore-index kind="HostResource" ancestor="false" source="manual"> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="currentSponsorClientId" direction="asc"/> <property name="^i" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="tld" direction="asc"/>
<property name="fullyQualifiedHostName" direction="asc"/> <property name="creationTime" direction="desc"/>
</datastore-index> </datastore-index>
<!-- For finding account balance of registrar and viewing billing history. --> <!-- For finding host resources by registrar. -->
<datastore-index kind="RegistrarBillingEntry" ancestor="true" source="manual"> <datastore-index kind="HostResource" ancestor="false" source="manual">
<property name="currency" direction="asc"/> <property name="currentSponsorClientId" direction="asc"/>
<property name="created" direction="desc"/> <property name="deletionTime" direction="asc"/>
</datastore-index> <property name="fullyQualifiedHostName" direction="asc"/>
<!-- For determining the active domains linked to a given contact. --> </datastore-index>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <!-- For finding account balance of registrar and viewing billing history. -->
<property name="allContacts.contact" direction="asc"/> <datastore-index kind="RegistrarBillingEntry" ancestor="true" source="manual">
<property name="deletionTime" direction="asc"/> <property name="currency" direction="asc"/>
</datastore-index> <property name="created" direction="desc"/>
<!-- For determining the active domains linked to a given host. --> </datastore-index>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <!-- For determining the active domains linked to a given contact. -->
<property name="nsHosts" direction="asc"/> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="deletionTime" direction="asc"/> <property name="allContacts.contact" direction="asc"/>
</datastore-index> <property name="deletionTime" direction="asc"/>
<!-- For RDAP searches by linked nameserver. --> </datastore-index>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <!-- For determining the active domains linked to a given host. -->
<property name="^i" direction="asc"/> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="nsHosts" direction="asc"/> <property name="nsHosts" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="deletionTime" direction="asc"/>
</datastore-index> </datastore-index>
<!-- For WHOIS IP address lookup --> <!-- For RDAP searches by linked nameserver. -->
<datastore-index kind="HostResource" ancestor="false" source="manual"> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="inetAddresses" direction="asc"/> <property name="^i" direction="asc"/>
<property name="deletionTime" direction="asc"/> <property name="nsHosts" direction="asc"/>
</datastore-index> <property name="deletionTime" direction="asc"/>
<!-- For Poll --> </datastore-index>
<datastore-index kind="PollMessage" ancestor="false" source="manual"> <!-- For WHOIS IP address lookup -->
<property name="clientId" direction="asc"/> <datastore-index kind="HostResource" ancestor="false" source="manual">
<property name="eventTime" direction="asc"/> <property name="inetAddresses" direction="asc"/>
</datastore-index> <property name="deletionTime" direction="asc"/>
<datastore-index kind="PollMessage" ancestor="true" source="manual"> </datastore-index>
<property name="clientId" direction="asc"/> <!-- For Poll -->
<property name="eventTime" direction="asc"/> <datastore-index kind="PollMessage" ancestor="false" source="manual">
</datastore-index> <property name="clientId" direction="asc"/>
<!-- For the history viewer. --> <property name="eventTime" direction="asc"/>
<datastore-index kind="HistoryEntry" ancestor="true" source="manual"> </datastore-index>
<property name="modificationTime" direction="asc"/> <datastore-index kind="PollMessage" ancestor="true" source="manual">
</datastore-index> <property name="clientId" direction="asc"/>
<!-- For RDAP. --> <property name="eventTime" direction="asc"/>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> </datastore-index>
<property name="^i" direction="asc"/> <!-- For the history viewer. -->
<property name="fullyQualifiedDomainName" direction="asc"/> <datastore-index kind="HistoryEntry" ancestor="true" source="manual">
</datastore-index> <property name="modificationTime" direction="asc"/>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> </datastore-index>
<property name="^i" direction="asc"/> <!-- For RDAP. -->
<property name="currentSponsorClientId" direction="asc"/> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="fullyQualifiedDomainName" direction="asc"/> <property name="^i" direction="asc"/>
</datastore-index> <property name="fullyQualifiedDomainName" direction="asc"/>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> </datastore-index>
<property name="^i" direction="asc"/> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="currentSponsorClientId" direction="asc"/> <property name="^i" direction="asc"/>
<property name="tld" direction="asc"/> <property name="currentSponsorClientId" direction="asc"/>
<property name="fullyQualifiedDomainName" direction="asc"/> <property name="fullyQualifiedDomainName" direction="asc"/>
</datastore-index> </datastore-index>
<datastore-index kind="DomainBase" ancestor="false" source="manual"> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="^i" direction="asc"/> <property name="^i" direction="asc"/>
<property name="tld" direction="asc"/> <property name="currentSponsorClientId" direction="asc"/>
<property name="fullyQualifiedDomainName" direction="asc"/> <property name="tld" direction="asc"/>
</datastore-index> <property name="fullyQualifiedDomainName" direction="asc"/>
<datastore-index kind="HostResource" ancestor="false" source="manual"> </datastore-index>
<property name="deletionTime" direction="asc"/> <datastore-index kind="DomainBase" ancestor="false" source="manual">
<property name="fullyQualifiedHostName" direction="asc"/> <property name="^i" direction="asc"/>
</datastore-index> <property name="tld" direction="asc"/>
<datastore-index kind="ContactResource" ancestor="false" source="manual"> <property name="fullyQualifiedDomainName" direction="asc"/>
<property name="deletionTime" direction="asc"/> </datastore-index>
<property name="searchName" direction="asc"/> <datastore-index kind="HostResource" ancestor="false" source="manual">
</datastore-index> <property name="deletionTime" direction="asc"/>
<property name="fullyQualifiedHostName" direction="asc"/>
</datastore-index>
<datastore-index kind="ContactResource" ancestor="false" source="manual">
<property name="deletionTime" direction="asc"/>
<property name="searchName" direction="asc"/>
</datastore-index>
</datastore-indexes> </datastore-indexes>

View file

@ -83,6 +83,7 @@ public abstract class EppResource extends BackupGroupRoot implements Buildable {
// Map the method to XML, not the field, because if we map the field (with an adaptor class) it // Map the method to XML, not the field, because if we map the field (with an adaptor class) it
// will never be omitted from the xml even if the timestamp inside creationTime is null and we // will never be omitted from the xml even if the timestamp inside creationTime is null and we
// return null from the adaptor. (Instead it gets written as an empty tag.) // return null from the adaptor. (Instead it gets written as an empty tag.)
@Index
CreateAutoTimestamp creationTime = CreateAutoTimestamp.create(null); CreateAutoTimestamp creationTime = CreateAutoTimestamp.create(null);
/** /**

View file

@ -33,17 +33,22 @@ final class ListDomainsCommand extends ListObjectsCommand {
required = true) required = true)
private List<String> tlds; private List<String> tlds;
@Parameter(
names = {"-n", "--limit"},
description = "Max number of domains to list, most recent first; defaults to no limit."
)
private int maxDomains = Integer.MAX_VALUE;
@Override @Override
String getCommandPath() { String getCommandPath() {
return ListDomainsAction.PATH; return ListDomainsAction.PATH;
} }
/** Returns a map of parameters to be sent to the server /** Returns a map of parameters to be sent to the server (in addition to the usual ones). */
* (in addition to the usual ones). */
@Override @Override
ImmutableMap<String, Object> getParameterMap() { ImmutableMap<String, Object> getParameterMap() {
String tldsParam = Joiner.on(',').join(tlds); String tldsParam = Joiner.on(',').join(tlds);
checkArgument(tldsParam.length() < 1024, "Total length of TLDs is too long for URL parameter"); checkArgument(tldsParam.length() < 1024, "Total length of TLDs is too long for URL parameter");
return ImmutableMap.of("tlds", tldsParam); return ImmutableMap.of("tlds", tldsParam, "limit", maxDomains);
} }
} }

View file

@ -65,11 +65,9 @@ abstract class ListObjectsCommand implements RemoteApiCommand, ServerSideCommand
/** Returns the path to the servlet task. */ /** Returns the path to the servlet task. */
abstract String getCommandPath(); abstract String getCommandPath();
/** Returns a map of parameters to be sent to the server /** Returns a map of parameters to be sent to the server (in addition to the usual ones). */
* (in addition to the usual ones). */
@Nullable
ImmutableMap<String, Object> getParameterMap() { ImmutableMap<String, Object> getParameterMap() {
return null; return ImmutableMap.of();
} }
@Override @Override
@ -84,10 +82,7 @@ abstract class ListObjectsCommand implements RemoteApiCommand, ServerSideCommand
if (fullFieldNames) { if (fullFieldNames) {
params.put(FULL_FIELD_NAMES_PARAM, Boolean.TRUE); params.put(FULL_FIELD_NAMES_PARAM, Boolean.TRUE);
} }
ImmutableMap<String, Object> extraParams = getParameterMap(); params.putAll(getParameterMap());
if (extraParams != null) {
params.putAll(extraParams);
}
// Call the server and get the response data. // Call the server and get the response data.
String response = connection.send( String response = connection.send(
getCommandPath(), getCommandPath(),

View file

@ -15,22 +15,23 @@
package google.registry.tools.server; package google.registry.tools.server;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.queryNotDeleted; import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.assertTldsExist; import static google.registry.model.registry.Registries.assertTldsExist;
import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST; import static google.registry.request.Action.Method.POST;
import static java.util.Comparator.comparing; import static java.util.Comparator.comparing;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet; import google.registry.model.EppResource;
import com.google.common.collect.Lists; import google.registry.model.EppResourceUtils;
import google.registry.model.domain.DomainResource; import google.registry.model.domain.DomainResource;
import google.registry.request.Action; import google.registry.request.Action;
import google.registry.request.Parameter; import google.registry.request.Parameter;
import google.registry.request.auth.Auth; import google.registry.request.auth.Auth;
import google.registry.util.Clock; import google.registry.util.Clock;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import org.joda.time.DateTime;
/** An action that lists domains, for use by the {@code nomulus list_domains} command. */ /** An action that lists domains, for use by the {@code nomulus list_domains} command. */
@Action( @Action(
@ -45,6 +46,7 @@ public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
public static final String PATH = "/_dr/admin/list/domains"; public static final String PATH = "/_dr/admin/list/domains";
@Inject @Parameter("tlds") ImmutableSet<String> tlds; @Inject @Parameter("tlds") ImmutableSet<String> tlds;
@Inject @Parameter("limit") int limit;
@Inject Clock clock; @Inject Clock clock;
@Inject ListDomainsAction() {} @Inject ListDomainsAction() {}
@ -56,12 +58,27 @@ public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
@Override @Override
public ImmutableSet<DomainResource> loadObjects() { public ImmutableSet<DomainResource> loadObjects() {
checkArgument(!tlds.isEmpty(), "Must specify TLDs to query"); checkArgument(!tlds.isEmpty(), "Must specify TLDs to query");
checkArgument(
tlds.size() <= MAX_NUM_SUBQUERIES,
"Cannot query more than %s TLDs simultaneously",
MAX_NUM_SUBQUERIES);
assertTldsExist(tlds); assertTldsExist(tlds);
ImmutableSortedSet.Builder<DomainResource> builder = DateTime now = clock.nowUtc();
new ImmutableSortedSet.Builder<>(comparing(DomainResource::getFullyQualifiedDomainName)); return ofy()
for (List<String> batch : Lists.partition(tlds.asList(), MAX_NUM_SUBQUERIES)) { .load()
builder.addAll(queryNotDeleted(DomainResource.class, clock.nowUtc(), "tld in", batch)); .type(DomainResource.class)
} .filter("tld in", tlds)
return builder.build(); // Get the N most recently created domains (requires ordering in descending order).
.order("-creationTime")
.limit(limit)
.list()
.stream()
.map(EppResourceUtils.transformAtTime(now))
// Deleted entities must be filtered out post-query because queries don't allow ordering
// with two filters.
.filter(d -> d.getDeletionTime().isAfter(now))
// Sort back to ascending order for nicer display.
.sorted(comparing(EppResource::getCreationTime))
.collect(toImmutableSet());
} }
} }

View file

@ -107,6 +107,7 @@ public abstract class ListObjectsAction<T extends ImmutableObject> implements Ru
// Get the object data first, so we can figure out the list of all available fields using the // Get the object data first, so we can figure out the list of all available fields using the
// data if necessary. // data if necessary.
ImmutableSet<T> objects = loadObjects(); ImmutableSet<T> objects = loadObjects();
logger.infofmt("Loaded %d objects.", objects.size());
// Get the list of fields we should return. // Get the list of fields we should return.
ImmutableSet<String> fieldsToUse = getFieldsToUse(objects); ImmutableSet<String> fieldsToUse = getFieldsToUse(objects);
// Convert the data into a table. // Convert the data into a table.

View file

@ -16,6 +16,7 @@ package google.registry.tools.server;
import static com.google.common.base.Strings.emptyToNull; import static com.google.common.base.Strings.emptyToNull;
import static google.registry.request.RequestParameters.extractBooleanParameter; import static google.registry.request.RequestParameters.extractBooleanParameter;
import static google.registry.request.RequestParameters.extractIntParameter;
import static google.registry.request.RequestParameters.extractOptionalParameter; import static google.registry.request.RequestParameters.extractOptionalParameter;
import static google.registry.request.RequestParameters.extractRequiredParameter; import static google.registry.request.RequestParameters.extractRequiredParameter;
@ -90,6 +91,12 @@ public class ToolsServerModule {
return ImmutableSet.copyOf(Splitter.on(',').split(tldsString)); return ImmutableSet.copyOf(Splitter.on(',').split(tldsString));
} }
@Provides
@Parameter("limit")
static int provideLimit(HttpServletRequest req) {
return extractIntParameter(req, "limit");
}
@Provides @Provides
@Parameter("rawKeys") @Parameter("rawKeys")
static String provideRawKeys(HttpServletRequest req) { static String provideRawKeys(HttpServletRequest req) {

View file

@ -311,6 +311,11 @@ public class DatastoreHelper {
return persistResource(newDomainResource(domainName)); return persistResource(newDomainResource(domainName));
} }
public static DomainResource persistActiveDomain(String domainName, DateTime creationTime) {
return persistResource(
newDomainResource(domainName).asBuilder().setCreationTimeForTest(creationTime).build());
}
public static DomainApplication persistActiveDomainApplication(String domainName) { public static DomainApplication persistActiveDomainApplication(String domainName) {
return persistResource(newDomainApplication(domainName)); return persistResource(newDomainApplication(domainName));
} }

View file

@ -16,11 +16,13 @@ package google.registry.tools;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows; import static google.registry.testing.JUnitBackports.assertThrows;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.server.ListDomainsAction; import google.registry.tools.server.ListDomainsAction;
import java.util.List;
import org.junit.Test; import org.junit.Test;
/** /**
@ -36,17 +38,28 @@ public class ListDomainsCommandTest extends ListObjectsCommandTestCase<ListDomai
} }
@Override @Override
protected List<String> getTlds() { protected ImmutableMap<String, Object> getOtherParameters() {
return ImmutableList.of("foo"); return ImmutableMap.of("tlds", "foo", "limit", Integer.MAX_VALUE);
} }
@Test @Test
public void test_tldsParamTooLong() throws Exception { public void test_tldsParamTooLong() {
String tldsParam = "--tld=foo,bar" + Strings.repeat(",baz", 300); String tldsParam = "--tlds=foo,bar" + Strings.repeat(",baz", 300);
IllegalArgumentException thrown = IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommand(tldsParam)); assertThrows(IllegalArgumentException.class, () -> runCommand(tldsParam));
assertThat(thrown) assertThat(thrown)
.hasMessageThat() .hasMessageThat()
.contains("Total length of TLDs is too long for URL parameter"); .contains("Total length of TLDs is too long for URL parameter");
} }
@Test
public void test_bothParamsSpecified() throws Exception {
runCommand("--tlds=foo,bar", "--limit=100");
verify(connection)
.send(
eq(getTaskPath()),
eq(ImmutableMap.of("tlds", "foo,bar", "limit", 100)),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
}
} }

View file

@ -14,6 +14,7 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.request.JsonResponse.JSON_SAFETY_PREFIX; import static google.registry.request.JsonResponse.JSON_SAFETY_PREFIX;
import static google.registry.tools.server.ListObjectsAction.FIELDS_PARAM; 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.FULL_FIELD_NAMES_PARAM;
@ -24,14 +25,11 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType; import com.google.common.net.MediaType;
import google.registry.tools.ServerSideCommand.Connection; import google.registry.tools.ServerSideCommand.Connection;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock; import org.mockito.Mock;
@ -40,32 +38,31 @@ import org.mockito.Mock;
public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand> public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
extends CommandTestCase<C> { extends CommandTestCase<C> {
@Mock @Mock Connection connection;
Connection connection;
/** /** Where to find the servlet task; set by the subclass. */
* Where to find the servlet task; set by the subclass.
*/
abstract String getTaskPath(); abstract String getTaskPath();
/** /** The other parameters to be used (for those subclasses that use them; defaults to empty). */
* The TLD to be used (for those subclasses that use TLDs; defaults to empty). protected ImmutableMap<String, Object> getOtherParameters() {
*/ return ImmutableMap.of();
protected List<String> getTlds() {
return ImmutableList.of();
} }
/** ImmutableList<String> otherParams = ImmutableList.of();
* The TLDs argument to be passed on the command line; null if not needed.
*/
@Nullable String tldsParameter;
@Before @Before
public void init() throws Exception { public void init() throws Exception {
tldsParameter = getTlds().isEmpty() ? null : ("--tld=" + Joiner.on(',').join(getTlds())); ImmutableMap<String, Object> otherParameters = getOtherParameters();
if (!otherParameters.isEmpty()) {
otherParams =
otherParameters
.entrySet()
.stream()
.map(entry -> String.format("--%s=%s", entry.getKey(), entry.getValue()))
.collect(toImmutableList());
}
command.setConnection(connection); command.setConnection(connection);
when( when(connection.send(
connection.send(
eq(getTaskPath()), eq(getTaskPath()),
anyMapOf(String.class, Object.class), anyMapOf(String.class, Object.class),
eq(MediaType.PLAIN_TEXT_UTF_8), eq(MediaType.PLAIN_TEXT_UTF_8),
@ -74,9 +71,8 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
} }
private void verifySent( private void verifySent(
String fields, String fields, Optional<Boolean> printHeaderRow, Optional<Boolean> fullFieldNames)
Optional<Boolean> printHeaderRow, throws Exception {
Optional<Boolean> fullFieldNames) throws Exception {
ImmutableMap.Builder<String, Object> params = new ImmutableMap.Builder<>(); ImmutableMap.Builder<String, Object> params = new ImmutableMap.Builder<>();
if (fields != null) { if (fields != null) {
@ -84,88 +80,68 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
} }
printHeaderRow.ifPresent(aBoolean -> params.put(PRINT_HEADER_ROW_PARAM, aBoolean)); printHeaderRow.ifPresent(aBoolean -> params.put(PRINT_HEADER_ROW_PARAM, aBoolean));
fullFieldNames.ifPresent(aBoolean -> params.put(FULL_FIELD_NAMES_PARAM, aBoolean)); fullFieldNames.ifPresent(aBoolean -> params.put(FULL_FIELD_NAMES_PARAM, aBoolean));
if (!getTlds().isEmpty()) { params.putAll(getOtherParameters());
params.put("tlds", Joiner.on(',').join(getTlds())); verify(connection)
} .send(
verify(connection).send( eq(getTaskPath()), eq(params.build()), eq(MediaType.PLAIN_TEXT_UTF_8), eq(new byte[0]));
eq(getTaskPath()),
eq(params.build()),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
} }
@Test @Test
public void testRun_noFields() throws Exception { public void testRun_noFields() throws Exception {
if (tldsParameter == null) { runCommand(otherParams);
runCommand();
} else {
runCommand(tldsParameter);
}
verifySent(null, Optional.empty(), Optional.empty()); verifySent(null, Optional.empty(), Optional.empty());
} }
@Test @Test
public void testRun_oneField() throws Exception { public void testRun_oneField() throws Exception {
if (tldsParameter == null) { runCommand(
runCommand("--fields=fieldName"); new ImmutableList.Builder<String>().addAll(otherParams).add("--fields=fieldName").build());
} else {
runCommand("--fields=fieldName", tldsParameter);
}
verifySent("fieldName", Optional.empty(), Optional.empty()); verifySent("fieldName", Optional.empty(), Optional.empty());
} }
@Test @Test
public void testRun_wildcardField() throws Exception { public void testRun_wildcardField() throws Exception {
if (tldsParameter == null) { runCommand(new ImmutableList.Builder<String>().addAll(otherParams).add("--fields=*").build());
runCommand("--fields=*");
} else {
runCommand("--fields=*", tldsParameter);
}
verifySent("*", Optional.empty(), Optional.empty()); verifySent("*", Optional.empty(), Optional.empty());
} }
@Test @Test
public void testRun_header() throws Exception { public void testRun_header() throws Exception {
if (tldsParameter == null) { runCommand(
runCommand("--fields=fieldName", "--header=true"); new ImmutableList.Builder<String>()
} else { .addAll(otherParams)
runCommand("--fields=fieldName", "--header=true", tldsParameter); .add("--fields=fieldName", "--header=true")
} .build());
verifySent("fieldName", Optional.of(Boolean.TRUE), Optional.empty()); verifySent("fieldName", Optional.of(Boolean.TRUE), Optional.empty());
} }
@Test @Test
public void testRun_noHeader() throws Exception { public void testRun_noHeader() throws Exception {
if (tldsParameter == null) { runCommand(
runCommand("--fields=fieldName", "--header=false"); new ImmutableList.Builder<String>()
} else { .addAll(otherParams)
runCommand("--fields=fieldName", "--header=false", tldsParameter); .add("--fields=fieldName", "--header=false")
} .build());
verifySent("fieldName", Optional.of(Boolean.FALSE), Optional.empty()); verifySent("fieldName", Optional.of(Boolean.FALSE), Optional.empty());
} }
@Test @Test
public void testRun_fullFieldNames() throws Exception { public void testRun_fullFieldNames() throws Exception {
if (tldsParameter == null) { runCommand(
runCommand("--fields=fieldName", "--full_field_names"); new ImmutableList.Builder<String>()
} else { .addAll(otherParams)
runCommand("--fields=fieldName", "--full_field_names", tldsParameter); .add("--fields=fieldName", "--full_field_names")
} .build());
verifySent("fieldName", Optional.empty(), Optional.of(Boolean.TRUE)); verifySent("fieldName", Optional.empty(), Optional.of(Boolean.TRUE));
} }
@Test @Test
public void testRun_allParameters() throws Exception { public void testRun_allParameters() throws Exception {
if (tldsParameter == null) { runCommand(
runCommand("--fields=fieldName,otherFieldName,*", "--header=true", "--full_field_names"); new ImmutableList.Builder<String>()
} else { .addAll(otherParams)
runCommand( .add("--fields=fieldName,otherFieldName,*", "--header=true", "--full_field_names")
"--fields=fieldName,otherFieldName,*", .build());
"--header=true", verifySent("fieldName,otherFieldName,*", Optional.of(Boolean.TRUE), Optional.of(Boolean.TRUE));
"--full_field_names",
tldsParameter);
}
verifySent(
"fieldName,otherFieldName,*", Optional.of(Boolean.TRUE), Optional.of(Boolean.TRUE));
} }
} }

View file

@ -39,7 +39,8 @@ public class ListDomainsActionTest extends ListActionTestCase {
public void init() throws Exception { public void init() throws Exception {
createTld("foo"); createTld("foo");
action = new ListDomainsAction(); action = new ListDomainsAction();
action.clock = new FakeClock(DateTime.parse("2000-01-01TZ")); action.clock = new FakeClock(DateTime.parse("2018-01-01TZ"));
action.limit = Integer.MAX_VALUE;
} }
@Test @Test
@ -110,7 +111,6 @@ public class ListDomainsActionTest extends ListActionTestCase {
"^example2.foo$"); "^example2.foo$");
} }
@Test @Test
public void testRun_twoLinesWithIdOnlyNoHeader() throws Exception { public void testRun_twoLinesWithIdOnlyNoHeader() throws Exception {
action.tlds = ImmutableSet.of("foo"); action.tlds = ImmutableSet.of("foo");
@ -231,4 +231,23 @@ public class ListDomainsActionTest extends ListActionTestCase {
null, null,
"^Field 'badfield' not found - recognized fields are:"); "^Field 'badfield' not found - recognized fields are:");
} }
@Test
public void testRun_limitFiltersOutOldestDomains() {
createTlds("bar", "baz");
action.tlds = ImmutableSet.of("foo", "bar");
action.limit = 2;
persistActiveDomain("example4.foo", DateTime.parse("2017-04-01TZ"));
persistActiveDomain("example1.foo", DateTime.parse("2017-01-01TZ"));
persistActiveDomain("example2.bar", DateTime.parse("2017-02-01TZ"));
persistActiveDomain("example3.bar", DateTime.parse("2017-03-01TZ"));
persistActiveDomain("example5.baz", DateTime.parse("2018-01-01TZ"));
testRunSuccess(
action,
null,
null,
null,
"^example3.bar$",
"^example4.foo$");
}
} }