Validate EAP currency in Registry build

Verify that the currency of all entries in the EAP fee schedule map matches
that of the registry object itself.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124967612
This commit is contained in:
mmuller 2016-06-15 10:43:23 -07:00 committed by Ben McIlwain
parent ec39f15a23
commit 5a4877805b
3 changed files with 30 additions and 8 deletions

View file

@ -826,15 +826,19 @@ public class Registry extends ImmutableObject implements Buildable {
checkArgument( checkArgument(
instance.getServerStatusChangeCost().getCurrencyUnit().equals(instance.currency), instance.getServerStatusChangeCost().getCurrencyUnit().equals(instance.currency),
"Server status change cost must be in the registry's currency"); "Server status change cost must be in the registry's currency");
Predicate<Money> currencyCheck = new Predicate<Money>(){
@Override
public boolean apply(Money money) {
return money.getCurrencyUnit().equals(instance.currency);
}};
checkArgument( checkArgument(
Iterables.all( Iterables.all(
instance.getRenewBillingCostTransitions().values(), instance.getRenewBillingCostTransitions().values(), currencyCheck),
new Predicate<Money>(){
@Override
public boolean apply(Money money) {
return money.getCurrencyUnit().equals(instance.currency);
}}),
"Renew cost must be in the registry's currency"); "Renew cost must be in the registry's currency");
checkArgument(
Iterables.all(
instance.eapFeeSchedule.toValueMap().values(), currencyCheck),
"All EAP fees must be in the registry's currency");
checkArgumentNotNull( checkArgumentNotNull(
instance.pricingEngineClassName, "All registries must have a configured pricing engine"); instance.pricingEngineClassName, "All registries must have a configured pricing engine");
instance.tldStrId = tldName; instance.tldStrId = tldName;

View file

@ -30,6 +30,7 @@ import com.beust.jcommander.Parameters;
import google.registry.model.registry.Registry; import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldState; import google.registry.model.registry.Registry.TldState;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money; import org.joda.money.Money;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -73,9 +74,17 @@ class CreateTldCommand extends CreateOrUpdateTldCommand {
void setCommandSpecificProperties(Registry.Builder builder) { void setCommandSpecificProperties(Registry.Builder builder) {
// Pick up the currency from the create cost. Since all costs must be in one currency, and that // Pick up the currency from the create cost. Since all costs must be in one currency, and that
// condition is enforced by the builder, it doesn't matter which cost we choose it from. // condition is enforced by the builder, it doesn't matter which cost we choose it from.
builder.setCurrency(createBillingCost != null CurrencyUnit currency = createBillingCost != null
? createBillingCost.getCurrencyUnit() ? createBillingCost.getCurrencyUnit()
: Registry.DEFAULT_CURRENCY); : Registry.DEFAULT_CURRENCY;
builder.setCurrency(currency);
// If this is a non-default currency, set the EAP fee schedule to a matching currency.
// TODO(b/29089413): once we have a flag for this, don't do this check if the flag is set.
if (currency != Registry.DEFAULT_CURRENCY) {
builder.setEapFeeSchedule(ImmutableSortedMap.of(START_OF_TIME, Money.zero(currency)));
}
} }
@Override @Override

View file

@ -434,4 +434,13 @@ public class RegistryTest extends EntityTestCase {
assertThat(registry.getEapFeeFor(clock.nowUtc().minusDays(2))).isEqualTo(Money.of(USD, 0)); assertThat(registry.getEapFeeFor(clock.nowUtc().minusDays(2))).isEqualTo(Money.of(USD, 0));
assertThat(registry.getEapFeeFor(clock.nowUtc().plusDays(2))).isEqualTo(Money.of(USD, 50)); assertThat(registry.getEapFeeFor(clock.nowUtc().plusDays(2))).isEqualTo(Money.of(USD, 50));
} }
@Test
public void testFailure_eapFee_wrongCurrency() {
thrown.expect(
IllegalArgumentException.class, "All EAP fees must be in the registry's currency");
Registry.get("tld").asBuilder()
.setEapFeeSchedule(ImmutableSortedMap.of(START_OF_TIME, Money.zero(EUR)))
.build();
}
} }