Extract premium pricing to a PricingEngine interface

This refactors the existing premium list functionality into the new
class StaticPremiumListPricingEngine, which implements PricingEngine.
A backfill @OnLoad is provided to default existing Registry entities
into the static implementation.  For now there is just this one
implementation.  Dagger map multibinding is used to generate the total
set of allowed pricing engines, and allows other parties to plug in
their own implementations.

The pricing engine is a required field on the Registry object.  If you
don't want a particular Registry to actually have a premium list, then
use the static pricing engine but don't actually set a premium list.

A subsequent CL will refactor the Key<PremiumList> field on the
Registry entity class to be handled solely by the
StaticPremiumListPricingEngine implementation.  Going forward, all
configuration and implementation details that are specific to a given
pricing engine should be handled by that pricing engine, and not as
fields on the Registry object.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=121850176
This commit is contained in:
mcilwain 2016-05-09 10:30:13 -07:00 committed by Justine Tunney
parent 87961fbb12
commit 047bbf186e
32 changed files with 659 additions and 200 deletions

View file

@ -46,6 +46,7 @@ java_library(
"//java/google/registry/mapreduce",
"//java/google/registry/model",
"//java/google/registry/monitoring/whitebox",
"//java/google/registry/pricing",
"//java/google/registry/request",
"//java/google/registry/security",
"//java/google/registry/security:servlets",

View file

@ -16,6 +16,7 @@ package google.registry.flows.domain;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.pricing.PricingEngineProxy.isPremiumName;
import static google.registry.testing.DatastoreHelper.assertBillingEvents;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.deleteTld;
@ -179,10 +180,9 @@ public class DomainCreateFlowTest extends ResourceFlowTestCase<DomainCreateFlow,
.setReason(Reason.CREATE)
.setTargetId(getUniqueIdFromCommand())
.setClientId("TheRegistrar")
.setCost(Registry.get(domainTld)
.isPremiumName(getUniqueIdFromCommand(), clock.nowUtc(), "TheRegistrar")
? Money.of(USD, 200)
: Money.of(USD, 26))
.setCost(isPremiumName(getUniqueIdFromCommand(), clock.nowUtc(), "TheRegistrar")
? Money.of(USD, 200)
: Money.of(USD, 26))
.setPeriodYears(2)
.setEventTime(clock.nowUtc())
.setBillingTime(billingTime)

View file

@ -16,9 +16,9 @@ package google.registry.model;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.RoidSuffixes.getRoidSuffixForTld;
import static google.registry.testing.DatastoreHelper.newRegistry;
import static google.registry.testing.DatastoreHelper.persistResource;
import google.registry.model.registry.Registry;
import google.registry.testing.AppEngineRule;
import org.junit.Rule;
@ -37,7 +37,7 @@ public class RoidSuffixesTest {
@Test
public void test_newlyCreatedRegistry_isAddedToRoidSuffixesList() {
persistResource(new Registry.Builder().setTldStr("tld").setRoidSuffix("MEOW").build());
persistResource(newRegistry("tld", "MEOW"));
assertThat(getRoidSuffixForTld("tld")).isEqualTo("MEOW");
}
}

View file

@ -21,6 +21,7 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.label.ReservedListTest.GET_NAME_FUNCTION;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newRegistry;
import static google.registry.testing.DatastoreHelper.persistPremiumList;
import static google.registry.testing.DatastoreHelper.persistReservedList;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
@ -115,9 +116,11 @@ public class RegistryTest extends EntityTestCase {
"tld-reserved16",
"port,FULLY_BLOCKED",
"manteau,FULLY_BLOCKED");
Registry registry1 = new Registry.Builder()
.setTldStr("propter")
.setReservedLists(ImmutableSet.of(rl15)).build();
Registry registry1 =
newRegistry("propter", "PROPTER")
.asBuilder()
.setReservedLists(ImmutableSet.of(rl15))
.build();
assertThat(registry1.getReservedLists()).hasSize(1);
Registry registry2 = registry1.asBuilder()
.setReservedLists(ImmutableSet.of(rl15, rl16))
@ -128,7 +131,7 @@ public class RegistryTest extends EntityTestCase {
@Test
public void testGetReservedLists_doesntReturnNullWhenUninitialized() throws Exception {
Registry registry = new Registry.Builder().setTldStr("foo").build();
Registry registry = newRegistry("foo", "FOO");
assertThat(registry.getReservedLists()).isNotNull();
assertThat(registry.getReservedLists()).isEmpty();
}
@ -307,57 +310,6 @@ public class RegistryTest extends EntityTestCase {
assertThat(registry.getStandardRenewCost(END_OF_TIME)).isEqualTo(Money.of(USD, 11));
}
@Test
public void testIsPremiumDomain() throws Exception {
createTld("example");
Registry registry = Registry.get("example");
assertThat(registry.isPremiumName("poor.example", clock.nowUtc(), "TheRegistrar")).isFalse();
assertThat(registry.isPremiumName("rich.example", clock.nowUtc(), "TheRegistrar")).isTrue();
assertThat(registry.isPremiumName("richer.example", clock.nowUtc(), "TheRegistrar")).isTrue();
}
public void testGetDomainCreateCost() throws Exception {
// The example tld has a premium price for "rich".
createTld("example");
Registry registry = Registry.get("example");
// The default value of 17 is set in createTld().
assertThat(registry.getDomainCreateCost("poor.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 13));
assertThat(registry.getDomainCreateCost("poor.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 26));
assertThat(registry.getDomainCreateCost("rich.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainCreateCost("rich.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
}
@Test
public void testGetDomainRenewCost() throws Exception {
// The example tld has a premium price for "rich".
createTld("example");
Registry registry = Registry.get("example").asBuilder()
.setRenewBillingCostTransitions(ImmutableSortedMap.of(
START_OF_TIME, Money.of(USD, 8),
clock.nowUtc(), Money.of(USD, 10)))
.build();
assertThat(registry.getDomainRenewCost("poor.example", START_OF_TIME, "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 8));
assertThat(registry.getDomainRenewCost("poor.example", START_OF_TIME, "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 16));
assertThat(registry.getDomainRenewCost("poor.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 10));
assertThat(registry.getDomainRenewCost("poor.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 20));
assertThat(registry.getDomainRenewCost("rich.example", START_OF_TIME, "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainRenewCost("rich.example", START_OF_TIME, "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
assertThat(registry.getDomainRenewCost("rich.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(registry.getDomainRenewCost("rich.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
}
@Test
public void testFailure_tldNeverSet() {
thrown.expect(IllegalArgumentException.class, "No registry TLD specified.");
@ -404,6 +356,13 @@ public class RegistryTest extends EntityTestCase {
.build();
}
@Test
public void testFailure_pricingEngineIsRequired() {
thrown.expect(
IllegalArgumentException.class, "All registries must have a configured pricing engine");
new Registry.Builder().setTldStr("invalid").build();
}
@Test
public void testFailure_negativeRenewBillingCostTransitionValue() {
thrown.expect(IllegalArgumentException.class, "billing cost cannot be negative");
@ -454,45 +413,4 @@ public class RegistryTest extends EntityTestCase {
thrown.expect(IllegalArgumentException.class, "cost must be in the registry's currency");
Registry.get("tld").asBuilder().setServerStatusChangeBillingCost(Money.of(EUR, 42)).build();
}
@Test
public void testFailure_isPremiumNameForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
Registry.get("tld").isPremiumName("test.example", clock.nowUtc(), "TheRegistrar");
}
@Test
public void testFailure_isPremiumNameForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
Registry.get("example").isPremiumName("rich.sld.example", clock.nowUtc(), "TheRegistrar");
}
@Test
public void testFailure_getCreateCostForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
Registry.get("tld").getDomainCreateCost("test.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getCreateCostForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
Registry.get("example")
.getDomainCreateCost("rich.sld.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getRenewCostForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
Registry.get("tld").getDomainRenewCost("test.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getRenewCostForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
Registry.get("example")
.getDomainRenewCost("rich.sld.example", clock.nowUtc(), "TheRegistrar", 1);
}
}

View file

@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.Key;
import google.registry.model.pricing.StaticPremiumListPricingEngine;
import google.registry.model.registry.Registry;
import google.registry.model.registry.label.PremiumList.PremiumListEntry;
import google.registry.model.registry.label.PremiumList.PremiumListRevision;
@ -70,7 +71,11 @@ public class PremiumListTest {
@Test
public void testGetPremiumPrice_returnsNoPriceWhenNoPremiumListConfigured() throws Exception {
createTld("ghost");
persistResource(new Registry.Builder().setTldStr("ghost").build());
persistResource(
new Registry.Builder()
.setTldStr("ghost")
.setPricingEngineClass(StaticPremiumListPricingEngine.class)
.build());
assertThat(Registry.get("ghost").getPremiumList()).isNull();
assertThat(getPremiumPrice("blah", "ghost")).isAbsent();
}

View file

@ -0,0 +1,34 @@
package(
default_testonly = 1,
default_visibility = ["//java/google/registry:registry_project"],
)
licenses(["notice"]) # Apache 2.0
load("//java/com/google/testing/builddefs:GenTestRules.bzl", "GenTestRules")
java_library(
name = "pricing",
srcs = glob(["*Test.java"]),
deps = [
"//java/com/google/common/collect",
"//java/com/google/common/io",
"//java/com/google/common/net",
"//third_party/java/joda_money",
"//third_party/java/joda_time",
"//third_party/java/junit",
"//third_party/java/mockito",
"//third_party/java/truth",
"//java/google/registry/model",
"//java/google/registry/pricing",
"//java/google/registry/util",
"//javatests/google/registry/testing",
],
)
GenTestRules(
name = "GeneratedTestRules",
test_files = glob(["*Test.java"]),
deps = [":pricing"],
)

View file

@ -0,0 +1,182 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.pricing;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.pricing.PricingEngineProxy.getDomainCreateCost;
import static google.registry.pricing.PricingEngineProxy.getDomainRenewCost;
import static google.registry.pricing.PricingEngineProxy.isPremiumName;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistPremiumList;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.ImmutableSortedMap;
import google.registry.model.registry.Registry;
import google.registry.model.registry.label.PremiumList;
import google.registry.testing.AppEngineRule;
import google.registry.testing.ExceptionRule;
import google.registry.testing.FakeClock;
import google.registry.util.Clock;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@link PricingEngineProxy}. */
@RunWith(JUnit4.class)
public class PricingEngineProxyTest {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
.build();
private Clock clock;
@Before
public void before() throws Exception {
PremiumList premiumList = persistPremiumList(
"rich,USD 100",
"richer,USD 999",
"fraction,USD 20.50");
createTld("moka");
persistResource(Registry.get("moka").asBuilder().setPremiumList(premiumList).build());
clock = new FakeClock(DateTime.parse("2016-03-17T12:01:00Z"));
}
@Test
public void test_getDomainCreateCost_multipleYears() {
assertThat(getDomainCreateCost("espresso.moka", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.parse("USD 13"));
assertThat(getDomainCreateCost("espresso.moka", clock.nowUtc(), "TheRegistrar", 5))
.isEqualTo(Money.parse("USD 65"));
assertThat(getDomainCreateCost("fraction.moka", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.parse("USD 20.50"));
assertThat(getDomainCreateCost("fraction.moka", clock.nowUtc(), "TheRegistrar", 3))
.isEqualTo(Money.parse("USD 61.50"));
}
@Test
public void test_getDomainRenewCost_multipleYears() {
assertThat(getDomainRenewCost("espresso.moka", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.parse("USD 11"));
assertThat(getDomainRenewCost("espresso.moka", clock.nowUtc(), "TheRegistrar", 5))
.isEqualTo(Money.parse("USD 55"));
assertThat(getDomainRenewCost("fraction.moka", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.parse("USD 20.50"));
assertThat(getDomainRenewCost("fraction.moka", clock.nowUtc(), "TheRegistrar", 3))
.isEqualTo(Money.parse("USD 61.50"));
}
@Test
public void testIsPremiumDomain() throws Exception {
createTld("example");
assertThat(isPremiumName("poor.example", clock.nowUtc(), "TheRegistrar")).isFalse();
assertThat(isPremiumName("rich.example", clock.nowUtc(), "TheRegistrar")).isTrue();
assertThat(isPremiumName("richer.example", clock.nowUtc(), "TheRegistrar")).isTrue();
}
@Test
public void testGetDomainCreateCost() throws Exception {
// The example tld has a premium price for "rich".
createTld("example");
// The default value of 17 is set in createTld().
assertThat(getDomainCreateCost("poor.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 13));
assertThat(getDomainCreateCost("poor.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 26));
assertThat(getDomainCreateCost("rich.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(getDomainCreateCost("rich.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
}
@Test
public void testGetDomainRenewCost() throws Exception {
// The example tld has a premium price for "rich".
createTld("example");
persistResource(
Registry.get("example")
.asBuilder()
.setRenewBillingCostTransitions(
ImmutableSortedMap.of(
START_OF_TIME, Money.of(USD, 8), clock.nowUtc(), Money.of(USD, 10)))
.build());
assertThat(getDomainRenewCost("poor.example", START_OF_TIME, "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 8));
assertThat(getDomainRenewCost("poor.example", START_OF_TIME, "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 16));
assertThat(getDomainRenewCost("poor.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 10));
assertThat(getDomainRenewCost("poor.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 20));
assertThat(getDomainRenewCost("rich.example", START_OF_TIME, "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(getDomainRenewCost("rich.example", START_OF_TIME, "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
assertThat(getDomainRenewCost("rich.example", clock.nowUtc(), "TheRegistrar", 1))
.isEqualTo(Money.of(USD, 100));
assertThat(getDomainRenewCost("rich.example", clock.nowUtc(), "TheRegistrar", 2))
.isEqualTo(Money.of(USD, 200));
}
@Test
public void testFailure_isPremiumNameForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
isPremiumName("test.example", clock.nowUtc(), "TheRegistrar");
}
@Test
public void testFailure_isPremiumNameForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
isPremiumName("rich.sld.example", clock.nowUtc(), "TheRegistrar");
}
@Test
public void testFailure_getCreateCostForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
getDomainCreateCost("test.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getCreateCostForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
getDomainCreateCost("rich.sld.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getRenewCostForSldNotUnderTld() {
thrown.expect(IllegalArgumentException.class);
getDomainRenewCost("test.example", clock.nowUtc(), "TheRegistrar", 1);
}
@Test
public void testFailure_getRenewCostForSldSubdomain() throws Exception {
createTld("example");
thrown.expect(IllegalArgumentException.class);
getDomainRenewCost("rich.sld.example", clock.nowUtc(), "TheRegistrar", 1);
}
}

View file

@ -52,6 +52,7 @@ java_library(
"//java/google/registry/dns:constants",
"//java/google/registry/flows",
"//java/google/registry/model",
"//java/google/registry/pricing",
"//java/google/registry/request",
"//java/google/registry/tmch",
"//java/google/registry/util",

View file

@ -25,6 +25,7 @@ import static google.registry.model.EppResourceUtils.createContactHostRoid;
import static google.registry.model.EppResourceUtils.createDomainRoid;
import static google.registry.model.domain.launch.ApplicationStatus.VALIDATED;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.pricing.PricingEngineProxy.getDomainRenewCost;
import static google.registry.util.CollectionUtils.difference;
import static google.registry.util.CollectionUtils.union;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
@ -78,6 +79,7 @@ import google.registry.model.index.EppResourceIndex;
import google.registry.model.index.ForeignKeyIndex;
import google.registry.model.ofy.ObjectifyService;
import google.registry.model.poll.PollMessage;
import google.registry.model.pricing.StaticPremiumListPricingEngine;
import google.registry.model.registrar.Registrar;
import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldState;
@ -232,6 +234,7 @@ public class DatastoreHelper {
.setServerStatusChangeBillingCost(Money.of(USD, 19))
// Always set a default premium list. Tests that don't want it can delete it.
.setPremiumList(persistPremiumList(tld, DEFAULT_PREMIUM_LIST_CONTENTS.get()))
.setPricingEngineClass(StaticPremiumListPricingEngine.class)
.build();
}
@ -436,7 +439,7 @@ public class DatastoreHelper {
eventTime.plus(Registry.get(domain.getTld()).getTransferGracePeriodLength()))
.setClientId("NewRegistrar")
.setPeriodYears(extendedRegistrationYears)
.setCost(Registry.get(domain.getTld()).getDomainRenewCost(
.setCost(getDomainRenewCost(
domain.getFullyQualifiedDomainName(),
costLookupTime,
gainingClientId,