Remove ofy support from auto timestamp classes (#1708)

Also remove the use of ZonedDateTime, as joda DateTime can already be persisted to SQL with an existing converted.
This commit is contained in:
Lai Jiang
2022-07-26 09:51:35 -04:00
committed by GitHub
parent 12905c1c1f
commit 1ca80010c0
22 changed files with 100 additions and 512 deletions
@@ -23,12 +23,11 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -86,10 +85,8 @@ class BillingEventTest {
void testParseBillingEventFromRecord_success() {
BillingEvent event = BillingEvent.parseFromRecord(schemaAndRecord);
assertThat(event.id()).isEqualTo(1);
assertThat(event.billingTime())
.isEqualTo(ZonedDateTime.of(2017, 10, 24, 9, 6, 3, 0, ZoneId.of("UTC")));
assertThat(event.eventTime())
.isEqualTo(ZonedDateTime.of(2017, 1, 19, 23, 59, 43, 0, ZoneId.of("UTC")));
assertThat(event.billingTime()).isEqualTo(DateTime.parse("2017-10-24T09:06:03Z"));
assertThat(event.eventTime()).isEqualTo(DateTime.parse("2017-01-19T23:59:43Z"));
assertThat(event.registrarId()).isEqualTo("myRegistrar");
assertThat(event.billingId()).isEqualTo("12345-CRRHELLO");
assertThat(event.poNumber()).isEmpty();
@@ -55,8 +55,6 @@ import google.registry.util.ResourceUtils;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Optional;
@@ -104,8 +102,8 @@ class InvoicingPipelineTest {
ImmutableList.of(
BillingEvent.create(
1,
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-04T00:00:00Z"),
DateTime.parse("2017-10-04T00:00:00Z"),
"theRegistrar",
"234",
"",
@@ -119,8 +117,8 @@ class InvoicingPipelineTest {
""),
BillingEvent.create(
2,
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-04T00:00:00Z"),
DateTime.parse("2017-10-04T00:00:00Z"),
"theRegistrar",
"234",
"",
@@ -134,8 +132,8 @@ class InvoicingPipelineTest {
""),
BillingEvent.create(
3,
ZonedDateTime.of(2017, 10, 2, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 9, 29, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-02T00:00:00Z"),
DateTime.parse("2017-09-29T00:00:00Z"),
"theRegistrar",
"234",
"",
@@ -149,8 +147,8 @@ class InvoicingPipelineTest {
""),
BillingEvent.create(
4,
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-04T00:00:00Z"),
DateTime.parse("2017-10-04T00:00:00Z"),
"bestdomains",
"456",
"116688",
@@ -164,8 +162,8 @@ class InvoicingPipelineTest {
""),
BillingEvent.create(
5,
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-04T00:00:00Z"),
DateTime.parse("2017-10-04T00:00:00Z"),
"anotherRegistrar",
"789",
"",
@@ -179,8 +177,8 @@ class InvoicingPipelineTest {
"SUNRISE ANCHOR_TENANT"),
BillingEvent.create(
6,
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-04T00:00:00Z"),
DateTime.parse("2017-10-04T00:00:00Z"),
"theRegistrar",
"234",
"",
@@ -194,8 +192,8 @@ class InvoicingPipelineTest {
""),
BillingEvent.create(
7,
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
ZonedDateTime.of(2017, 10, 4, 0, 0, 0, 0, ZoneId.of("UTC")),
DateTime.parse("2017-10-04T00:00:00Z"),
DateTime.parse("2017-10-04T00:00:00Z"),
"theRegistrar",
"234",
"",
@@ -19,10 +19,11 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static org.joda.time.DateTimeZone.UTC;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Ignore;
import google.registry.model.common.CrossTldSingleton;
import google.registry.testing.AppEngineExtension;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaUnitTestExtension;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -31,22 +32,19 @@ import org.junit.jupiter.api.extension.RegisterExtension;
public class CreateAutoTimestampTest {
@RegisterExtension
public final AppEngineExtension appEngine =
AppEngineExtension.builder()
.withCloudSql()
.withOfyTestEntities(CreateAutoTimestampTestObject.class)
.withJpaUnitTestEntities(CreateAutoTimestampTestObject.class)
.build();
public final JpaUnitTestExtension jpaUnitTestExtension =
new JpaTestExtensions.Builder()
.withEntityClass(CreateAutoTimestampTestObject.class)
.buildUnitTestExtension();
/** Timestamped class. */
@Entity(name = "CatTestEntity")
@javax.persistence.Entity
@Entity
public static class CreateAutoTimestampTestObject extends CrossTldSingleton {
@Ignore @javax.persistence.Id long id = SINGLETON_ID;
@Id long id = SINGLETON_ID;
CreateAutoTimestamp createTime = CreateAutoTimestamp.create(null);
}
private CreateAutoTimestampTestObject reload() {
private static CreateAutoTimestampTestObject reload() {
return loadByEntity(new CreateAutoTimestampTestObject());
}
@@ -61,7 +59,7 @@ public class CreateAutoTimestampTest {
return tm().getTransactionTime();
});
tm().clearSessionCache();
assertThat(reload().createTime.timestamp).isEqualTo(transactionTime);
assertThat(reload().createTime.getTimestamp()).isEqualTo(transactionTime);
}
@Test
@@ -74,6 +72,6 @@ public class CreateAutoTimestampTest {
tm().put(object);
});
tm().clearSessionCache();
assertThat(reload().createTime.timestamp).isEqualTo(oldCreateTime);
assertThat(reload().createTime.getTimestamp()).isEqualTo(oldCreateTime);
}
}
@@ -16,59 +16,42 @@ package google.registry.model;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.time.DateTimeZone.UTC;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Ignore;
import google.registry.model.common.CrossTldSingleton;
import google.registry.model.ofy.Ofy;
import google.registry.persistence.VKey;
import google.registry.testing.AppEngineExtension;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaUnitTestExtension;
import google.registry.testing.FakeClock;
import google.registry.testing.InjectExtension;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Unit tests for {@link UpdateAutoTimestamp}. */
public class UpdateAutoTimestampTest {
FakeClock clock = new FakeClock();
private final FakeClock clock = new FakeClock();
@RegisterExtension
public final AppEngineExtension appEngine =
AppEngineExtension.builder()
.withCloudSql()
.withJpaUnitTestEntities(UpdateAutoTimestampTestObject.class)
.withOfyTestEntities(UpdateAutoTimestampTestObject.class)
public final JpaUnitTestExtension jpaUnitTestExtension =
new JpaTestExtensions.Builder()
.withClock(clock)
.build();
@RegisterExtension public final InjectExtension inject = new InjectExtension();
@BeforeEach
void beforeEach() {
inject.setStaticField(Ofy.class, "clock", clock);
}
.withEntityClass(UpdateAutoTimestampTestObject.class)
.buildUnitTestExtension();
/** Timestamped class. */
@Entity(name = "UatTestEntity")
@javax.persistence.Entity
@Entity
public static class UpdateAutoTimestampTestObject extends CrossTldSingleton {
@Ignore @javax.persistence.Id long id = SINGLETON_ID;
@Id long id = SINGLETON_ID;
UpdateAutoTimestamp updateTime = UpdateAutoTimestamp.create(null);
}
private UpdateAutoTimestampTestObject reload() {
private static UpdateAutoTimestampTestObject reload() {
return tm().transact(
() ->
tm().loadByKey(
VKey.create(
UpdateAutoTimestampTestObject.class,
1L,
Key.create(new UpdateAutoTimestampTestObject()))));
() -> tm().loadByKey(VKey.createSql(UpdateAutoTimestampTestObject.class, 1L)));
}
@Test
@@ -78,12 +61,12 @@ public class UpdateAutoTimestampTest {
() -> {
clock.advanceOneMilli();
UpdateAutoTimestampTestObject object = new UpdateAutoTimestampTestObject();
assertThat(object.updateTime.timestamp).isNull();
assertThat(object.updateTime.getTimestamp()).isEqualTo(START_OF_TIME);
tm().insert(object);
return tm().getTransactionTime();
});
tm().clearSessionCache();
assertThat(reload().updateTime.timestamp).isEqualTo(transactionTime);
assertThat(reload().updateTime.getTimestamp()).isEqualTo(transactionTime);
}
@Test
@@ -99,7 +82,7 @@ public class UpdateAutoTimestampTest {
UpdateAutoTimestampTestObject object = reload();
clock.advanceOneMilli();
try (UpdateAutoTimestamp.DisableAutoUpdateResource disabler =
try (UpdateAutoTimestamp.DisableAutoUpdateResource ignoredDisabler =
new UpdateAutoTimestamp.DisableAutoUpdateResource()) {
DateTime secondTransactionTime =
tm().transact(
@@ -109,7 +92,7 @@ public class UpdateAutoTimestampTest {
});
assertThat(secondTransactionTime).isGreaterThan(initialTime);
}
assertThat(reload().updateTime.timestamp).isEqualTo(initialTime);
assertThat(reload().updateTime.getTimestamp()).isEqualTo(initialTime);
}
@Test
@@ -124,7 +107,7 @@ public class UpdateAutoTimestampTest {
return tm().getTransactionTime();
});
tm().clearSessionCache();
assertThat(reload().updateTime.timestamp).isEqualTo(transactionTime);
assertThat(reload().updateTime.getTimestamp()).isEqualTo(transactionTime);
}
@Test
@@ -1,116 +0,0 @@
// Copyright 2019 The Nomulus 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 google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaUnitTestExtension;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZonedDateTime;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Unit tests for {@link ZonedDateTimeConverter}. */
public class ZonedDateTimeConverterTest {
@RegisterExtension
public final JpaUnitTestExtension jpaExtension =
new JpaTestExtensions.Builder().withEntityClass(TestEntity.class).buildUnitTestExtension();
private final ZonedDateTimeConverter converter = new ZonedDateTimeConverter();
@Test
void convertToDatabaseColumn_returnsNullIfInputIsNull() {
assertThat(converter.convertToDatabaseColumn(null)).isNull();
}
@Test
void convertToDatabaseColumn_convertsCorrectly() {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-09-01T01:01:01Z");
assertThat(converter.convertToDatabaseColumn(zonedDateTime).toInstant())
.isEqualTo(zonedDateTime.toInstant());
}
@Test
void convertToEntityAttribute_returnsNullIfInputIsNull() {
assertThat(converter.convertToEntityAttribute(null)).isNull();
}
@Test
void convertToEntityAttribute_convertsCorrectly() {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-09-01T01:01:01Z");
Instant instant = zonedDateTime.toInstant();
assertThat(converter.convertToEntityAttribute(Timestamp.from(instant)))
.isEqualTo(zonedDateTime);
}
@Test
void converter_generatesTimestampWithNormalizedZone() {
ZonedDateTime zdt = ZonedDateTime.parse("2019-09-01T01:01:01Z");
TestEntity entity = new TestEntity("normalized_utc_time", zdt);
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm()
.transact(
() -> jpaTm().getEntityManager().find(TestEntity.class, "normalized_utc_time"));
assertThat(retrievedEntity.zdt.toString()).isEqualTo("2019-09-01T01:01:01Z");
}
@Test
void converter_convertsNonNormalizedZoneCorrectly() {
ZonedDateTime zdt = ZonedDateTime.parse("2019-09-01T01:01:01Z[UTC]");
TestEntity entity = new TestEntity("non_normalized_utc_time", zdt);
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm()
.transact(
() -> jpaTm().getEntityManager().find(TestEntity.class, "non_normalized_utc_time"));
assertThat(retrievedEntity.zdt.toString()).isEqualTo("2019-09-01T01:01:01Z");
}
@Test
void converter_convertsNonUtcZoneCorrectly() {
ZonedDateTime zdt = ZonedDateTime.parse("2019-09-01T01:01:01+05:00");
TestEntity entity = new TestEntity("new_york_time", zdt);
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "new_york_time"));
assertThat(retrievedEntity.zdt.toString()).isEqualTo("2019-08-31T20:01:01Z");
}
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.
private static class TestEntity extends ImmutableObject {
@Id String name;
ZonedDateTime zdt;
public TestEntity() {}
TestEntity(String name, ZonedDateTime zdt) {
this.name = name;
this.zdt = zdt;
}
}
}
@@ -1,9 +1,3 @@
class google.registry.model.CreateAutoTimestamp {
org.joda.time.DateTime timestamp;
}
class google.registry.model.UpdateAutoTimestamp {
org.joda.time.DateTime timestamp;
}
class google.registry.model.billing.BillingEvent$Cancellation {
@Id java.lang.Long id;
@Parent com.googlecode.objectify.Key<google.registry.model.domain.DomainHistory> parent;
@@ -81,7 +75,6 @@ enum google.registry.model.billing.BillingEvent$RenewalPriceBehavior {
}
class google.registry.model.common.EntityGroupRoot {
@Id java.lang.String id;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
}
class google.registry.model.common.GaeUserIdConverter {
@Id long id;
@@ -102,8 +95,6 @@ class google.registry.model.contact.ContactAuthInfo {
}
class google.registry.model.contact.ContactBase {
@Id java.lang.String repoId;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.model.contact.ContactAuthInfo authInfo;
google.registry.model.contact.ContactPhoneNumber fax;
google.registry.model.contact.ContactPhoneNumber voice;
@@ -144,8 +135,6 @@ class google.registry.model.contact.ContactPhoneNumber {
}
class google.registry.model.contact.ContactResource {
@Id java.lang.String repoId;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.model.contact.ContactAuthInfo authInfo;
google.registry.model.contact.ContactPhoneNumber fax;
google.registry.model.contact.ContactPhoneNumber voice;
@@ -191,8 +180,6 @@ class google.registry.model.domain.DomainAuthInfo {
}
class google.registry.model.domain.DomainBase {
@Id java.lang.String repoId;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.model.domain.DomainAuthInfo authInfo;
google.registry.model.domain.launch.LaunchNotice launchNotice;
google.registry.model.transfer.DomainTransferData transferData;
@@ -223,8 +210,6 @@ class google.registry.model.domain.DomainBase {
}
class google.registry.model.domain.DomainContent {
@Id java.lang.String repoId;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.model.domain.DomainAuthInfo authInfo;
google.registry.model.domain.launch.LaunchNotice launchNotice;
google.registry.model.transfer.DomainTransferData transferData;
@@ -334,8 +319,6 @@ class google.registry.model.domain.token.AllocationToken {
@Id java.lang.String token;
boolean discountPremiums;
double discountFraction;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.model.billing.BillingEvent$RenewalPriceBehavior renewalPriceBehavior;
google.registry.model.common.TimedTransitionProperty<google.registry.model.domain.token.AllocationToken$TokenStatus> tokenStatusTransitions;
google.registry.model.domain.token.AllocationToken$RegistrationBehavior registrationBehavior;
@@ -393,8 +376,6 @@ class google.registry.model.eppcommon.Trid {
}
class google.registry.model.host.HostBase {
@Id java.lang.String repoId;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.persistence.VKey<google.registry.model.domain.DomainBase> superordinateDomain;
java.lang.String creationClientId;
java.lang.String currentSponsorClientId;
@@ -425,8 +406,6 @@ class google.registry.model.host.HostHistory {
}
class google.registry.model.host.HostResource {
@Id java.lang.String repoId;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.persistence.VKey<google.registry.model.domain.DomainBase> superordinateDomain;
java.lang.String creationClientId;
java.lang.String currentSponsorClientId;
@@ -443,7 +422,6 @@ class google.registry.model.index.EppResourceIndex {
@Id java.lang.String id;
@Parent com.googlecode.objectify.Key<google.registry.model.index.EppResourceIndexBucket> bucket;
com.googlecode.objectify.Key<? extends google.registry.model.EppResource> reference;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
java.lang.String kind;
}
class google.registry.model.index.EppResourceIndexBucket {
@@ -451,19 +429,16 @@ class google.registry.model.index.EppResourceIndexBucket {
}
class google.registry.model.index.ForeignKeyIndex$ForeignKeyContactIndex {
@Id java.lang.String foreignKey;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.persistence.VKey<E> topReference;
org.joda.time.DateTime deletionTime;
}
class google.registry.model.index.ForeignKeyIndex$ForeignKeyDomainIndex {
@Id java.lang.String foreignKey;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.persistence.VKey<E> topReference;
org.joda.time.DateTime deletionTime;
}
class google.registry.model.index.ForeignKeyIndex$ForeignKeyHostIndex {
@Id java.lang.String foreignKey;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
google.registry.persistence.VKey<E> topReference;
org.joda.time.DateTime deletionTime;
}
@@ -489,7 +464,6 @@ class google.registry.model.poll.PollMessage$OneTime {
}
class google.registry.model.rde.RdeRevision {
@Id java.lang.String id;
google.registry.model.UpdateAutoTimestamp updateTimestamp;
int revision;
}
class google.registry.model.registrar.Registrar {
@@ -498,8 +472,6 @@ class google.registry.model.registrar.Registrar {
boolean blockPremiumNames;
boolean contactsRequireSyncing;
boolean registryLockAllowed;
google.registry.model.CreateAutoTimestamp creationTime;
google.registry.model.UpdateAutoTimestamp lastUpdateTime;
google.registry.model.registrar.Registrar$State state;
google.registry.model.registrar.Registrar$Type type;
google.registry.model.registrar.RegistrarAddress internationalizedAddress;