Enforce currency consistency for server status billing cost

Also adds tests for currency consistency for other costs, and cleans
up some of the testing for negative currencies (since I was there).
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120231198
This commit is contained in:
nickfelt 2016-04-19 08:29:27 -07:00 committed by Justine Tunney
parent c5d09227c5
commit 706f3b44ed
9 changed files with 59 additions and 15 deletions

View file

@ -657,7 +657,7 @@ public class Registry extends BackupGroupRoot implements Buildable {
} }
public Builder setCreateBillingCost(Money amount) { public Builder setCreateBillingCost(Money amount) {
checkArgument(amount.isPositiveOrZero(), "billing costs must be non-negative"); checkArgument(amount.isPositiveOrZero(), "create billing cost cannot be negative");
getInstance().createBillingCost = amount; getInstance().createBillingCost = amount;
return this; return this;
} }
@ -698,7 +698,7 @@ public class Registry extends BackupGroupRoot implements Buildable {
} }
public Builder setRestoreBillingCost(Money amount) { public Builder setRestoreBillingCost(Money amount) {
checkArgument(amount.isPositiveOrZero(), "billing costs must be non-negative"); checkArgument(amount.isPositiveOrZero(), "restore billing cost cannot be negative");
getInstance().restoreBillingCost = amount; getInstance().restoreBillingCost = amount;
return this; return this;
} }
@ -713,7 +713,7 @@ public class Registry extends BackupGroupRoot implements Buildable {
*/ */
public Builder setRenewBillingCostTransitions( public Builder setRenewBillingCostTransitions(
ImmutableSortedMap<DateTime, Money> renewCostsMap) { ImmutableSortedMap<DateTime, Money> renewCostsMap) {
checkNotNull(renewCostsMap, "Renew billing costs map cannot be null"); checkNotNull(renewCostsMap, "renew billing costs map cannot be null");
checkArgument(Iterables.all( checkArgument(Iterables.all(
renewCostsMap.values(), renewCostsMap.values(),
new Predicate<Money>() { new Predicate<Money>() {
@ -721,7 +721,7 @@ public class Registry extends BackupGroupRoot implements Buildable {
public boolean apply(Money amount) { public boolean apply(Money amount) {
return amount.isPositiveOrZero(); return amount.isPositiveOrZero();
}}), }}),
"Renew billing costs cannot be negative"); "renew billing cost cannot be negative");
getInstance().renewBillingCostTransitions = getInstance().renewBillingCostTransitions =
TimedTransitionProperty.fromValueMap(renewCostsMap, BillingCostTransition.class); TimedTransitionProperty.fromValueMap(renewCostsMap, BillingCostTransition.class);
return this; return this;
@ -733,7 +733,8 @@ public class Registry extends BackupGroupRoot implements Buildable {
} }
public Builder setServerStatusChangeBillingCost(Money amount) { public Builder setServerStatusChangeBillingCost(Money amount) {
checkArgument(amount.isPositiveOrZero(), "billing costs must be non-negative"); checkArgument(
amount.isPositiveOrZero(), "server status change billing cost cannot be negative");
getInstance().serverStatusChangeBillingCost = amount; getInstance().serverStatusChangeBillingCost = amount;
return this; return this;
} }
@ -785,6 +786,9 @@ public class Registry extends BackupGroupRoot implements Buildable {
checkArgument( checkArgument(
instance.getStandardRestoreCost().getCurrencyUnit().equals(instance.currency), instance.getStandardRestoreCost().getCurrencyUnit().equals(instance.currency),
"Restore cost must be in the registry's currency"); "Restore cost must be in the registry's currency");
checkArgument(
instance.getServerStatusChangeCost().getCurrencyUnit().equals(instance.currency),
"Server status change cost must be in the registry's currency");
checkArgument( checkArgument(
Iterables.all( Iterables.all(
instance.getRenewBillingCostTransitions().values(), instance.getRenewBillingCostTransitions().values(),
@ -793,7 +797,7 @@ public class Registry extends BackupGroupRoot implements Buildable {
public boolean apply(Money money) { public boolean apply(Money money) {
return money.getCurrencyUnit().equals(instance.currency); return money.getCurrencyUnit().equals(instance.currency);
}}), }}),
"Renew costs must be in the registry's currency"); "Renew cost must be in the registry's currency");
instance.tldStrId = tldName; instance.tldStrId = tldName;
instance.tldUnicode = Idn.toUnicode(tldName); instance.tldUnicode = Idn.toUnicode(tldName);
return super.build(); return super.build();

View file

@ -1139,6 +1139,7 @@ public class DomainApplicationCreateFlowTest
.setCreateBillingCost(Money.of(EUR, 13)) .setCreateBillingCost(Money.of(EUR, 13))
.setRestoreBillingCost(Money.of(EUR, 11)) .setRestoreBillingCost(Money.of(EUR, 11))
.setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7))) .setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7)))
.setServerStatusChangeBillingCost(Money.of(EUR, 19))
.build()); .build());
persistContactsAndHosts(); persistContactsAndHosts();
clock.advanceOneMilli(); clock.advanceOneMilli();

