From 5a4877805b6678acc9bf2b26e05bc4d711546b77 Mon Sep 17 00:00:00 2001 From: mmuller Date: Wed, 15 Jun 2016 10:43:23 -0700 Subject: [PATCH] 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 --- .../google/registry/model/registry/Registry.java | 16 ++++++++++------ java/google/registry/tools/CreateTldCommand.java | 13 +++++++++++-- .../registry/model/registry/RegistryTest.java | 9 +++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/java/google/registry/model/registry/Registry.java b/java/google/registry/model/registry/Registry.java index 98cb50e85..a56efb6e3 100644 --- a/java/google/registry/model/registry/Registry.java +++ b/java/google/registry/model/registry/Registry.java @@ -826,15 +826,19 @@ public class Registry extends ImmutableObject implements Buildable { checkArgument( instance.getServerStatusChangeCost().getCurrencyUnit().equals(instance.currency), "Server status change cost must be in the registry's currency"); + Predicate currencyCheck = new Predicate(){ + @Override + public boolean apply(Money money) { + return money.getCurrencyUnit().equals(instance.currency); + }}; checkArgument( Iterables.all( - instance.getRenewBillingCostTransitions().values(), - new Predicate(){ - @Override - public boolean apply(Money money) { - return money.getCurrencyUnit().equals(instance.currency); - }}), + instance.getRenewBillingCostTransitions().values(), currencyCheck), "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( instance.pricingEngineClassName, "All registries must have a configured pricing engine"); instance.tldStrId = tldName; diff --git a/java/google/registry/tools/CreateTldCommand.java b/java/google/registry/tools/CreateTldCommand.java index c0d8058f3..af7baf641 100644 --- a/java/google/registry/tools/CreateTldCommand.java +++ b/java/google/registry/tools/CreateTldCommand.java @@ -30,6 +30,7 @@ import com.beust.jcommander.Parameters; import google.registry.model.registry.Registry; import google.registry.model.registry.Registry.TldState; +import org.joda.money.CurrencyUnit; import org.joda.money.Money; import javax.annotation.Nullable; @@ -73,9 +74,17 @@ class CreateTldCommand extends CreateOrUpdateTldCommand { void setCommandSpecificProperties(Registry.Builder builder) { // 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. - builder.setCurrency(createBillingCost != null + CurrencyUnit currency = createBillingCost != null ? 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 diff --git a/javatests/google/registry/model/registry/RegistryTest.java b/javatests/google/registry/model/registry/RegistryTest.java index 61bf1c5f5..0b3a6dda0 100644 --- a/javatests/google/registry/model/registry/RegistryTest.java +++ b/javatests/google/registry/model/registry/RegistryTest.java @@ -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().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(); + } }