mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 16:37:13 +02:00
Add assertTldsExist(Iterable<String>) to check multiple TLDs at once
This is better than calling assertTldExists() inside a for loop because you can throw a single exception reporting all bad TLDs at once rather than only getting as far as the first failure. And then it's also a one-liner instead of 3 lines. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=152412876
This commit is contained in:
parent
783033c261
commit
5081d780dc
8 changed files with 31 additions and 28 deletions
|
@ -29,7 +29,7 @@ import static google.registry.config.RegistryConfig.getDefaultRegistrarWhoisServ
|
|||
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.model.ofy.Ofy.RECOMMENDED_MEMCACHE_EXPIRATION;
|
||||
import static google.registry.model.registry.Registries.assertTldExists;
|
||||
import static google.registry.model.registry.Registries.assertTldsExist;
|
||||
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
|
||||
import static google.registry.util.CollectionUtils.nullToEmptyImmutableSortedCopy;
|
||||
import static google.registry.util.X509Utils.getCertificateHash;
|
||||
|
@ -670,10 +670,7 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
|
|||
}
|
||||
|
||||
public Builder setAllowedTlds(Set<String> allowedTlds) {
|
||||
for (String tld : allowedTlds) {
|
||||
assertTldExists(tld);
|
||||
}
|
||||
getInstance().allowedTlds = ImmutableSortedSet.copyOf(allowedTlds);
|
||||
getInstance().allowedTlds = ImmutableSortedSet.copyOf(assertTldsExist(allowedTlds));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,8 +15,9 @@
|
|||
package google.registry.model.registry;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Predicates.equalTo;
|
||||
import static com.google.common.base.Predicates.in;
|
||||
import static com.google.common.base.Predicates.not;
|
||||
import static com.google.common.base.Strings.emptyToNull;
|
||||
import static com.google.common.collect.Maps.filterValues;
|
||||
import static google.registry.model.CacheUtils.memoizeWithShortExpiration;
|
||||
|
@ -24,8 +25,10 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
|
|||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.net.InternetDomainName;
|
||||
|
@ -76,15 +79,25 @@ public final class Registries {
|
|||
return ImmutableSet.copyOf(filterValues(cache.get(), equalTo(type)).keySet());
|
||||
}
|
||||
|
||||
/** Shortcut to check whether a tld exists or else throw. If it exists, it is returned back. */
|
||||
/** Pass-through check that the specified TLD exists, otherwise throw an IAE. */
|
||||
public static String assertTldExists(String tld) {
|
||||
checkArgument(
|
||||
getTlds().contains(checkNotNull(emptyToNull(tld), "Null or empty TLD specified")),
|
||||
getTlds().contains(checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified")),
|
||||
"TLD %s does not exist",
|
||||
tld);
|
||||
return tld;
|
||||
}
|
||||
|
||||
/** Pass-through check that every TLD in the given iterable exists, otherwise throw an IAE. */
|
||||
public static Iterable<String> assertTldsExist(Iterable<String> tlds) {
|
||||
for (String tld : tlds) {
|
||||
checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified");
|
||||
}
|
||||
ImmutableSet<String> badTlds = FluentIterable.from(tlds).filter(not(in(getTlds()))).toSet();
|
||||
checkArgument(badTlds.isEmpty(), "TLDs do not exist: %s", Joiner.on(", ").join(badTlds));
|
||||
return tlds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns TLD which the domain name or hostname falls under, no matter how many levels of
|
||||
* sublabels there are.
|
||||
|
|
|
@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
import static com.google.common.base.Strings.isNullOrEmpty;
|
||||
import static com.google.common.collect.Sets.difference;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.model.registry.Registries.assertTldExists;
|
||||
import static google.registry.model.registry.Registries.assertTldsExist;
|
||||
import static google.registry.util.TokenUtils.TokenType.LRP;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
|
@ -49,7 +49,7 @@ import google.registry.util.TokenUtils;
|
|||
import java.io.StringReader;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
@ -74,7 +74,7 @@ public class CreateLrpTokensCommand implements RemoteApiCommand {
|
|||
names = {"-t", "--tlds"},
|
||||
description = "Comma-delimited list of TLDs that the tokens to create will be valid on",
|
||||
required = true)
|
||||
private String tlds;
|
||||
private List<String> tlds;
|
||||
|
||||
@Parameter(
|
||||
names = {"-i", "--input"},
|
||||
|
@ -121,10 +121,7 @@ public class CreateLrpTokensCommand implements RemoteApiCommand {
|
|||
checkArgument(
|
||||
(assignee == null) || (metadataColumns == null),
|
||||
"Metadata columns cannot be specified along with an assignee.");
|
||||
final Set<String> validTlds = ImmutableSet.copyOf(Splitter.on(',').split(tlds));
|
||||
for (String tld : validTlds) {
|
||||
assertTldExists(tld);
|
||||
}
|
||||
ImmutableSet<String> validTlds = ImmutableSet.copyOf(assertTldsExist(tlds));
|
||||
|
||||
LineReader reader = new LineReader(
|
||||
(assigneesFile != null)
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
package google.registry.tools;
|
||||
|
||||
import static google.registry.model.registry.Registries.assertTldExists;
|
||||
import static google.registry.model.registry.Registries.assertTldsExist;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
import static org.joda.time.Duration.standardMinutes;
|
||||
|
||||
|
@ -54,9 +54,7 @@ final class GenerateZoneFilesCommand implements ServerSideCommand {
|
|||
|
||||
@Override
|
||||
public void run() throws IOException {
|
||||
for (String tld : mainParameters) {
|
||||
assertTldExists(tld);
|
||||
}
|
||||
assertTldsExist(mainParameters);
|
||||
ImmutableMap<String, Object> params = ImmutableMap.of(
|
||||
"tlds", mainParameters,
|
||||
"exportTime", exportDate.toString());
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
package google.registry.tools;
|
||||
|
||||
import static google.registry.model.registry.Registries.assertTldExists;
|
||||
import static google.registry.model.registry.Registries.assertTldsExist;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
|
@ -33,8 +33,8 @@ final class GetTldCommand implements RemoteApiCommand {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
for (String tld : mainParameters) {
|
||||
System.out.println(Registry.get(assertTldExists(tld)));
|
||||
for (String tld : assertTldsExist(mainParameters)) {
|
||||
System.out.println(Registry.get(tld));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ package google.registry.tools.server;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static google.registry.model.EppResourceUtils.queryNotDeleted;
|
||||
import static google.registry.model.registry.Registries.assertTldExists;
|
||||
import static google.registry.model.registry.Registries.assertTldsExist;
|
||||
import static google.registry.request.Action.Method.GET;
|
||||
import static google.registry.request.Action.Method.POST;
|
||||
|
||||
|
@ -68,9 +68,7 @@ public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
|
|||
@Override
|
||||
public ImmutableSet<DomainResource> loadObjects() {
|
||||
checkArgument(!tlds.isEmpty(), "Must specify TLDs to query");
|
||||
for (String tld : tlds) {
|
||||
assertTldExists(tld);
|
||||
}
|
||||
assertTldsExist(tlds);
|
||||
ImmutableSortedSet.Builder<DomainResource> builder =
|
||||
new ImmutableSortedSet.Builder<DomainResource>(COMPARATOR);
|
||||
for (List<String> batch : Lists.partition(tlds.asList(), MAX_NUM_SUBQUERIES)) {
|
||||
|
|
|
@ -258,7 +258,7 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
|
|||
|
||||
@Test
|
||||
public void testFailure_badTld() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class, "TLD foo does not exist");
|
||||
thrown.expect(IllegalArgumentException.class, "TLDs do not exist: foo");
|
||||
runCommand("--assignee=domain.tld", "--tlds=foo");
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ public class ListDomainsActionTest extends ListActionTestCase {
|
|||
null,
|
||||
null,
|
||||
null,
|
||||
"^TLD %%%badtld%%% does not exist$");
|
||||
"^TLDs do not exist: %%%badtld%%%$");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue