mv com/google/domain/registry google/registry

This change renames directories in preparation for the great package
rename. The repository is now in a broken state because the code
itself hasn't been updated. However this should ensure that git
correctly preserves history for each file.
This commit is contained in:
Justine Tunney 2016-05-13 18:55:08 -04:00
parent a41677aea1
commit 5012893c1d
2396 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,222 @@
// 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 com.google.domain.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.domain.registry.model.ofy.ObjectifyService.ofy;
import static com.google.domain.registry.testing.DatastoreHelper.createTld;
import static com.google.domain.registry.testing.DatastoreHelper.persistActiveDomain;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static com.google.domain.registry.util.DateTimeUtils.END_OF_TIME;
import static org.joda.money.CurrencyUnit.USD;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.collect.ImmutableSet;
import com.google.domain.registry.model.EntityTestCase;
import com.google.domain.registry.model.billing.BillingEvent.Flag;
import com.google.domain.registry.model.billing.BillingEvent.Reason;
import com.google.domain.registry.model.domain.DomainResource;
import com.google.domain.registry.model.domain.GracePeriod;
import com.google.domain.registry.model.domain.rgp.GracePeriodStatus;
import com.google.domain.registry.model.reporting.HistoryEntry;
import com.google.domain.registry.testing.ExceptionRule;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/** Unit tests for {@link BillingEvent}. */
public class BillingEventTest extends EntityTestCase {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
private final DateTime now = DateTime.now(UTC);
HistoryEntry historyEntry;
HistoryEntry historyEntry2;
DomainResource domain;
BillingEvent.OneTime oneTime;
BillingEvent.Recurring recurring;
BillingEvent.Cancellation cancellationOneTime;
BillingEvent.Cancellation cancellationRecurring;
BillingEvent.Modification modification;
@Before
public void setUp() throws Exception {
createTld("tld");
domain = persistActiveDomain("foo.tld");
historyEntry = persistResource(
new HistoryEntry.Builder()
.setParent(domain)
.setModificationTime(now)
.build());
historyEntry2 = persistResource(
new HistoryEntry.Builder()
.setParent(domain)
.setModificationTime(now.plusDays(1))
.build());
oneTime = persistResource(commonInit(
new BillingEvent.OneTime.Builder()
.setParent(historyEntry)
.setReason(Reason.CREATE)
.setFlags(ImmutableSet.of(BillingEvent.Flag.ANCHOR_TENANT))
.setPeriodYears(2)
.setCost(Money.of(USD, 1))
.setEventTime(now)
.setBillingTime(now.plusDays(5))));
recurring = persistResource(commonInit(
new BillingEvent.Recurring.Builder()
.setParent(historyEntry)
.setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
.setReason(Reason.RENEW)
.setEventTime(now.plusYears(1))
.setRecurrenceEndTime(END_OF_TIME)));
cancellationOneTime = persistResource(commonInit(
new BillingEvent.Cancellation.Builder()
.setParent(historyEntry2)
.setReason(Reason.CREATE)
.setEventTime(now.plusDays(1))
.setBillingTime(now.plusDays(5))
.setOneTimeEventRef(Ref.create(oneTime))));
cancellationRecurring = persistResource(commonInit(
new BillingEvent.Cancellation.Builder()
.setParent(historyEntry2)
.setReason(Reason.RENEW)
.setEventTime(now.plusDays(1))
.setBillingTime(now.plusYears(1).plusDays(45))
.setRecurringEventRef(Ref.create(recurring))));
modification = persistResource(commonInit(
new BillingEvent.Modification.Builder()
.setParent(historyEntry2)
.setReason(Reason.CREATE)
.setCost(Money.of(USD, 1))
.setDescription("Something happened")
.setEventTime(now.plusDays(1))
.setEventRef(Ref.create(oneTime))));
}
private <E extends BillingEvent, B extends BillingEvent.Builder<E, B>> E commonInit(B builder) {
return builder
.setClientId("a registrar")
.setTargetId("foo.tld")
.build();
}
@Test
public void testPersistence() throws Exception {
assertThat(ofy().load().entity(oneTime).now()).isEqualTo(oneTime);
assertThat(ofy().load().entity(recurring).now()).isEqualTo(recurring);
assertThat(ofy().load().entity(cancellationOneTime).now()).isEqualTo(cancellationOneTime);
assertThat(ofy().load().entity(cancellationRecurring).now()).isEqualTo(cancellationRecurring);
assertThat(ofy().load().entity(modification).now()).isEqualTo(modification);
}
@Test
public void testParenting() throws Exception {
// Note that these are all tested separately because BillingEvent is an abstract base class that
// lacks the @Entity annotation, and thus we cannot call .type(BillingEvent.class)
assertThat(ofy().load().type(BillingEvent.OneTime.class).ancestor(domain).list())
.containsExactly(oneTime);
assertThat(ofy().load().type(BillingEvent.Recurring.class).ancestor(domain).list())
.containsExactly(recurring);
assertThat(ofy().load().type(BillingEvent.Cancellation.class).ancestor(domain).list())
.containsExactly(cancellationOneTime, cancellationRecurring);
assertThat(ofy().load().type(BillingEvent.Modification.class).ancestor(domain).list())
.containsExactly(modification);
assertThat(ofy().load().type(BillingEvent.OneTime.class).ancestor(historyEntry).list())
.containsExactly(oneTime);
assertThat(ofy().load().type(BillingEvent.Recurring.class).ancestor(historyEntry).list())
.containsExactly(recurring);
assertThat(ofy().load().type(BillingEvent.Cancellation.class).ancestor(historyEntry2).list())
.containsExactly(cancellationOneTime, cancellationRecurring);
assertThat(ofy().load().type(BillingEvent.Modification.class).ancestor(historyEntry2).list())
.containsExactly(modification);
}
@Test
public void testIndexing() throws Exception {
verifyIndexing(oneTime, "clientId", "eventTime", "billingTime");
verifyIndexing(
recurring, "clientId", "eventTime", "recurrenceEndTime", "recurrenceTimeOfYear.timeString");
verifyIndexing(cancellationOneTime, "clientId", "eventTime", "billingTime");
verifyIndexing(modification, "clientId", "eventTime");
}
@Test
public void testSuccess_cancellation_forGracePeriod_withOneTime() {
BillingEvent.Cancellation newCancellation = BillingEvent.Cancellation.forGracePeriod(
GracePeriod.forBillingEvent(GracePeriodStatus.ADD, oneTime),
historyEntry2,
"foo.tld");
// Set ID to be the same to ignore for the purposes of comparison.
newCancellation = newCancellation.asBuilder().setId(cancellationOneTime.getId()).build();
assertThat(newCancellation).isEqualTo(cancellationOneTime);
}
@Test
public void testSuccess_cancellation_forGracePeriod_withRecurring() {
BillingEvent.Cancellation newCancellation = BillingEvent.Cancellation.forGracePeriod(
GracePeriod.createForRecurring(
GracePeriodStatus.AUTO_RENEW,
now.plusYears(1).plusDays(45),
"a registrar",
Ref.create(recurring)),
historyEntry2,
"foo.tld");
// Set ID to be the same to ignore for the purposes of comparison.
newCancellation = newCancellation.asBuilder().setId(cancellationRecurring.getId()).build();
assertThat(newCancellation).isEqualTo(cancellationRecurring);
}
@Test
public void testFailure_cancellation_forGracePeriodWithoutBillingEvent() {
thrown.expect(IllegalArgumentException.class, "grace period without billing event");
BillingEvent.Cancellation.forGracePeriod(
GracePeriod.createWithoutBillingEvent(
GracePeriodStatus.REDEMPTION,
now.plusDays(1),
"a registrar"),
historyEntry,
"foo.tld");
}
@Test
public void testFailure_cancellationWithNoBillingEvent() {
thrown.expect(IllegalStateException.class, "exactly one billing event");
cancellationOneTime.asBuilder().setOneTimeEventRef(null).setRecurringEventRef(null).build();
}
@Test
public void testFailure_cancellationWithBothBillingEvents() {
thrown.expect(IllegalStateException.class, "exactly one billing event");
cancellationOneTime.asBuilder()
.setOneTimeEventRef(Ref.create(oneTime))
.setRecurringEventRef(Ref.create(recurring))
.build();
}
@Test
public void testDeadCodeThatDeletedScrapCommandsReference() throws Exception {
assertThat(recurring.getParentKey()).isEqualTo(Key.create(historyEntry));
new BillingEvent.OneTime.Builder().setParent(Key.create(historyEntry));
}
}

View file

@ -0,0 +1,189 @@
// 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 com.google.domain.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.ImmutableMap;
import com.google.domain.registry.model.EntityTestCase;
import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.testing.ExceptionRule;
import com.googlecode.objectify.Key;
import org.joda.money.CurrencyMismatchException;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link RegistrarBillingEntry}. */
@RunWith(JUnit4.class)
public final class RegistrarBillingEntryTest extends EntityTestCase {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
@Test
public void testIndexing() throws Exception {
verifyIndexing(
persistResource(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setTransactionId("goblin-market")
.setDescription("USD Invoice for December 1984")
.setAmount(Money.parse("USD 10.00"))
.build()),
"currency",
"created");
}
@Test
public void testGetters() throws Exception {
RegistrarBillingEntry entry =
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setTransactionId("goblin-market")
.setDescription("USD Invoice for December 1984")
.setAmount(Money.parse("USD 10.00"))
.build();
assertThat(entry.getId()).isEqualTo(1L);
assertThat(entry.getParent()).isEqualTo(Key.create(Registrar.loadByClientId("NewRegistrar")));
assertThat(entry.getCreated()).isEqualTo(DateTime.parse("1984-12-18TZ"));
assertThat(entry.getTransactionId()).isEqualTo("goblin-market");
assertThat(entry.getDescription()).isEqualTo("USD Invoice for December 1984");
assertThat(entry.getAmount()).isEqualTo(Money.parse("USD 10.00"));
assertThat(entry.getBalance()).isEqualTo(Money.parse("USD 10.00"));
}
@Test
public void testToJsonMap() throws Exception {
assertThat(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setTransactionId("goblin-market")
.setDescription("USD Invoice for December 1984")
.setAmount(Money.parse("USD 10.00"))
.build()
.toJsonMap())
.isEqualTo(
new ImmutableMap.Builder<String, Object>()
.put("id", 1L)
.put("transactionId", "goblin-market")
.put("created", "1984-12-18T00:00:00.000Z")
.put("description", "USD Invoice for December 1984")
.put("currency", "USD")
.put("amount", "10.00")
.put("balance", "10.00")
.build());
}
@Test
public void testBadTimeOrdering_causesError() throws Exception {
thrown.expect(IllegalStateException.class, "Created timestamp not after previous");
new RegistrarBillingEntry.Builder()
.setPrevious(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.parse("USD 10.00"))
.build())
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-17TZ"))
.setTransactionId("goblin")
.setDescription("USD Invoice for August")
.setAmount(Money.parse("USD 3.50"))
.build();
}
@Test
public void testRegistrarMismatch_causesError() throws Exception {
thrown.expect(IllegalStateException.class, "Parent not same as previous");
new RegistrarBillingEntry.Builder()
.setPrevious(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.parse("USD 10.00"))
.build())
.setParent(Registrar.loadByClientId("TheRegistrar"))
.setCreated(DateTime.parse("1984-12-17TZ"))
.setTransactionId("goblin")
.setDescription("USD Invoice for August")
.setAmount(Money.parse("USD 3.50"))
.build();
}
@Test
public void testCurrencyMismatch_causesError() throws Exception {
thrown.expect(CurrencyMismatchException.class);
new RegistrarBillingEntry.Builder()
.setPrevious(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.parse("USD 10.00"))
.build())
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-17TZ"))
.setTransactionId("goblin")
.setDescription("JPY Invoice for August")
.setAmount(Money.parse("JPY 350"))
.build();
}
@Test
public void testZeroAmount_causesError() throws Exception {
thrown.expect(IllegalArgumentException.class, "Amount can't be zero");
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December")
.setAmount(Money.zero(USD))
.build();
}
@Test
public void testEmptyTransactionId_becomeNull() {
assertThat(
new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(Registrar.loadByClientId("NewRegistrar"))
.setTransactionId("")
.setCreated(DateTime.parse("1984-12-18TZ"))
.setDescription("USD Invoice for December 1984")
.setAmount(Money.parse("USD 10.00"))
.build()
.getTransactionId())
.isNull();
}
}

View file

@ -0,0 +1,144 @@
// 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 com.google.domain.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.domain.registry.testing.DatastoreHelper.createTlds;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static com.google.domain.registry.testing.DatastoreHelper.persistSimpleGlobalResources;
import static com.google.domain.registry.util.DateTimeUtils.START_OF_TIME;
import static java.util.Arrays.asList;
import static org.joda.money.CurrencyUnit.JPY;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.ImmutableSortedMap;
import com.google.domain.registry.model.ofy.Ofy;
import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.model.registry.Registry;
import com.google.domain.registry.testing.AppEngineRule;
import com.google.domain.registry.testing.FakeClock;
import com.google.domain.registry.testing.InjectRule;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.Map;
/** Unit tests for {@link RegistrarBillingUtils}. */
@RunWith(JUnit4.class)
public final class RegistrarBillingUtilsTest {
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule
public InjectRule inject = new InjectRule();
private final FakeClock clock = new FakeClock(DateTime.parse("1984-12-18TZ"));
private Registrar registrar;
@Before
public void before() throws Exception {
inject.setStaticField(Ofy.class, "clock", clock);
registrar = Registrar.loadByClientId("NewRegistrar");
createTlds("xn--q9jyb4c", "com", "net");
persistResource(
Registry.get("xn--q9jyb4c").asBuilder()
.setCurrency(JPY)
.setRenewBillingCostTransitions(
ImmutableSortedMap.of(START_OF_TIME, Money.parse("JPY 110")))
.setCreateBillingCost(Money.parse("JPY 130"))
.setRestoreBillingCost(Money.parse("JPY 170"))
.setServerStatusChangeBillingCost(Money.parse("JPY 190"))
.build());
}
@Test
public void testGetCurrencies_returnsAllCurrenciesEnabledOnRegistrySorted() {
assertThat(RegistrarBillingUtils.getCurrencies())
.containsExactly(JPY, USD)
.inOrder();
}
@Test
public void testLoadBalance_noHistory_returnsZeroes() {
Map<CurrencyUnit, Money> balance = RegistrarBillingUtils.loadBalance(registrar);
assertThat(balance).hasSize(2);
assertThat(balance).containsEntry(USD, Money.parse("USD 0.00"));
assertThat(balance).containsEntry(JPY, Money.parse("JPY 0"));
}
@Test
public void testLoadBalance_oneCurrency_hasTwoEntriesWithSumAndZero() {
RegistrarBillingEntry entry1 = new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(registrar)
.setCreated(clock.nowUtc())
.setDescription("USD Invoice for July")
.setAmount(Money.parse("USD 10.00"))
.build();
clock.advanceBy(Duration.standardDays(30));
RegistrarBillingEntry entry2 = new RegistrarBillingEntry.Builder()
.setPrevious(entry1)
.setParent(registrar)
.setCreated(clock.nowUtc())
.setDescription("USD Invoice for August")
.setAmount(Money.parse("USD 23.00"))
.build();
persistSimpleGlobalResources(asList(entry1, entry2));
Map<CurrencyUnit, Money> balance = RegistrarBillingUtils.loadBalance(registrar);
assertThat(balance).hasSize(2);
assertThat(balance).containsEntry(USD, Money.parse("USD 33.00"));
assertThat(balance).containsEntry(JPY, Money.parse("JPY 0"));
}
@Test
public void testLoadBalance_twoCurrencies_hasTwoEntriesWithSum() {
RegistrarBillingEntry entry1 = new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(registrar)
.setCreated(clock.nowUtc())
.setDescription("USD Invoice for July")
.setAmount(Money.parse("USD 10.00"))
.build();
clock.advanceBy(Duration.standardDays(30));
RegistrarBillingEntry entry2 = new RegistrarBillingEntry.Builder()
.setPrevious(entry1)
.setParent(registrar)
.setCreated(clock.nowUtc())
.setDescription("USD Invoice for August")
.setAmount(Money.parse("USD 3.50"))
.build();
RegistrarBillingEntry entry3 = new RegistrarBillingEntry.Builder()
.setPrevious(null)
.setParent(registrar)
.setCreated(clock.nowUtc())
.setDescription("JPY Invoice for August")
.setAmount(Money.parse("JPY 666"))
.build();
persistSimpleGlobalResources(asList(entry1, entry2, entry3));
Map<CurrencyUnit, Money> balance = RegistrarBillingUtils.loadBalance(registrar);
assertThat(balance).hasSize(2);
assertThat(balance).containsEntry(USD, Money.parse("USD 13.50"));
assertThat(balance).containsEntry(JPY, Money.parse("JPY 666"));
}
}

