Automatically apply JPA type converters (#305)

* Automatically apply JPA type converters

* Include converters in tests and schema generation too
This commit is contained in:
Ben McIlwain 2019-10-08 13:16:39 -04:00 committed by GitHub
parent 0fd7cf29b5
commit 5dc058ec99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 20 deletions

View file

@ -27,7 +27,7 @@ import javax.persistence.Converter;
import org.joda.time.DateTime;
/** JPA converter to for storing/retrieving CreateAutoTimestamp objects. */
@Converter
@Converter(autoApply = true)
public class CreateAutoTimestampConverter
implements AttributeConverter<CreateAutoTimestamp, Timestamp> {

View file

@ -25,7 +25,7 @@ import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/** JPA converter for storing/retrieving UpdateAutoTimestamp objects. */
@Converter
@Converter(autoApply = true)
public class UpdateAutoTimestampConverter
implements AttributeConverter<UpdateAutoTimestamp, Timestamp> {

View file

@ -21,12 +21,10 @@ import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import google.registry.model.Buildable;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
import google.registry.persistence.CreateAutoTimestampConverter;
import google.registry.util.DateTimeUtils;
import java.time.ZonedDateTime;
import java.util.Optional;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@ -109,7 +107,6 @@ public final class RegistryLock extends ImmutableObject implements Buildable {
/** Creation timestamp is when the lock/unlock is first requested. */
@Column(nullable = false)
@Convert(converter = CreateAutoTimestampConverter.class)
private CreateAutoTimestamp creationTimestamp = CreateAutoTimestamp.create(null);
/**

View file

@ -17,12 +17,10 @@ package google.registry.schema.tld;
import static com.google.common.base.Preconditions.checkState;
import google.registry.model.CreateAutoTimestamp;
import google.registry.persistence.CreateAutoTimestampConverter;
import java.math.BigDecimal;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -56,7 +54,6 @@ public class PremiumList {
private Long revisionId;
@Column(nullable = false)
@Convert(converter = CreateAutoTimestampConverter.class)
private CreateAutoTimestamp creationTimestamp = CreateAutoTimestamp.create(null);
@Column(nullable = false)

View file

@ -27,8 +27,10 @@ import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.eppcommon.Trid;
import google.registry.model.transfer.BaseTransferObject;
import google.registry.model.transfer.TransferData;
import google.registry.persistence.CreateAutoTimestampConverter;
import google.registry.persistence.NomulusNamingStrategy;
import google.registry.persistence.NomulusPostgreSQLDialect;
import google.registry.persistence.UpdateAutoTimestampConverter;
import google.registry.schema.domain.RegistryLock;
import google.registry.schema.tld.PremiumList;
import google.registry.schema.tmch.ClaimsList;
@ -62,6 +64,7 @@ public class GenerateSqlSchemaCommand implements Command {
ImmutableSet.of(
BaseTransferObject.class,
ClaimsList.class,
CreateAutoTimestampConverter.class,
DelegationSignerData.class,
DesignatedContact.class,
DomainBase.class,
@ -70,7 +73,8 @@ public class GenerateSqlSchemaCommand implements Command {
PremiumList.class,
RegistryLock.class,
TransferData.class,
Trid.class);
Trid.class,
UpdateAutoTimestampConverter.class);
@VisibleForTesting
public static final String DB_OPTIONS_CLASH =

View file

@ -32,6 +32,10 @@
<class>google.registry.model.transfer.TransferData</class>
<class>google.registry.model.eppcommon.Trid</class>
<!-- Customized type converters -->
<class>google.registry.persistence.CreateAutoTimestampConverter</class>
<class>google.registry.persistence.UpdateAutoTimestampConverter</class>
<!-- TODO(weiminyu): check out application-layer validation. -->
<validation-mode>NONE</validation-mode>
</persistence-unit>

View file

@ -19,6 +19,7 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.JUnitBackports.assertThrows;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.persistence.CreateAutoTimestampConverter;
import google.registry.schema.domain.RegistryLock;
import google.registry.schema.domain.RegistryLock.Action;
import google.registry.testing.AppEngineRule;
@ -37,7 +38,9 @@ public final class RegistryLockDaoTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().withEntityClass(RegistryLock.class).build();
new JpaTransactionManagerRule.Builder()
.withEntityClass(RegistryLock.class, CreateAutoTimestampConverter.class)
.build();
@Test
public void testSaveAndLoad_success() {

View file

@ -18,6 +18,7 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import google.registry.persistence.PersistenceModule;
import google.registry.testing.FakeClock;
@ -145,9 +146,9 @@ public class JpaTransactionManagerRule extends ExternalResource {
return this;
}
/** Adds an annotated class to the known entities for the database. */
public Builder withEntityClass(Class clazz) {
this.extraEntityClasses.add(clazz);
/** Adds annotated class(es) to the known entities for the database. */
public Builder withEntityClass(Class... classes) {
this.extraEntityClasses.addAll(ImmutableSet.copyOf(classes));
return this;
}

View file

@ -19,7 +19,6 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.cfg.Environment;
@ -44,7 +43,7 @@ public class CreateAutoTimestampConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.withEntityClass(TestEntity.class, CreateAutoTimestampConverter.class)
.withProperty(Environment.HBM2DDL_AUTO, "update")
.build();
@ -78,7 +77,6 @@ public class CreateAutoTimestampConverterTest {
@Id String name;
@Convert(converter = CreateAutoTimestampConverter.class)
CreateAutoTimestamp cat;
public TestEntity() {}

View file

@ -19,7 +19,6 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.ImmutableObject;
import google.registry.model.UpdateAutoTimestamp;
import google.registry.model.transaction.JpaTransactionManagerRule;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.cfg.Environment;
@ -43,7 +42,7 @@ public class UpdateAutoTimestampConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.withEntityClass(TestEntity.class, UpdateAutoTimestampConverter.class)
.withProperty(Environment.HBM2DDL_AUTO, "update")
.build();
@ -89,7 +88,6 @@ public class UpdateAutoTimestampConverterTest {
@Id String name;
@Convert(converter = UpdateAutoTimestampConverter.class)
UpdateAutoTimestamp uat;
public TestEntity() {}

View file

@ -20,6 +20,7 @@ import static google.registry.testing.JUnitBackports.assertThrows;
import com.google.common.collect.ImmutableMap;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.persistence.CreateAutoTimestampConverter;
import java.math.BigDecimal;
import javax.persistence.PersistenceException;
import org.joda.money.CurrencyUnit;
@ -34,7 +35,9 @@ public class PremiumListDaoTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().withEntityClass(PremiumList.class).build();
new JpaTransactionManagerRule.Builder()
.withEntityClass(PremiumList.class, CreateAutoTimestampConverter.class)
.build();
private static final ImmutableMap<String, BigDecimal> TEST_PRICES =
ImmutableMap.of(