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
This commit is contained in:
mcilwain 2018-01-09 17:45:37 -08:00 committed by Ben McIlwain
parent 5f62947691
commit ccbe958063
5 changed files with 56 additions and 3 deletions

View file

@ -15,6 +15,7 @@
package google.registry.model; package google.registry.model;
import google.registry.model.translators.CreateAutoTimestampTranslatorFactory; import google.registry.model.translators.CreateAutoTimestampTranslatorFactory;
import javax.annotation.Nullable;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** /**
@ -27,11 +28,12 @@ public class CreateAutoTimestamp extends ImmutableObject {
DateTime timestamp; DateTime timestamp;
/** Returns the timestamp. */ /** Returns the timestamp. */
@Nullable
public DateTime getTimestamp() { public DateTime getTimestamp() {
return timestamp; return timestamp;
} }
public static CreateAutoTimestamp create(DateTime timestamp) { public static CreateAutoTimestamp create(@Nullable DateTime timestamp) {
CreateAutoTimestamp instance = new CreateAutoTimestamp(); CreateAutoTimestamp instance = new CreateAutoTimestamp();
instance.timestamp = timestamp; instance.timestamp = timestamp;
return instance; return instance;

View file

@ -202,7 +202,8 @@ public abstract class EppResource extends BackupGroupRoot implements Buildable {
* normal EPP flows. * normal EPP flows.
*/ */
public B setCreationTime(DateTime creationTime) { public B setCreationTime(DateTime creationTime) {
checkState(getInstance().creationTime.timestamp == null, checkState(
getInstance().creationTime.getTimestamp() == null,
"creationTime can only be set once for EppResource."); "creationTime can only be set once for EppResource.");
getInstance().creationTime = CreateAutoTimestamp.create(creationTime); getInstance().creationTime = CreateAutoTimestamp.create(creationTime);
return thisCastToDerived(); return thisCastToDerived();

View file

@ -15,6 +15,7 @@
package google.registry.model.domain; package google.registry.model.domain;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
@ -22,8 +23,11 @@ import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id; import com.googlecode.objectify.annotation.Id;
import google.registry.model.BackupGroupRoot; import google.registry.model.BackupGroupRoot;
import google.registry.model.Buildable; import google.registry.model.Buildable;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.annotations.ReportedOn; import google.registry.model.annotations.ReportedOn;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import java.util.Optional;
import org.joda.time.DateTime;
/** An entity representing an allocation token. */ /** An entity representing an allocation token. */
@ReportedOn @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. */ /** The key of the history entry for which the token was used. Null if not yet used. */
Key<HistoryEntry> redemptionHistoryEntry; Key<HistoryEntry> redemptionHistoryEntry;
/** When this token was created. */
CreateAutoTimestamp creationTime = CreateAutoTimestamp.create(null);
public String getToken() { public String getToken() {
return token; return token;
} }
@ -48,6 +55,10 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
return redemptionHistoryEntry != null; return redemptionHistoryEntry != null;
} }
public Optional<DateTime> getCreationTime() {
return Optional.ofNullable(creationTime.getTimestamp());
}
@Override @Override
public Builder asBuilder() { public Builder asBuilder() {
return new Builder(clone(this)); return new Builder(clone(this));
@ -73,5 +84,12 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
checkArgumentNotNull(redemptionHistoryEntry, "redemptionHistoryEntry must not be null"); checkArgumentNotNull(redemptionHistoryEntry, "redemptionHistoryEntry must not be null");
return this; 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;
}
} }
} }

View file

@ -15,12 +15,15 @@
package google.registry.model.domain; package google.registry.model.domain;
import static com.google.common.truth.Truth.assertThat; 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.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.model.EntityTestCase; import google.registry.model.EntityTestCase;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import org.joda.time.DateTime;
import org.junit.Test; import org.junit.Test;
/** Unit tests for {@link AllocationToken}. */ /** Unit tests for {@link AllocationToken}. */
@ -33,12 +36,40 @@ public class AllocationTokenTest extends EntityTestCase {
new AllocationToken.Builder() new AllocationToken.Builder()
.setToken("abc123") .setToken("abc123")
.setRedemptionHistoryEntry(Key.create(HistoryEntry.class, 1L)) .setRedemptionHistoryEntry(Key.create(HistoryEntry.class, 1L))
.setCreationTime(DateTime.parse("2010-11-12T05:00:00Z"))
.build()); .build());
assertThat(ofy().load().entity(token).now()).isEqualTo(token); assertThat(ofy().load().entity(token).now()).isEqualTo(token);
} }
@Test @Test
public void testIndexing() throws Exception { 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");
} }
} }

View file

@ -178,6 +178,7 @@ enum google.registry.model.contact.PostalInfo$Type {
class google.registry.model.domain.AllocationToken { class google.registry.model.domain.AllocationToken {
@Id java.lang.String token; @Id java.lang.String token;
com.googlecode.objectify.Key<google.registry.model.reporting.HistoryEntry> redemptionHistoryEntry; com.googlecode.objectify.Key<google.registry.model.reporting.HistoryEntry> redemptionHistoryEntry;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp; google.registry.model.UpdateAutoTimestamp updateTimestamp;
} }
class google.registry.model.domain.DesignatedContact { class google.registry.model.domain.DesignatedContact {