Convert AllocationToken-related classes to tm() (#909)

* Convert AllocationToken-related classes to tm()

For the most part this is a fairly simple converstion -- changing Key
references to VKey references, using JPA transactions when necessary,
and using the TransactionManager interface. There's a bit of cleanup too
in related code
This commit is contained in:
gbrodman 2020-12-21 15:56:49 -05:00 committed by GitHub
parent 6e2bbd1a7e
commit e550c94cbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 201 additions and 178 deletions

View file

@ -16,15 +16,14 @@ package google.registry.model.domain.token;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.CANCELLED;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.ENDED;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.NOT_STARTED;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.VALID;
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.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.persistActiveDomain;
import static google.registry.testing.DatabaseHelper.persistResource;
@ -40,12 +39,14 @@ import google.registry.model.domain.DomainBase;
import google.registry.model.domain.token.AllocationToken.TokenStatus;
import google.registry.model.domain.token.AllocationToken.TokenType;
import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey;
import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import google.registry.testing.TestOfyOnly;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link AllocationToken}. */
@DualDatabaseTest
public class AllocationTokenTest extends EntityTestCase {
public AllocationTokenTest() {
@ -57,7 +58,7 @@ public class AllocationTokenTest extends EntityTestCase {
createTld("foo");
}
@Test
@TestOfyAndSql
void testPersistence() {
AllocationToken unlimitedUseToken =
persistResource(
@ -77,7 +78,7 @@ public class AllocationTokenTest extends EntityTestCase {
.put(DateTime.now(UTC).plusWeeks(8), TokenStatus.ENDED)
.build())
.build());
assertThat(ofy().load().entity(unlimitedUseToken).now()).isEqualTo(unlimitedUseToken);
assertThat(transactIfJpaTm(() -> tm().load(unlimitedUseToken))).isEqualTo(unlimitedUseToken);
DomainBase domain = persistActiveDomain("example.foo");
Key<HistoryEntry> historyEntryKey = Key.create(Key.create(domain), HistoryEntry.class, 1);
@ -90,32 +91,10 @@ public class AllocationTokenTest extends EntityTestCase {
.setCreationTimeForTest(DateTime.parse("2010-11-12T05:00:00Z"))
.setTokenType(SINGLE_USE)
.build());
assertThat(ofy().load().entity(singleUseToken).now()).isEqualTo(singleUseToken);
jpaTm()
.transact(
() -> {
jpaTm().insert(unlimitedUseToken);
jpaTm().insert(singleUseToken);
});
jpaTm()
.transact(
() -> {
assertAboutImmutableObjects()
.that(jpaTm().load(VKey.createSql(AllocationToken.class, "abc123Unlimited")))
.isEqualExceptFields(
unlimitedUseToken,
"creationTime",
"updateTimestamp",
"redemptionHistoryEntry");
assertAboutImmutableObjects()
.that(jpaTm().load(VKey.createSql(AllocationToken.class, "abc123Single")))
.isEqualExceptFields(
singleUseToken, "creationTime", "updateTimestamp", "redemptionHistoryEntry");
});
assertThat(transactIfJpaTm(() -> tm().load(singleUseToken))).isEqualTo(singleUseToken);
}
@Test
@TestOfyOnly
void testIndexing() throws Exception {
DomainBase domain = persistActiveDomain("blahdomain.foo");
Key<HistoryEntry> historyEntryKey = Key.create(Key.create(domain), HistoryEntry.class, 1);
@ -133,7 +112,7 @@ public class AllocationTokenTest extends EntityTestCase {
"domainName");
}
@Test
@TestOfyAndSql
void testCreationTime_autoPopulates() {
AllocationToken tokenBeforePersisting =
new AllocationToken.Builder().setToken("abc123").setTokenType(SINGLE_USE).build();
@ -142,7 +121,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(tokenAfterPersisting.getCreationTime()).hasValue(fakeClock.nowUtc());
}
@Test
@TestOfyAndSql
void testSetCreationTime_cantCallMoreThanOnce() {
AllocationToken.Builder builder =
new AllocationToken.Builder()
@ -156,7 +135,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Creation time can only be set once");
}
@Test
@TestOfyAndSql
void testSetToken_cantCallMoreThanOnce() {
AllocationToken.Builder builder = new AllocationToken.Builder().setToken("foobar");
IllegalStateException thrown =
@ -164,7 +143,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Token can only be set once");
}
@Test
@TestOfyAndSql
void testSetTokenType_cantCallMoreThanOnce() {
AllocationToken.Builder builder =
new AllocationToken.Builder().setTokenType(TokenType.UNLIMITED_USE);
@ -173,7 +152,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Token type can only be set once");
}
@Test
@TestOfyAndSql
void testBuild_DomainNameWithLessThanTwoParts() {
IllegalArgumentException thrown =
assertThrows(
@ -191,7 +170,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Invalid domain name: example");
}
@Test
@TestOfyAndSql
void testBuild_invalidTld() {
IllegalArgumentException thrown =
assertThrows(
@ -209,7 +188,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Invalid domain name: example.nosuchtld");
}
@Test
@TestOfyAndSql
void testBuild_domainNameOnlyOnSingleUse() {
AllocationToken.Builder builder =
new AllocationToken.Builder()
@ -222,7 +201,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Domain name can only be specified for SINGLE_USE tokens");
}
@Test
@TestOfyAndSql
void testBuild_redemptionHistoryEntryOnlyInSingleUse() {
DomainBase domain = persistActiveDomain("blahdomain.foo");
Key<HistoryEntry> historyEntryKey = Key.create(Key.create(domain), HistoryEntry.class, 1);
@ -237,7 +216,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Redemption history entry can only be specified for SINGLE_USE tokens");
}
@Test
@TestOfyAndSql
void testSetTransitions_notStartOfTime() {
IllegalArgumentException thrown =
assertThrows(
@ -255,7 +234,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("tokenStatusTransitions map must start at START_OF_TIME.");
}
@Test
@TestOfyAndSql
void testSetTransitions_badInitialValue() {
IllegalArgumentException thrown =
assertThrows(
@ -272,14 +251,14 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("tokenStatusTransitions must start with NOT_STARTED");
}
@Test
@TestOfyAndSql
void testSetTransitions_invalidInitialTransitions() {
// NOT_STARTED can only go to VALID or CANCELLED
assertBadInitialTransition(NOT_STARTED);
assertBadInitialTransition(ENDED);
}
@Test
@TestOfyAndSql
void testSetTransitions_badTransitionsFromValid() {
// VALID can only go to ENDED or CANCELLED
assertBadTransition(
@ -300,14 +279,14 @@ public class AllocationTokenTest extends EntityTestCase {
NOT_STARTED);
}
@Test
@TestOfyAndSql
void testSetTransitions_terminalTransitions() {
// both ENDED and CANCELLED are terminal
assertTerminal(ENDED);
assertTerminal(CANCELLED);
}
@Test
@TestOfyAndSql
void testSetDiscountFractionTooHigh() {
IllegalArgumentException thrown =
assertThrows(
@ -318,7 +297,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Discount fraction must be between 0 and 1 inclusive");
}
@Test
@TestOfyAndSql
void testSetDiscountFractionTooLow() {
IllegalArgumentException thrown =
assertThrows(
@ -329,7 +308,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Discount fraction must be between 0 and 1 inclusive");
}
@Test
@TestOfyAndSql
void testSetDiscountYearsTooHigh() {
IllegalArgumentException thrown =
assertThrows(
@ -340,7 +319,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Discount years must be between 1 and 10 inclusive");
}
@Test
@TestOfyAndSql
void testSetDiscountYearsTooLow() {
IllegalArgumentException thrown =
assertThrows(
@ -351,7 +330,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Discount years must be between 1 and 10 inclusive");
}
@Test
@TestOfyAndSql
void testBuild_noTokenType() {
IllegalArgumentException thrown =
assertThrows(
@ -360,7 +339,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Token type must be specified");
}
@Test
@TestOfyAndSql
void testBuild_noToken() {
IllegalArgumentException thrown =
assertThrows(
@ -369,7 +348,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Token must not be null or empty");
}
@Test
@TestOfyAndSql
void testBuild_emptyToken() {
IllegalArgumentException thrown =
assertThrows(
@ -378,7 +357,7 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Token must not be blank");
}
@Test
@TestOfyAndSql
void testBuild_discountPremiumsRequiresDiscountFraction() {
IllegalArgumentException thrown =
assertThrows(
@ -394,7 +373,7 @@ public class AllocationTokenTest extends EntityTestCase {
.isEqualTo("Discount premiums can only be specified along with a discount fraction");
}
@Test
@TestOfyAndSql
void testBuild_discountYearsRequiresDiscountFraction() {
IllegalArgumentException thrown =
assertThrows(

View file

@ -18,6 +18,8 @@ import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.toArray;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.time.DateTimeZone.UTC;
@ -185,7 +187,7 @@ public abstract class CommandTestCase<C extends Command> {
/** Reloads the given resource from Datastore. */
<T> T reloadResource(T resource) {
return ofy().load().entity(resource).now();
return transactIfJpaTm(() -> tm().load(resource));
}
/** Returns count of all poll messages in Datastore. */

View file

@ -16,23 +16,28 @@ package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.domain.token.AllocationToken.TokenType.SINGLE_USE;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.testing.DatabaseHelper.createTlds;
import static google.registry.testing.DatabaseHelper.persistActiveDomain;
import static google.registry.testing.DatabaseHelper.persistResource;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import google.registry.model.domain.DomainBase;
import google.registry.model.domain.token.AllocationToken;
import google.registry.model.domain.token.AllocationToken.TokenType;
import google.registry.model.reporting.HistoryEntry;
import java.util.Collection;
import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import java.util.Arrays;
import javax.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link DeleteAllocationTokensCommand}. */
@DualDatabaseTest
class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocationTokensCommand> {
private AllocationToken preRed1;
@ -53,38 +58,38 @@ class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocation
othrNot = persistToken("asdgfho7HASDS", null, false);
}
@Test
@TestOfyAndSql
void test_deleteOnlyUnredeemedTokensWithPrefix() throws Exception {
runCommandForced("--prefix", "prefix");
assertThat(reloadTokens(preNot1, preNot2)).isEmpty();
assertNonexistent(preNot1, preNot2);
assertThat(reloadTokens(preRed1, preRed2, othrRed, othrNot))
.containsExactly(preRed1, preRed2, othrRed, othrNot);
}
@Test
@TestOfyAndSql
void test_deleteSingleAllocationToken() throws Exception {
runCommandForced("--prefix", "asdgfho7HASDS");
assertThat(reloadTokens(othrNot)).isEmpty();
assertNonexistent(othrNot);
assertThat(reloadTokens(preRed1, preRed2, preNot1, preNot2, othrRed))
.containsExactly(preRed1, preRed2, preNot1, preNot2, othrRed);
}
@Test
@TestOfyAndSql
void test_deleteParticularTokens() throws Exception {
runCommandForced("--tokens", "prefix2978204,asdgfho7HASDS");
assertThat(reloadTokens(preNot1, othrNot)).isEmpty();
assertNonexistent(preNot1, othrNot);
assertThat(reloadTokens(preRed1, preRed2, preNot2, othrRed))
.containsExactly(preRed1, preRed2, preNot2, othrRed);
}
@Test
@TestOfyAndSql
void test_deleteTokensWithNonExistentPrefix_doesNothing() throws Exception {
runCommandForced("--prefix", "nonexistent");
assertThat(reloadTokens(preRed1, preRed2, preNot1, preNot2, othrRed, othrNot))
.containsExactly(preRed1, preRed2, preNot1, preNot2, othrRed, othrNot);
}
@Test
@TestOfyAndSql
void test_dryRun_deletesNothing() throws Exception {
runCommandForced("--prefix", "prefix", "--dry_run");
assertThat(reloadTokens(preRed1, preRed2, preNot1, preNot2, othrRed, othrNot))
@ -92,27 +97,27 @@ class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocation
assertInStdout("Would delete tokens: prefix2978204, prefix8ZZZhs8");
}
@Test
@TestOfyAndSql
void test_defaultOptions_doesntDeletePerDomainTokens() throws Exception {
AllocationToken preDom1 = persistToken("prefixasdfg897as", "foo.bar", false);
AllocationToken preDom2 = persistToken("prefix98HAZXadbn", "foo.bar", true);
runCommandForced("--prefix", "prefix");
assertThat(reloadTokens(preNot1, preNot2)).isEmpty();
assertNonexistent(preNot1, preNot2);
assertThat(reloadTokens(preRed1, preRed2, preDom1, preDom2, othrRed, othrNot))
.containsExactly(preRed1, preRed2, preDom1, preDom2, othrRed, othrNot);
}
@Test
@TestOfyAndSql
void test_withDomains_doesDeletePerDomainTokens() throws Exception {
AllocationToken preDom1 = persistToken("prefixasdfg897as", "foo.bar", false);
AllocationToken preDom2 = persistToken("prefix98HAZXadbn", "foo.bar", true);
runCommandForced("--prefix", "prefix", "--with_domains");
assertThat(reloadTokens(preNot1, preNot2, preDom1)).isEmpty();
assertNonexistent(preNot1, preNot2, preDom1);
assertThat(reloadTokens(preRed1, preRed2, preDom2, othrRed, othrNot))
.containsExactly(preRed1, preRed2, preDom2, othrRed, othrNot);
}
@Test
@TestOfyAndSql
void testSkipUnlimitedUseTokens() throws Exception {
AllocationToken unlimitedUseToken =
persistResource(
@ -124,17 +129,18 @@ class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocation
assertThat(reloadTokens(unlimitedUseToken)).containsExactly(unlimitedUseToken);
}
@Test
@TestOfyAndSql
void test_batching() throws Exception {
for (int i = 0; i < 50; i++) {
persistToken(String.format("batch%2d", i), null, i % 2 == 0);
}
assertThat(ofy().load().type(AllocationToken.class).count()).isEqualTo(56);
assertThat(transactIfJpaTm(() -> tm().loadAll(AllocationToken.class).size())).isEqualTo(56);
runCommandForced("--prefix", "batch");
assertThat(ofy().load().type(AllocationToken.class).count()).isEqualTo(56 - 25);
assertThat(transactIfJpaTm(() -> tm().loadAll(AllocationToken.class).size()))
.isEqualTo(56 - 25);
}
@Test
@TestOfyAndSql
void test_prefixIsRequired() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, this::runCommandForced);
@ -143,7 +149,7 @@ class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocation
.isEqualTo("Must provide one of --tokens or --prefix, not both / neither");
}
@Test
@TestOfyAndSql
void testFailure_bothPrefixAndTokens() {
IllegalArgumentException thrown =
assertThrows(
@ -154,7 +160,7 @@ class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocation
.isEqualTo("Must provide one of --tokens or --prefix, not both / neither");
}
@Test
@TestOfyAndSql
void testFailure_emptyPrefix() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommandForced("--prefix", ""));
@ -177,7 +183,11 @@ class DeleteAllocationTokensCommandTest extends CommandTestCase<DeleteAllocation
return persistResource(builder.build());
}
private static Collection<AllocationToken> reloadTokens(AllocationToken... tokens) {
return ofy().load().entities(tokens).values();
private static ImmutableList<AllocationToken> reloadTokens(AllocationToken... tokens) {
return transactIfJpaTm(() -> tm().loadAll(ImmutableSet.copyOf(tokens)));
}
private static void assertNonexistent(AllocationToken... tokens) {
Arrays.stream(tokens).forEach(t -> transactIfJpaTm(() -> assertThat(tm().exists(t)).isFalse()));
}
}

View file

@ -17,7 +17,8 @@ package google.registry.tools;
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.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.testing.DatabaseHelper.assertAllocationTokens;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.persistResource;
@ -43,8 +44,10 @@ import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey;
import google.registry.testing.DeterministicStringGenerator;
import google.registry.testing.DeterministicStringGenerator.Rule;
import google.registry.testing.DualDatabaseTest;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeSleeper;
import google.registry.testing.TestOfyAndSql;
import google.registry.util.Retrier;
import google.registry.util.StringGenerator.Alphabets;
import java.io.File;
@ -52,10 +55,10 @@ import java.util.Collection;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
/** Unit tests for {@link GenerateAllocationTokensCommand}. */
@DualDatabaseTest
class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAllocationTokensCommand> {
@BeforeEach
@ -65,14 +68,14 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
new Retrier(new FakeSleeper(new FakeClock(DateTime.parse("2000-01-01TZ"))), 3);
}
@Test
@TestOfyAndSql
void testSuccess_oneToken() throws Exception {
runCommand("--prefix", "blah", "--number", "1", "--length", "9");
assertAllocationTokens(createToken("blah123456789", null, null));
assertInStdout("blah123456789");
}
@Test
@TestOfyAndSql
void testSuccess_threeTokens() throws Exception {
runCommand("--prefix", "foo", "--number", "3", "--length", "10");
assertAllocationTokens(
@ -82,14 +85,14 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
assertInStdout("foo123456789A\nfooBCDEFGHJKL\nfooMNPQRSTUVW");
}
@Test
@TestOfyAndSql
void testSuccess_defaults() throws Exception {
runCommand("--number", "1");
assertAllocationTokens(createToken("123456789ABCDEFG", null, null));
assertInStdout("123456789ABCDEFG");
}
@Test
@TestOfyAndSql
void testSuccess_retry() throws Exception {
command = spy(command);
RemoteApiException fakeException = new RemoteApiException("foo", "foo", "foo", new Exception());
@ -104,7 +107,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
verify(command, times(3)).saveTokens(ArgumentMatchers.any());
}
@Test
@TestOfyAndSql
void testSuccess_tokenCollision() throws Exception {
AllocationToken existingToken =
persistResource(
@ -117,24 +120,24 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
assertInStdout("DEADBEEFDEFGHJKLMNPQ");
}
@Test
@TestOfyAndSql
void testSuccess_dryRun_outputsButDoesntSave() throws Exception {
runCommand("--prefix", "foo", "--number", "2", "--length", "10", "--dry_run");
assertAllocationTokens();
assertInStdout("foo123456789A\nfooBCDEFGHJKL");
}
@Test
@TestOfyAndSql
void testSuccess_largeNumberOfTokens() throws Exception {
command.stringGenerator =
new DeterministicStringGenerator(Alphabets.BASE_58, Rule.PREPEND_COUNTER);
runCommand("--prefix", "ooo", "--number", "100", "--length", "16");
// The deterministic string generator makes it too much hassle to assert about each token, so
// just assert total number.
assertThat(ofy().load().type(AllocationToken.class).count()).isEqualTo(100);
assertThat(transactIfJpaTm(() -> tm().loadAll(AllocationToken.class).size())).isEqualTo(100);
}
@Test
@TestOfyAndSql
void testSuccess_domainNames() throws Exception {
createTld("tld");
File domainNamesFile = tmpDir.resolve("domain_names.txt").toFile();
@ -148,7 +151,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
"foo1.tld,123456789ABCDEFG\nboo2.tld,HJKLMNPQRSTUVWXY\nbaz9.tld,Zabcdefghijkmnop");
}
@Test
@TestOfyAndSql
void testSuccess_promotionToken() throws Exception {
DateTime promoStart = DateTime.now(UTC);
DateTime promoEnd = promoStart.plusMonths(1);
@ -182,24 +185,24 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.build());
}
@Test
@TestOfyAndSql
void testSuccess_specifyTokens() throws Exception {
runCommand("--tokens", "foobar,foobaz");
assertAllocationTokens(createToken("foobar", null, null), createToken("foobaz", null, null));
assertInStdout("foobar", "foobaz");
}
@Test
@TestOfyAndSql
void testSuccess_specifyManyTokens() throws Exception {
command.stringGenerator =
new DeterministicStringGenerator(Alphabets.BASE_58, Rule.PREPEND_COUNTER);
Collection<String> sampleTokens = command.stringGenerator.createStrings(13, 100);
runCommand("--tokens", Joiner.on(",").join(sampleTokens));
assertInStdout(Iterables.toArray(sampleTokens, String.class));
assertThat(ofy().load().type(AllocationToken.class).count()).isEqualTo(100);
assertThat(transactIfJpaTm(() -> tm().loadAll(AllocationToken.class).size())).isEqualTo(100);
}
@Test
@TestOfyAndSql
void testFailure_mustSpecifyNumberOfTokensOrDomainsFile() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommand("--prefix", "FEET"));
@ -208,7 +211,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isEqualTo("Must specify exactly one of '--number', '--domain_names_file', and '--tokens'");
}
@Test
@TestOfyAndSql
void testFailure_mustNotSpecifyBothNumberOfTokensAndDomainsFile() {
IllegalArgumentException thrown =
assertThrows(
@ -223,7 +226,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isEqualTo("Must specify exactly one of '--number', '--domain_names_file', and '--tokens'");
}
@Test
@TestOfyAndSql
void testFailure_mustNotSpecifyBothNumberOfTokensAndTokenStrings() {
IllegalArgumentException thrown =
assertThrows(
@ -238,7 +241,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isEqualTo("Must specify exactly one of '--number', '--domain_names_file', and '--tokens'");
}
@Test
@TestOfyAndSql
void testFailure_mustNotSpecifyBothTokenStringsAndDomainsFile() {
IllegalArgumentException thrown =
assertThrows(
@ -253,7 +256,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isEqualTo("Must specify exactly one of '--number', '--domain_names_file', and '--tokens'");
}
@Test
@TestOfyAndSql
void testFailure_specifiesAlreadyExistingToken() throws Exception {
runCommand("--tokens", "foobar");
beforeEachCommandTestCase(); // reset the command variables
@ -264,7 +267,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isEqualTo("Cannot create specified tokens; the following tokens already exist: [foobar]");
}
@Test
@TestOfyAndSql
void testFailure_invalidTokenType() {
ParameterException thrown =
assertThrows(
@ -275,7 +278,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isEqualTo("Invalid value for -t parameter. Allowed values:[SINGLE_USE, UNLIMITED_USE]");
}
@Test
@TestOfyAndSql
void testFailure_invalidTokenStatusTransition() {
assertThat(
assertThrows(
@ -290,7 +293,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isInstanceOf(IllegalArgumentException.class);
}
@Test
@TestOfyAndSql
void testFailure_lengthOfZero() {
IllegalArgumentException thrown =
assertThrows(
@ -302,7 +305,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
"Token length should not be 0. To generate exact tokens, use the --tokens parameter.");
}
@Test
@TestOfyAndSql
void testFailure_unlimitedUseMustHaveTransitions() {
assertThat(
assertThrows(

View file

@ -30,12 +30,14 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import google.registry.model.domain.token.AllocationToken;
import google.registry.model.domain.token.AllocationToken.TokenStatus;
import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
@DualDatabaseTest
class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocationTokensCommand> {
@Test
@TestOfyAndSql
void testUpdateTlds_setTlds() throws Exception {
AllocationToken token =
persistResource(builderWithPromo().setAllowedTlds(ImmutableSet.of("toRemove")).build());
@ -43,7 +45,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(token).getAllowedTlds()).containsExactly("tld", "example");
}
@Test
@TestOfyAndSql
void testUpdateTlds_clearTlds() throws Exception {
AllocationToken token =
persistResource(builderWithPromo().setAllowedTlds(ImmutableSet.of("toRemove")).build());
@ -51,7 +53,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(token).getAllowedTlds()).isEmpty();
}
@Test
@TestOfyAndSql
void testUpdateClientIds_setClientIds() throws Exception {
AllocationToken token =
persistResource(
@ -61,7 +63,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
.containsExactly("clientone", "clienttwo");
}
@Test
@TestOfyAndSql
void testUpdateClientIds_clearClientIds() throws Exception {
AllocationToken token =
persistResource(
@ -70,14 +72,14 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(token).getAllowedRegistrarIds()).isEmpty();
}
@Test
@TestOfyAndSql
void testUpdateDiscountFraction() throws Exception {
AllocationToken token = persistResource(builderWithPromo().setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--discount_fraction", "0.15");
assertThat(reloadResource(token).getDiscountFraction()).isEqualTo(0.15);
}
@Test
@TestOfyAndSql
void testUpdateDiscountPremiums() throws Exception {
AllocationToken token =
persistResource(
@ -88,14 +90,14 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(token).shouldDiscountPremiums()).isFalse();
}
@Test
@TestOfyAndSql
void testUpdateDiscountYears() throws Exception {
AllocationToken token = persistResource(builderWithPromo().setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--discount_years", "4");
assertThat(reloadResource(token).getDiscountYears()).isEqualTo(4);
}
@Test
@TestOfyAndSql
void testUpdateStatusTransitions() throws Exception {
DateTime now = DateTime.now(UTC);
AllocationToken token = persistResource(builderWithPromo().build());
@ -110,7 +112,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
.containsExactly(START_OF_TIME, NOT_STARTED, now.minusDays(1), VALID, now, CANCELLED);
}
@Test
@TestOfyAndSql
void testUpdateStatusTransitions_badTransitions() {
DateTime now = DateTime.now(UTC);
persistResource(builderWithPromo().build());
@ -130,7 +132,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
.isEqualTo("tokenStatusTransitions map cannot transition from NOT_STARTED to ENDED.");
}
@Test
@TestOfyAndSql
void testUpdate_onlyWithPrefix() throws Exception {
AllocationToken token =
persistResource(builderWithPromo().setAllowedTlds(ImmutableSet.of("tld")).build());
@ -146,7 +148,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(otherToken).getAllowedTlds()).isEmpty();
}
@Test
@TestOfyAndSql
void testUpdate_onlyTokensProvided() throws Exception {
AllocationToken firstToken =
persistResource(builderWithPromo().setAllowedTlds(ImmutableSet.of("tld")).build());
@ -170,7 +172,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(thirdToken).getAllowedTlds()).isEmpty();
}
@Test
@TestOfyAndSql
void testDoNothing() throws Exception {
AllocationToken token =
persistResource(
@ -186,7 +188,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloaded.getDiscountFraction()).isEqualTo(token.getDiscountFraction());
}
@Test
@TestOfyAndSql
void testFailure_bothTokensAndPrefix() {
assertThat(
assertThrows(
@ -196,7 +198,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
.isEqualTo("Must provide one of --tokens or --prefix, not both / neither");
}
@Test
@TestOfyAndSql
void testFailure_neitherTokensNorPrefix() {
assertThat(
assertThrows(
@ -205,7 +207,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
.isEqualTo("Must provide one of --tokens or --prefix, not both / neither");
}
@Test
@TestOfyAndSql
void testFailure_emptyPrefix() {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> runCommandForced("--prefix", ""));