Remove temporary @OnLoad used for setting period in DomainApplication

Now that the registration period has been added to DomainApplication, we
can remove this @OnLoad that was populating it for objects that were
missing the period.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159464438
This commit is contained in:
Ben McIlwain 2017-06-19 12:53:57 -07:00
parent fc78fd7d4a
commit fd1051f90a
4 changed files with 2 additions and 182 deletions

View file

@ -14,22 +14,15 @@
package google.registry.model.domain; package google.registry.model.domain;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy; import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.googlecode.objectify.annotation.EntitySubclass; import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.OnLoad;
import google.registry.model.annotations.ExternalMessagingName; import google.registry.model.annotations.ExternalMessagingName;
import google.registry.model.domain.launch.ApplicationStatus; import google.registry.model.domain.launch.ApplicationStatus;
import google.registry.model.domain.launch.LaunchPhase; import google.registry.model.domain.launch.LaunchPhase;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.eppinput.EppInput;
import google.registry.model.eppinput.EppInput.ResourceCommandWrapper;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.smd.EncodedSignedMark; import google.registry.model.smd.EncodedSignedMark;
import google.registry.xml.XmlTransformer;
import java.io.ByteArrayInputStream;
import java.util.List; import java.util.List;
import org.joda.money.Money; import org.joda.money.Money;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -66,69 +59,6 @@ public class DomainApplication extends DomainBase {
/** The amount paid at auction for the right to register the domain. Used only for reporting. */ /** The amount paid at auction for the right to register the domain. Used only for reporting. */
Money auctionPrice; Money auctionPrice;
// TODO(b/32447342): remove this once the period has been populated on all DomainApplications
@OnLoad
void setYears() {
if (period == null) {
// Extract the registration period from the XML used to create the domain application.
try {
HistoryEntry history = ofy().load()
.type(HistoryEntry.class)
.ancestor(this)
.order("modificationTime")
.first()
.now();
if (history != null) {
byte[] xmlBytes = history.getXmlBytes();
EppInput eppInput = unmarshal(EppInput.class, xmlBytes);
period = ((DomainCommand.Create)
((ResourceCommandWrapper) eppInput.getCommandWrapper().getCommand())
.getResourceCommand()).getPeriod();
}
} catch (Exception e) {
// If we encounter an exception, give up on this defaulting.
}
}
}
/**
* Unmarshal bytes into Epp classes.
*
* <p>This method, and the things it depends on, are copied from EppXmlTransformer, because that
* class is in the flows directory, and we don't want a build dependency of model on build. It can
* be removed when the @OnLoad method is removed.
*
* @param clazz type to return, specified as a param to enforce typesafe generics
* @see "http://errorprone.info/bugpattern/TypeParameterUnusedInFormals"
*/
private static <T> T unmarshal(Class<T> clazz, byte[] bytes) throws Exception {
return INPUT_TRANSFORMER.unmarshal(clazz, new ByteArrayInputStream(bytes));
}
// Hardcoded XML schemas, ordered with respect to dependency.
private static final ImmutableList<String> SCHEMAS = ImmutableList.of(
"eppcom.xsd",
"epp.xsd",
"contact.xsd",
"host.xsd",
"domain.xsd",
"rgp.xsd",
"secdns.xsd",
"fee06.xsd",
"fee11.xsd",
"fee12.xsd",
"metadata.xsd",
"mark.xsd",
"dsig.xsd",
"smd.xsd",
"launch.xsd",
"allocate.xsd");
private static final XmlTransformer INPUT_TRANSFORMER =
new XmlTransformer(SCHEMAS, EppInput.class);
// End of copied stuff used by @OnLoad method
@Override @Override
public String getFullyQualifiedDomainName() { public String getFullyQualifiedDomainName() {
return fullyQualifiedDomainName; return fullyQualifiedDomainName;

View file

@ -16,7 +16,6 @@ package google.registry.model.domain;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.EppResourceUtils.loadDomainApplication; import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.cloneAndSetAutoTimestamps; import static google.registry.testing.DatastoreHelper.cloneAndSetAutoTimestamps;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newDomainApplication; import static google.registry.testing.DatastoreHelper.newDomainApplication;
@ -24,17 +23,12 @@ import static google.registry.testing.DatastoreHelper.newHostResource;
import static google.registry.testing.DatastoreHelper.persistActiveContact; import static google.registry.testing.DatastoreHelper.persistActiveContact;
import static google.registry.testing.DatastoreHelper.persistActiveHost; import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.FullFieldsTestEntityHelper.makeHistoryEntry;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.money.CurrencyUnit.USD; import static org.joda.money.CurrencyUnit.USD;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.VoidWork;
import google.registry.model.EntityTestCase; import google.registry.model.EntityTestCase;
import google.registry.model.domain.launch.ApplicationStatus; import google.registry.model.domain.launch.ApplicationStatus;
import google.registry.model.domain.launch.LaunchNotice; import google.registry.model.domain.launch.LaunchNotice;
@ -44,11 +38,9 @@ import google.registry.model.eppcommon.AuthInfo.PasswordAuth;
import google.registry.model.eppcommon.StatusValue; import google.registry.model.eppcommon.StatusValue;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.host.HostResource; import google.registry.model.host.HostResource;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.smd.EncodedSignedMark; import google.registry.model.smd.EncodedSignedMark;
import google.registry.testing.ExceptionRule; import google.registry.testing.ExceptionRule;
import org.joda.money.Money; import org.joda.money.Money;
import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -92,7 +84,7 @@ public class DomainApplicationTest extends EntityTestCase {
LaunchNotice.create("tcnid", "validatorId", START_OF_TIME, START_OF_TIME)) LaunchNotice.create("tcnid", "validatorId", START_OF_TIME, START_OF_TIME))
.setCreationTrid(Trid.create("client-creation-trid", "server-trid")) .setCreationTrid(Trid.create("client-creation-trid", "server-trid"))
.setPhase(LaunchPhase.LANDRUSH) .setPhase(LaunchPhase.LANDRUSH)
// TODO(b/32447342): set period .setPeriod(Period.create(5, Period.Unit.YEARS))
.setEncodedSignedMarks(ImmutableList.of(EncodedSignedMark.create("base64", "abcdefg="))) .setEncodedSignedMarks(ImmutableList.of(EncodedSignedMark.create("base64", "abcdefg=")))
.setApplicationStatus(ApplicationStatus.ALLOCATED) .setApplicationStatus(ApplicationStatus.ALLOCATED)
.setAuctionPrice(Money.of(USD, 11)) .setAuctionPrice(Money.of(USD, 11))
@ -169,71 +161,4 @@ public class DomainApplicationTest extends EntityTestCase {
// If there are circular references, this will overflow the stack. // If there are circular references, this will overflow the stack.
domainApplication.toHydratedString(); domainApplication.toHydratedString();
} }
// TODO(b/32447342): remove this once the period has been populated on all DomainApplications
private void triggerTheOnLoadMethod() {
ofy().transact(new VoidWork() {
@Override
public void vrun() {
domainApplication = ofy().load().fromEntity(ofy().save().toEntity(domainApplication));
}
});
}
// TODO(b/32447342): remove this once the period has been populated on all DomainApplications
@Test
public void testPeriodIsNullByDefault() {
triggerTheOnLoadMethod();
assertThat(domainApplication.getPeriod()).isNull();
}
// TODO(b/32447342): remove this once the period has been populated on all DomainApplications
@Test
public void testPeriodIsOneYearBecauseHistoryEntryHasNoPeriod() {
persistResource(makeHistoryEntry(
domainApplication,
HistoryEntry.Type.DOMAIN_APPLICATION_CREATE,
Period.create(5, Period.Unit.YEARS),
"testing",
DateTime.now(UTC),
loadFileWithSubstitutions(
getClass(), "domain_create_landrush.xml", ImmutableMap.<String, String>of())));
triggerTheOnLoadMethod();
assertThat(domainApplication.getPeriod()).isNotNull();
assertThat(domainApplication.getPeriod()).isEqualTo(Period.create(1, Period.Unit.YEARS));
}
// TODO(b/32447342): remove this once the period has been populated on all DomainApplications
@Test
public void testPeriodDefaultedFromHistoryEntry() {
persistResource(makeHistoryEntry(
domainApplication,
HistoryEntry.Type.DOMAIN_APPLICATION_CREATE,
Period.create(5, Period.Unit.YEARS),
"testing",
DateTime.now(UTC),
loadFileWithSubstitutions(
getClass(), "domain_create_landrush_with_period.xml", ImmutableMap.of("PERIOD", "5"))));
triggerTheOnLoadMethod();
assertThat(domainApplication.getPeriod()).isNotNull();
assertThat(domainApplication.getPeriod()).isEqualTo(Period.create(5, Period.Unit.YEARS));
}
// TODO(b/32447342): remove this once the period has been populated on all DomainApplications
@Test
public void testPeriodAlreadySet() {
domainApplication = persistResource(
domainApplication.asBuilder().setPeriod(Period.create(1, Period.Unit.YEARS)).build());
persistResource(makeHistoryEntry(
domainApplication,
HistoryEntry.Type.DOMAIN_APPLICATION_CREATE,
Period.create(5, Period.Unit.YEARS),
"testing",
DateTime.now(UTC),
loadFileWithSubstitutions(
getClass(), "domain_create_landrush_with_period.xml", ImmutableMap.of("PERIOD", "5"))));
triggerTheOnLoadMethod();
assertThat(domainApplication.getPeriod()).isNotNull();
assertThat(domainApplication.getPeriod()).isEqualTo(Period.create(1, Period.Unit.YEARS));
}
} }

