Add Ability to return registrar contacts of a certain type

This is required by ICANN Consistent Labeling & Display policy that WHOIS domain query response contains registrar abuse contact's phone number and email address. Add a helper function to load registrar contact of a certain type for a given registrar.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=153606137
This commit is contained in:
jianglai 2017-04-19 10:49:40 -07:00 committed by Ben McIlwain
parent d35be27b65
commit 8f05ca466f
2 changed files with 86 additions and 23 deletions

View file

@ -188,6 +188,17 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
immutableEnumSet( immutableEnumSet(
Type.REAL, Type.PDT, Type.OTE, Type.EXTERNAL_MONITORING, Type.MONITORING, Type.INTERNAL); 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<RegistrarContact> CONTACT_EMAIL_COMPARATOR =
new Comparator<RegistrarContact>() {
@Override
public int compare(RegistrarContact rc1, RegistrarContact rc2) {
return rc1.getEmailAddress().compareTo(rc2.getEmailAddress());
}
};
@Parent @Parent
Key<EntityGroupRoot> parent = getCrossTldKey(); Key<EntityGroupRoot> parent = getCrossTldKey();
@ -532,14 +543,30 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
* address. * address.
*/ */
public ImmutableSortedSet<RegistrarContact> getContacts() { public ImmutableSortedSet<RegistrarContact> getContacts() {
return FluentIterable return FluentIterable.from(getContactsIterable())
.from(ofy().load().type(RegistrarContact.class).ancestor(Registrar.this))
.filter(notNull()) .filter(notNull())
.toSortedSet(new Comparator<RegistrarContact>() { .toSortedSet(CONTACT_EMAIL_COMPARATOR);
@Override }
public int compare(RegistrarContact rc1, RegistrarContact rc2) {
return rc1.getEmailAddress().compareTo(rc2.getEmailAddress()); /**
}}); * Returns a list of {@link RegistrarContact} objects of a given type for this registrar sorted by
* their email address.
*/
public ImmutableSortedSet<RegistrarContact> getContactsOfType(final RegistrarContact.Type type) {
return FluentIterable.from(getContactsIterable())
.filter(notNull())
.filter(
new Predicate<RegistrarContact>() {
@Override
public boolean apply(@Nullable RegistrarContact contact) {
return contact.getTypes().contains(type);
}
})
.toSortedSet(CONTACT_EMAIL_COMPARATOR);
}
private Iterable<RegistrarContact> getContactsIterable() {
return ofy().load().type(RegistrarContact.class).ancestor(Registrar.this);
} }
@Override @Override

View file

@ -29,6 +29,7 @@ import static google.registry.testing.DatastoreHelper.persistSimpleResources;
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.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import google.registry.model.EntityTestCase; import google.registry.model.EntityTestCase;
import google.registry.model.common.EntityGroupRoot; import google.registry.model.common.EntityGroupRoot;
import google.registry.model.registrar.Registrar.State; import google.registry.model.registrar.Registrar.State;
@ -46,7 +47,8 @@ public class RegistrarTest extends EntityTestCase {
@Rule @Rule
public ExceptionRule thrown = new ExceptionRule(); public ExceptionRule thrown = new ExceptionRule();
Registrar registrar; private Registrar registrar;
private RegistrarContact abuseAdminContact;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -98,7 +100,7 @@ public class RegistrarTest extends EntityTestCase {
.setPhonePasscode("01234") .setPhonePasscode("01234")
.build()); .build());
persistResource(registrar); persistResource(registrar);
persistSimpleResources(ImmutableList.of( abuseAdminContact =
new RegistrarContact.Builder() new RegistrarContact.Builder()
.setParent(registrar) .setParent(registrar)
.setName("John Abused") .setName("John Abused")
@ -107,20 +109,20 @@ public class RegistrarTest extends EntityTestCase {
.setVisibleInWhoisAsTech(false) .setVisibleInWhoisAsTech(false)
.setPhoneNumber("+1.2125551213") .setPhoneNumber("+1.2125551213")
.setFaxNumber("+1.2125551213") .setFaxNumber("+1.2125551213")
.setTypes(ImmutableSet.of( .setTypes(ImmutableSet.of(RegistrarContact.Type.ABUSE, RegistrarContact.Type.ADMIN))
RegistrarContact.Type.ABUSE, .build();
RegistrarContact.Type.ADMIN)) persistSimpleResources(
.build(), ImmutableList.of(
new RegistrarContact.Builder() abuseAdminContact,
.setParent(registrar) new RegistrarContact.Builder()
.setName("John Doe") .setParent(registrar)
.setEmailAddress("johndoe@example.com") .setName("John Doe")
.setPhoneNumber("+1.2125551213") .setEmailAddress("johndoe@example.com")
.setFaxNumber("+1.2125551213") .setPhoneNumber("+1.2125551213")
.setTypes(ImmutableSet.of( .setFaxNumber("+1.2125551213")
RegistrarContact.Type.LEGAL, .setTypes(
RegistrarContact.Type.MARKETING)) ImmutableSet.of(RegistrarContact.Type.LEGAL, RegistrarContact.Type.MARKETING))
.build())); .build()));
} }
@Test @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<RegistrarContact> techContacts =
registrar.getContactsOfType(RegistrarContact.Type.TECH);
assertThat(techContacts).containsExactly(newTechContact, newTechAbuseContact).inOrder();
ImmutableSortedSet<RegistrarContact> abuseContacts =
registrar.getContactsOfType(RegistrarContact.Type.ABUSE);
assertThat(abuseContacts).containsExactly(newTechAbuseContact, abuseAdminContact).inOrder();
}
@Test @Test
public void testFailure_missingRegistrarType() throws Exception { public void testFailure_missingRegistrarType() throws Exception {
thrown.expect(NullPointerException.class); thrown.expect(NullPointerException.class);