View file

@ -558,6 +558,7 @@ public class DomainCreateFlowTest extends ResourceFlowTestCase<DomainCreateFlow,
.setCreateBillingCost(Money.of(EUR, 13)) .setCreateBillingCost(Money.of(EUR, 13))
.setRestoreBillingCost(Money.of(EUR, 11)) .setRestoreBillingCost(Money.of(EUR, 11))
.setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7))) .setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7)))
.setServerStatusChangeBillingCost(Money.of(EUR, 19))
.build()); .build());
persistContactsAndHosts(); persistContactsAndHosts();
runFlow(); runFlow();

View file

@ -340,6 +340,7 @@ public class DomainRenewFlowTest extends ResourceFlowTestCase<DomainRenewFlow, D
.setCreateBillingCost(Money.of(EUR, 13)) .setCreateBillingCost(Money.of(EUR, 13))
.setRestoreBillingCost(Money.of(EUR, 11)) .setRestoreBillingCost(Money.of(EUR, 11))
.setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7))) .setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7)))
.setServerStatusChangeBillingCost(Money.of(EUR, 19))
.build()); .build());
persistDomain(); persistDomain();
runFlow(); runFlow();

View file

@ -284,6 +284,7 @@ public class DomainRestoreRequestFlowTest extends
.setCreateBillingCost(Money.of(EUR, 13)) .setCreateBillingCost(Money.of(EUR, 13))
.setRestoreBillingCost(Money.of(EUR, 11)) .setRestoreBillingCost(Money.of(EUR, 11))
.setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7))) .setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7)))
.setServerStatusChangeBillingCost(Money.of(EUR, 19))
.build()); .build());
runFlow(); runFlow();
} }

View file

@ -443,6 +443,7 @@ public class DomainTransferRequestFlowTest
.setCreateBillingCost(Money.of(EUR, 13)) .setCreateBillingCost(Money.of(EUR, 13))
.setRestoreBillingCost(Money.of(EUR, 11)) .setRestoreBillingCost(Money.of(EUR, 11))
.setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7))) .setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 7)))
.setServerStatusChangeBillingCost(Money.of(EUR, 19))
.build()); .build());
doFailingTest("domain_transfer_request_fee.xml"); doFailingTest("domain_transfer_request_fee.xml");
} }

View file