View file

@ -0,0 +1,190 @@
// 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 com.google.domain.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.domain.registry.model.ofy.ObjectifyService.ofy;
import static com.google.domain.registry.testing.DatastoreHelper.createTld;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static com.google.domain.registry.util.DateTimeUtils.END_OF_TIME;
import static com.google.domain.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.collect.ImmutableMap;
import com.google.domain.registry.model.EntityTestCase;
import com.google.domain.registry.model.billing.RegistrarCredit.CreditType;
import com.google.domain.registry.model.billing.RegistrarCreditBalance.BalanceMap;
import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.testing.ExceptionRule;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.Map;
/** Unit tests for {@link RegistrarCreditBalance}. */
public class RegistrarCreditBalanceTest extends EntityTestCase {
@Rule
public ExceptionRule thrown = new ExceptionRule();
private DateTime then = clock.nowUtc().plusDays(1);
private Registrar theRegistrar;
private RegistrarCredit unpersistedCredit;
private RegistrarCredit credit;
private RegistrarCreditBalance balance;
Map<DateTime, ? extends Map<DateTime, Money>> rawBalanceMap;
@Before
public void setUp() throws Exception {
createTld("tld");
theRegistrar = Registrar.loadByClientId("TheRegistrar");
unpersistedCredit = makeCredit(theRegistrar, clock.nowUtc());
credit = persistResource(makeCredit(theRegistrar, clock.nowUtc()));
balance = persistResource(
makeBalance(credit, Money.parse("USD 90"), clock.nowUtc(), clock.nowUtc()));
persistResource(
makeBalance(credit, Money.parse("USD 80"), clock.nowUtc(), clock.nowUtc().plusMillis(1)));
persistResource(
makeBalance(credit, Money.parse("USD 70"), clock.nowUtc(), clock.nowUtc().plusMillis(2)));
persistResource(
makeBalance(credit, Money.parse("USD 40"), then, then));
persistResource(
makeBalance(credit, Money.parse("USD 30"), then, then.plusMillis(1)));
persistResource(
makeBalance(credit, Money.parse("USD 20"), then, then.plusMillis(2)));
rawBalanceMap = ImmutableMap.of(
clock.nowUtc(),
ImmutableMap.of(
clock.nowUtc(), Money.parse("USD 90"),
clock.nowUtc().plusMillis(1), Money.parse("USD 80"),
clock.nowUtc().plusMillis(2), Money.parse("USD 70")),
then,
ImmutableMap.of(
then, Money.parse("USD 40"),
then.plusMillis(1), Money.parse("USD 30"),
then.plusMillis(2), Money.parse("USD 20")));
}
@Test
public void testPersistence() throws Exception {
assertThat(ofy().load().entity(balance).now()).isEqualTo(balance);
}
@Test
public void testIndexing() throws Exception {
// No indexing needed, so we don't expect any indices.
verifyIndexing(balance);
}
@Test
public void testSuccess_balanceWithUnpersistedCredit() throws Exception {
balance.asBuilder().setParent(unpersistedCredit).build();
}
@Test
public void testFailure_balanceNotInCreditCurrency() throws Exception {
thrown.expect(IllegalStateException.class);
balance.asBuilder()
.setAmount(Money.parse("JPY 1"))
.build();
}
@Test
public void testFailure_balanceNotInCreditCurrencyWithUnpersistedCredit() throws Exception {
thrown.expect(IllegalStateException.class);
balance.asBuilder()
.setParent(unpersistedCredit)
.setAmount(Money.parse("JPY 1"))
.build();
}
@Test
public void testSuccess_balanceMap_createForCredit() throws Exception {
assertThat(BalanceMap.createForCredit(credit)).isEqualTo(rawBalanceMap);
}
@Test
public void testSuccess_balanceMap_createForEmptyCredit() throws Exception {
assertThat(BalanceMap.createForCredit(makeCredit(theRegistrar, clock.nowUtc()))).isEmpty();
}
@Test
public void testSuccess_balanceMap_getActiveBalance_emptyMap() throws Exception {
BalanceMap map = new BalanceMap(ImmutableMap.<DateTime, Map<DateTime, Money>>of());
assertThat(map.getActiveBalanceAtTime(START_OF_TIME)).isAbsent();
assertThat(map.getActiveBalanceAtTime(clock.nowUtc())).isAbsent();
assertThat(map.getActiveBalanceAtTime(END_OF_TIME)).isAbsent();
assertThat(map.getActiveBalanceBeforeTime(START_OF_TIME)).isAbsent();
assertThat(map.getActiveBalanceBeforeTime(clock.nowUtc())).isAbsent();
assertThat(map.getActiveBalanceBeforeTime(END_OF_TIME)).isAbsent();
}
@Test
public void testSuccess_balanceMap_getActiveBalanceAtTime() throws Exception {
BalanceMap map = new BalanceMap(rawBalanceMap);
assertThat(map.getActiveBalanceAtTime(START_OF_TIME)).isAbsent();
assertThat(map.getActiveBalanceAtTime(clock.nowUtc().minusMillis(1))).isAbsent();
assertThat(map.getActiveBalanceAtTime(clock.nowUtc()).get()).isEqualTo(Money.parse("USD 70"));
assertThat(map.getActiveBalanceAtTime(clock.nowUtc().plusMillis(1)).get())
.isEqualTo(Money.parse("USD 70"));
assertThat(map.getActiveBalanceAtTime(then.minusMillis(1)).get())
.isEqualTo(Money.parse("USD 70"));
assertThat(map.getActiveBalanceAtTime(then).get()).isEqualTo(Money.parse("USD 20"));
assertThat(map.getActiveBalanceAtTime(then.plusMillis(1)).get())
.isEqualTo(Money.parse("USD 20"));
assertThat(map.getActiveBalanceAtTime(END_OF_TIME).get()).isEqualTo(Money.parse("USD 20"));
}
@Test
public void testSuccess_balanceMap_getActiveBalanceBeforeTime() throws Exception {
BalanceMap map = new BalanceMap(rawBalanceMap);
assertThat(map.getActiveBalanceBeforeTime(START_OF_TIME)).isAbsent();
assertThat(map.getActiveBalanceBeforeTime(clock.nowUtc().minusMillis(1))).isAbsent();
assertThat(map.getActiveBalanceBeforeTime(clock.nowUtc())).isAbsent();
assertThat(map.getActiveBalanceBeforeTime(clock.nowUtc().plusMillis(1)).get())
.isEqualTo(Money.parse("USD 70"));
assertThat(map.getActiveBalanceBeforeTime(then.minusMillis(1)).get())
.isEqualTo(Money.parse("USD 70"));
assertThat(map.getActiveBalanceBeforeTime(then).get()).isEqualTo(Money.parse("USD 70"));
assertThat(map.getActiveBalanceBeforeTime(then.plusMillis(1)).get())
.isEqualTo(Money.parse("USD 20"));
assertThat(map.getActiveBalanceBeforeTime(END_OF_TIME).get()).isEqualTo(Money.parse("USD 20"));
}
private static RegistrarCredit makeCredit(Registrar parent, DateTime creationTime) {
return new RegistrarCredit.Builder()
.setParent(parent)
.setType(CreditType.PROMOTION)
.setCurrency(CurrencyUnit.USD)
.setTld("tld")
.setCreationTime(creationTime)
.build();
}
private static RegistrarCreditBalance makeBalance(
RegistrarCredit parent, Money amount, DateTime effectiveTime, DateTime writtenTime) {
return new RegistrarCreditBalance.Builder()
.setParent(parent)
.setEffectiveTime(effectiveTime)
.setAmount(amount)
.setWrittenTime(writtenTime)
.build();
}
}

