diff --git a/core/src/main/java/google/registry/model/billing/BillingEvent.java b/core/src/main/java/google/registry/model/billing/BillingEvent.java index 951b6b9e2..d96ae2657 100644 --- a/core/src/main/java/google/registry/model/billing/BillingEvent.java +++ b/core/src/main/java/google/registry/model/billing/BillingEvent.java @@ -347,7 +347,7 @@ public abstract class BillingEvent extends ImmutableObject @Override public VKey createVKey() { - return VKey.create(getClass(), getId(), Key.create(this)); + return VKey.create(OneTime.class, getId(), Key.create(this)); } public static VKey createVKey(Key key) { @@ -489,7 +489,7 @@ public abstract class BillingEvent extends ImmutableObject @Override public VKey createVKey() { - return VKey.create(getClass(), getId(), Key.create(this)); + return VKey.create(Recurring.class, getId(), Key.create(this)); } public static VKey createVKey(Key key) { @@ -613,7 +613,7 @@ public abstract class BillingEvent extends ImmutableObject @Override public VKey createVKey() { - return VKey.create(getClass(), getId(), Key.create(this)); + return VKey.create(Cancellation.class, getId(), Key.create(this)); } public static VKey createVKey(Key key) { @@ -697,7 +697,7 @@ public abstract class BillingEvent extends ImmutableObject @Override public VKey createVKey() { - return VKey.create(getClass(), getId(), Key.create(this)); + return VKey.create(Modification.class, getId(), Key.create(this)); } public static VKey createVKey(Key key) { diff --git a/core/src/main/java/google/registry/model/contact/ContactBase.java b/core/src/main/java/google/registry/model/contact/ContactBase.java index 0d4dd6f90..99a698ab7 100644 --- a/core/src/main/java/google/registry/model/contact/ContactBase.java +++ b/core/src/main/java/google/registry/model/contact/ContactBase.java @@ -19,7 +19,6 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static google.registry.model.EppResourceUtils.projectResourceOntoBuilderAtTime; import com.google.common.collect.ImmutableList; -import com.googlecode.objectify.Key; import com.googlecode.objectify.annotation.IgnoreSave; import com.googlecode.objectify.annotation.Index; import com.googlecode.objectify.condition.IfNull; @@ -185,8 +184,9 @@ public class ContactBase extends EppResource implements ResourceWithTransferData @Override public VKey createVKey() { - // TODO(mmuller): create symmetric keys if we can ever reload both sides. - return VKey.create(ContactBase.class, getRepoId(), Key.create(this)); + throw new UnsupportedOperationException( + "ContactBase is not an actual persisted entity you can create a key to;" + + " use ContactResource instead"); } public String getContactId() { diff --git a/core/src/main/java/google/registry/model/contact/ContactHistory.java b/core/src/main/java/google/registry/model/contact/ContactHistory.java index 76c3abe97..b6dcab38a 100644 --- a/core/src/main/java/google/registry/model/contact/ContactHistory.java +++ b/core/src/main/java/google/registry/model/contact/ContactHistory.java @@ -81,7 +81,8 @@ public class ContactHistory extends HistoryEntry { @Override public Builder setParent(Key parent) { super.setParent(parent); - getInstance().contactRepoId = VKey.create(ContactResource.class, parent.getName(), parent); + getInstance().contactRepoId = + VKey.create(ContactResource.class, parent.getName(), (Key) parent); return this; } } diff --git a/core/src/main/java/google/registry/model/domain/DomainBase.java b/core/src/main/java/google/registry/model/domain/DomainBase.java index 3a5580d73..1e69c0a01 100644 --- a/core/src/main/java/google/registry/model/domain/DomainBase.java +++ b/core/src/main/java/google/registry/model/domain/DomainBase.java @@ -100,7 +100,7 @@ public class DomainBase extends DomainContent return cloneDomainProjectedAtTime(this, now); } - public static VKey createVKey(Key key) { + public static VKey createVKey(Key key) { return VKey.create(DomainBase.class, key.getName(), key); } diff --git a/core/src/main/java/google/registry/model/domain/DomainContent.java b/core/src/main/java/google/registry/model/domain/DomainContent.java index be7fc5e33..ac8ba30a5 100644 --- a/core/src/main/java/google/registry/model/domain/DomainContent.java +++ b/core/src/main/java/google/registry/model/domain/DomainContent.java @@ -40,7 +40,6 @@ import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Ordering; import com.google.common.collect.Sets; import com.google.common.collect.Streams; -import com.googlecode.objectify.Key; import com.googlecode.objectify.annotation.Ignore; import com.googlecode.objectify.annotation.IgnoreSave; import com.googlecode.objectify.annotation.Index; @@ -419,7 +418,7 @@ public class DomainContent extends EppResource * parallels the logic in {@code DomainTransferApproveFlow} which handles explicit client * approvals. */ - protected static T cloneDomainProjectedAtTime(T domain, DateTime now) { + static T cloneDomainProjectedAtTime(T domain, DateTime now) { DomainTransferData transferData = domain.getTransferData(); DateTime transferExpirationTime = transferData.getPendingTransferExpirationTime(); @@ -608,7 +607,7 @@ public class DomainContent extends EppResource *

The registrant field is only set if {@code includeRegistrant} is true, as this field needs * to be set in some circumstances but not in others. */ - protected void setContactFields(Set contacts, boolean includeRegistrant) { + void setContactFields(Set contacts, boolean includeRegistrant) { // Set the individual contact fields. for (DesignatedContact contact : contacts) { switch (contact.getType()) { @@ -634,15 +633,13 @@ public class DomainContent extends EppResource @Override public VKey createVKey() { - return VKey.create(DomainBase.class, getRepoId(), Key.create(this)); - } - - public static VKey createVKey(Key key) { - return VKey.create(DomainBase.class, key.getName(), key); + throw new UnsupportedOperationException( + "DomainContent is not an actual persisted entity you can create a key to;" + + " use DomainBase instead"); } /** Predicate to determine if a given {@link DesignatedContact} is the registrant. */ - protected static final Predicate IS_REGISTRANT = + static final Predicate IS_REGISTRANT = (DesignatedContact contact) -> DesignatedContact.Type.REGISTRANT.equals(contact.type); /** An override of {@link EppResource#asBuilder} with tighter typing. */ diff --git a/core/src/main/java/google/registry/model/domain/DomainHistory.java b/core/src/main/java/google/registry/model/domain/DomainHistory.java index 8858a3441..4718d820e 100644 --- a/core/src/main/java/google/registry/model/domain/DomainHistory.java +++ b/core/src/main/java/google/registry/model/domain/DomainHistory.java @@ -104,7 +104,8 @@ public class DomainHistory extends HistoryEntry { @Override public Builder setParent(Key parent) { super.setParent(parent); - getInstance().domainRepoId = VKey.create(DomainBase.class, parent.getName(), parent); + getInstance().domainRepoId = + VKey.create(DomainBase.class, parent.getName(), (Key) parent); return this; } } diff --git a/core/src/main/java/google/registry/model/domain/token/AllocationToken.java b/core/src/main/java/google/registry/model/domain/token/AllocationToken.java index 87c26637d..6cdcde941 100644 --- a/core/src/main/java/google/registry/model/domain/token/AllocationToken.java +++ b/core/src/main/java/google/registry/model/domain/token/AllocationToken.java @@ -208,7 +208,7 @@ public class AllocationToken extends BackupGroupRoot implements Buildable { } public VKey createVKey() { - return VKey.create(getClass(), getToken(), Key.create(this)); + return VKey.create(AllocationToken.class, getToken(), Key.create(this)); } @Override diff --git a/core/src/main/java/google/registry/model/host/HostBase.java b/core/src/main/java/google/registry/model/host/HostBase.java index a0cb2459b..bfb020126 100644 --- a/core/src/main/java/google/registry/model/host/HostBase.java +++ b/core/src/main/java/google/registry/model/host/HostBase.java @@ -22,7 +22,6 @@ import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DomainNameUtils.canonicalizeDomainName; import com.google.common.collect.ImmutableSet; -import com.googlecode.objectify.Key; import com.googlecode.objectify.annotation.IgnoreSave; import com.googlecode.objectify.annotation.Index; import com.googlecode.objectify.condition.IfNull; @@ -126,7 +125,9 @@ public class HostBase extends EppResource { @Override public VKey createVKey() { - return VKey.create(HostBase.class, getRepoId(), Key.create(this)); + throw new UnsupportedOperationException( + "HostBase is not an actual persisted entity you can create a key to;" + + " use HostResource instead"); } @Deprecated diff --git a/core/src/main/java/google/registry/model/host/HostHistory.java b/core/src/main/java/google/registry/model/host/HostHistory.java index e1587246d..2a9b7a4dc 100644 --- a/core/src/main/java/google/registry/model/host/HostHistory.java +++ b/core/src/main/java/google/registry/model/host/HostHistory.java @@ -83,7 +83,8 @@ public class HostHistory extends HistoryEntry { @Override public Builder setParent(Key parent) { super.setParent(parent); - getInstance().hostRepoId = VKey.create(HostResource.class, parent.getName(), parent); + getInstance().hostRepoId = + VKey.create(HostResource.class, parent.getName(), (Key) parent); return this; } } diff --git a/core/src/main/java/google/registry/model/poll/PollMessage.java b/core/src/main/java/google/registry/model/poll/PollMessage.java index c1fcbf805..b53769f70 100644 --- a/core/src/main/java/google/registry/model/poll/PollMessage.java +++ b/core/src/main/java/google/registry/model/poll/PollMessage.java @@ -301,7 +301,7 @@ public abstract class PollMessage extends ImmutableObject @Override public VKey createVKey() { - return VKey.create(this.getClass(), getId(), Key.create(this)); + return VKey.create(OneTime.class, getId(), Key.create(this)); } @Override @@ -402,7 +402,7 @@ public abstract class PollMessage extends ImmutableObject @Override public VKey createVKey() { - return VKey.create(this.getClass(), getId(), Key.create(this)); + return VKey.create(Autorenew.class, getId(), Key.create(this)); } @Override diff --git a/core/src/main/java/google/registry/model/translators/VKeyTranslatorFactory.java b/core/src/main/java/google/registry/model/translators/VKeyTranslatorFactory.java index 4213b239a..98bf13ee5 100644 --- a/core/src/main/java/google/registry/model/translators/VKeyTranslatorFactory.java +++ b/core/src/main/java/google/registry/model/translators/VKeyTranslatorFactory.java @@ -25,6 +25,7 @@ import com.googlecode.objectify.annotation.EntitySubclass; import google.registry.persistence.VKey; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import javax.annotation.Nullable; /** * Translator factory for VKey. @@ -38,7 +39,7 @@ public class VKeyTranslatorFactory extends AbstractSimpleTranslatorFactory CLASS_REGISTRY = + private static final ImmutableMap> CLASS_REGISTRY = ALL_CLASSES.stream() .filter(clazz -> !clazz.isAnnotationPresent(EntitySubclass.class)) .collect(toImmutableMap(com.googlecode.objectify.Key::getKind, identity())); @@ -49,18 +50,23 @@ public class VKeyTranslatorFactory extends AbstractSimpleTranslatorFactory createVKey(Key datastoreKey) { + @Nullable + public static VKey createVKey(@Nullable Key datastoreKey) { + if (datastoreKey == null) { + return null; + } return createVKey(com.googlecode.objectify.Key.create(datastoreKey)); } /** Create a VKey from an objectify Key. */ - public static VKey createVKey(com.googlecode.objectify.Key key) { + @Nullable + public static VKey createVKey(@Nullable com.googlecode.objectify.Key key) { if (key == null) { return null; } // Try to create the VKey from its reference type. - Class clazz = CLASS_REGISTRY.get(key.getKind()); + Class clazz = (Class) CLASS_REGISTRY.get(key.getKind()); checkArgument(clazz != null, "Unknown Key type: %s", key.getKind()); try { Method createVKeyMethod = @@ -93,13 +99,16 @@ public class VKeyTranslatorFactory extends AbstractSimpleTranslatorFactory createTranslator() { return new SimpleTranslator() { + + @Nullable @Override - public VKey loadValue(Key datastoreValue) { + public VKey loadValue(@Nullable Key datastoreValue) { return createVKey(datastoreValue); } + @Nullable @Override - public Key saveValue(VKey key) { + public Key saveValue(@Nullable VKey key) { return key == null ? null : key.getOfyKey().getRaw(); } }; diff --git a/core/src/main/java/google/registry/persistence/VKey.java b/core/src/main/java/google/registry/persistence/VKey.java index a390e7c30..a06ced5ea 100644 --- a/core/src/main/java/google/registry/persistence/VKey.java +++ b/core/src/main/java/google/registry/persistence/VKey.java @@ -22,6 +22,7 @@ import google.registry.model.ImmutableObject; import google.registry.model.translators.VKeyTranslatorFactory; import java.io.Serializable; import java.util.Optional; +import javax.annotation.Nullable; /** * VKey is an abstraction that encapsulates the key concept. @@ -52,49 +53,31 @@ public class VKey extends ImmutableObject implements Serializable { * *

Deprecated. Create symmetric keys with create() instead. */ - public static VKey createSql(Class kind, Object sqlKey) { + public static VKey createSql(Class kind, Object sqlKey) { checkArgumentNotNull(kind, "kind must not be null"); checkArgumentNotNull(sqlKey, "sqlKey must not be null"); - return new VKey(kind, null, sqlKey); + return new VKey(kind, null, sqlKey); } /** Creates a {@link VKey} which only contains the ofy primary key. */ - public static VKey createOfy( - Class kind, com.googlecode.objectify.Key ofyKey) { + public static VKey createOfy(Class kind, com.googlecode.objectify.Key ofyKey) { checkArgumentNotNull(kind, "kind must not be null"); checkArgumentNotNull(ofyKey, "ofyKey must not be null"); - return new VKey(kind, ofyKey, null); - } - - /** - * Creates a {@link VKey} which only contains the ofy primary key by specifying the id of the - * {@link Key}. - */ - public static VKey createOfy(Class kind, long id) { - return createOfy(kind, Key.create(kind, id)); - } - - /** - * Creates a {@link VKey} which only contains the ofy primary key by specifying the name of the - * {@link Key}. - */ - public static VKey createOfy(Class kind, String name) { - checkArgumentNotNull(kind, "name must not be null"); - return createOfy(kind, Key.create(kind, name)); + return new VKey(kind, ofyKey, null); } /** Creates a {@link VKey} which only contains both sql and ofy primary key. */ public static VKey create( - Class kind, Object sqlKey, com.googlecode.objectify.Key ofyKey) { + Class kind, Object sqlKey, com.googlecode.objectify.Key ofyKey) { checkArgumentNotNull(kind, "kind must not be null"); checkArgumentNotNull(sqlKey, "sqlKey must not be null"); checkArgumentNotNull(ofyKey, "ofyKey must not be null"); - return new VKey(kind, ofyKey, sqlKey); + return new VKey(kind, ofyKey, sqlKey); } /** Creates a symmetric {@link VKey} in which both sql and ofy keys are {@code id}. */ - public static VKey create(Class kind, long id) { - return new VKey(kind, Key.create(kind, id), id); + public static VKey create(Class kind, long id) { + return new VKey(kind, Key.create(kind, id), id); } /** Returns the type of the entity. */ @@ -125,6 +108,7 @@ public class VKey extends ImmutableObject implements Serializable { } /** Convenience method to construct a VKey from an objectify Key. */ + @Nullable public static VKey from(Key key) { return VKeyTranslatorFactory.createVKey(key); } diff --git a/core/src/main/java/google/registry/rdap/RdapDomainSearchAction.java b/core/src/main/java/google/registry/rdap/RdapDomainSearchAction.java index a6231a921..90f23e181 100644 --- a/core/src/main/java/google/registry/rdap/RdapDomainSearchAction.java +++ b/core/src/main/java/google/registry/rdap/RdapDomainSearchAction.java @@ -295,7 +295,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { query = query.filter("currentSponsorClientId", desiredRegistrar.get()); } return StreamSupport.stream(query.keys().spliterator(), false) - .map(key -> VKey.from(key)) + .map(VKey::from) .collect(toImmutableSet()); } @@ -406,7 +406,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { } return searchByNameserverRefs( StreamSupport.stream(query.keys().spliterator(), false) - .map(key -> VKey.from(key)) + .map(VKey::from) .collect(toImmutableSet())); } diff --git a/core/src/test/java/google/registry/schema/registrar/RegistrarDaoTest.java b/core/src/test/java/google/registry/schema/registrar/RegistrarDaoTest.java index 608e0cdb1..a04b84728 100644 --- a/core/src/test/java/google/registry/schema/registrar/RegistrarDaoTest.java +++ b/core/src/test/java/google/registry/schema/registrar/RegistrarDaoTest.java @@ -51,7 +51,7 @@ public class RegistrarDaoTest { private Registrar testRegistrar; @BeforeEach - public void setUp() { + void setUp() { testRegistrar = new Registrar.Builder() .setType(Registrar.Type.TEST) @@ -69,14 +69,14 @@ public class RegistrarDaoTest { } @Test - public void saveNew_worksSuccessfully() { + void saveNew_worksSuccessfully() { assertThat(jpaTm().transact(() -> jpaTm().checkExists(testRegistrar))).isFalse(); jpaTm().transact(() -> jpaTm().saveNew(testRegistrar)); assertThat(jpaTm().transact(() -> jpaTm().checkExists(testRegistrar))).isTrue(); } @Test - public void update_worksSuccessfully() { + void update_worksSuccessfully() { jpaTm().transact(() -> jpaTm().saveNew(testRegistrar)); Registrar persisted = jpaTm().transact(() -> jpaTm().load(registrarKey)); assertThat(persisted.getRegistrarName()).isEqualTo("registrarName"); @@ -91,7 +91,7 @@ public class RegistrarDaoTest { } @Test - public void update_throwsExceptionWhenEntityDoesNotExist() { + void update_throwsExceptionWhenEntityDoesNotExist() { assertThat(jpaTm().transact(() -> jpaTm().checkExists(testRegistrar))).isFalse(); assertThrows( IllegalArgumentException.class, @@ -99,7 +99,7 @@ public class RegistrarDaoTest { } @Test - public void load_worksSuccessfully() { + void load_worksSuccessfully() { assertThat(jpaTm().transact(() -> jpaTm().checkExists(testRegistrar))).isFalse(); jpaTm().transact(() -> jpaTm().saveNew(testRegistrar)); Registrar persisted = jpaTm().transact(() -> jpaTm().load(registrarKey));