Add renewal logic in allocation token related commands (#1596)

* Add renewal price behavior to allocation token related command

* Add details to renewal price behavior
This commit is contained in:
Rachel Guan
2022-05-05 15:48:45 -04:00
committed by GitHub
parent dd9c576146
commit cc62530345
5 changed files with 195 additions and 3 deletions
@@ -162,7 +162,7 @@ public class AllocationTokenTest extends EntityTestCase {
}
@TestOfyAndSql
void testgetRenewalBehavior_returnsDefaultRenewBehavior() {
void testGetRenewalBehavior_returnsDefaultRenewBehavior() {
assertThat(
persistResource(
new AllocationToken.Builder()
@@ -174,7 +174,7 @@ public class AllocationTokenTest extends EntityTestCase {
}
@TestOfyAndSql
void testsetRenewalBehavior_assertsRenewalBehaviorIsNotDefault() {
void testSetRenewalBehavior_assertsRenewalBehaviorIsNotDefault() {
assertThat(
persistResource(
new AllocationToken.Builder()
@@ -187,7 +187,7 @@ public class AllocationTokenTest extends EntityTestCase {
}
@TestOfyAndSql
void testsetRenewalBehavior_assertRenewalBehaviorIsModified() {
void testSetRenewalBehavior_assertRenewalBehaviorIsModified() {
AllocationToken token =
persistResource(
new AllocationToken.Builder()
@@ -15,6 +15,8 @@
package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.NONPREMIUM;
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.SPECIFIED;
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.testing.DatabaseHelper.assertAllocationTokens;
@@ -193,6 +195,73 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
assertInStdout("foobar", "foobaz");
}
@TestOfyAndSql
void testSuccess_renewalPriceBehaviorIsDefault() throws Exception {
runCommand("--tokens", "foobar,foobaz", "--renewal_price_behavior", "DEFAULT");
assertAllocationTokens(createToken("foobar", null, null), createToken("foobaz", null, null));
assertInStdout("foobar", "foobaz");
}
@TestOfyAndSql
void testSuccess_renewalPriceBehaviorIsSetToDefaultByDefault() throws Exception {
runCommand("--tokens", "foobar,foobaz");
assertAllocationTokens(createToken("foobar", null, null), createToken("foobaz", null, null));
assertInStdout("foobar", "foobaz");
}
@TestOfyAndSql
void testSuccess_renewalPriceBehaviorIsNonPremium() throws Exception {
runCommand("--tokens", "foobar,foobaz", "--renewal_price_behavior", "NONPREMIUM");
assertAllocationTokens(
createToken("foobar", null, null).asBuilder().setRenewalPriceBehavior(NONPREMIUM).build(),
createToken("foobaz", null, null).asBuilder().setRenewalPriceBehavior(NONPREMIUM).build());
assertInStdout("foobar", "foobaz");
}
@TestOfyAndSql
void testSuccess_renewalPriceBehaviorIsSpecified() throws Exception {
runCommand("--tokens", "foobar,foobaz", "--renewal_price_behavior", "SPECIFIED");
assertAllocationTokens(
createToken("foobar", null, null).asBuilder().setRenewalPriceBehavior(SPECIFIED).build(),
createToken("foobaz", null, null).asBuilder().setRenewalPriceBehavior(SPECIFIED).build());
assertInStdout("foobar", "foobaz");
}
@TestOfyAndSql
void testSuccess_renewalPriceBehaviorIsSpecifiedButMixedCase() throws Exception {
runCommand("--tokens", "foobar,foobaz", "--renewal_price_behavior", "speCIFied");
assertAllocationTokens(
createToken("foobar", null, null).asBuilder().setRenewalPriceBehavior(SPECIFIED).build(),
createToken("foobaz", null, null).asBuilder().setRenewalPriceBehavior(SPECIFIED).build());
assertInStdout("foobar", "foobaz");
}
@TestOfyAndSql
void testFailure_renewalPriceBehaviorIsInvalid() {
ParameterException thrown =
assertThrows(
ParameterException.class,
() -> runCommand("--tokens", "foobar,foobaz", "--renewal_price_behavior", "SPEXIFIED"));
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
"Invalid value for --renewal_price_behavior parameter. Allowed values:[DEFAULT,"
+ " NONPREMIUM, SPECIFIED]");
}
@TestOfyAndSql
void testFailure_renewalPriceBehaviorIsEmptyString() {
ParameterException thrown =
assertThrows(
ParameterException.class,
() -> runCommand("--tokens", "foobar,foobaz", "--renewal_price_behavior", ""));
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
"Invalid value for --renewal_price_behavior parameter. Allowed values:[DEFAULT,"
+ " NONPREMIUM, SPECIFIED]");
}
@TestOfyAndSql
void testSuccess_specifyManyTokens() throws Exception {
command.stringGenerator =
@@ -15,6 +15,9 @@
package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.DEFAULT;
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.NONPREMIUM;
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.SPECIFIED;
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;
@@ -26,6 +29,7 @@ import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.time.DateTimeZone.UTC;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.beust.jcommander.ParameterException;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import google.registry.model.domain.token.AllocationToken;
@@ -34,6 +38,7 @@ import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import org.joda.time.DateTime;
/** Unit tests for {@link UpdateAllocationTokensCommand}. */
@DualDatabaseTest
class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocationTokensCommand> {
@@ -97,6 +102,96 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
assertThat(reloadResource(token).getDiscountYears()).isEqualTo(4);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setToSpecified() throws Exception {
AllocationToken token = persistResource(builderWithPromo().setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "SPECIFIED");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(SPECIFIED);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setToDefault() throws Exception {
AllocationToken token =
persistResource(
builderWithPromo().setRenewalPriceBehavior(SPECIFIED).setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "default");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(DEFAULT);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setToNonPremium() throws Exception {
AllocationToken token =
persistResource(
builderWithPromo().setRenewalPriceBehavior(SPECIFIED).setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "NONpremium");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(NONPREMIUM);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setFromDefaultToDefault() throws Exception {
AllocationToken token = persistResource(builderWithPromo().setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "defauLT");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(DEFAULT);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setFromSpecifiedToSpecified() throws Exception {
AllocationToken token =
persistResource(
builderWithPromo().setRenewalPriceBehavior(SPECIFIED).setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "SPecified");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(SPECIFIED);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setFromNonPremiumToDefault() throws Exception {
AllocationToken token =
persistResource(
builderWithPromo()
.setRenewalPriceBehavior(NONPREMIUM)
.setDiscountFraction(0.5)
.build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "defauLT");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(DEFAULT);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setToMixedCaseDefault() throws Exception {
AllocationToken token =
persistResource(
builderWithPromo().setRenewalPriceBehavior(SPECIFIED).setDiscountFraction(0.5).build());
runCommandForced("--prefix", "token", "--renewal_price_behavior", "deFauLt");
assertThat(reloadResource(token).getRenewalPriceBehavior()).isEqualTo(DEFAULT);
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setToInvalidBehavior_throwsException() {
ParameterException thrown =
assertThrows(
ParameterException.class,
() -> runCommandForced("--prefix", "token", "--renewal_price_behavior", "premium"));
persistResource(builderWithPromo().setDiscountFraction(0.5).build());
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
"Invalid value for --renewal_price_behavior parameter. Allowed values:[DEFAULT,"
+ " NONPREMIUM, SPECIFIED]");
}
@TestOfyAndSql
void testUpdateRenewalPriceBehavior_setToEmptyString_throwsException() {
ParameterException thrown =
assertThrows(
ParameterException.class,
() -> runCommandForced("--prefix", "token", "--renewal_price_behavior", ""));
persistResource(builderWithPromo().setDiscountFraction(0.5).build());
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
"Invalid value for --renewal_price_behavior parameter. Allowed values:[DEFAULT,"
+ " NONPREMIUM, SPECIFIED]");
}
@TestOfyAndSql
void testUpdateStatusTransitions() throws Exception {
DateTime now = DateTime.now(UTC);