Make Registrar load methods return Optionals instead of Nullables

This makes the code more understandable from callsites, and also forces
users of this function to deal with the situation where the registrar
with a given client ID might not be present (it was previously silently
NPEing from some of the callsites).

This also adds a test helper method loadRegistrar(clientId) that retains
the old functionality for terseness in tests. It also fixes some instances
of using the load method with the wrong cachedness -- some uses in high-
traffic situations (WHOIS) that should have caching, but also low-traffic
reporting that don't benefit from caching so might as well always be
current.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162990468
This commit is contained in:
mcilwain 2017-07-24 14:43:20 -07:00 committed by Ben McIlwain
parent 84fdeebc2f
commit d536cef20f
81 changed files with 707 additions and 602 deletions

View file

@ -15,13 +15,13 @@
package google.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.persistResource;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import google.registry.model.EntityTestCase;
import google.registry.model.registrar.Registrar;
import google.registry.testing.ExceptionRule;
import org.joda.money.CurrencyMismatchException;
import org.joda.money.Money;
@ -44,7 +44,7 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
persistResource(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setTransactionId("goblin-market")
.setDescription("USD Invoice for December 1984")
@ -59,14 +59,14 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
RegistrarBillingEntry entry =
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setTransactionId("goblin-market")
.setDescription("USD Invoice for December 1984")
.setAmount(Money.parse("USD 10.00"))
.build();
assertThat(entry.getId()).isEqualTo(1L);
assertThat(entry.getParent()).isEqualTo(Key.create(Registrar.loadByClientId("NewRegistrar")));
assertThat(entry.getParent()).isEqualTo(Key.create(loadRegistrar("NewRegistrar")));
assertThat(entry.getCreated()).isEqualTo(DateTime.parse("1984-12-18TZ"));
assertThat(entry.getTransactionId()).isEqualTo("goblin-market");
assertThat(entry.getDescription()).isEqualTo("USD Invoice for December 1984");
@ -79,7 +79,7 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
assertThat(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setTransactionId("goblin-market")
.setDescription("USD Invoice for December 1984")
@ -105,12 +105,12 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
.setPrevious(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.parse("USD 10.00"))
.build())
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-17TZ"))
.setTransactionId("goblin")
.setDescription("USD Invoice for August")
@ -125,12 +125,12 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
.setPrevious(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.parse("USD 10.00"))
.build())
.setParent(Registrar.loadByClientId("TheRegistrar"))
.setParent(loadRegistrar("TheRegistrar"))
.setCreated(DateTime.parse("1984-12-17TZ"))
.setTransactionId("goblin")
.setDescription("USD Invoice for August")
@ -145,12 +145,12 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
.setPrevious(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.parse("USD 10.00"))
.build())
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-17TZ"))
.setTransactionId("goblin")
.setDescription("JPY Invoice for August")
@ -163,7 +163,7 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
thrown.expect(IllegalArgumentException.class, "Amount can't be zero");
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.zero(USD))
@ -175,7 +175,7 @@ public final class RegistrarBillingEntryTest extends EntityTestCase {
assertThat(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setParent(loadRegistrar("NewRegistrar"))
.setTransactionId("")
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December 1984")

View file

@ -16,6 +16,7 @@ package google.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTlds;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DatastoreHelper.persistSimpleResources;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -57,7 +58,7 @@ public final class RegistrarBillingUtilsTest {
@Before
public void before() throws Exception {
inject.setStaticField(Ofy.class, "clock", clock);
registrar = Registrar.loadByClientId("NewRegistrar");
registrar = loadRegistrar("NewRegistrar");
createTlds("xn--q9jyb4c", "com", "net");
persistResource(
Registry.get("xn--q9jyb4c").asBuilder()

View file

@ -17,6 +17,7 @@ package google.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -53,7 +54,7 @@ public class RegistrarCreditBalanceTest extends EntityTestCase {
@Before
public void setUp() throws Exception {
createTld("tld");
theRegistrar = Registrar.loadByClientId("TheRegistrar");
theRegistrar = loadRegistrar("TheRegistrar");
unpersistedCredit = makeCredit(theRegistrar, clock.nowUtc());
credit = persistResource(makeCredit(theRegistrar, clock.nowUtc()));
balance = persistResource(

View file

@ -401,7 +401,7 @@ public class RegistrarTest extends EntityTestCase {
ofy().transact(new VoidWork() {
@Override
public void vrun() {
assertThat(Registrar.loadByClientIdCached("registrar")).isNotNull();
assertThat(Registrar.loadByClientIdCached("registrar")).isPresent();
// Load something as a control to make sure we are seeing loaded keys in the session cache.
ofy().load().entity(abuseAdminContact).now();
assertThat(ofy().getSessionKeys()).contains(Key.create(abuseAdminContact));
@ -409,7 +409,31 @@ public class RegistrarTest extends EntityTestCase {
}});
ofy().clearSessionCache();
// Conversely, loads outside of a transaction should end up in the session cache.
assertThat(Registrar.loadByClientIdCached("registrar")).isNotNull();
assertThat(Registrar.loadByClientIdCached("registrar")).isPresent();
assertThat(ofy().getSessionKeys()).contains(Key.create(registrar));
}
@Test
public void testFailure_loadByClientId_clientIdIsNull() throws Exception {
thrown.expect(IllegalArgumentException.class, "clientId must be specified");
Registrar.loadByClientId(null);
}
@Test
public void testFailure_loadByClientId_clientIdIsEmpty() throws Exception {
thrown.expect(IllegalArgumentException.class, "clientId must be specified");
Registrar.loadByClientId("");
}
@Test
public void testFailure_loadByClientIdCached_clientIdIsNull() throws Exception {
thrown.expect(IllegalArgumentException.class, "clientId must be specified");
Registrar.loadByClientIdCached(null);
}
@Test
public void testFailure_loadByClientIdCached_clientIdIsEmpty() throws Exception {
thrown.expect(IllegalArgumentException.class, "clientId must be specified");
Registrar.loadByClientIdCached("");
}
}