Remove ofy support from Address (#1759)

This commit is contained in:
Lai Jiang
2022-08-26 12:35:48 -04:00
committed by GitHub
parent 78249e1329
commit bd4af052a6
8 changed files with 185 additions and 114 deletions
@@ -15,63 +15,156 @@
package google.registry.model.eppcommon;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
import google.registry.model.ImmutableObject;
import google.registry.model.eppcommon.AddressTest.TestEntity.TestAddress;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaUnitTestExtension;
import java.io.StringReader;
import java.io.StringWriter;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Tests for {@link Address}. */
class AddressTest {
@Test
void onLoad_setsIndividualStreetLinesSuccessfully() {
Address address = new Address();
address.onLoad(ImmutableList.of("line1", "line2", "line3"));
assertThat(address.streetLine1).isEqualTo("line1");
assertThat(address.streetLine2).isEqualTo("line2");
assertThat(address.streetLine3).isEqualTo("line3");
@RegisterExtension
public final JpaUnitTestExtension jpa =
new JpaTestExtensions.Builder().withEntityClass(TestEntity.class).buildUnitTestExtension();
private static final String ENTITY_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
+ "<testEntity>\n"
+ " <address>\n"
+ " <street>123 W 14th St</street>\n"
+ " <street>8th Fl</street>\n"
+ " <street>Rm 8</street>\n"
+ " <city>New York</city>\n"
+ " <sp>NY</sp>\n"
+ " <pc>10011</pc>\n"
+ " <cc>US</cc>\n"
+ " </address>\n"
+ "</testEntity>\n";
private TestAddress address = createAddress("123 W 14th St", "8th Fl", "Rm 8");
private TestEntity entity = new TestEntity(1L, address);
private static TestEntity saveAndLoad(TestEntity entity) {
insertInDb(entity);
return loadByEntity(entity);
}
@Test
void onLoad_setsOnlyNonNullStreetLines() {
Address address = new Address();
address.onLoad(ImmutableList.of("line1", "line2"));
assertThat(address.streetLine1).isEqualTo("line1");
assertThat(address.streetLine2).isEqualTo("line2");
assertThat(address.streetLine3).isNull();
void testSuccess_setStreet() {
assertAddress(address, "123 W 14th St", "8th Fl", "Rm 8");
}
/** Test the persist behavior. */
@Test
void testSuccess_saveAndLoadStreetLines() {
assertAddress(saveAndLoad(entity).address, "123 W 14th St", "8th Fl", "Rm 8");
}
/** Test the merge behavior. */
@Test
void testSuccess_putAndLoadStreetLines() {
jpaTm().transact(() -> jpaTm().put(entity));
assertAddress(loadByEntity(entity).address, "123 W 14th St", "8th Fl", "Rm 8");
}
@Test
void onLoad_doNothingIfInputIsNull() {
Address address = new Address();
address.onLoad(null);
assertThat(address.streetLine1).isNull();
assertThat(address.streetLine2).isNull();
assertThat(address.streetLine3).isNull();
void testSuccess_setsNullStreetLine() {
entity = new TestEntity(1L, createAddress("line1", "line2"));
TestEntity savedEntity = saveAndLoad(entity);
assertAddress(savedEntity.address, "line1", "line2");
assertThat(savedEntity.address.streetLine3).isNull();
}
@Test
void postLoad_setsStreetListSuccessfully() {
Address address = new Address();
address.streetLine1 = "line1";
address.streetLine2 = "line2";
address.streetLine3 = "line3";
address.postLoad();
assertThat(address.street).containsExactly("line1", "line2", "line3");
void testFailure_tooManyStreetLines() {
assertThrows(
IllegalArgumentException.class, () -> createAddress("line1", "line2", "line3", "line4"));
}
@Test
void postLoad_setsOnlyNonNullStreetLines() {
Address address = new Address();
address.streetLine1 = "line1";
address.streetLine2 = "line2";
address.postLoad();
assertThat(address.street).containsExactly("line1", "line2");
void testFailure_emptyStreetLine() {
assertThrows(IllegalArgumentException.class, () -> createAddress("line1", "", "line3"));
}
@Test
void postLoad_doNothingIfInputIsNull() {
Address address = new Address();
address.postLoad();
assertThat(address.street).isNull();
void testSuccess_pojoToAndFromXml() throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(TestEntity.class);
// POJO to XML
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(entity, sw);
String xml = sw.toString();
assertThat(xml).isEqualTo(ENTITY_XML);
// XML to POJO
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
TestEntity unmarshalledEntity = (TestEntity) unmarshaller.unmarshal(new StringReader(xml));
assertAddress(unmarshalledEntity.address, "123 W 14th St", "8th Fl", "Rm 8");
}
private static TestAddress createAddress(String... streetList) {
return new TestAddress.Builder()
.setStreet(ImmutableList.copyOf(streetList))
.setCity("New York")
.setState("NY")
.setZip("10011")
.setCountryCode("US")
.build();
}
private static void assertAddress(TestAddress address, String... streetList) {
assertThat(address.street).containsExactly((Object[]) streetList);
if (streetList.length > 0) {
assertThat(address.streetLine1).isEqualTo(streetList[0]);
}
if (streetList.length > 1) {
assertThat(address.streetLine2).isEqualTo(streetList[1]);
}
if (streetList.length > 2) {
assertThat(address.streetLine3).isEqualTo(streetList[2]);
}
}
@Entity(name = "TestEntity")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class TestEntity extends ImmutableObject {
@XmlTransient @Id long id;
@XmlElement TestAddress address;
TestEntity() {}
TestEntity(Long id, TestAddress address) {
this.id = id;
this.address = address;
}
@Embeddable
public static class TestAddress extends Address {
public static class Builder extends Address.Builder<TestAddress> {}
}
}
}
@@ -5,13 +5,6 @@ class google.registry.model.common.GaeUserIdConverter {
@Id long id;
com.google.appengine.api.users.User user;
}
class google.registry.model.contact.ContactAddress {
java.lang.String city;
java.lang.String countryCode;
java.lang.String state;
java.lang.String zip;
java.util.List<java.lang.String> street;
}
class google.registry.model.contact.ContactAuthInfo {
google.registry.model.eppcommon.AuthInfo$PasswordAuth pw;
}
@@ -21,8 +14,6 @@ class google.registry.model.contact.ContactBase {
google.registry.model.contact.ContactPhoneNumber fax;
google.registry.model.contact.ContactPhoneNumber voice;
google.registry.model.contact.Disclose disclose;
google.registry.model.contact.PostalInfo internationalizedPostalInfo;
google.registry.model.contact.PostalInfo localizedPostalInfo;
google.registry.model.transfer.ContactTransferData transferData;
java.lang.String contactId;
java.lang.String creationClientId;
@@ -61,8 +52,6 @@ class google.registry.model.contact.ContactResource {
google.registry.model.contact.ContactPhoneNumber fax;
google.registry.model.contact.ContactPhoneNumber voice;
google.registry.model.contact.Disclose disclose;
google.registry.model.contact.PostalInfo internationalizedPostalInfo;
google.registry.model.contact.PostalInfo localizedPostalInfo;
google.registry.model.transfer.ContactTransferData transferData;
java.lang.String contactId;
java.lang.String creationClientId;
@@ -87,12 +76,6 @@ class google.registry.model.contact.Disclose {
class google.registry.model.contact.Disclose$PostalInfoChoice {
google.registry.model.contact.PostalInfo$Type type;
}
class google.registry.model.contact.PostalInfo {
google.registry.model.contact.ContactAddress address;
google.registry.model.contact.PostalInfo$Type type;
java.lang.String name;
java.lang.String org;
}
enum google.registry.model.contact.PostalInfo$Type {
INTERNATIONALIZED;
LOCALIZED;
@@ -372,8 +355,6 @@ class google.registry.model.registrar.Registrar {
boolean registryLockAllowed;
google.registry.model.registrar.Registrar$State state;
google.registry.model.registrar.Registrar$Type type;
google.registry.model.registrar.RegistrarAddress internationalizedAddress;
google.registry.model.registrar.RegistrarAddress localizedAddress;
java.lang.Long ianaIdentifier;
java.lang.String clientCertificate;
java.lang.String clientCertificateHash;
@@ -414,13 +395,6 @@ enum google.registry.model.registrar.Registrar$Type {
REAL;
TEST;
}
class google.registry.model.registrar.RegistrarAddress {
java.lang.String city;
java.lang.String countryCode;
java.lang.String state;
java.lang.String zip;
java.util.List<java.lang.String> street;
}
class google.registry.model.reporting.DomainTransactionRecord {
google.registry.model.reporting.DomainTransactionRecord$TransactionReportField reportField;
java.lang.Integer reportAmount;