diff --git a/java/google/registry/model/registrar/Registrar.java b/java/google/registry/model/registrar/Registrar.java index 94fbd816a..92410e874 100644 --- a/java/google/registry/model/registrar/Registrar.java +++ b/java/google/registry/model/registrar/Registrar.java @@ -188,6 +188,17 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable immutableEnumSet( Type.REAL, Type.PDT, Type.OTE, Type.EXTERNAL_MONITORING, Type.MONITORING, Type.INTERNAL); + /** + * Compare two instances of {@link RegistrarContact} by their email addresses lexicographically. + */ + private static final Comparator CONTACT_EMAIL_COMPARATOR = + new Comparator() { + @Override + public int compare(RegistrarContact rc1, RegistrarContact rc2) { + return rc1.getEmailAddress().compareTo(rc2.getEmailAddress()); + } + }; + @Parent Key parent = getCrossTldKey(); @@ -532,14 +543,30 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable * address. */ public ImmutableSortedSet getContacts() { - return FluentIterable - .from(ofy().load().type(RegistrarContact.class).ancestor(Registrar.this)) + return FluentIterable.from(getContactsIterable()) .filter(notNull()) - .toSortedSet(new Comparator() { - @Override - public int compare(RegistrarContact rc1, RegistrarContact rc2) { - return rc1.getEmailAddress().compareTo(rc2.getEmailAddress()); - }}); + .toSortedSet(CONTACT_EMAIL_COMPARATOR); + } + + /** + * Returns a list of {@link RegistrarContact} objects of a given type for this registrar sorted by + * their email address. + */ + public ImmutableSortedSet getContactsOfType(final RegistrarContact.Type type) { + return FluentIterable.from(getContactsIterable()) + .filter(notNull()) + .filter( + new Predicate() { + @Override + public boolean apply(@Nullable RegistrarContact contact) { + return contact.getTypes().contains(type); + } + }) + .toSortedSet(CONTACT_EMAIL_COMPARATOR); + } + + private Iterable getContactsIterable() { + return ofy().load().type(RegistrarContact.class).ancestor(Registrar.this); } @Override diff --git a/javatests/google/registry/model/registrar/RegistrarTest.java b/javatests/google/registry/model/registrar/RegistrarTest.java index a433274af..9e2ca33b1 100644 --- a/javatests/google/registry/model/registrar/RegistrarTest.java +++ b/javatests/google/registry/model/registrar/RegistrarTest.java @@ -29,6 +29,7 @@ import static google.registry.testing.DatastoreHelper.persistSimpleResources; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSortedSet; import google.registry.model.EntityTestCase; import google.registry.model.common.EntityGroupRoot; import google.registry.model.registrar.Registrar.State; @@ -46,7 +47,8 @@ public class RegistrarTest extends EntityTestCase { @Rule public ExceptionRule thrown = new ExceptionRule(); - Registrar registrar; + private Registrar registrar; + private RegistrarContact abuseAdminContact; @Before public void setUp() throws Exception { @@ -98,7 +100,7 @@ public class RegistrarTest extends EntityTestCase { .setPhonePasscode("01234") .build()); persistResource(registrar); - persistSimpleResources(ImmutableList.of( + abuseAdminContact = new RegistrarContact.Builder() .setParent(registrar) .setName("John Abused") @@ -107,20 +109,20 @@ public class RegistrarTest extends EntityTestCase { .setVisibleInWhoisAsTech(false) .setPhoneNumber("+1.2125551213") .setFaxNumber("+1.2125551213") - .setTypes(ImmutableSet.of( - RegistrarContact.Type.ABUSE, - RegistrarContact.Type.ADMIN)) - .build(), - new RegistrarContact.Builder() - .setParent(registrar) - .setName("John Doe") - .setEmailAddress("johndoe@example.com") - .setPhoneNumber("+1.2125551213") - .setFaxNumber("+1.2125551213") - .setTypes(ImmutableSet.of( - RegistrarContact.Type.LEGAL, - RegistrarContact.Type.MARKETING)) - .build())); + .setTypes(ImmutableSet.of(RegistrarContact.Type.ABUSE, RegistrarContact.Type.ADMIN)) + .build(); + persistSimpleResources( + ImmutableList.of( + abuseAdminContact, + new RegistrarContact.Builder() + .setParent(registrar) + .setName("John Doe") + .setEmailAddress("johndoe@example.com") + .setPhoneNumber("+1.2125551213") + .setFaxNumber("+1.2125551213") + .setTypes( + ImmutableSet.of(RegistrarContact.Type.LEGAL, RegistrarContact.Type.MARKETING)) + .build())); } @Test @@ -274,6 +276,40 @@ public class RegistrarTest extends EntityTestCase { } } + @Test + public void testSuccess_getContactsByType() throws Exception { + RegistrarContact newTechContact = + persistSimpleResource( + new RegistrarContact.Builder() + .setParent(registrar) + .setName("Jake Tech") + .setEmailAddress("jaketech@example.com") + .setVisibleInWhoisAsAdmin(true) + .setVisibleInWhoisAsTech(true) + .setPhoneNumber("+1.2125551213") + .setFaxNumber("+1.2125551213") + .setTypes(ImmutableSet.of(RegistrarContact.Type.TECH)) + .build()); + RegistrarContact newTechAbuseContact = + persistSimpleResource( + new RegistrarContact.Builder() + .setParent(registrar) + .setName("Jim Tech-Abuse") + .setEmailAddress("jimtechAbuse@example.com") + .setVisibleInWhoisAsAdmin(true) + .setVisibleInWhoisAsTech(true) + .setPhoneNumber("+1.2125551213") + .setFaxNumber("+1.2125551213") + .setTypes(ImmutableSet.of(RegistrarContact.Type.TECH, RegistrarContact.Type.ABUSE)) + .build()); + ImmutableSortedSet techContacts = + registrar.getContactsOfType(RegistrarContact.Type.TECH); + assertThat(techContacts).containsExactly(newTechContact, newTechAbuseContact).inOrder(); + ImmutableSortedSet abuseContacts = + registrar.getContactsOfType(RegistrarContact.Type.ABUSE); + assertThat(abuseContacts).containsExactly(newTechAbuseContact, abuseAdminContact).inOrder(); + } + @Test public void testFailure_missingRegistrarType() throws Exception { thrown.expect(NullPointerException.class);