Remove the ofy().load() inside of HostResource.cloneProjectedAtTime

In fact, completely eviscerate cloneProjectedAtTime (to be removed in
a followup CL) in favor of doing the projection of transfers and the
loading of values from the superordinate domain at call sites. This
is one of the issues that blocked the memcache audit work, since the
load inside of cloneProjectedAtTime could not be controlled by the
caller.

Note: fixed a minor bug where a subordinate host created after its superordinate domain was last transferred should have lastTransferTime==null but was previously reporting the domain's lastTransferTime.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149769125
This commit is contained in:
cgoldfeder 2017-03-10 10:05:26 -08:00 committed by Ben McIlwain
parent 1f000b94e6
commit 9174855a47
67 changed files with 970 additions and 471 deletions

View file

@ -30,14 +30,11 @@ import google.registry.model.billing.BillingEvent;
import google.registry.model.domain.DomainResource;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.eppcommon.Trid;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.transfer.TransferData;
import google.registry.model.transfer.TransferData.TransferServerApproveEntity;
import google.registry.model.transfer.TransferStatus;
import google.registry.testing.ExceptionRule;
import java.net.InetAddress;
import javax.annotation.Nullable;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
@ -49,13 +46,18 @@ public class HostResourceTest extends EntityTestCase {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
HostResource hostResource;
final DateTime day3 = clock.nowUtc();
final DateTime day2 = day3.minusDays(1);
final DateTime day1 = day2.minusDays(1);
DomainResource domain;
HostResource host;
@Before
public void setUp() throws Exception {
createTld("com");
// Set up a new persisted registrar entity.
persistResource(
domain = persistResource(
newDomainResource("example.com").asBuilder()
.setRepoId("1-COM")
.setTransferData(new TransferData.Builder()
@ -71,7 +73,7 @@ public class HostResourceTest extends EntityTestCase {
.setTransferRequestTrid(Trid.create("client trid"))
.build())
.build());
hostResource =
host =
persistResource(
cloneAndSetAutoTimestamps(
new HostResource.Builder()
@ -83,17 +85,15 @@ public class HostResourceTest extends EntityTestCase {
.setLastTransferTime(clock.nowUtc())
.setInetAddresses(ImmutableSet.of(InetAddresses.forString("127.0.0.1")))
.setStatusValues(ImmutableSet.of(StatusValue.OK))
.setSuperordinateDomain(
Key.create(
loadByForeignKey(DomainResource.class, "example.com", clock.nowUtc())))
.setSuperordinateDomain(Key.create(domain))
.build()));
}
@Test
public void testPersistence() throws Exception {
assertThat(loadByForeignKey(
HostResource.class, hostResource.getForeignKey(), clock.nowUtc()))
.isEqualTo(hostResource.cloneProjectedAtTime(clock.nowUtc()));
HostResource.class, host.getForeignKey(), clock.nowUtc()))
.isEqualTo(host);
}
@Test
@ -101,7 +101,7 @@ public class HostResourceTest extends EntityTestCase {
// Clone it and save it before running the indexing test so that its transferData fields are
// populated from the superordinate domain.
verifyIndexing(
persistResource(hostResource.cloneProjectedAtTime(clock.nowUtc())),
persistResource(host),
"deletionTime",
"fullyQualifiedHostName",
"inetAddresses",
@ -111,30 +111,17 @@ public class HostResourceTest extends EntityTestCase {
@Test
public void testEmptyStringsBecomeNull() {
assertThat(new HostResource.Builder().setCurrentSponsorClientId(null).build()
.getCurrentSponsorClientId())
assertThat(new HostResource.Builder().setPersistedCurrentSponsorClientId(null).build()
.getPersistedCurrentSponsorClientId())
.isNull();
assertThat(new HostResource.Builder().setCurrentSponsorClientId("").build()
.getCurrentSponsorClientId())
assertThat(new HostResource.Builder().setPersistedCurrentSponsorClientId("").build()
.getPersistedCurrentSponsorClientId())
.isNull();
assertThat(new HostResource.Builder().setCurrentSponsorClientId(" ").build()
.getCurrentSponsorClientId())
assertThat(new HostResource.Builder().setPersistedCurrentSponsorClientId(" ").build()
.getPersistedCurrentSponsorClientId())
.isNotNull();
}
@Test
public void testCurrentSponsorClientId_comesFromSuperordinateDomain() {
assertThat(hostResource.getCurrentSponsorClientId()).isNull();
HostResource projectedHost =
loadByForeignKey(HostResource.class, hostResource.getForeignKey(), clock.nowUtc());
assertThat(projectedHost.getCurrentSponsorClientId())
.isEqualTo(loadByForeignKey(
DomainResource.class,
"example.com",
clock.nowUtc())
.getCurrentSponsorClientId());
}
@Test
public void testEmptySetsBecomeNull() throws Exception {
assertThat(new HostResource.Builder().setInetAddresses(null).build().inetAddresses).isNull();
@ -169,113 +156,108 @@ public class HostResourceTest extends EntityTestCase {
.hasExactlyStatusValues(StatusValue.CLIENT_HOLD);
}
@Nullable
private DateTime runCloneProjectedAtTimeTest(
@Nullable DateTime domainTransferTime,
@Nullable DateTime hostTransferTime,
@Nullable DateTime superordinateChangeTime) {
DomainResource domain = loadByForeignKey(
DomainResource.class, "example.com", clock.nowUtc());
persistResource(
domain.asBuilder().setTransferData(null).setLastTransferTime(domainTransferTime).build());
hostResource = persistResource(
hostResource.asBuilder()
.setLastSuperordinateChange(superordinateChangeTime)
.setLastTransferTime(hostTransferTime)
.build());
return hostResource.cloneProjectedAtTime(clock.nowUtc()).getLastTransferTime();
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeComesOffHostWhenTransferredMoreRecently() {
assertThat(runCloneProjectedAtTimeTest(
clock.nowUtc().minusDays(10), clock.nowUtc().minusDays(2), clock.nowUtc().minusDays(1)))
.isEqualTo(clock.nowUtc().minusDays(2));
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeNullWhenAllTransfersAreNull() {
assertThat(runCloneProjectedAtTimeTest(null, null, null)).isNull();
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeComesOffHostWhenTimeOnDomainIsNull() {
assertThat(runCloneProjectedAtTimeTest(null, clock.nowUtc().minusDays(30), null))
.isEqualTo(clock.nowUtc().minusDays(30));
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeIsNullWhenHostMovedAfterDomainTransferred() {
assertThat(runCloneProjectedAtTimeTest(
clock.nowUtc().minusDays(30), null, clock.nowUtc().minusDays(20)))
.isNull();
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeComesOffDomainWhenTimeOnHostIsNull() {
assertThat(runCloneProjectedAtTimeTest(clock.nowUtc().minusDays(5), null, null))
.isEqualTo(clock.nowUtc().minusDays(5));
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeComesOffDomainWhenLastMoveIsntNull() {
assertThat(runCloneProjectedAtTimeTest(
clock.nowUtc().minusDays(5), null, clock.nowUtc().minusDays(10)))
.isEqualTo(clock.nowUtc().minusDays(5));
}
@Test
public void testCloneProjectedAtTime_lastTransferTimeComesOffDomainWhenThatIsMostRecent() {
assertThat(runCloneProjectedAtTimeTest(
clock.nowUtc().minusDays(5), clock.nowUtc().minusDays(20), clock.nowUtc().minusDays(10)))
.isEqualTo(clock.nowUtc().minusDays(5));
}
@Test
public void testExpiredTransfer_subordinateHost() {
DomainResource domain = loadByForeignKey(
DomainResource.class, "example.com", clock.nowUtc());
persistResource(domain.asBuilder()
.setTransferData(domain.getTransferData().asBuilder()
.setTransferStatus(TransferStatus.PENDING)
.setPendingTransferExpirationTime(clock.nowUtc().plusDays(1))
.setGainingClientId("winner")
.setExtendedRegistrationYears(2)
.setServerApproveBillingEvent(Key.create(
new BillingEvent.OneTime.Builder()
.setParent(new HistoryEntry.Builder().setParent(domain).build())
.setCost(Money.parse("USD 100"))
.setBillingTime(clock.nowUtc().plusYears(2))
.setReason(BillingEvent.Reason.TRANSFER)
.setClientId("TheRegistrar")
.setTargetId("example.com")
.setEventTime(clock.nowUtc().plusYears(2))
.setPeriodYears(2)
.build()))
.build())
.build());
HostResource afterTransfer = hostResource.cloneProjectedAtTime(clock.nowUtc().plusDays(1));
assertThat(afterTransfer.getCurrentSponsorClientId()).isEqualTo("winner");
assertThat(afterTransfer.getLastTransferTime()).isEqualTo(clock.nowUtc().plusDays(1));
}
@Test
public void testToHydratedString_notCircular() {
// If there are circular references, this will overflow the stack.
hostResource.toHydratedString();
host.toHydratedString();
}
@Test
public void testFailure_uppercaseHostName() {
thrown.expect(
IllegalArgumentException.class, "Host name must be in puny-coded, lower-case form");
hostResource.asBuilder().setFullyQualifiedHostName("AAA.BBB.CCC");
host.asBuilder().setFullyQualifiedHostName("AAA.BBB.CCC");
}
@Test
public void testFailure_utf8HostName() {
thrown.expect(
IllegalArgumentException.class, "Host name must be in puny-coded, lower-case form");
hostResource.asBuilder().setFullyQualifiedHostName("みんな.みんな.みんな");
host.asBuilder().setFullyQualifiedHostName("みんな.みんな.みんな");
}
@Test
public void testComputeLastTransferTime_hostNeverSwitchedDomains_domainWasNeverTransferred() {
domain = domain.asBuilder().setLastTransferTime(null).build();
host = host.asBuilder()
.setLastTransferTime(null)
.setLastSuperordinateChange(null)
.build();
assertThat(host.computeLastTransferTime(domain)).isNull();
}
@Test
public void testComputeLastTransferTime_hostNeverSwitchedDomains_domainWasTransferred() {
// Host was created on Day 1.
// Domain was transferred on Day 2.
// Host was always subordinate to domain (and was created before the transfer).
domain = domain.asBuilder().setLastTransferTime(day2).build();
host = host.asBuilder()
.setCreationTimeForTest(day1)
.setLastTransferTime(null)
.setLastSuperordinateChange(null)
.build();
assertThat(host.computeLastTransferTime(domain)).isEqualTo(day2);
}
@Test
public void testComputeLastTransferTime_hostCreatedAfterDomainWasTransferred() {
// Domain was transferred on Day 1.
// Host was created subordinate to domain on Day 2.
domain = domain.asBuilder().setLastTransferTime(day1).build();
host =
persistResource(
cloneAndSetAutoTimestamps(
new HostResource.Builder()
.setCreationTime(day2)
.setRepoId("DEADBEEF-COM")
.setFullyQualifiedHostName("ns1.example.com")
.setCreationClientId("a registrar")
.setLastEppUpdateTime(clock.nowUtc())
.setLastEppUpdateClientId("another registrar")
.setInetAddresses(ImmutableSet.of(InetAddresses.forString("127.0.0.1")))
.setStatusValues(ImmutableSet.of(StatusValue.OK))
.setSuperordinateDomain(Key.create(domain))
.build()));
assertThat(host.computeLastTransferTime(domain)).isNull();
}
@Test
public void testComputeLastTransferTime_hostWasTransferred_domainWasNeverTransferred() {
// Host was transferred on Day 1.
// Host was made subordinate to domain on Day 2.
// Domain was never transferred.
domain = domain.asBuilder().setLastTransferTime(null).build();
host = host.asBuilder()
.setLastTransferTime(day1)
.setLastSuperordinateChange(day2)
.build();
assertThat(host.computeLastTransferTime(domain)).isEqualTo(day1);
}
@Test
public void testComputeLastTransferTime_domainWasTransferredBeforeHostBecameSubordinate() {
// Host was transferred on Day 1.
// Domain was transferred on Day 2.
// Host was made subordinate to domain on Day 3.
domain = domain.asBuilder().setLastTransferTime(day2).build();
host = host.asBuilder()
.setLastTransferTime(day1)
.setLastSuperordinateChange(day3)
.build();
assertThat(host.computeLastTransferTime(domain)).isEqualTo(day1);
}
@Test
public void testComputeLastTransferTime_domainWasTransferredAfterHostBecameSubordinate() {
// Host was transferred on Day 1.
// Host was made subordinate to domain on Day 2.
// Domain was transferred on Day 3.
domain = domain.asBuilder().setLastTransferTime(day3).build();
host = host.asBuilder()
.setLastTransferTime(day1)
.setLastSuperordinateChange(day2)
.build();
assertThat(host.computeLastTransferTime(domain)).isEqualTo(day3);
}
}