From ccbe958063bf0f91df6fdf65e529f32b68e86abb Mon Sep 17 00:00:00 2001 From: mcilwain Date: Tue, 9 Jan 2018 17:45:37 -0800 Subject: [PATCH] Add create timestamp as field on AllocationToken entity This definitely seems like a useful thing to have around. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=181401799 --- .../registry/model/CreateAutoTimestamp.java | 4 ++- java/google/registry/model/EppResource.java | 3 +- .../model/domain/AllocationToken.java | 18 ++++++++++ .../model/domain/AllocationTokenTest.java | 33 ++++++++++++++++++- javatests/google/registry/model/schema.txt | 1 + 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/java/google/registry/model/CreateAutoTimestamp.java b/java/google/registry/model/CreateAutoTimestamp.java index 1df2f305f..2a3200514 100644 --- a/java/google/registry/model/CreateAutoTimestamp.java +++ b/java/google/registry/model/CreateAutoTimestamp.java @@ -15,6 +15,7 @@ package google.registry.model; import google.registry.model.translators.CreateAutoTimestampTranslatorFactory; +import javax.annotation.Nullable; import org.joda.time.DateTime; /** @@ -27,11 +28,12 @@ public class CreateAutoTimestamp extends ImmutableObject { DateTime timestamp; /** Returns the timestamp. */ + @Nullable public DateTime getTimestamp() { return timestamp; } - public static CreateAutoTimestamp create(DateTime timestamp) { + public static CreateAutoTimestamp create(@Nullable DateTime timestamp) { CreateAutoTimestamp instance = new CreateAutoTimestamp(); instance.timestamp = timestamp; return instance; diff --git a/java/google/registry/model/EppResource.java b/java/google/registry/model/EppResource.java index 285adced5..59e6c3e4b 100644 --- a/java/google/registry/model/EppResource.java +++ b/java/google/registry/model/EppResource.java @@ -202,7 +202,8 @@ public abstract class EppResource extends BackupGroupRoot implements Buildable { * normal EPP flows. */ public B setCreationTime(DateTime creationTime) { - checkState(getInstance().creationTime.timestamp == null, + checkState( + getInstance().creationTime.getTimestamp() == null, "creationTime can only be set once for EppResource."); getInstance().creationTime = CreateAutoTimestamp.create(creationTime); return thisCastToDerived(); diff --git a/java/google/registry/model/domain/AllocationToken.java b/java/google/registry/model/domain/AllocationToken.java index fe40bfd4e..9aca4e3b0 100644 --- a/java/google/registry/model/domain/AllocationToken.java +++ b/java/google/registry/model/domain/AllocationToken.java @@ -15,6 +15,7 @@ package google.registry.model.domain; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import com.googlecode.objectify.Key; @@ -22,8 +23,11 @@ import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Id; import google.registry.model.BackupGroupRoot; import google.registry.model.Buildable; +import google.registry.model.CreateAutoTimestamp; import google.registry.model.annotations.ReportedOn; import google.registry.model.reporting.HistoryEntry; +import java.util.Optional; +import org.joda.time.DateTime; /** An entity representing an allocation token. */ @ReportedOn @@ -36,6 +40,9 @@ public class AllocationToken extends BackupGroupRoot implements Buildable { /** The key of the history entry for which the token was used. Null if not yet used. */ Key redemptionHistoryEntry; + /** When this token was created. */ + CreateAutoTimestamp creationTime = CreateAutoTimestamp.create(null); + public String getToken() { return token; } @@ -48,6 +55,10 @@ public class AllocationToken extends BackupGroupRoot implements Buildable { return redemptionHistoryEntry != null; } + public Optional getCreationTime() { + return Optional.ofNullable(creationTime.getTimestamp()); + } + @Override public Builder asBuilder() { return new Builder(clone(this)); @@ -73,5 +84,12 @@ public class AllocationToken extends BackupGroupRoot implements Buildable { checkArgumentNotNull(redemptionHistoryEntry, "redemptionHistoryEntry must not be null"); return this; } + + public Builder setCreationTime(DateTime creationTime) { + checkState( + getInstance().creationTime.getTimestamp() == null, "creationTime can only be set once"); + getInstance().creationTime = CreateAutoTimestamp.create(creationTime); + return this; + } } } diff --git a/javatests/google/registry/model/domain/AllocationTokenTest.java b/javatests/google/registry/model/domain/AllocationTokenTest.java index bb9e6592c..4bc7ae161 100644 --- a/javatests/google/registry/model/domain/AllocationTokenTest.java +++ b/javatests/google/registry/model/domain/AllocationTokenTest.java @@ -15,12 +15,15 @@ package google.registry.model.domain; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.testing.DatastoreHelper.persistResource; +import static google.registry.testing.JUnitBackports.expectThrows; import com.googlecode.objectify.Key; import google.registry.model.EntityTestCase; import google.registry.model.reporting.HistoryEntry; +import org.joda.time.DateTime; import org.junit.Test; /** Unit tests for {@link AllocationToken}. */ @@ -33,12 +36,40 @@ public class AllocationTokenTest extends EntityTestCase { new AllocationToken.Builder() .setToken("abc123") .setRedemptionHistoryEntry(Key.create(HistoryEntry.class, 1L)) + .setCreationTime(DateTime.parse("2010-11-12T05:00:00Z")) .build()); assertThat(ofy().load().entity(token).now()).isEqualTo(token); } @Test public void testIndexing() throws Exception { - verifyIndexing(new AllocationToken.Builder().setToken("abc123").build(), "token"); + verifyIndexing( + new AllocationToken.Builder() + .setToken("abc123") + .setCreationTime(DateTime.parse("2010-11-12T05:00:00Z")) + .build(), + "token"); + } + + @Test + public void testCreationTime_autoPopulates() throws Exception { + AllocationToken tokenBeforePersisting = + new AllocationToken.Builder().setToken("abc123").build(); + assertThat(tokenBeforePersisting.getCreationTime()).isEmpty(); + AllocationToken tokenAfterPersisting = persistResource(tokenBeforePersisting); + assertThat(tokenAfterPersisting.getCreationTime()).hasValue(clock.nowUtc()); + } + + @Test + public void testSetCreationTime_cantCallMoreThanOnce() throws Exception { + AllocationToken.Builder builder = + new AllocationToken.Builder() + .setToken("foobar") + .setCreationTime(DateTime.parse("2010-11-12T05:00:00Z")); + IllegalStateException thrown = + expectThrows( + IllegalStateException.class, + () -> builder.setCreationTime(DateTime.parse("2010-11-13T05:00:00Z"))); + assertThat(thrown).hasMessageThat().isEqualTo("creationTime can only be set once"); } } diff --git a/javatests/google/registry/model/schema.txt b/javatests/google/registry/model/schema.txt index 0623e0147..65785d797 100644 --- a/javatests/google/registry/model/schema.txt +++ b/javatests/google/registry/model/schema.txt @@ -178,6 +178,7 @@ enum google.registry.model.contact.PostalInfo$Type { class google.registry.model.domain.AllocationToken { @Id java.lang.String token; com.googlecode.objectify.Key redemptionHistoryEntry; + google.registry.model.CreateAutoTimestamp creationTime; google.registry.model.UpdateAutoTimestamp updateTimestamp; } class google.registry.model.domain.DesignatedContact {