View file

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<create>
<domain:create
xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
<domain:name>example.tld</domain:name>
<domain:period unit="y">%PERIOD%</domain:period>
<domain:registrant>jd1234</domain:registrant>
<domain:contact type="admin">sh8013</domain:contact>
<domain:contact type="tech">sh8013</domain:contact>
<domain:authInfo>
<domain:pw>2fooBAR</domain:pw>
</domain:authInfo>
</domain:create>
</create>
<extension>
<launch:create
xmlns:launch="urn:ietf:params:xml:ns:launch-1.0">
<launch:phase>landrush</launch:phase>
</launch:create>
</extension>
<clTRID>ABC-12345</clTRID>
</command>
</epp>

View file

@ -275,21 +275,11 @@ public final class FullFieldsTestEntityHelper {
Period period, Period period,
String reason, String reason,
DateTime modificationTime) { DateTime modificationTime) {
return makeHistoryEntry(resource, type, period, reason, modificationTime, "<xml></xml>");
}
public static HistoryEntry makeHistoryEntry(
EppResource resource,
HistoryEntry.Type type,
Period period,
String reason,
DateTime modificationTime,
String xml) {
HistoryEntry.Builder builder = new HistoryEntry.Builder() HistoryEntry.Builder builder = new HistoryEntry.Builder()
.setParent(resource) .setParent(resource)
.setType(type) .setType(type)
.setPeriod(period) .setPeriod(period)
.setXmlBytes(xml.getBytes(UTF_8)) .setXmlBytes("<xml></xml>".getBytes(UTF_8))
.setModificationTime(modificationTime) .setModificationTime(modificationTime)
.setClientId("foo") .setClientId("foo")
.setTrid(Trid.create("ABC-123", "server-trid")) .setTrid(Trid.create("ABC-123", "server-trid"))