@ -25,6 +25,7 @@ import static com.google.domain.registry.testing.DatastoreHelper.persistPremiumL
import static com.google.domain.registry.testing.DatastoreHelper.persistReservedList; import static com.google.domain.registry.testing.DatastoreHelper.persistReservedList;
import static com.google.domain.registry.util.DateTimeUtils.END_OF_TIME; import static com.google.domain.registry.util.DateTimeUtils.END_OF_TIME;
import static com.google.domain.registry.util.DateTimeUtils.START_OF_TIME; import static com.google.domain.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.money.CurrencyUnit.EUR;
import static org.joda.money.CurrencyUnit.USD; import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -399,23 +400,54 @@ public class RegistryTest extends EntityTestCase {
} }
@Test @Test
public void testFailure_badRenewBillingCostTransitionValue() { public void testFailure_negativeRenewBillingCostTransitionValue() {
thrown.expect(IllegalArgumentException.class); thrown.expect(IllegalArgumentException.class, "billing cost cannot be negative");
Registry.get("tld").asBuilder() Registry.get("tld").asBuilder()
.setRenewBillingCostTransitions(ImmutableSortedMap.of(clock.nowUtc(), Money.of(USD, -42))) .setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(USD, -42)));
}
@Test
public void testFailure_negativeCreateBillingCost() {
thrown.expect(IllegalArgumentException.class, "billing cost cannot be negative");
Registry.get("tld").asBuilder().setCreateBillingCost(Money.of(USD, -42));
}
@Test
public void testFailure_negativeRestoreBillingCost() {
thrown.expect(IllegalArgumentException.class, "billing cost cannot be negative");
Registry.get("tld").asBuilder().setRestoreBillingCost(Money.of(USD, -42));
}
@Test
public void testFailure_negativeServerStatusChangeBillingCost() {
thrown.expect(IllegalArgumentException.class, "billing cost cannot be negative");
Registry.get("tld").asBuilder().setServerStatusChangeBillingCost(Money.of(USD, -42));
}
@Test
public void testFailure_renewBillingCostTransitionValue_wrongCurrency() {
thrown.expect(IllegalArgumentException.class, "cost must be in the registry's currency");
Registry.get("tld").asBuilder()
.setRenewBillingCostTransitions(ImmutableSortedMap.of(START_OF_TIME, Money.of(EUR, 42)))
.build(); .build();
} }
@Test @Test
public void testFailure_badCreateBillingCost() { public void testFailure_createBillingCost_wrongCurrency() {
thrown.expect(IllegalArgumentException.class); thrown.expect(IllegalArgumentException.class, "cost must be in the registry's currency");
Registry.get("tld").asBuilder().setCreateBillingCost(Money.of(USD, -42)).build(); Registry.get("tld").asBuilder().setCreateBillingCost(Money.of(EUR, 42)).build();
} }
@Test @Test
public void testFailure_badRestoreBillingCost() { public void testFailure_restoreBillingCost_wrongCurrency() {
thrown.expect(IllegalArgumentException.class); thrown.expect(IllegalArgumentException.class, "cost must be in the registry's currency");
Registry.get("tld").asBuilder().setRestoreBillingCost(Money.of(USD, -42)).build(); Registry.get("tld").asBuilder().setRestoreBillingCost(Money.of(EUR, 42)).build();
}
@Test
public void testFailure_serverStatusChangeBillingCost_wrongCurrency() {
thrown.expect(IllegalArgumentException.class, "cost must be in the registry's currency");
Registry.get("tld").asBuilder().setServerStatusChangeBillingCost(Money.of(EUR, 42)).build();
} }
@Test @Test

View file

@ -165,6 +165,7 @@ public class CreateTldCommandTest extends CommandTestCase<CreateTldCommand> {
"--create_billing_cost=\"JPY 12345\"", "--create_billing_cost=\"JPY 12345\"",
"--restore_billing_cost=\"JPY 67890\"", "--restore_billing_cost=\"JPY 67890\"",
"--initial_renew_billing_cost=\"JPY 101112\"", "--initial_renew_billing_cost=\"JPY 101112\"",
"--server_status_change_cost=\"JPY 97865\"",
"--roid_suffix=Q9JYB4C", "--roid_suffix=Q9JYB4C",
"xn--q9jyb4c"); "xn--q9jyb4c");
Registry registry = Registry.get("xn--q9jyb4c"); Registry registry = Registry.get("xn--q9jyb4c");

View file

@ -215,11 +215,13 @@ public class UpdateTldCommandTest extends CommandTestCase<UpdateTldCommand> {
.setRestoreBillingCost(Money.ofMajor(JPY, 1)) .setRestoreBillingCost(Money.ofMajor(JPY, 1))
.setRenewBillingCostTransitions( .setRenewBillingCostTransitions(
ImmutableSortedMap.of(START_OF_TIME, Money.ofMajor(JPY, 1))) ImmutableSortedMap.of(START_OF_TIME, Money.ofMajor(JPY, 1)))
.setServerStatusChangeBillingCost(Money.ofMajor(JPY, 1))
.build()); .build());
runCommandForced( runCommandForced(
"--create_billing_cost=\"JPY 12345\"", "--create_billing_cost=\"JPY 12345\"",
"--restore_billing_cost=\"JPY 67890\"", "--restore_billing_cost=\"JPY 67890\"",
"--renew_billing_cost_transitions=\"0=JPY 101112\"", "--renew_billing_cost_transitions=\"0=JPY 101112\"",
"--server_status_change_cost=\"JPY 97865\"",
"xn--q9jyb4c"); "xn--q9jyb4c");
assertThat(Registry.get("xn--q9jyb4c").getStandardCreateCost()) assertThat(Registry.get("xn--q9jyb4c").getStandardCreateCost())
.isEqualTo(Money.ofMajor(JPY, 12345)); .isEqualTo(Money.ofMajor(JPY, 12345));