Add a new method to load all Registry entities of a given type (#373)

* Add a new method to load all Registry entities of a given type

This is useful for things that need to know more than just the TLD strings
themselves, which is all Registries currently provides.
This commit is contained in:
Ben McIlwain 2019-11-18 17:40:42 -05:00 committed by GitHub
parent fcfbf00f0e
commit dbf1ff2096
2 changed files with 24 additions and 0 deletions

View file

@ -33,6 +33,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.google.common.net.InternetDomainName; import com.google.common.net.InternetDomainName;
import com.googlecode.objectify.Key;
import google.registry.model.registry.Registry.TldType; import google.registry.model.registry.Registry.TldType;
import java.util.Optional; import java.util.Optional;
@ -80,6 +81,15 @@ public final class Registries {
return ImmutableSet.copyOf(filterValues(cache.get(), equalTo(type)).keySet()); return ImmutableSet.copyOf(filterValues(cache.get(), equalTo(type)).keySet());
} }
/** Returns the Registry entities themselves of the given type loaded fresh from Datastore. */
public static ImmutableSet<Registry> getTldEntitiesOfType(TldType type) {
ImmutableSet<Key<Registry>> keys =
filterValues(cache.get(), equalTo(type)).keySet().stream()
.map(tld -> Key.create(getCrossTldKey(), Registry.class, tld))
.collect(toImmutableSet());
return ImmutableSet.copyOf(tm().doTransactionless(() -> ofy().load().keys(keys).values()));
}
/** Pass-through check that the specified TLD exists, otherwise throw an IAE. */ /** Pass-through check that the specified TLD exists, otherwise throw an IAE. */
public static String assertTldExists(String tld) { public static String assertTldExists(String tld) {
checkArgument( checkArgument(

View file

@ -17,9 +17,12 @@ package google.registry.model.registry;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat; import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.DatastoreHelper.createTlds;
import static google.registry.testing.DatastoreHelper.newRegistry;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.assertThrows; import static google.registry.testing.JUnitBackports.assertThrows;
import com.google.common.net.InternetDomainName; import com.google.common.net.InternetDomainName;
import google.registry.model.registry.Registry.TldType;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -32,6 +35,7 @@ public class RegistriesTest {
@Rule @Rule
public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
private void initTestTlds() { private void initTestTlds() {
createTlds("foo", "a.b.c"); // Test a multipart tld. createTlds("foo", "a.b.c"); // Test a multipart tld.
} }
@ -42,6 +46,16 @@ public class RegistriesTest {
assertThat(Registries.getTlds()).containsExactly("foo", "a.b.c"); assertThat(Registries.getTlds()).containsExactly("foo", "a.b.c");
} }
@Test
public void test_getTldEntities() {
initTestTlds();
persistResource(newRegistry("testtld", "TESTTLD").asBuilder().setTldType(TldType.TEST).build());
assertThat(Registries.getTldEntitiesOfType(TldType.REAL))
.containsExactly(Registry.get("foo"), Registry.get("a.b.c"));
assertThat(Registries.getTldEntitiesOfType(TldType.TEST))
.containsExactly(Registry.get("testtld"));
}
@Test @Test
public void testGetTlds_withNoRegistriesPersisted_returnsEmptySet() { public void testGetTlds_withNoRegistriesPersisted_returnsEmptySet() {
assertThat(Registries.getTlds()).isEmpty(); assertThat(Registries.getTlds()).isEmpty();