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

@ -16,6 +16,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
@ -31,7 +32,7 @@ final class DeleteCreditCommand extends MutatingCommand {
names = "--registrar",
description = "Client ID of the registrar owning the credit to delete",
required = true)
private String registrarId;
private String clientId;
@Parameter(
names = "--credit_id",
@ -42,13 +43,14 @@ final class DeleteCreditCommand extends MutatingCommand {
@Override
protected void init() throws Exception {
Registrar registrar =
checkNotNull(Registrar.loadByClientId(registrarId), "Registrar %s not found", registrarId);
checkArgumentPresent(
Registrar.loadByClientId(clientId), "Registrar %s not found", clientId);
RegistrarCredit credit = ofy().load()
.type(RegistrarCredit.class)
.parent(registrar)
.id(creditId)
.now();
checkNotNull(credit, "Registrar credit for %s with ID %s not found", registrarId, creditId);
checkNotNull(credit, "Registrar credit for %s with ID %s not found", clientId, creditId);
stageEntityChange(credit, null);
for (RegistrarCreditBalance balance :