diff --git a/java/google/registry/model/EntityClasses.java b/java/google/registry/model/EntityClasses.java index a8fd6270d..c5a08fbfb 100644 --- a/java/google/registry/model/EntityClasses.java +++ b/java/google/registry/model/EntityClasses.java @@ -23,6 +23,7 @@ import google.registry.model.common.Cursor; import google.registry.model.common.EntityGroupRoot; import google.registry.model.common.GaeUserIdConverter; import google.registry.model.contact.ContactResource; +import google.registry.model.domain.AllocationToken; import google.registry.model.domain.DomainApplication; import google.registry.model.domain.DomainBase; import google.registry.model.domain.DomainResource; @@ -62,6 +63,7 @@ public final class EntityClasses { @SuppressWarnings("unchecked") // varargs public static final ImmutableSet> ALL_CLASSES = ImmutableSet.of( + AllocationToken.class, BillingEvent.Cancellation.class, BillingEvent.Modification.class, BillingEvent.OneTime.class, diff --git a/java/google/registry/model/domain/AllocationToken.java b/java/google/registry/model/domain/AllocationToken.java new file mode 100644 index 000000000..fe40bfd4e --- /dev/null +++ b/java/google/registry/model/domain/AllocationToken.java @@ -0,0 +1,77 @@ +// Copyright 2017 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.model.domain; + +import static com.google.common.base.Preconditions.checkArgument; +import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; + +import com.googlecode.objectify.Key; +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.annotations.ReportedOn; +import google.registry.model.reporting.HistoryEntry; + +/** An entity representing an allocation token. */ +@ReportedOn +@Entity +public class AllocationToken extends BackupGroupRoot implements Buildable { + + /** The allocation token string. */ + @Id String token; + + /** The key of the history entry for which the token was used. Null if not yet used. */ + Key redemptionHistoryEntry; + + public String getToken() { + return token; + } + + public Key getRedemptionHistoryEntry() { + return redemptionHistoryEntry; + } + + public boolean isRedeemed() { + return redemptionHistoryEntry != null; + } + + @Override + public Builder asBuilder() { + return new Builder(clone(this)); + } + + /** A builder for constructing {@link AllocationToken} objects, since they are immutable. */ + public static class Builder extends Buildable.Builder { + public Builder() {} + + private Builder(AllocationToken instance) { + super(instance); + } + + public Builder setToken(String token) { + checkArgumentNotNull(token, "token must not be null"); + checkArgument(!token.isEmpty(), "token must not be blank"); + getInstance().token = token; + return this; + } + + public Builder setRedemptionHistoryEntry(Key redemptionHistoryEntry) { + getInstance().redemptionHistoryEntry = + checkArgumentNotNull(redemptionHistoryEntry, "redemptionHistoryEntry must not be null"); + return this; + } + } +} diff --git a/javatests/google/registry/export/backup_kinds.txt b/javatests/google/registry/export/backup_kinds.txt index 4f82771b1..dfb89696a 100644 --- a/javatests/google/registry/export/backup_kinds.txt +++ b/javatests/google/registry/export/backup_kinds.txt @@ -1,3 +1,4 @@ +AllocationToken Cancellation ContactResource Cursor diff --git a/javatests/google/registry/export/reporting_kinds.txt b/javatests/google/registry/export/reporting_kinds.txt index 466f1093a..2d6a7260d 100644 --- a/javatests/google/registry/export/reporting_kinds.txt +++ b/javatests/google/registry/export/reporting_kinds.txt @@ -1,3 +1,4 @@ +AllocationToken Cancellation ContactResource DomainApplicationIndex diff --git a/javatests/google/registry/model/domain/AllocationTokenTest.java b/javatests/google/registry/model/domain/AllocationTokenTest.java new file mode 100644 index 000000000..bb9e6592c --- /dev/null +++ b/javatests/google/registry/model/domain/AllocationTokenTest.java @@ -0,0 +1,44 @@ +// Copyright 2017 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.model.domain; + +import static com.google.common.truth.Truth.assertThat; +import static google.registry.model.ofy.ObjectifyService.ofy; +import static google.registry.testing.DatastoreHelper.persistResource; + +import com.googlecode.objectify.Key; +import google.registry.model.EntityTestCase; +import google.registry.model.reporting.HistoryEntry; +import org.junit.Test; + +/** Unit tests for {@link AllocationToken}. */ +public class AllocationTokenTest extends EntityTestCase { + + @Test + public void testPersistence() throws Exception { + AllocationToken token = + persistResource( + new AllocationToken.Builder() + .setToken("abc123") + .setRedemptionHistoryEntry(Key.create(HistoryEntry.class, 1L)) + .build()); + assertThat(ofy().load().entity(token).now()).isEqualTo(token); + } + + @Test + public void testIndexing() throws Exception { + verifyIndexing(new AllocationToken.Builder().setToken("abc123").build(), "token"); + } +} diff --git a/javatests/google/registry/model/schema.txt b/javatests/google/registry/model/schema.txt index 22664bf20..0623e0147 100644 --- a/javatests/google/registry/model/schema.txt +++ b/javatests/google/registry/model/schema.txt @@ -175,6 +175,11 @@ enum google.registry.model.contact.PostalInfo$Type { INTERNATIONALIZED; LOCALIZED; } +class google.registry.model.domain.AllocationToken { + @Id java.lang.String token; + com.googlecode.objectify.Key redemptionHistoryEntry; + google.registry.model.UpdateAutoTimestamp updateTimestamp; +} class google.registry.model.domain.DesignatedContact { com.googlecode.objectify.Key contact; google.registry.model.domain.DesignatedContact$Type type;