View file

@ -0,0 +1,101 @@
// 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 com.google.domain.registry.model.billing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.domain.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static com.google.domain.registry.model.ofy.ObjectifyService.ofy;
import static com.google.domain.registry.testing.DatastoreHelper.createTld;
import static com.google.domain.registry.testing.DatastoreHelper.persistResource;
import static org.joda.money.CurrencyUnit.JPY;
import static org.joda.money.CurrencyUnit.USD;
import com.google.domain.registry.model.EntityTestCase;
import com.google.domain.registry.model.billing.RegistrarCredit.CreditType;
import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.model.registry.Registry;
import com.google.domain.registry.testing.ExceptionRule;
import org.joda.money.CurrencyUnit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/** Unit tests for {@link RegistrarCredit}. */
public class RegistrarCreditTest extends EntityTestCase {
@Rule
public ExceptionRule thrown = new ExceptionRule();
private RegistrarCredit auctionCredit;
private RegistrarCredit promoCredit;
@Before
public void setUp() throws Exception {
createTld("tld");
Registrar theRegistrar = ofy().load()
.type(Registrar.class)
.parent(getCrossTldKey())
.id("TheRegistrar").now();
auctionCredit = persistResource(
new RegistrarCredit.Builder()
.setParent(theRegistrar)
.setType(CreditType.AUCTION)
.setCurrency(CurrencyUnit.USD)
.setTld("tld")
.setCreationTime(clock.nowUtc())
.build());
promoCredit = persistResource(
new RegistrarCredit.Builder()
.setParent(theRegistrar)
.setType(CreditType.PROMOTION)
.setCurrency(CurrencyUnit.USD)
.setTld("tld")
.setCreationTime(clock.nowUtc())
.build());
}
@Test
public void testPersistence() throws Exception {
assertThat(ofy().load().entity(auctionCredit).now()).isEqualTo(auctionCredit);
assertThat(ofy().load().entity(promoCredit).now()).isEqualTo(promoCredit);
}
@Test
public void testIndexing() throws Exception {
// No indexing needed, so we don't expect any indices.
verifyIndexing(auctionCredit);
verifyIndexing(promoCredit);
}
@Test
public void testFailure_missingTld() throws Exception {
thrown.expect(NullPointerException.class, "tld");
promoCredit.asBuilder().setTld(null).build();
}
@Test
public void testFailure_NonexistentTld() throws Exception {
thrown.expect(IllegalArgumentException.class, "example");
promoCredit.asBuilder().setTld("example").build();
}
@Test
public void testFailure_CurrencyDoesNotMatchTldCurrency() throws Exception {
thrown.expect(IllegalArgumentException.class, "currency");
assertThat(Registry.get("tld").getCurrency()).isEqualTo(USD);
promoCredit.asBuilder().setTld("tld").setCurrency(JPY).build();
}
}