mirror of
https://github.com/google/nomulus.git
synced 2025-08-12 20:49:37 +02:00
Add defaultPromoTokens to Registry (#1850)
* Add defaultPromoTokens to Registry * Remove flyway files from this PR * Fix merge conflicts * Add back flyway file * Add more info to error messages * Change to a list * Fix javadoc * Change error message * Add note to field declaration
This commit is contained in:
parent
c13962554e
commit
fa3f2a2f21
6 changed files with 232 additions and 0 deletions
|
@ -17,6 +17,8 @@ package google.registry.model.tld;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.model.domain.token.AllocationToken.TokenType.DEFAULT_PROMO;
|
||||
import static google.registry.model.domain.token.AllocationToken.TokenType.SINGLE_USE;
|
||||
import static google.registry.model.tld.Registry.TldState.GENERAL_AVAILABILITY;
|
||||
import static google.registry.model.tld.Registry.TldState.PREDELEGATION;
|
||||
import static google.registry.model.tld.Registry.TldState.QUIET_PERIOD;
|
||||
|
@ -26,6 +28,7 @@ import static google.registry.testing.DatabaseHelper.createTld;
|
|||
import static google.registry.testing.DatabaseHelper.newRegistry;
|
||||
import static google.registry.testing.DatabaseHelper.persistPremiumList;
|
||||
import static google.registry.testing.DatabaseHelper.persistReservedList;
|
||||
import static google.registry.testing.DatabaseHelper.persistResource;
|
||||
import static google.registry.util.DateTimeUtils.END_OF_TIME;
|
||||
import static google.registry.util.DateTimeUtils.START_OF_TIME;
|
||||
import static java.math.RoundingMode.UNNECESSARY;
|
||||
|
@ -38,11 +41,13 @@ import com.google.common.collect.ImmutableSet;
|
|||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import google.registry.dns.writer.VoidDnsWriter;
|
||||
import google.registry.model.EntityTestCase;
|
||||
import google.registry.model.domain.token.AllocationToken;
|
||||
import google.registry.model.tld.Registry.RegistryNotFoundException;
|
||||
import google.registry.model.tld.Registry.TldState;
|
||||
import google.registry.model.tld.label.PremiumList;
|
||||
import google.registry.model.tld.label.PremiumListDao;
|
||||
import google.registry.model.tld.label.ReservedList;
|
||||
import google.registry.persistence.VKey;
|
||||
import google.registry.util.SerializeUtils;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
@ -628,4 +633,81 @@ public final class RegistryTest extends EntityTestCase {
|
|||
IllegalArgumentException.class,
|
||||
() -> Registry.get("tld").asBuilder().setRoidSuffix("ABC-DEF"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_setDefaultPromoTokens() {
|
||||
Registry registry = Registry.get("tld");
|
||||
assertThat(registry.getDefaultPromoTokens()).isEmpty();
|
||||
AllocationToken token1 =
|
||||
persistResource(
|
||||
new AllocationToken()
|
||||
.asBuilder()
|
||||
.setToken("abc123")
|
||||
.setTokenType(DEFAULT_PROMO)
|
||||
.setAllowedTlds(ImmutableSet.of("tld"))
|
||||
.build());
|
||||
AllocationToken token2 =
|
||||
persistResource(
|
||||
new AllocationToken()
|
||||
.asBuilder()
|
||||
.setToken("token")
|
||||
.setTokenType(DEFAULT_PROMO)
|
||||
.setAllowedTlds(ImmutableSet.of("tld"))
|
||||
.build());
|
||||
ImmutableList<VKey<AllocationToken>> tokens =
|
||||
ImmutableList.of(token1.createVKey(), token2.createVKey());
|
||||
registry = registry.asBuilder().setDefaultPromoTokens(tokens).build();
|
||||
assertThat(registry.getDefaultPromoTokens()).isEqualTo(tokens);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailure_setDefaultPromoTokensWrongTokenType() {
|
||||
Registry registry = Registry.get("tld");
|
||||
assertThat(registry.getDefaultPromoTokens()).isEmpty();
|
||||
AllocationToken token1 =
|
||||
persistResource(
|
||||
new AllocationToken()
|
||||
.asBuilder()
|
||||
.setToken("abc123")
|
||||
.setTokenType(SINGLE_USE)
|
||||
.setAllowedTlds(ImmutableSet.of("tld"))
|
||||
.build());
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
registry
|
||||
.asBuilder()
|
||||
.setDefaultPromoTokens(ImmutableList.of(token1.createVKey()))
|
||||
.build());
|
||||
assertThat(thrown.getMessage())
|
||||
.isEqualTo(
|
||||
"Token abc123 has an invalid token type of SINGLE_USE. DefaultPromoTokens must be of"
|
||||
+ " the type DEFAULT_PROMO");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailure_setDefaultPromoTokensNotValidForTld() {
|
||||
Registry registry = Registry.get("tld");
|
||||
assertThat(registry.getDefaultPromoTokens()).isEmpty();
|
||||
AllocationToken token1 =
|
||||
persistResource(
|
||||
new AllocationToken()
|
||||
.asBuilder()
|
||||
.setToken("abc123")
|
||||
.setTokenType(DEFAULT_PROMO)
|
||||
.setAllowedTlds(ImmutableSet.of("example"))
|
||||
.build());
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
registry
|
||||
.asBuilder()
|
||||
.setDefaultPromoTokens(ImmutableList.of(token1.createVKey()))
|
||||
.build());
|
||||
assertThat(thrown.getMessage())
|
||||
.isEqualTo(
|
||||
"The token abc123 is not valid for this TLD. The valid TLDs for it are [example]");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
// Copyright 2022 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.persistence.converter;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.model.domain.token.AllocationToken.TokenType.SINGLE_USE;
|
||||
import static google.registry.model.domain.token.AllocationToken.TokenType.UNLIMITED_USE;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
|
||||
import static google.registry.testing.DatabaseHelper.insertInDb;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.model.domain.token.AllocationToken;
|
||||
import google.registry.persistence.VKey;
|
||||
import google.registry.persistence.transaction.JpaTestExtensions;
|
||||
import google.registry.persistence.transaction.JpaTestExtensions.JpaUnitTestExtension;
|
||||
import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
/** Unit tests for {@link google.registry.persistence.converter.AllocationTokenListConverter}. */
|
||||
public class AllocationTokenListConverterTest {
|
||||
|
||||
@RegisterExtension
|
||||
public final JpaUnitTestExtension jpaExtension =
|
||||
new JpaTestExtensions.Builder()
|
||||
.withEntityClass(TestAllocationTokenVKeyList.class)
|
||||
.buildUnitTestExtension();
|
||||
|
||||
@Test
|
||||
void testRoundTrip() {
|
||||
AllocationToken token1 =
|
||||
new AllocationToken().asBuilder().setToken("abc123").setTokenType(SINGLE_USE).build();
|
||||
AllocationToken token2 =
|
||||
new AllocationToken().asBuilder().setToken("token").setTokenType(UNLIMITED_USE).build();
|
||||
List<VKey<AllocationToken>> tokens = ImmutableList.of(token1.createVKey(), token2.createVKey());
|
||||
TestAllocationTokenVKeyList testAllocationTokenVKeyList =
|
||||
new TestAllocationTokenVKeyList(tokens);
|
||||
insertInDb(testAllocationTokenVKeyList);
|
||||
TestAllocationTokenVKeyList persisted =
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> jpaTm().getEntityManager().find(TestAllocationTokenVKeyList.class, "id"));
|
||||
assertThat(persisted.tokenList).isEqualTo(tokens);
|
||||
}
|
||||
|
||||
@Entity(name = "TestAllocationTokenVKeyList")
|
||||
static class TestAllocationTokenVKeyList extends ImmutableObject {
|
||||
@Id String id = "id";
|
||||
|
||||
List<VKey<AllocationToken>> tokenList;
|
||||
|
||||
TestAllocationTokenVKeyList() {}
|
||||
|
||||
TestAllocationTokenVKeyList(List<VKey<AllocationToken>> tokenList) {
|
||||
this.tokenList = tokenList;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue