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(
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
Key<EntityGroupRoot> parent = getCrossTldKey();
@ -532,14 +543,30 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
* address.
*/
public ImmutableSortedSet<RegistrarContact> getContacts() {
return FluentIterable
.from(ofy().load().type(RegistrarContact.class).ancestor(Registrar.this))
return FluentIterable.from(getContactsIterable())
.filter(notNull())
.toSortedSet(new Comparator<RegistrarContact>() {
.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<RegistrarContact> getContactsOfType(final RegistrarContact.Type type) {
return FluentIterable.from(getContactsIterable())
.filter(notNull())
.filter(
new Predicate<RegistrarContact>() {
@Override
public int compare(RegistrarContact rc1, RegistrarContact rc2) {
return rc1.getEmailAddress().compareTo(rc2.getEmailAddress());
}});
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

View file

@ -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,19 +109,19 @@ public class RegistrarTest extends EntityTestCase {
.setVisibleInWhoisAsTech(false)
.setPhoneNumber("+1.2125551213")
.setFaxNumber("+1.2125551213")
.setTypes(ImmutableSet.of(
RegistrarContact.Type.ABUSE,
RegistrarContact.Type.ADMIN))
.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))
.setTypes(
ImmutableSet.of(RegistrarContact.Type.LEGAL, RegistrarContact.Type.MARKETING))
.build()));
}
@ -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
public void testFailure_missingRegistrarType() throws Exception {
thrown.expect(NullPointerException.class);