Add/use more DatabaseHelper convenience methods (#1327)

* Add/use more DatabaseHelper convenience methods

This also fixes up some existing uses of "put" in test code that should be
inserts or updates (depending on which is intended). Doing an insert/update
makes stronger guarantees about an entity either not existing or existing,
depending on what you're doing.

* Convert more Object -> ImmutableObject

* Merge branch 'master' into tx-manager-sigs

* Revert breaking PremiumListDao change

* Refactor more insertInDb()

* Fight more testing errors

* Merge branch 'master' into tx-manager-sigs

* Merge branch 'master' into tx-manager-sigs

* Merge branch 'master' into tx-manager-sigs

* Merge branch 'master' into tx-manager-sigs

* Add removeTmOverrideForTest() calls

* Merge branch 'master' into tx-manager-sigs
This commit is contained in:
Ben McIlwain 2021-09-28 17:16:54 -04:00 committed by GitHub
parent 469ce5589b
commit 47a38e8309
34 changed files with 449 additions and 558 deletions

View file

@ -134,7 +134,7 @@ public class DatastoreTransactionManager implements TransactionManager {
} }
@Override @Override
public void insertWithoutBackup(Object entity) { public void insertWithoutBackup(ImmutableObject entity) {
putWithoutBackup(entity); putWithoutBackup(entity);
} }
@ -160,7 +160,7 @@ public class DatastoreTransactionManager implements TransactionManager {
} }
@Override @Override
public void putWithoutBackup(Object entity) { public void putWithoutBackup(ImmutableObject entity) {
syncIfTransactionless(getOfy().saveWithoutBackup().entities(toDatastoreEntity(entity))); syncIfTransactionless(getOfy().saveWithoutBackup().entities(toDatastoreEntity(entity)));
} }
@ -180,12 +180,12 @@ public class DatastoreTransactionManager implements TransactionManager {
} }
@Override @Override
public void updateAll(Object... entities) { public void updateAll(ImmutableObject... entities) {
updateAll(ImmutableList.of(entities)); updateAll(ImmutableList.copyOf(entities));
} }
@Override @Override
public void updateWithoutBackup(Object entity) { public void updateWithoutBackup(ImmutableObject entity) {
putWithoutBackup(entity); putWithoutBackup(entity);
} }

View file

@ -318,7 +318,7 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
} }
@Override @Override
public void insertWithoutBackup(Object entity) { public void insertWithoutBackup(ImmutableObject entity) {
insert(entity); insert(entity);
} }
@ -356,7 +356,7 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
} }
@Override @Override
public void putWithoutBackup(Object entity) { public void putWithoutBackup(ImmutableObject entity) {
put(entity); put(entity);
} }
@ -386,12 +386,12 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
} }
@Override @Override
public void updateAll(Object... entities) { public void updateAll(ImmutableObject... entities) {
updateAll(ImmutableList.of(entities)); updateAll(ImmutableList.copyOf(entities));
} }
@Override @Override
public void updateWithoutBackup(Object entity) { public void updateWithoutBackup(ImmutableObject entity) {
update(entity); update(entity);
} }

View file

@ -111,7 +111,7 @@ public interface TransactionManager {
* "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in * "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in
* SQL. * SQL.
*/ */
void insertWithoutBackup(Object entity); void insertWithoutBackup(ImmutableObject entity);
/** /**
* Persists all new entities in the database without writing any backup if the underlying database * Persists all new entities in the database without writing any backup if the underlying database
@ -144,7 +144,7 @@ public interface TransactionManager {
* "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in * "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in
* SQL. * SQL.
*/ */
void putWithoutBackup(Object entity); void putWithoutBackup(ImmutableObject entity);
/** /**
* Persists all new entities or update the existing entities in the database without writing * Persists all new entities or update the existing entities in the database without writing
@ -165,7 +165,7 @@ public interface TransactionManager {
void updateAll(ImmutableCollection<?> entities); void updateAll(ImmutableCollection<?> entities);
/** Updates all entities in the database, throws exception if any entity does not exist. */ /** Updates all entities in the database, throws exception if any entity does not exist. */
void updateAll(Object... entities); void updateAll(ImmutableObject... entities);
/** /**
* Updates an entity in the database without writing commit logs if the underlying database is * Updates an entity in the database without writing commit logs if the underlying database is
@ -177,7 +177,7 @@ public interface TransactionManager {
* "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in * "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in
* SQL. * SQL.
*/ */
void updateWithoutBackup(Object entity); void updateWithoutBackup(ImmutableObject entity);
/** /**
* Updates all entities in the database without writing any backup if the underlying database is * Updates all entities in the database without writing any backup if the underlying database is

View file

@ -132,7 +132,9 @@ public class TransactionManagerFactory {
/** /**
* Sets the return of {@link #tm()} to the given instance of {@link TransactionManager}. * Sets the return of {@link #tm()} to the given instance of {@link TransactionManager}.
* *
* <p>Used when overriding the per-test transaction manager for dual-database tests. * <p>Used when overriding the per-test transaction manager for dual-database tests. Should be
* matched with a corresponding invocation of {@link #removeTmOverrideForTest()} either at the end
* of the test or in an <code>@AfterEach</code> handler.
*/ */
@VisibleForTesting @VisibleForTesting
public static void setTmForTest(TransactionManager newTm) { public static void setTmForTest(TransactionManager newTm) {

View file

@ -27,6 +27,7 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm; import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newDomainBase; import static google.registry.testing.DatabaseHelper.newDomainBase;
import static google.registry.testing.DatabaseHelper.persistActiveContact; import static google.registry.testing.DatabaseHelper.persistActiveContact;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -163,12 +164,7 @@ public class ReplayCommitLogsToSqlActionTest {
@Test @Test
void testReplay_multipleDiffFiles() throws Exception { void testReplay_multipleDiffFiles() throws Exception {
jpaTm() insertInDb(TestObject.create("previous to keep"), TestObject.create("previous to delete"));
.transact(
() -> {
jpaTm().insertWithoutBackup(TestObject.create("previous to keep"));
jpaTm().insertWithoutBackup(TestObject.create("previous to delete"));
});
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
// Create 3 transactions, across two diff files. // Create 3 transactions, across two diff files.
// Before: {"previous to keep", "previous to delete"} // Before: {"previous to keep", "previous to delete"}
@ -216,7 +212,7 @@ public class ReplayCommitLogsToSqlActionTest {
@Test @Test
void testReplay_noManifests() throws Exception { void testReplay_noManifests() throws Exception {
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
jpaTm().transact(() -> jpaTm().insertWithoutBackup(TestObject.create("previous to keep"))); insertInDb(TestObject.create("previous to keep"));
saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(1)); saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(1));
saveDiffFile(gcsUtils, createCheckpoint(now.minusMillis(2))); saveDiffFile(gcsUtils, createCheckpoint(now.minusMillis(2)));
jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMillis(1))); jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMillis(1)));
@ -228,7 +224,7 @@ public class ReplayCommitLogsToSqlActionTest {
void testReplay_dryRun() throws Exception { void testReplay_dryRun() throws Exception {
action.dryRun = true; action.dryRun = true;
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
jpaTm().transact(() -> jpaTm().insertWithoutBackup(TestObject.create("previous to keep"))); insertInDb(TestObject.create("previous to keep"));
Key<CommitLogBucket> bucketKey = getBucketKey(1); Key<CommitLogBucket> bucketKey = getBucketKey(1);
Key<CommitLogManifest> manifestKey = CommitLogManifest.createKey(bucketKey, now); Key<CommitLogManifest> manifestKey = CommitLogManifest.createKey(bucketKey, now);
saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(2)); saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(2));
@ -253,7 +249,7 @@ public class ReplayCommitLogsToSqlActionTest {
@Test @Test
void testReplay_manifestWithNoDeletions() throws Exception { void testReplay_manifestWithNoDeletions() throws Exception {
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
jpaTm().transact(() -> jpaTm().insertWithoutBackup(TestObject.create("previous to keep"))); insertInDb(TestObject.create("previous to keep"));
Key<CommitLogBucket> bucketKey = getBucketKey(1); Key<CommitLogBucket> bucketKey = getBucketKey(1);
Key<CommitLogManifest> manifestKey = CommitLogManifest.createKey(bucketKey, now); Key<CommitLogManifest> manifestKey = CommitLogManifest.createKey(bucketKey, now);
saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(2)); saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(2));
@ -271,12 +267,7 @@ public class ReplayCommitLogsToSqlActionTest {
@Test @Test
void testReplay_manifestWithNoMutations() throws Exception { void testReplay_manifestWithNoMutations() throws Exception {
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
jpaTm() insertInDb(TestObject.create("previous to keep"), TestObject.create("previous to delete"));
.transact(
() -> {
jpaTm().insertWithoutBackup(TestObject.create("previous to keep"));
jpaTm().insertWithoutBackup(TestObject.create("previous to delete"));
});
saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(2)); saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(2));
jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1).minusMillis(1))); jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1).minusMillis(1)));
saveDiffFile( saveDiffFile(
@ -293,7 +284,7 @@ public class ReplayCommitLogsToSqlActionTest {
@Test @Test
void testReplay_mutateExistingEntity() throws Exception { void testReplay_mutateExistingEntity() throws Exception {
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
jpaTm().transact(() -> jpaTm().put(TestObject.create("existing", "a"))); insertInDb(TestObject.create("existing", "a"));
Key<CommitLogManifest> manifestKey = CommitLogManifest.createKey(getBucketKey(1), now); Key<CommitLogManifest> manifestKey = CommitLogManifest.createKey(getBucketKey(1), now);
saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(1).minusMillis(1)); saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(1).minusMillis(1));
jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1))); jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1)));
@ -312,7 +303,7 @@ public class ReplayCommitLogsToSqlActionTest {
@Test @Test
void testReplay_deleteMissingEntity() throws Exception { void testReplay_deleteMissingEntity() throws Exception {
DateTime now = fakeClock.nowUtc(); DateTime now = fakeClock.nowUtc();
jpaTm().transact(() -> jpaTm().put(TestObject.create("previous to keep", "a"))); insertInDb(TestObject.create("previous to keep", "a"));
saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(1).minusMillis(1)); saveDiffFileNotToRestore(gcsUtils, now.minusMinutes(1).minusMillis(1));
jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1))); jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1)));
saveDiffFile( saveDiffFile(
@ -368,7 +359,7 @@ public class ReplayCommitLogsToSqlActionTest {
// Create and save to SQL a registrar contact that we will delete // Create and save to SQL a registrar contact that we will delete
RegistrarContact toDelete = AppEngineExtension.makeRegistrarContact1(); RegistrarContact toDelete = AppEngineExtension.makeRegistrarContact1();
jpaTm().transact(() -> jpaTm().put(toDelete)); insertInDb(toDelete);
jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1).minusMillis(1))); jpaTm().transact(() -> SqlReplayCheckpoint.set(now.minusMinutes(1).minusMillis(1)));
// spy the txn manager so we can see what order things were inserted/removed // spy the txn manager so we can see what order things were inserted/removed
@ -569,8 +560,7 @@ public class ReplayCommitLogsToSqlActionTest {
.asBuilder() .asBuilder()
.setDsData(ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {0, 1, 2}))) .setDsData(ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {0, 1, 2})))
.build(); .build();
jpaTm().transact(() -> jpaTm().put(domain)); insertInDb(domain);
assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(DelegationSignerData.class))).isNotEmpty(); assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(DelegationSignerData.class))).isNotEmpty();
saveDiffFile( saveDiffFile(
@ -580,12 +570,8 @@ public class ReplayCommitLogsToSqlActionTest {
getBucketKey(1), now.minusMinutes(3), ImmutableSet.of(Key.create(domain)))); getBucketKey(1), now.minusMinutes(3), ImmutableSet.of(Key.create(domain))));
runAndAssertSuccess(now.minusMinutes(1), 1, 1); runAndAssertSuccess(now.minusMinutes(1), 1, 1);
jpaTm() assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(DomainBase.class))).isEmpty();
.transact( assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(DelegationSignerData.class))).isEmpty();
() -> {
assertThat(jpaTm().loadAllOf(DomainBase.class)).isEmpty();
assertThat(jpaTm().loadAllOf(DelegationSignerData.class)).isEmpty();
});
} }
@Test @Test
@ -627,7 +613,7 @@ public class ReplayCommitLogsToSqlActionTest {
ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {4, 5, 6})); ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {4, 5, 6}));
DomainBase domainWithDsData = DomainBase domainWithDsData =
newDomainBase("example.tld").asBuilder().setDsData(dsData).build(); newDomainBase("example.tld").asBuilder().setDsData(dsData).build();
jpaTm().transact(() -> jpaTm().put(domainWithDsData)); insertInDb(domainWithDsData);
// Replay a version of that domain without the dsData // Replay a version of that domain without the dsData
Key<CommitLogManifest> manifestKeyOne = Key<CommitLogManifest> manifestKeyOne =
@ -639,7 +625,7 @@ public class ReplayCommitLogsToSqlActionTest {
// Create an object (any object) to delete via replay to trigger Hibernate flush events // Create an object (any object) to delete via replay to trigger Hibernate flush events
TestObject testObject = TestObject.create("foo", "bar"); TestObject testObject = TestObject.create("foo", "bar");
jpaTm().transact(() -> jpaTm().put(testObject)); insertInDb(testObject);
// Replay the original domain, with the original dsData // Replay the original domain, with the original dsData
Key<CommitLogManifest> manifestKeyTwo = Key<CommitLogManifest> manifestKeyTwo =

View file

@ -14,7 +14,6 @@
package google.registry.beam.common; package google.registry.beam.common;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.AppEngineExtension.makeRegistrar1; import static google.registry.testing.AppEngineExtension.makeRegistrar1;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newRegistry; import static google.registry.testing.DatabaseHelper.newRegistry;
@ -78,7 +77,7 @@ public class RegistryJpaReadTest {
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
Registrar ofyRegistrar = AppEngineExtension.makeRegistrar2(); Registrar ofyRegistrar = AppEngineExtension.makeRegistrar2();
jpaTm().transact(() -> jpaTm().put(ofyRegistrar)); insertInDb(ofyRegistrar);
ImmutableList.Builder<ContactResource> builder = new ImmutableList.Builder<>(); ImmutableList.Builder<ContactResource> builder = new ImmutableList.Builder<>();
@ -87,7 +86,7 @@ public class RegistryJpaReadTest {
builder.add(contact); builder.add(contact);
} }
contacts = builder.build(); contacts = builder.build();
jpaTm().transact(() -> jpaTm().putAll(contacts)); insertInDb(contacts);
} }
@Test @Test

View file

@ -17,6 +17,8 @@ package google.registry.beam.common;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ImmutableObjectSubject.immutableObjectCorrespondence; import static google.registry.model.ImmutableObjectSubject.immutableObjectCorrespondence;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.newContactResource;
import static google.registry.testing.DatabaseHelper.putInDb;
import com.google.appengine.api.datastore.Entity; import com.google.appengine.api.datastore.Entity;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -32,7 +34,6 @@ import google.registry.model.registrar.Registrar;
import google.registry.persistence.transaction.JpaTestExtensions; import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationTestExtension; import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationTestExtension;
import google.registry.testing.AppEngineExtension; import google.registry.testing.AppEngineExtension;
import google.registry.testing.DatabaseHelper;
import google.registry.testing.DatastoreEntityExtension; import google.registry.testing.DatastoreEntityExtension;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import google.registry.testing.InjectExtension; import google.registry.testing.InjectExtension;
@ -76,12 +77,12 @@ class RegistryJpaWriteTest implements Serializable {
// Required for contacts created below. // Required for contacts created below.
Registrar ofyRegistrar = AppEngineExtension.makeRegistrar2(); Registrar ofyRegistrar = AppEngineExtension.makeRegistrar2();
store.insertOrUpdate(ofyRegistrar); store.insertOrUpdate(ofyRegistrar);
jpaTm().transact(() -> jpaTm().put(store.loadAsOfyEntity(ofyRegistrar))); putInDb(store.loadAsOfyEntity(ofyRegistrar));
ImmutableList.Builder<Entity> builder = new ImmutableList.Builder<>(); ImmutableList.Builder<Entity> builder = new ImmutableList.Builder<>();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
ContactResource contact = DatabaseHelper.newContactResource("contact_" + i); ContactResource contact = newContactResource("contact_" + i);
store.insertOrUpdate(contact); store.insertOrUpdate(contact);
builder.add(store.loadAsDatastoreEntity(contact)); builder.add(store.loadAsDatastoreEntity(contact));
} }
@ -106,8 +107,7 @@ class RegistryJpaWriteTest implements Serializable {
.withJpaConverter(Transforms::convertVersionedEntityToSqlEntity)); .withJpaConverter(Transforms::convertVersionedEntityToSqlEntity));
testPipeline.run().waitUntilFinish(); testPipeline.run().waitUntilFinish();
ImmutableList<?> sqlContacts = jpaTm().transact(() -> jpaTm().loadAllOf(ContactResource.class)); assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(ContactResource.class)))
assertThat(sqlContacts)
.comparingElementsUsing(immutableObjectCorrespondence("revisions", "updateTimestamp")) .comparingElementsUsing(immutableObjectCorrespondence("revisions", "updateTimestamp"))
.containsExactlyElementsIn( .containsExactlyElementsIn(
contacts.stream() contacts.stream()

View file

@ -25,6 +25,7 @@ import com.google.appengine.api.datastore.EntityNotFoundException;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.backup.CommitLogExports; import google.registry.backup.CommitLogExports;
import google.registry.backup.VersionedEntity; import google.registry.backup.VersionedEntity;
import google.registry.model.ImmutableObject;
import google.registry.model.ofy.CommitLogCheckpoint; import google.registry.model.ofy.CommitLogCheckpoint;
import google.registry.testing.AppEngineExtension; import google.registry.testing.AppEngineExtension;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
@ -124,7 +125,7 @@ public final class BackupTestStore implements AutoCloseable {
* *
* <p>See {@link #loadAsDatastoreEntity} and {@link VersionedEntity} for more information. * <p>See {@link #loadAsDatastoreEntity} and {@link VersionedEntity} for more information.
*/ */
public Object loadAsOfyEntity(Object ofyEntity) { public ImmutableObject loadAsOfyEntity(ImmutableObject ofyEntity) {
try { try {
return auditedOfy().load().fromEntity(datastoreService.get(Key.create(ofyEntity).getRaw())); return auditedOfy().load().fromEntity(datastoreService.get(Key.create(ofyEntity).getRaw()));
} catch (EntityNotFoundException e) { } catch (EntityNotFoundException e) {

View file

@ -189,8 +189,8 @@ public abstract class EntityTestCase {
return fields; return fields;
} }
/** Verify indexing for an entity. */ /** Verify Datastore indexing for an entity. */
public void verifyIndexing(Object obj, String... indexed) throws Exception { public void verifyDatastoreIndexing(Object obj, String... indexed) throws Exception {
Set<String> indexedSet = ImmutableSet.copyOf(indexed); Set<String> indexedSet = ImmutableSet.copyOf(indexed);
Set<String> allSet = getAllPotentiallyIndexedFieldPaths(obj.getClass()); Set<String> allSet = getAllPotentiallyIndexedFieldPaths(obj.getClass());
// Sanity test that the indexed fields we listed were found. // Sanity test that the indexed fields we listed were found.

View file

@ -236,24 +236,24 @@ public class BillingEventTest extends EntityTestCase {
@TestOfyOnly @TestOfyOnly
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing( verifyDatastoreIndexing(
oneTime, oneTime,
"clientId", "clientId",
"eventTime", "eventTime",
"billingTime", "billingTime",
"syntheticCreationTime", "syntheticCreationTime",
"allocationToken"); "allocationToken");
verifyIndexing( verifyDatastoreIndexing(
oneTimeSynthetic, oneTimeSynthetic,
"clientId", "clientId",
"eventTime", "eventTime",
"billingTime", "billingTime",
"syntheticCreationTime", "syntheticCreationTime",
"allocationToken"); "allocationToken");
verifyIndexing( verifyDatastoreIndexing(
recurring, "clientId", "eventTime", "recurrenceEndTime", "recurrenceTimeOfYear.timeString"); recurring, "clientId", "eventTime", "recurrenceEndTime", "recurrenceTimeOfYear.timeString");
verifyIndexing(cancellationOneTime, "clientId", "eventTime", "billingTime"); verifyDatastoreIndexing(cancellationOneTime, "clientId", "eventTime", "billingTime");
verifyIndexing(modification, "clientId", "eventTime"); verifyDatastoreIndexing(modification, "clientId", "eventTime");
} }
@TestOfyAndSql @TestOfyAndSql

View file

@ -75,7 +75,7 @@ public class CursorTest extends EntityTestCase {
final DateTime time = DateTime.parse("2012-07-12T03:30:00.000Z"); final DateTime time = DateTime.parse("2012-07-12T03:30:00.000Z");
tm().transact(() -> tm().put(Cursor.createGlobal(RECURRING_BILLING, time))); tm().transact(() -> tm().put(Cursor.createGlobal(RECURRING_BILLING, time)));
Cursor cursor = tm().transact(() -> tm().loadByKey(Cursor.createGlobalVKey(RECURRING_BILLING))); Cursor cursor = tm().transact(() -> tm().loadByKey(Cursor.createGlobalVKey(RECURRING_BILLING)));
verifyIndexing(cursor); verifyDatastoreIndexing(cursor);
} }
@TestOfyAndSql @TestOfyAndSql

View file

@ -17,15 +17,15 @@ package google.registry.model.contact;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat; import static com.google.common.truth.Truth8.assertThat;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm; import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm;
import static google.registry.testing.ContactResourceSubject.assertAboutContacts; import static google.registry.testing.ContactResourceSubject.assertAboutContacts;
import static google.registry.testing.DatabaseHelper.cloneAndSetAutoTimestamps; import static google.registry.testing.DatabaseHelper.cloneAndSetAutoTimestamps;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation; import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
import static google.registry.testing.SqlHelper.saveRegistrar;
import static google.registry.util.DateTimeUtils.END_OF_TIME; import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -33,7 +33,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.model.EntityTestCase; import google.registry.model.EntityTestCase;
import google.registry.model.ImmutableObjectSubject;
import google.registry.model.contact.Disclose.PostalInfoChoice; import google.registry.model.contact.Disclose.PostalInfoChoice;
import google.registry.model.contact.PostalInfo.Type; import google.registry.model.contact.PostalInfo.Type;
import google.registry.model.eppcommon.AuthInfo.PasswordAuth; import google.registry.model.eppcommon.AuthInfo.PasswordAuth;
@ -45,11 +44,14 @@ import google.registry.model.index.ForeignKeyIndex;
import google.registry.model.index.ForeignKeyIndex.ForeignKeyContactIndex; import google.registry.model.index.ForeignKeyIndex.ForeignKeyContactIndex;
import google.registry.model.transfer.ContactTransferData; import google.registry.model.transfer.ContactTransferData;
import google.registry.model.transfer.TransferStatus; import google.registry.model.transfer.TransferStatus;
import google.registry.persistence.VKey; import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import google.registry.testing.TestOfyOnly;
import google.registry.testing.TestSqlOnly;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link ContactResource}. */ /** Unit tests for {@link ContactResource}. */
@DualDatabaseTest
public class ContactResourceTest extends EntityTestCase { public class ContactResourceTest extends EntityTestCase {
private ContactResource originalContact; private ContactResource originalContact;
@ -66,11 +68,11 @@ public class ContactResourceTest extends EntityTestCase {
new ContactResource.Builder() new ContactResource.Builder()
.setContactId("contact_id") .setContactId("contact_id")
.setRepoId("1-FOOBAR") .setRepoId("1-FOOBAR")
.setCreationRegistrarId("registrar1") .setCreationRegistrarId("TheRegistrar")
.setLastEppUpdateTime(fakeClock.nowUtc()) .setLastEppUpdateTime(fakeClock.nowUtc())
.setLastEppUpdateRegistrarId("registrar2") .setLastEppUpdateRegistrarId("NewRegistrar")
.setLastTransferTime(fakeClock.nowUtc()) .setLastTransferTime(fakeClock.nowUtc())
.setPersistedCurrentSponsorRegistrarId("registrar3") .setPersistedCurrentSponsorRegistrarId("NewRegistrar")
.setLocalizedPostalInfo( .setLocalizedPostalInfo(
new PostalInfo.Builder() new PostalInfo.Builder()
.setType(Type.LOCALIZED) .setType(Type.LOCALIZED)
@ -116,8 +118,8 @@ public class ContactResourceTest extends EntityTestCase {
.setStatusValues(ImmutableSet.of(StatusValue.OK)) .setStatusValues(ImmutableSet.of(StatusValue.OK))
.setTransferData( .setTransferData(
new ContactTransferData.Builder() new ContactTransferData.Builder()
.setGainingRegistrarId("gaining") .setGainingRegistrarId("TheRegistrar")
.setLosingRegistrarId("losing") .setLosingRegistrarId("NewRegistrar")
.setPendingTransferExpirationTime(fakeClock.nowUtc()) .setPendingTransferExpirationTime(fakeClock.nowUtc())
.setTransferRequestTime(fakeClock.nowUtc()) .setTransferRequestTime(fakeClock.nowUtc())
.setTransferStatus(TransferStatus.SERVER_APPROVED) .setTransferStatus(TransferStatus.SERVER_APPROVED)
@ -128,34 +130,28 @@ public class ContactResourceTest extends EntityTestCase {
contactResource = persistResource(cloneAndSetAutoTimestamps(originalContact)); contactResource = persistResource(cloneAndSetAutoTimestamps(originalContact));
} }
@Test @TestOfyAndSql
void testContactBaseToContactResource() { void testContactBaseToContactResource() {
ImmutableObjectSubject.assertAboutImmutableObjects() assertAboutImmutableObjects()
.that(new ContactResource.Builder().copyFrom(contactResource).build()) .that(new ContactResource.Builder().copyFrom(contactResource).build())
.isEqualExceptFields(contactResource, "updateTimestamp", "revisions"); .isEqualExceptFields(contactResource, "updateTimestamp", "revisions");
} }
@Test @TestSqlOnly
void testCloudSqlPersistence_failWhenViolateForeignKeyConstraint() { void testCloudSqlPersistence_failWhenViolateForeignKeyConstraint() {
assertThrowForeignKeyViolation(() -> insertInDb(originalContact)); assertThrowForeignKeyViolation(
() ->
insertInDb(
originalContact
.asBuilder()
.setRepoId("2-FOOBAR")
.setCreationRegistrarId("nonexistent-registrar")
.build()));
} }
@Test @TestSqlOnly
void testCloudSqlPersistence_succeed() { void testCloudSqlPersistence_succeed() {
saveRegistrar("registrar1"); ContactResource persisted = loadByEntity(originalContact);
saveRegistrar("registrar2");
saveRegistrar("registrar3");
saveRegistrar("gaining");
saveRegistrar("losing");
insertInDb(originalContact);
ContactResource persisted =
jpaTm()
.transact(
() ->
jpaTm()
.loadByKey(
VKey.createSql(ContactResource.class, originalContact.getRepoId())));
ContactResource fixed = ContactResource fixed =
originalContact originalContact
.asBuilder() .asBuilder()
@ -167,12 +163,10 @@ public class ContactResourceTest extends EntityTestCase {
.setServerApproveEntities(null) .setServerApproveEntities(null)
.build()) .build())
.build(); .build();
ImmutableObjectSubject.assertAboutImmutableObjects() assertAboutImmutableObjects().that(persisted).isEqualExceptFields(fixed, "updateTimestamp");
.that(persisted)
.isEqualExceptFields(fixed, "updateTimestamp");
} }
@Test @TestOfyAndSql
void testPersistence() { void testPersistence() {
assertThat( assertThat(
loadByForeignKey( loadByForeignKey(
@ -180,12 +174,13 @@ public class ContactResourceTest extends EntityTestCase {
.hasValue(contactResource); .hasValue(contactResource);
} }
@Test @TestOfyOnly
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing(contactResource, "deletionTime", "currentSponsorClientId", "searchName"); verifyDatastoreIndexing(
contactResource, "deletionTime", "currentSponsorClientId", "searchName");
} }
@Test @TestOfyAndSql
void testEmptyStringsBecomeNull() { void testEmptyStringsBecomeNull() {
assertThat(new ContactResource.Builder().setContactId(null).build().getContactId()).isNull(); assertThat(new ContactResource.Builder().setContactId(null).build().getContactId()).isNull();
assertThat(new ContactResource.Builder().setContactId("").build().getContactId()).isNull(); assertThat(new ContactResource.Builder().setContactId("").build().getContactId()).isNull();
@ -217,7 +212,7 @@ public class ContactResourceTest extends EntityTestCase {
.isNotNull(); .isNotNull();
} }
@Test @TestOfyAndSql
void testEmptyTransferDataBecomesNull() { void testEmptyTransferDataBecomesNull() {
ContactResource withNull = new ContactResource.Builder().setTransferData(null).build(); ContactResource withNull = new ContactResource.Builder().setTransferData(null).build();
ContactResource withEmpty = ContactResource withEmpty =
@ -226,7 +221,7 @@ public class ContactResourceTest extends EntityTestCase {
assertThat(withEmpty.transferData).isNull(); assertThat(withEmpty.transferData).isNull();
} }
@Test @TestOfyAndSql
void testImplicitStatusValues() { void testImplicitStatusValues() {
// OK is implicit if there's no other statuses. // OK is implicit if there's no other statuses.
assertAboutContacts() assertAboutContacts()
@ -248,7 +243,7 @@ public class ContactResourceTest extends EntityTestCase {
.hasExactlyStatusValues(StatusValue.CLIENT_HOLD); .hasExactlyStatusValues(StatusValue.CLIENT_HOLD);
} }
@Test @TestOfyAndSql
void testExpiredTransfer() { void testExpiredTransfer() {
ContactResource afterTransfer = ContactResource afterTransfer =
contactResource contactResource
@ -269,7 +264,7 @@ public class ContactResourceTest extends EntityTestCase {
assertThat(afterTransfer.getLastTransferTime()).isEqualTo(fakeClock.nowUtc().plusDays(1)); assertThat(afterTransfer.getLastTransferTime()).isEqualTo(fakeClock.nowUtc().plusDays(1));
} }
@Test @TestOfyAndSql
void testSetCreationTime_cantBeCalledTwice() { void testSetCreationTime_cantBeCalledTwice() {
IllegalStateException thrown = IllegalStateException thrown =
assertThrows( assertThrows(
@ -278,13 +273,13 @@ public class ContactResourceTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().contains("creationTime can only be set once"); assertThat(thrown).hasMessageThat().contains("creationTime can only be set once");
} }
@Test @TestOfyAndSql
void testToHydratedString_notCircular() { void testToHydratedString_notCircular() {
// If there are circular references, this will overflow the stack. // If there are circular references, this will overflow the stack.
contactResource.toHydratedString(); contactResource.toHydratedString();
} }
@Test @TestOfyAndSql
void testBeforeDatastoreSaveOnReplay_indexes() { void testBeforeDatastoreSaveOnReplay_indexes() {
ImmutableList<ForeignKeyContactIndex> foreignKeyIndexes = ImmutableList<ForeignKeyContactIndex> foreignKeyIndexes =
ofyTm().loadAllOf(ForeignKeyContactIndex.class); ofyTm().loadAllOf(ForeignKeyContactIndex.class);

View file

@ -19,6 +19,10 @@ import static google.registry.flows.domain.DomainTransferUtils.createPendingTran
import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects; import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.DatabaseHelper.loadByKey;
import static google.registry.testing.DatabaseHelper.updateInDb;
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation; import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
import static google.registry.testing.SqlHelper.saveRegistrar; import static google.registry.testing.SqlHelper.saveRegistrar;
import static google.registry.util.DateTimeUtils.END_OF_TIME; import static google.registry.util.DateTimeUtils.END_OF_TIME;
@ -139,40 +143,19 @@ public class DomainBaseSqlTest {
@TestSqlOnly @TestSqlOnly
void testDomainBasePersistence() { void testDomainBasePersistence() {
persistDomain(); persistDomain();
assertEqualDomainExcept(loadByKey(domain.createVKey()));
jpaTm()
.transact(
() -> {
DomainBase result = jpaTm().loadByKey(domain.createVKey());
assertEqualDomainExcept(result);
});
} }
@TestSqlOnly @TestSqlOnly
void testHostForeignKeyConstraints() { void testHostForeignKeyConstraints() {
assertThrowForeignKeyViolation( // Persist the domain without the associated host object.
() -> assertThrowForeignKeyViolation(() -> insertInDb(contact, contact2, domain));
jpaTm()
.transact(
() -> {
// Persist the domain without the associated host object.
jpaTm().insert(contact);
jpaTm().insert(contact2);
jpaTm().insert(domain);
}));
} }
@TestSqlOnly @TestSqlOnly
void testContactForeignKeyConstraints() { void testContactForeignKeyConstraints() {
assertThrowForeignKeyViolation( // Persist the domain without the associated contact objects.
() -> assertThrowForeignKeyViolation(() -> insertInDb(domain, host));
jpaTm()
.transact(
() -> {
// Persist the domain without the associated contact objects.
jpaTm().insert(domain);
jpaTm().insert(host);
}));
} }
@TestSqlOnly @TestSqlOnly
@ -184,13 +167,8 @@ public class DomainBaseSqlTest {
DomainBase persisted = jpaTm().loadByKey(domain.createVKey()); DomainBase persisted = jpaTm().loadByKey(domain.createVKey());
jpaTm().put(persisted.asBuilder().build()); jpaTm().put(persisted.asBuilder().build());
}); });
jpaTm() // Load the domain in its entirety.
.transact( assertEqualDomainExcept(loadByKey(domain.createVKey()));
() -> {
// Load the domain in its entirety.
DomainBase result = jpaTm().loadByKey(domain.createVKey());
assertEqualDomainExcept(result);
});
} }
@TestSqlOnly @TestSqlOnly
@ -379,24 +357,12 @@ public class DomainBaseSqlTest {
@TestSqlOnly @TestSqlOnly
void testUpdates() { void testUpdates() {
createTld("com"); createTld("com");
jpaTm() insertInDb(contact, contact2, domain, host);
.transact(
() -> {
jpaTm().insert(contact);
jpaTm().insert(contact2);
jpaTm().insert(domain);
jpaTm().insert(host);
});
domain = domain.asBuilder().setNameservers(ImmutableSet.of()).build(); domain = domain.asBuilder().setNameservers(ImmutableSet.of()).build();
jpaTm().transact(() -> jpaTm().put(domain)); updateInDb(domain);
jpaTm() assertAboutImmutableObjects()
.transact( .that(loadByEntity(domain))
() -> { .isEqualExceptFields(domain, "updateTimestamp", "creationTime");
DomainBase result = jpaTm().loadByKey(domain.createVKey());
assertAboutImmutableObjects()
.that(result)
.isEqualExceptFields(domain, "updateTimestamp", "creationTime");
});
} }
static ContactResource makeContact(String repoId) { static ContactResource makeContact(String repoId) {
@ -410,128 +376,109 @@ public class DomainBaseSqlTest {
private void persistDomain() { private void persistDomain() {
createTld("com"); createTld("com");
jpaTm() insertInDb(contact, contact2, domain, host);
.transact(
() -> {
// Persist the contacts. Note that these need to be persisted before the domain
// otherwise we get a foreign key constraint error. If we ever decide to defer the
// relevant foreign key checks to commit time, then the order would not matter.
jpaTm().insert(contact);
jpaTm().insert(contact2);
// Persist the domain.
jpaTm().insert(domain);
// Persist the host. This does _not_ need to be persisted before the domain,
// because only the row in the join table (DomainHost) is subject to foreign key
// constraints, and Hibernate knows to insert it after domain and host.
jpaTm().insert(host);
});
} }
@TestSqlOnly @TestSqlOnly
void persistDomainWithCompositeVKeys() { void persistDomainWithCompositeVKeys() {
createTld("com"); createTld("com");
jpaTm() historyEntry =
.transact( new DomainHistory.Builder()
() -> { .setId(100L)
historyEntry = .setType(HistoryEntry.Type.DOMAIN_CREATE)
new DomainHistory.Builder() .setPeriod(Period.create(1, Period.Unit.YEARS))
.setId(100L) .setModificationTime(DateTime.now(UTC))
.setType(HistoryEntry.Type.DOMAIN_CREATE) .setDomainRepoId("4-COM")
.setPeriod(Period.create(1, Period.Unit.YEARS)) .setRegistrarId("registrar1")
.setModificationTime(DateTime.now(UTC)) // These are non-null, but I don't think some tests set them.
.setDomainRepoId("4-COM") .setReason("felt like it")
.setRegistrarId("registrar1") .setRequestedByRegistrar(false)
// These are non-null, but I don't think some tests set them. .setXmlBytes(new byte[0])
.setReason("felt like it") .build();
.setRequestedByRegistrar(false) BillingEvent.Recurring billEvent =
.setXmlBytes(new byte[0]) new BillingEvent.Recurring.Builder()
.build(); .setId(200L)
BillingEvent.Recurring billEvent = .setReason(Reason.RENEW)
new BillingEvent.Recurring.Builder() .setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
.setId(200L) .setTargetId("example.com")
.setReason(Reason.RENEW) .setRegistrarId("registrar1")
.setFlags(ImmutableSet.of(Flag.AUTO_RENEW)) .setDomainRepoId("4-COM")
.setTargetId("example.com") .setDomainHistoryRevisionId(1L)
.setRegistrarId("registrar1") .setEventTime(DateTime.now(UTC).plusYears(1))
.setDomainRepoId("4-COM") .setRecurrenceEndTime(END_OF_TIME)
.setDomainHistoryRevisionId(1L) .setParent(historyEntry)
.setEventTime(DateTime.now(UTC).plusYears(1)) .build();
.setRecurrenceEndTime(END_OF_TIME) PollMessage.Autorenew autorenewPollMessage =
.setParent(historyEntry) new PollMessage.Autorenew.Builder()
.build(); .setId(300L)
PollMessage.Autorenew autorenewPollMessage = .setRegistrarId("registrar1")
new PollMessage.Autorenew.Builder() .setEventTime(DateTime.now(UTC).plusYears(1))
.setId(300L) .setParent(historyEntry)
.setRegistrarId("registrar1") .build();
.setEventTime(DateTime.now(UTC).plusYears(1)) PollMessage.OneTime deletePollMessage =
.setParent(historyEntry) new PollMessage.OneTime.Builder()
.build(); .setId(400L)
PollMessage.OneTime deletePollMessage = .setRegistrarId("registrar1")
new PollMessage.OneTime.Builder() .setEventTime(DateTime.now(UTC).plusYears(1))
.setId(400L) .setParent(historyEntry)
.setRegistrarId("registrar1") .build();
.setEventTime(DateTime.now(UTC).plusYears(1)) BillingEvent.OneTime oneTimeBillingEvent =
.setParent(historyEntry) new BillingEvent.OneTime.Builder()
.build(); .setId(500L)
BillingEvent.OneTime oneTimeBillingEvent = // Use SERVER_STATUS so we don't have to add a period.
new BillingEvent.OneTime.Builder() .setReason(Reason.SERVER_STATUS)
.setId(500L) .setTargetId("example.com")
// Use SERVER_STATUS so we don't have to add a period. .setRegistrarId("registrar1")
.setReason(Reason.SERVER_STATUS) .setDomainRepoId("4-COM")
.setTargetId("example.com") .setBillingTime(DateTime.now(UTC))
.setRegistrarId("registrar1") .setCost(Money.of(USD, 100))
.setDomainRepoId("4-COM") .setEventTime(DateTime.now(UTC).plusYears(1))
.setBillingTime(DateTime.now(UTC)) .setParent(historyEntry)
.setCost(Money.of(USD, 100)) .build();
.setEventTime(DateTime.now(UTC).plusYears(1)) DomainTransferData transferData =
.setParent(historyEntry) new DomainTransferData.Builder()
.build(); .setServerApproveBillingEvent(oneTimeBillingEvent.createVKey())
DomainTransferData transferData = .setServerApproveAutorenewEvent(billEvent.createVKey())
new DomainTransferData.Builder() .setServerApproveAutorenewPollMessage(autorenewPollMessage.createVKey())
.setServerApproveBillingEvent(oneTimeBillingEvent.createVKey()) .build();
.setServerApproveAutorenewEvent(billEvent.createVKey()) gracePeriods =
.setServerApproveAutorenewPollMessage(autorenewPollMessage.createVKey()) ImmutableSet.of(
.build(); GracePeriod.create(
gracePeriods = GracePeriodStatus.ADD,
ImmutableSet.of( "4-COM",
GracePeriod.create( END_OF_TIME,
GracePeriodStatus.ADD, "registrar1",
"4-COM", oneTimeBillingEvent.createVKey()),
END_OF_TIME, GracePeriod.createForRecurring(
"registrar1", GracePeriodStatus.AUTO_RENEW,
oneTimeBillingEvent.createVKey()), "4-COM",
GracePeriod.createForRecurring( END_OF_TIME,
GracePeriodStatus.AUTO_RENEW, "registrar1",
"4-COM", billEvent.createVKey()));
END_OF_TIME,
"registrar1",
billEvent.createVKey()));
jpaTm().insert(contact); domain =
jpaTm().insert(contact2); domain
jpaTm().insert(host); .asBuilder()
domain = .setAutorenewBillingEvent(billEvent.createVKey())
domain .setAutorenewPollMessage(autorenewPollMessage.createVKey())
.asBuilder() .setDeletePollMessage(deletePollMessage.createVKey())
.setAutorenewBillingEvent(billEvent.createVKey()) .setTransferData(transferData)
.setAutorenewPollMessage(autorenewPollMessage.createVKey()) .setGracePeriods(gracePeriods)
.setDeletePollMessage(deletePollMessage.createVKey()) .build();
.setTransferData(transferData) historyEntry = historyEntry.asBuilder().setDomain(domain).build();
.setGracePeriods(gracePeriods) insertInDb(
.build(); contact,
historyEntry = historyEntry.asBuilder().setDomain(domain).build(); contact2,
jpaTm().insert(historyEntry); host,
jpaTm().insert(autorenewPollMessage); historyEntry,
jpaTm().insert(billEvent); autorenewPollMessage,
jpaTm().insert(deletePollMessage); billEvent,
jpaTm().insert(oneTimeBillingEvent); deletePollMessage,
jpaTm().insert(domain); oneTimeBillingEvent,
}); domain);
// Store the existing BillingRecurrence VKey. This happens after the event has been persisted. // Store the existing BillingRecurrence VKey. This happens after the event has been persisted.
DomainBase persisted = jpaTm().transact(() -> jpaTm().loadByKey(domain.createVKey())); DomainBase persisted = loadByKey(domain.createVKey());
// Verify that the domain data has been persisted. // Verify that the domain data has been persisted.
// dsData still isn't persisted. gracePeriods appears to have the same values but for some // dsData still isn't persisted. gracePeriods appears to have the same values but for some
@ -539,8 +486,7 @@ public class DomainBaseSqlTest {
assertEqualDomainExcept(persisted, "creationTime", "dsData", "gracePeriods"); assertEqualDomainExcept(persisted, "creationTime", "dsData", "gracePeriods");
// Verify that the DomainContent object from the history record sets the fields correctly. // Verify that the DomainContent object from the history record sets the fields correctly.
DomainHistory persistedHistoryEntry = DomainHistory persistedHistoryEntry = loadByKey(historyEntry.createVKey());
jpaTm().transact(() -> jpaTm().loadByKey(historyEntry.createVKey()));
assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewPollMessage()) assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewPollMessage())
.isEqualTo(domain.getAutorenewPollMessage()); .isEqualTo(domain.getAutorenewPollMessage());
assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewBillingEvent()) assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewBillingEvent())
@ -562,114 +508,110 @@ public class DomainBaseSqlTest {
@TestSqlOnly @TestSqlOnly
void persistDomainWithLegacyVKeys() { void persistDomainWithLegacyVKeys() {
createTld("com"); createTld("com");
jpaTm() historyEntry =
.transact( new DomainHistory.Builder()
() -> { .setId(100L)
historyEntry = .setType(HistoryEntry.Type.DOMAIN_CREATE)
new DomainHistory.Builder() .setPeriod(Period.create(1, Period.Unit.YEARS))
.setId(100L) .setModificationTime(DateTime.now(UTC))
.setType(HistoryEntry.Type.DOMAIN_CREATE) .setDomainRepoId("4-COM")
.setPeriod(Period.create(1, Period.Unit.YEARS)) .setRegistrarId("registrar1")
.setModificationTime(DateTime.now(UTC)) // These are non-null, but I don't think some tests set them.
.setDomainRepoId("4-COM") .setReason("felt like it")
.setRegistrarId("registrar1") .setRequestedByRegistrar(false)
// These are non-null, but I don't think some tests set them. .setXmlBytes(new byte[0])
.setReason("felt like it") .build();
.setRequestedByRegistrar(false) BillingEvent.Recurring billEvent =
.setXmlBytes(new byte[0]) new BillingEvent.Recurring.Builder()
.build(); .setId(200L)
BillingEvent.Recurring billEvent = .setReason(Reason.RENEW)
new BillingEvent.Recurring.Builder() .setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
.setId(200L) .setTargetId("example.com")
.setReason(Reason.RENEW) .setRegistrarId("registrar1")
.setFlags(ImmutableSet.of(Flag.AUTO_RENEW)) .setDomainRepoId("4-COM")
.setTargetId("example.com") .setDomainHistoryRevisionId(1L)
.setRegistrarId("registrar1") .setEventTime(DateTime.now(UTC).plusYears(1))
.setDomainRepoId("4-COM") .setRecurrenceEndTime(END_OF_TIME)
.setDomainHistoryRevisionId(1L) .setParent(historyEntry)
.setEventTime(DateTime.now(UTC).plusYears(1)) .build();
.setRecurrenceEndTime(END_OF_TIME) PollMessage.Autorenew autorenewPollMessage =
.setParent(historyEntry) new PollMessage.Autorenew.Builder()
.build(); .setId(300L)
PollMessage.Autorenew autorenewPollMessage = .setRegistrarId("registrar1")
new PollMessage.Autorenew.Builder() .setEventTime(DateTime.now(UTC).plusYears(1))
.setId(300L) .setParent(historyEntry)
.setRegistrarId("registrar1") .build();
.setEventTime(DateTime.now(UTC).plusYears(1)) PollMessage.OneTime deletePollMessage =
.setParent(historyEntry) new PollMessage.OneTime.Builder()
.build(); .setId(400L)
PollMessage.OneTime deletePollMessage = .setRegistrarId("registrar1")
new PollMessage.OneTime.Builder() .setEventTime(DateTime.now(UTC).plusYears(1))
.setId(400L) .setParent(historyEntry)
.setRegistrarId("registrar1") .build();
.setEventTime(DateTime.now(UTC).plusYears(1)) BillingEvent.OneTime oneTimeBillingEvent =
.setParent(historyEntry) new BillingEvent.OneTime.Builder()
.build(); .setId(500L)
BillingEvent.OneTime oneTimeBillingEvent = // Use SERVER_STATUS so we don't have to add a period.
new BillingEvent.OneTime.Builder() .setReason(Reason.SERVER_STATUS)
.setId(500L) .setTargetId("example.com")
// Use SERVER_STATUS so we don't have to add a period. .setRegistrarId("registrar1")
.setReason(Reason.SERVER_STATUS) .setDomainRepoId("4-COM")
.setTargetId("example.com") .setBillingTime(DateTime.now(UTC))
.setRegistrarId("registrar1") .setCost(Money.of(USD, 100))
.setDomainRepoId("4-COM") .setEventTime(DateTime.now(UTC).plusYears(1))
.setBillingTime(DateTime.now(UTC)) .setParent(historyEntry)
.setCost(Money.of(USD, 100)) .build();
.setEventTime(DateTime.now(UTC).plusYears(1)) DomainTransferData transferData =
.setParent(historyEntry) createPendingTransferData(
.build(); new DomainTransferData.Builder()
DomainTransferData transferData = .setTransferRequestTrid(Trid.create("foo", "bar"))
createPendingTransferData( .setTransferRequestTime(fakeClock.nowUtc())
new DomainTransferData.Builder() .setGainingRegistrarId("registrar2")
.setTransferRequestTrid(Trid.create("foo", "bar")) .setLosingRegistrarId("registrar1")
.setTransferRequestTime(fakeClock.nowUtc()) .setPendingTransferExpirationTime(fakeClock.nowUtc().plusDays(1)),
.setGainingRegistrarId("registrar2") ImmutableSet.of(oneTimeBillingEvent, billEvent, autorenewPollMessage),
.setLosingRegistrarId("registrar1") Period.create(0, Unit.YEARS));
.setPendingTransferExpirationTime(fakeClock.nowUtc().plusDays(1)), gracePeriods =
ImmutableSet.of(oneTimeBillingEvent, billEvent, autorenewPollMessage), ImmutableSet.of(
Period.create(0, Unit.YEARS)); GracePeriod.create(
gracePeriods = GracePeriodStatus.ADD,
ImmutableSet.of( "4-COM",
GracePeriod.create( END_OF_TIME,
GracePeriodStatus.ADD, "registrar1",
"4-COM", oneTimeBillingEvent.createVKey()),
END_OF_TIME, GracePeriod.createForRecurring(
"registrar1", GracePeriodStatus.AUTO_RENEW,
oneTimeBillingEvent.createVKey()), "4-COM",
GracePeriod.createForRecurring( END_OF_TIME,
GracePeriodStatus.AUTO_RENEW, "registrar1",
"4-COM", billEvent.createVKey()));
END_OF_TIME,
"registrar1",
billEvent.createVKey()));
jpaTm().insert(contact); domain =
jpaTm().insert(contact2); domain
jpaTm().insert(host); .asBuilder()
domain = .setAutorenewBillingEvent(
domain createLegacyVKey(BillingEvent.Recurring.class, billEvent.getId()))
.asBuilder() .setAutorenewPollMessage(
.setAutorenewBillingEvent( createLegacyVKey(PollMessage.Autorenew.class, autorenewPollMessage.getId()))
createLegacyVKey(BillingEvent.Recurring.class, billEvent.getId())) .setDeletePollMessage(
.setAutorenewPollMessage( createLegacyVKey(PollMessage.OneTime.class, deletePollMessage.getId()))
createLegacyVKey( .setTransferData(transferData)
PollMessage.Autorenew.class, autorenewPollMessage.getId())) .setGracePeriods(gracePeriods)
.setDeletePollMessage( .build();
createLegacyVKey(PollMessage.OneTime.class, deletePollMessage.getId())) historyEntry = historyEntry.asBuilder().setDomain(domain).build();
.setTransferData(transferData) insertInDb(
.setGracePeriods(gracePeriods) contact,
.build(); contact2,
historyEntry = historyEntry.asBuilder().setDomain(domain).build(); host,
jpaTm().insert(historyEntry); historyEntry,
jpaTm().insert(autorenewPollMessage); autorenewPollMessage,
jpaTm().insert(billEvent); billEvent,
jpaTm().insert(deletePollMessage); deletePollMessage,
jpaTm().insert(oneTimeBillingEvent); oneTimeBillingEvent,
jpaTm().insert(domain); domain);
});
// Store the existing BillingRecurrence VKey. This happens after the event has been persisted. // Store the existing BillingRecurrence VKey. This happens after the event has been persisted.
DomainBase persisted = jpaTm().transact(() -> jpaTm().loadByKey(domain.createVKey())); DomainBase persisted = loadByKey(domain.createVKey());
// Verify that the domain data has been persisted. // Verify that the domain data has been persisted.
// dsData still isn't persisted. gracePeriods appears to have the same values but for some // dsData still isn't persisted. gracePeriods appears to have the same values but for some
@ -677,8 +619,7 @@ public class DomainBaseSqlTest {
assertEqualDomainExcept(persisted, "creationTime", "dsData", "gracePeriods"); assertEqualDomainExcept(persisted, "creationTime", "dsData", "gracePeriods");
// Verify that the DomainContent object from the history record sets the fields correctly. // Verify that the DomainContent object from the history record sets the fields correctly.
DomainHistory persistedHistoryEntry = DomainHistory persistedHistoryEntry = loadByKey(historyEntry.createVKey());
jpaTm().transact(() -> jpaTm().loadByKey(historyEntry.createVKey()));
assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewPollMessage()) assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewPollMessage())
.isEqualTo(domain.getAutorenewPollMessage()); .isEqualTo(domain.getAutorenewPollMessage());
assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewBillingEvent()) assertThat(persistedHistoryEntry.getDomainContent().get().getAutorenewBillingEvent())

View file

@ -208,7 +208,7 @@ public class DomainBaseTest extends EntityTestCase {
@Test @Test
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing( verifyDatastoreIndexing(
domain, domain,
"allContacts.contact", "allContacts.contact",
"fullyQualifiedDomainName", "fullyQualifiedDomainName",

View file

@ -97,7 +97,7 @@ public class AllocationTokenTest extends EntityTestCase {
void testIndexing() throws Exception { void testIndexing() throws Exception {
DomainBase domain = persistActiveDomain("blahdomain.foo"); DomainBase domain = persistActiveDomain("blahdomain.foo");
Key<HistoryEntry> historyEntryKey = Key.create(Key.create(domain), HistoryEntry.class, 1); Key<HistoryEntry> historyEntryKey = Key.create(Key.create(domain), HistoryEntry.class, 1);
verifyIndexing( verifyDatastoreIndexing(
persistResource( persistResource(
new AllocationToken.Builder() new AllocationToken.Builder()
.setToken("abc123") .setToken("abc123")

View file

@ -19,9 +19,9 @@ import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableO
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.DatabaseHelper.newContactResource; import static google.registry.testing.DatabaseHelper.newContactResource;
import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid; import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid;
import static google.registry.testing.SqlHelper.saveRegistrar;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
@ -32,23 +32,23 @@ import google.registry.model.contact.ContactResource;
import google.registry.model.eppcommon.Trid; import google.registry.model.eppcommon.Trid;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import org.junit.jupiter.api.Test; import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyOnly;
import google.registry.testing.TestSqlOnly;
/** Tests for {@link ContactHistory}. */ /** Tests for {@link ContactHistory}. */
@DualDatabaseTest
public class ContactHistoryTest extends EntityTestCase { public class ContactHistoryTest extends EntityTestCase {
ContactHistoryTest() { ContactHistoryTest() {
super(JpaEntityCoverageCheck.ENABLED); super(JpaEntityCoverageCheck.ENABLED);
} }
@Test @TestSqlOnly
void testPersistence() { void testPersistence() {
saveRegistrar("TheRegistrar");
ContactResource contact = newContactResourceWithRoid("contactId", "contact1"); ContactResource contact = newContactResourceWithRoid("contactId", "contact1");
insertInDb(contact); insertInDb(contact);
VKey<ContactResource> contactVKey = contact.createVKey(); ContactResource contactFromDb = loadByEntity(contact);
ContactResource contactFromDb = jpaTm().transact(() -> jpaTm().loadByKey(contactVKey));
ContactHistory contactHistory = createContactHistory(contactFromDb); ContactHistory contactHistory = createContactHistory(contactFromDb);
insertInDb(contactHistory); insertInDb(contactHistory);
jpaTm() jpaTm()
@ -60,14 +60,11 @@ public class ContactHistoryTest extends EntityTestCase {
}); });
} }
@Test @TestSqlOnly
void testLegacyPersistence_nullContactBase() { void testLegacyPersistence_nullContactBase() {
saveRegistrar("TheRegistrar");
ContactResource contact = newContactResourceWithRoid("contactId", "contact1"); ContactResource contact = newContactResourceWithRoid("contactId", "contact1");
insertInDb(contact); insertInDb(contact);
VKey<ContactResource> contactVKey = contact.createVKey(); ContactResource contactFromDb = loadByEntity(contact);
ContactResource contactFromDb = jpaTm().transact(() -> jpaTm().loadByKey(contactVKey));
ContactHistory contactHistory = ContactHistory contactHistory =
createContactHistory(contactFromDb).asBuilder().setContact(null).build(); createContactHistory(contactFromDb).asBuilder().setContact(null).build();
insertInDb(contactHistory); insertInDb(contactHistory);
@ -81,10 +78,8 @@ public class ContactHistoryTest extends EntityTestCase {
}); });
} }
@Test @TestOfyOnly
void testOfyPersistence() { void testOfyPersistence() {
saveRegistrar("TheRegistrar");
ContactResource contact = newContactResourceWithRoid("contactId", "contact1"); ContactResource contact = newContactResourceWithRoid("contactId", "contact1");
tm().transact(() -> tm().insert(contact)); tm().transact(() -> tm().insert(contact));
VKey<ContactResource> contactVKey = contact.createVKey(); VKey<ContactResource> contactVKey = contact.createVKey();
@ -105,9 +100,8 @@ public class ContactHistoryTest extends EntityTestCase {
assertThat(hostHistoryFromDb).isEqualTo(historyEntryFromDb); assertThat(hostHistoryFromDb).isEqualTo(historyEntryFromDb);
} }
@Test @TestSqlOnly
void testBeforeSqlSave_afterContactPersisted() { void testBeforeSqlSave_afterContactPersisted() {
saveRegistrar("TheRegistrar");
ContactResource contactResource = newContactResource("contactId"); ContactResource contactResource = newContactResource("contactId");
ContactHistory contactHistory = ContactHistory contactHistory =
new ContactHistory.Builder() new ContactHistory.Builder()

View file

@ -26,6 +26,7 @@ import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid; import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid;
import static google.registry.testing.DatabaseHelper.newDomainBase; import static google.registry.testing.DatabaseHelper.newDomainBase;
import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid; import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid;
import static google.registry.testing.DatabaseHelper.putInDb;
import static google.registry.util.DateTimeUtils.END_OF_TIME; import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -146,14 +147,8 @@ public class DomainHistoryTest extends EntityTestCase {
"tld", "TLD", ImmutableSortedMap.of(START_OF_TIME, GENERAL_AVAILABILITY)); "tld", "TLD", ImmutableSortedMap.of(START_OF_TIME, GENERAL_AVAILABILITY));
tm().transact(() -> tm().insert(registry)); tm().transact(() -> tm().insert(registry));
Registries.resetCache(); Registries.resetCache();
jpaTm() insertInDb(
.transact( registry, makeRegistrar2().asBuilder().setAllowedTlds(ImmutableSet.of("tld")).build());
() -> {
jpaTm().insert(registry);
jpaTm()
.insert(
makeRegistrar2().asBuilder().setAllowedTlds(ImmutableSet.of("tld")).build());
});
HostResource host = newHostResourceWithRoid("ns1.example.com", "host1"); HostResource host = newHostResourceWithRoid("ns1.example.com", "host1");
ContactResource contact = newContactResourceWithRoid("contactId", "contact1"); ContactResource contact = newContactResourceWithRoid("contactId", "contact1");
@ -164,12 +159,7 @@ public class DomainHistoryTest extends EntityTestCase {
tm().insert(host); tm().insert(host);
tm().insert(contact); tm().insert(contact);
}); });
jpaTm() insertInDb(host, contact);
.transact(
() -> {
jpaTm().insert(host);
jpaTm().insert(contact);
});
DomainBase domain = DomainBase domain =
newDomainBase("example.tld", "domainRepoId", contact) newDomainBase("example.tld", "domainRepoId", contact)
.asBuilder() .asBuilder()
@ -190,7 +180,7 @@ public class DomainHistoryTest extends EntityTestCase {
// Reload and rewrite. // Reload and rewrite.
DomainHistory domainHistoryFromDb2 = tm().transact(() -> tm().loadByKey(domainHistoryVKey)); DomainHistory domainHistoryFromDb2 = tm().transact(() -> tm().loadByKey(domainHistoryVKey));
jpaTm().transact(() -> jpaTm().put(domainHistoryFromDb2)); putInDb(domainHistoryFromDb2);
} }
@TestSqlOnly @TestSqlOnly

View file

@ -19,6 +19,7 @@ import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableO
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.DatabaseHelper.newHostResource; import static google.registry.testing.DatabaseHelper.newHostResource;
import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid; import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid;
import static google.registry.testing.SqlHelper.saveRegistrar; import static google.registry.testing.SqlHelper.saveRegistrar;
@ -32,24 +33,23 @@ import google.registry.model.host.HostHistory;
import google.registry.model.host.HostResource; import google.registry.model.host.HostResource;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import org.junit.jupiter.api.Test; import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyOnly;
import google.registry.testing.TestSqlOnly;
/** Tests for {@link HostHistory}. */ /** Tests for {@link HostHistory}. */
@DualDatabaseTest
public class HostHistoryTest extends EntityTestCase { public class HostHistoryTest extends EntityTestCase {
HostHistoryTest() { HostHistoryTest() {
super(JpaEntityCoverageCheck.ENABLED); super(JpaEntityCoverageCheck.ENABLED);
} }
@Test @TestSqlOnly
void testPersistence() { void testPersistence() {
saveRegistrar("TheRegistrar");
HostResource host = newHostResourceWithRoid("ns1.example.com", "host1"); HostResource host = newHostResourceWithRoid("ns1.example.com", "host1");
insertInDb(host); insertInDb(host);
VKey<HostResource> hostVKey = HostResource hostFromDb = loadByEntity(host);
VKey.create(HostResource.class, "host1", Key.create(HostResource.class, "host1"));
HostResource hostFromDb = jpaTm().transact(() -> jpaTm().loadByKey(hostVKey));
HostHistory hostHistory = createHostHistory(hostFromDb); HostHistory hostHistory = createHostHistory(hostFromDb);
insertInDb(hostHistory); insertInDb(hostHistory);
jpaTm() jpaTm()
@ -61,13 +61,12 @@ public class HostHistoryTest extends EntityTestCase {
}); });
} }
@Test @TestSqlOnly
void testLegacyPersistence_nullHostBase() { void testLegacyPersistence_nullHostBase() {
saveRegistrar("TheRegistrar");
HostResource host = newHostResourceWithRoid("ns1.example.com", "host1"); HostResource host = newHostResourceWithRoid("ns1.example.com", "host1");
insertInDb(host); insertInDb(host);
HostResource hostFromDb = jpaTm().transact(() -> jpaTm().loadByKey(host.createVKey())); HostResource hostFromDb = loadByEntity(host);
HostHistory hostHistory = createHostHistory(hostFromDb).asBuilder().setHost(null).build(); HostHistory hostHistory = createHostHistory(hostFromDb).asBuilder().setHost(null).build();
insertInDb(hostHistory); insertInDb(hostHistory);
@ -80,7 +79,7 @@ public class HostHistoryTest extends EntityTestCase {
}); });
} }
@Test @TestOfyOnly
void testOfySave() { void testOfySave() {
saveRegistrar("registrar1"); saveRegistrar("registrar1");
@ -105,9 +104,8 @@ public class HostHistoryTest extends EntityTestCase {
assertThat(hostHistoryFromDb).isEqualTo(historyEntryFromDb); assertThat(hostHistoryFromDb).isEqualTo(historyEntryFromDb);
} }
@Test @TestSqlOnly
void testBeforeSqlSave_afterHostPersisted() { void testBeforeSqlSave_afterHostPersisted() {
saveRegistrar("TheRegistrar");
HostResource hostResource = newHostResource("ns1.example.tld"); HostResource hostResource = newHostResource("ns1.example.tld");
HostHistory hostHistory = HostHistory hostHistory =
new HostHistory.Builder() new HostHistory.Builder()

View file

@ -20,6 +20,7 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm; import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByKey;
import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid; import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid;
import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid; import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid;
import static google.registry.util.CollectionUtils.nullToEmpty; import static google.registry.util.CollectionUtils.nullToEmpty;
@ -79,14 +80,8 @@ public class LegacyHistoryObjectTest extends EntityTestCase {
ContactHistory legacyContactHistory = (ContactHistory) fromObjectify; ContactHistory legacyContactHistory = (ContactHistory) fromObjectify;
// Next, save that from-Datastore object in SQL and verify we can load it back in // Next, save that from-Datastore object in SQL and verify we can load it back in
jpaTm() insertInDb(contact, legacyContactHistory);
.transact( ContactHistory legacyHistoryFromSql = loadByKey(legacyContactHistory.createVKey());
() -> {
jpaTm().insert(contact);
jpaTm().insert(legacyContactHistory);
});
ContactHistory legacyHistoryFromSql =
jpaTm().transact(() -> jpaTm().loadByKey(legacyContactHistory.createVKey()));
assertAboutImmutableObjects() assertAboutImmutableObjects()
.that(legacyContactHistory) .that(legacyContactHistory)
.isEqualExceptFields(legacyHistoryFromSql); .isEqualExceptFields(legacyHistoryFromSql);
@ -171,14 +166,8 @@ public class LegacyHistoryObjectTest extends EntityTestCase {
HostHistory legacyHostHistory = (HostHistory) fromObjectify; HostHistory legacyHostHistory = (HostHistory) fromObjectify;
// Next, save that from-Datastore object in SQL and verify we can load it back in // Next, save that from-Datastore object in SQL and verify we can load it back in
jpaTm() insertInDb(host, legacyHostHistory);
.transact( HostHistory legacyHistoryFromSql = loadByKey(legacyHostHistory.createVKey());
() -> {
jpaTm().insert(host);
jpaTm().insert(legacyHostHistory);
});
HostHistory legacyHistoryFromSql =
jpaTm().transact(() -> jpaTm().loadByKey(legacyHostHistory.createVKey()));
assertAboutImmutableObjects().that(legacyHostHistory).isEqualExceptFields(legacyHistoryFromSql); assertAboutImmutableObjects().that(legacyHostHistory).isEqualExceptFields(legacyHistoryFromSql);
// can't compare hostRepoId directly since it doesn't save the ofy key in SQL // can't compare hostRepoId directly since it doesn't save the ofy key in SQL
assertThat(legacyHostHistory.getParentVKey().getSqlKey()) assertThat(legacyHostHistory.getParentVKey().getSqlKey())

View file

@ -121,7 +121,7 @@ class HostResourceTest extends EntityTestCase {
void testIndexing() throws Exception { void testIndexing() throws Exception {
// Clone it and save it before running the indexing test so that its transferData fields are // Clone it and save it before running the indexing test so that its transferData fields are
// populated from the superordinate domain. // populated from the superordinate domain.
verifyIndexing( verifyDatastoreIndexing(
persistResource(host), persistResource(host),
"deletionTime", "deletionTime",
"fullyQualifiedHostName", "fullyQualifiedHostName",

View file

@ -49,7 +49,7 @@ class EppResourceIndexTest extends EntityTestCase {
@Test @Test
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing(Iterables.getOnlyElement(getEppResourceIndexObjects()), "kind"); verifyDatastoreIndexing(Iterables.getOnlyElement(getEppResourceIndexObjects()), "kind");
} }
@Test @Test

View file

@ -79,7 +79,7 @@ class ForeignKeyIndexTest extends EntityTestCase {
void testIndexing() throws Exception { void testIndexing() throws Exception {
// Persist a host and implicitly persist a ForeignKeyIndex for it. // Persist a host and implicitly persist a ForeignKeyIndex for it.
persistActiveHost("ns1.example.com"); persistActiveHost("ns1.example.com");
verifyIndexing( verifyDatastoreIndexing(
ForeignKeyIndex.load(HostResource.class, "ns1.example.com", fakeClock.nowUtc()), ForeignKeyIndex.load(HostResource.class, "ns1.example.com", fakeClock.nowUtc()),
"deletionTime"); "deletionTime");
} }

View file

@ -15,10 +15,10 @@
package google.registry.model.poll; package google.registry.model.poll;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByKey;
import static google.registry.testing.DatabaseHelper.newDomainBase; import static google.registry.testing.DatabaseHelper.newDomainBase;
import static google.registry.testing.DatabaseHelper.persistActiveContact; import static google.registry.testing.DatabaseHelper.persistActiveContact;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
@ -94,17 +94,13 @@ public class PollMessageTest extends EntityTestCase {
@TestSqlOnly @TestSqlOnly
void testCloudSqlSupportForPolymorphicVKey() { void testCloudSqlSupportForPolymorphicVKey() {
insertInDb(oneTime); insertInDb(oneTime);
PollMessage persistedOneTime = PollMessage persistedOneTime = loadByKey(VKey.createSql(PollMessage.class, oneTime.getId()));
jpaTm()
.transact(() -> jpaTm().loadByKey(VKey.createSql(PollMessage.class, oneTime.getId())));
assertThat(persistedOneTime).isInstanceOf(PollMessage.OneTime.class); assertThat(persistedOneTime).isInstanceOf(PollMessage.OneTime.class);
assertThat(persistedOneTime).isEqualTo(oneTime); assertThat(persistedOneTime).isEqualTo(oneTime);
insertInDb(autoRenew); insertInDb(autoRenew);
PollMessage persistedAutoRenew = PollMessage persistedAutoRenew =
jpaTm() loadByKey(VKey.createSql(PollMessage.class, autoRenew.getId()));
.transact(
() -> jpaTm().loadByKey(VKey.createSql(PollMessage.class, autoRenew.getId())));
assertThat(persistedAutoRenew).isInstanceOf(PollMessage.Autorenew.class); assertThat(persistedAutoRenew).isInstanceOf(PollMessage.Autorenew.class);
assertThat(persistedAutoRenew).isEqualTo(autoRenew); assertThat(persistedAutoRenew).isEqualTo(autoRenew);
} }
@ -149,6 +145,6 @@ public class PollMessageTest extends EntityTestCase {
.setAutorenewEndTime(fakeClock.nowUtc().plusDays(365)) .setAutorenewEndTime(fakeClock.nowUtc().plusDays(365))
.setTargetId("foobar.foo") .setTargetId("foobar.foo")
.build()); .build());
verifyIndexing(pollMessage); verifyDatastoreIndexing(pollMessage);
} }
} }

View file

@ -137,7 +137,7 @@ class RegistrarTest extends EntityTestCase {
@TestOfyOnly @TestOfyOnly
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing(registrar, "registrarName", "ianaIdentifier"); verifyDatastoreIndexing(registrar, "registrarName", "ianaIdentifier");
} }
@TestOfyAndSql @TestOfyAndSql

View file

@ -100,12 +100,7 @@ public class ReplicateToDatastoreActionTest {
TestObject bar = TestObject.create("bar"); TestObject bar = TestObject.create("bar");
TestObject baz = TestObject.create("baz"); TestObject baz = TestObject.create("baz");
jpaTm() insertInDb(foo, bar);
.transact(
() -> {
jpaTm().insert(foo);
jpaTm().insert(bar);
});
runAndVerifySuccess(); runAndVerifySuccess();
assertThat(ofyTm().transact(() -> ofyTm().loadByKey(foo.key()))).isEqualTo(foo); assertThat(ofyTm().transact(() -> ofyTm().loadByKey(foo.key()))).isEqualTo(foo);
@ -219,7 +214,7 @@ public class ReplicateToDatastoreActionTest {
@Test @Test
void testBeforeDatastoreSaveCallback() { void testBeforeDatastoreSaveCallback() {
TestObject testObject = TestObject.create("foo"); TestObject testObject = TestObject.create("foo");
jpaTm().transact(() -> jpaTm().put(testObject)); insertInDb(testObject);
action.run(); action.run();
assertThat(ofyTm().loadAllOf(TestObject.class)).containsExactly(testObject); assertThat(ofyTm().loadAllOf(TestObject.class)).containsExactly(testObject);
assertThat(TestObject.beforeDatastoreSaveCallCount).isEqualTo(1); assertThat(TestObject.beforeDatastoreSaveCallCount).isEqualTo(1);

View file

@ -157,6 +157,6 @@ class HistoryEntryTest extends EntityTestCase {
@TestOfyOnly @TestOfyOnly
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing(domainHistory.asHistoryEntry(), "modificationTime", "clientId"); verifyDatastoreIndexing(domainHistory.asHistoryEntry(), "modificationTime", "clientId");
} }
} }

View file

@ -14,11 +14,12 @@
package google.registry.model.reporting; package google.registry.model.reporting;
import static com.google.common.truth.Truth.assertThat; import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.model.reporting.Spec11ThreatMatch.ThreatType.MALWARE; import static google.registry.model.reporting.Spec11ThreatMatch.ThreatType.MALWARE;
import static google.registry.model.reporting.Spec11ThreatMatch.ThreatType.UNWANTED_SOFTWARE; import static google.registry.model.reporting.Spec11ThreatMatch.ThreatType.UNWANTED_SOFTWARE;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation; import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
import static google.registry.testing.SqlHelper.saveRegistrar; import static google.registry.testing.SqlHelper.saveRegistrar;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -107,53 +108,20 @@ public final class Spec11ThreatMatchTest extends EntityTestCase {
void testPersistence() { void testPersistence() {
createTld("tld"); createTld("tld");
saveRegistrar(REGISTRAR_ID); saveRegistrar(REGISTRAR_ID);
insertInDb(registrantContact, domain, host, threat);
jpaTm() assertAboutImmutableObjects().that(loadByEntity(threat)).isEqualExceptFields(threat, "id");
.transact(
() -> {
jpaTm().insert(registrantContact);
jpaTm().insert(domain);
jpaTm().insert(host);
jpaTm().insert(threat);
});
VKey<Spec11ThreatMatch> threatVKey = VKey.createSql(Spec11ThreatMatch.class, threat.getId());
Spec11ThreatMatch persistedThreat = jpaTm().transact(() -> jpaTm().loadByKey(threatVKey));
// Threat object saved for the first time doesn't have an ID; it is generated by SQL
threat.id = persistedThreat.id;
assertThat(threat).isEqualTo(persistedThreat);
} }
@TestSqlOnly @TestSqlOnly
@Disabled("We can't rely on foreign keys until we've migrated to SQL") @Disabled("We can't rely on foreign keys until we've migrated to SQL")
void testThreatForeignKeyConstraints() { void testThreatForeignKeyConstraints() {
assertThrowForeignKeyViolation( // Persist the threat without the associated registrar.
() -> { assertThrowForeignKeyViolation(() -> insertInDb(host, registrantContact, domain, threat));
jpaTm()
.transact(
() -> {
// Persist the threat without the associated registrar.
jpaTm().insert(host);
jpaTm().insert(registrantContact);
jpaTm().insert(domain);
jpaTm().insert(threat);
});
});
saveRegistrar(REGISTRAR_ID); saveRegistrar(REGISTRAR_ID);
assertThrowForeignKeyViolation( // Persist the threat without the associated domain.
() -> { assertThrowForeignKeyViolation(() -> insertInDb(registrantContact, host, threat));
jpaTm()
.transact(
() -> {
// Persist the threat without the associated domain.
jpaTm().insert(registrantContact);
jpaTm().insert(host);
jpaTm().insert(threat);
});
});
} }
@TestOfyAndSql @TestOfyAndSql

View file

@ -95,7 +95,7 @@ public final class RegistryTest extends EntityTestCase {
@TestOfyOnly @TestOfyOnly
void testIndexing() throws Exception { void testIndexing() throws Exception {
verifyIndexing(Registry.get("tld")); verifyDatastoreIndexing(Registry.get("tld"));
} }
@TestOfyAndSql @TestOfyAndSql

View file

@ -16,6 +16,7 @@ package google.registry.persistence.transaction;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import google.registry.model.ImmutableObject; import google.registry.model.ImmutableObject;
@ -49,7 +50,7 @@ class CriteriaQueryBuilderTest {
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
jpaTm().transact(() -> jpaTm().putAll(ImmutableList.of(entity1, entity2, entity3))); insertInDb(entity1, entity2, entity3);
} }
@Test @Test

View file

@ -18,7 +18,9 @@ import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.assertDetachedFromEntityManager; import static google.registry.testing.DatabaseHelper.assertDetachedFromEntityManager;
import static google.registry.testing.DatabaseHelper.existsInDb;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByKey;
import static google.registry.testing.TestDataHelper.fileClassPath; import static google.registry.testing.TestDataHelper.fileClassPath;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -46,6 +48,8 @@ import javax.persistence.IdClass;
import javax.persistence.OptimisticLockException; import javax.persistence.OptimisticLockException;
import javax.persistence.RollbackException; import javax.persistence.RollbackException;
import org.hibernate.exception.JDBCConnectionException; import org.hibernate.exception.JDBCConnectionException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.extension.RegisterExtension;
@ -80,6 +84,16 @@ class JpaTransactionManagerImplTest {
TestEntity.class, TestCompoundIdEntity.class, TestNamedCompoundIdEntity.class) TestEntity.class, TestCompoundIdEntity.class, TestNamedCompoundIdEntity.class)
.buildUnitTestExtension(); .buildUnitTestExtension();
@BeforeEach
void beforeEach() {
TransactionManagerFactory.setTmForTest(jpaTm());
}
@AfterEach
void afterEach() {
TransactionManagerFactory.removeTmOverrideForTest();
}
@Test @Test
void transact_succeeds() { void transact_succeeds() {
assertPersonEmpty(); assertPersonEmpty();
@ -139,10 +153,10 @@ class JpaTransactionManagerImplTest {
@Test @Test
void insert_succeeds() { void insert_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse(); assertThat(existsInDb(theEntity)).isFalse();
insertInDb(theEntity); jpaTm().transact(() -> jpaTm().insert(theEntity));
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isTrue(); assertThat(existsInDb(theEntity)).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().loadByKey(theEntityKey))).isEqualTo(theEntity); assertThat(loadByKey(theEntityKey)).isEqualTo(theEntity);
} }
@Test @Test
@ -258,17 +272,17 @@ class JpaTransactionManagerImplTest {
@Test @Test
void insert_throwsExceptionIfEntityExists() { void insert_throwsExceptionIfEntityExists() {
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse(); assertThat(existsInDb(theEntity)).isFalse();
insertInDb(theEntity); jpaTm().transact(() -> jpaTm().insert(theEntity));
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isTrue(); assertThat(existsInDb(theEntity)).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().loadByKey(theEntityKey))).isEqualTo(theEntity); assertThat(loadByKey(theEntityKey)).isEqualTo(theEntity);
assertThrows(RollbackException.class, () -> insertInDb(theEntity)); assertThrows(RollbackException.class, () -> jpaTm().transact(() -> jpaTm().insert(theEntity)));
} }
@Test @Test
void createCompoundIdEntity_succeeds() { void createCompoundIdEntity_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isFalse(); assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isFalse();
insertInDb(compoundIdEntity); jpaTm().transact(() -> jpaTm().insert(compoundIdEntity));
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isTrue(); assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().loadByKey(compoundIdEntityKey))) assertThat(jpaTm().transact(() -> jpaTm().loadByKey(compoundIdEntityKey)))
.isEqualTo(compoundIdEntity); .isEqualTo(compoundIdEntity);
@ -278,18 +292,12 @@ class JpaTransactionManagerImplTest {
void createNamedCompoundIdEntity_succeeds() { void createNamedCompoundIdEntity_succeeds() {
// Compound IDs should also work even if the field names don't match up exactly // Compound IDs should also work even if the field names don't match up exactly
TestNamedCompoundIdEntity entity = new TestNamedCompoundIdEntity("foo", 1); TestNamedCompoundIdEntity entity = new TestNamedCompoundIdEntity("foo", 1);
insertInDb(entity); jpaTm().transact(() -> jpaTm().insert(entity));
jpaTm() assertThat(existsInDb(entity)).isTrue();
.transact( assertThat(
() -> { loadByKey(
assertThat(jpaTm().exists(entity)).isTrue(); VKey.createSql(TestNamedCompoundIdEntity.class, new NamedCompoundId("foo", 1))))
assertThat( .isEqualTo(entity);
jpaTm()
.loadByKey(
VKey.createSql(
TestNamedCompoundIdEntity.class, new NamedCompoundId("foo", 1))))
.isEqualTo(entity);
});
} }
@Test @Test

View file

@ -17,7 +17,9 @@ package google.registry.schema.registrar;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.registrar.RegistrarContact.Type.WHOIS; import static google.registry.model.registrar.RegistrarContact.Type.WHOIS;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.setTmForTest;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.SqlHelper.saveRegistrar; import static google.registry.testing.SqlHelper.saveRegistrar;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -48,6 +50,7 @@ class RegistrarContactTest {
@BeforeEach @BeforeEach
public void beforeEach() { public void beforeEach() {
setTmForTest(jpaTm());
testRegistrar = saveRegistrar("registrarId"); testRegistrar = saveRegistrar("registrarId");
testRegistrarPoc = testRegistrarPoc =
new RegistrarContact.Builder() new RegistrarContact.Builder()
@ -67,8 +70,6 @@ class RegistrarContactTest {
@Test @Test
void testPersistence_succeeds() { void testPersistence_succeeds() {
insertInDb(testRegistrarPoc); insertInDb(testRegistrarPoc);
RegistrarContact persisted = assertThat(loadByEntity(testRegistrarPoc)).isEqualTo(testRegistrarPoc);
jpaTm().transact(() -> jpaTm().loadByKey(testRegistrarPoc.createVKey()));
assertThat(persisted).isEqualTo(testRegistrarPoc);
} }
} }

View file

@ -16,7 +16,10 @@ package google.registry.schema.registrar;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.existsInDb;
import static google.registry.testing.DatabaseHelper.insertInDb; import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.loadByKey;
import static google.registry.testing.DatabaseHelper.updateInDb;
import static org.joda.time.DateTimeZone.UTC; import static org.joda.time.DateTimeZone.UTC;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -26,9 +29,11 @@ import google.registry.model.registrar.RegistrarAddress;
import google.registry.persistence.VKey; import google.registry.persistence.VKey;
import google.registry.persistence.transaction.JpaTestExtensions; import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationWithCoverageExtension; import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationWithCoverageExtension;
import google.registry.persistence.transaction.TransactionManagerFactory;
import google.registry.testing.DatastoreEntityExtension; import google.registry.testing.DatastoreEntityExtension;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -52,7 +57,8 @@ public class RegistrarDaoTest {
private Registrar testRegistrar; private Registrar testRegistrar;
@BeforeEach @BeforeEach
void setUp() { void beforeEach() {
TransactionManagerFactory.setTmForTest(jpaTm());
testRegistrar = testRegistrar =
new Registrar.Builder() new Registrar.Builder()
.setType(Registrar.Type.TEST) .setType(Registrar.Type.TEST)
@ -69,41 +75,39 @@ public class RegistrarDaoTest {
.build(); .build();
} }
@AfterEach
void afterEach() {
TransactionManagerFactory.removeTmOverrideForTest();
}
@Test @Test
void saveNew_worksSuccessfully() { void saveNew_worksSuccessfully() {
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isFalse(); assertThat(existsInDb(testRegistrar)).isFalse();
insertInDb(testRegistrar); insertInDb(testRegistrar);
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isTrue(); assertThat(existsInDb(testRegistrar)).isTrue();
} }
@Test @Test
void update_worksSuccessfully() { void update_worksSuccessfully() {
insertInDb(testRegistrar); insertInDb(testRegistrar);
Registrar persisted = jpaTm().transact(() -> jpaTm().loadByKey(registrarKey)); Registrar persisted = loadByKey(registrarKey);
assertThat(persisted.getRegistrarName()).isEqualTo("registrarName"); assertThat(persisted.getRegistrarName()).isEqualTo("registrarName");
jpaTm() updateInDb(persisted.asBuilder().setRegistrarName("changedRegistrarName").build());
.transact( Registrar updated = loadByKey(registrarKey);
() ->
jpaTm()
.update(
persisted.asBuilder().setRegistrarName("changedRegistrarName").build()));
Registrar updated = jpaTm().transact(() -> jpaTm().loadByKey(registrarKey));
assertThat(updated.getRegistrarName()).isEqualTo("changedRegistrarName"); assertThat(updated.getRegistrarName()).isEqualTo("changedRegistrarName");
} }
@Test @Test
void update_throwsExceptionWhenEntityDoesNotExist() { void update_throwsExceptionWhenEntityDoesNotExist() {
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isFalse(); assertThat(existsInDb(testRegistrar)).isFalse();
assertThrows( assertThrows(IllegalArgumentException.class, () -> updateInDb(testRegistrar));
IllegalArgumentException.class,
() -> jpaTm().transact(() -> jpaTm().update(testRegistrar)));
} }
@Test @Test
void load_worksSuccessfully() { void load_worksSuccessfully() {
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isFalse(); assertThat(existsInDb(testRegistrar)).isFalse();
insertInDb(testRegistrar); insertInDb(testRegistrar);
Registrar persisted = jpaTm().transact(() -> jpaTm().loadByKey(registrarKey)); Registrar persisted = loadByKey(registrarKey);
assertThat(persisted.getRegistrarId()).isEqualTo("registrarId"); assertThat(persisted.getRegistrarId()).isEqualTo("registrarId");
assertThat(persisted.getRegistrarName()).isEqualTo("registrarName"); assertThat(persisted.getRegistrarName()).isEqualTo("registrarName");

View file

@ -986,18 +986,18 @@ public class DatabaseHelper {
* <p><b>Note:</b> Your resource will not be enrolled in a commit log. If you want backups, use * <p><b>Note:</b> Your resource will not be enrolled in a commit log. If you want backups, use
* {@link #persistResourceWithCommitLog(Object)}. * {@link #persistResourceWithCommitLog(Object)}.
*/ */
public static <R> R persistResource(final R resource) { public static <R extends ImmutableObject> R persistResource(final R resource) {
return persistResource(resource, false); return persistResource(resource, false);
} }
/** Same as {@link #persistResource(Object)} with backups enabled. */ /** Same as {@link #persistResource(Object)} with backups enabled. */
public static <R> R persistResourceWithCommitLog(final R resource) { public static <R extends ImmutableObject> R persistResourceWithCommitLog(final R resource) {
return persistResource(resource, true); return persistResource(resource, true);
} }
private static <R> void saveResource(R resource, boolean wantBackup) { private static <R extends ImmutableObject> void saveResource(R resource, boolean wantBackup) {
if (tm().isOfy()) { if (tm().isOfy()) {
Consumer<Object> saver = Consumer<ImmutableObject> saver =
wantBackup || alwaysSaveWithBackup ? tm()::put : tm()::putWithoutBackup; wantBackup || alwaysSaveWithBackup ? tm()::put : tm()::putWithoutBackup;
saver.accept(resource); saver.accept(resource);
if (resource instanceof EppResource) { if (resource instanceof EppResource) {
@ -1011,7 +1011,7 @@ public class DatabaseHelper {
} }
private static <R extends EppResource> void persistEppResourceExtras( private static <R extends EppResource> void persistEppResourceExtras(
R resource, EppResourceIndex index, Consumer<Object> saver) { R resource, EppResourceIndex index, Consumer<ImmutableObject> saver) {
assertWithMessage("Cannot persist an EppResource with a missing repoId in tests") assertWithMessage("Cannot persist an EppResource with a missing repoId in tests")
.that(resource.getRepoId()) .that(resource.getRepoId())
.isNotEmpty(); .isNotEmpty();
@ -1021,7 +1021,8 @@ public class DatabaseHelper {
} }
} }
private static <R> R persistResource(final R resource, final boolean wantBackup) { private static <R extends ImmutableObject> R persistResource(
final R resource, final boolean wantBackup) {
assertWithMessage("Attempting to persist a Builder is almost certainly an error in test code") assertWithMessage("Attempting to persist a Builder is almost certainly an error in test code")
.that(resource) .that(resource)
.isNotInstanceOf(Buildable.Builder.class); .isNotInstanceOf(Buildable.Builder.class);
@ -1042,7 +1043,7 @@ public class DatabaseHelper {
tm().transact( tm().transact(
() -> { () -> {
if (tm().isOfy()) { if (tm().isOfy()) {
Consumer<Object> saver = tm()::put; Consumer<ImmutableObject> saver = tm()::put;
saver.accept(resource); saver.accept(resource);
persistEppResourceExtras(resource, eppResourceIndex, saver); persistEppResourceExtras(resource, eppResourceIndex, saver);
} else { } else {
@ -1054,11 +1055,12 @@ public class DatabaseHelper {
return transactIfJpaTm(() -> tm().loadByEntity(resource)); return transactIfJpaTm(() -> tm().loadByEntity(resource));
} }
public static <R> void persistResources(final Iterable<R> resources) { public static <R extends ImmutableObject> void persistResources(final Iterable<R> resources) {
persistResources(resources, false); persistResources(resources, false);
} }
private static <R> void persistResources(final Iterable<R> resources, final boolean wantBackup) { private static <R extends ImmutableObject> void persistResources(
final Iterable<R> resources, final boolean wantBackup) {
for (R resource : resources) { for (R resource : resources) {
assertWithMessage("Attempting to persist a Builder is almost certainly an error in test code") assertWithMessage("Attempting to persist a Builder is almost certainly an error in test code")
.that(resource) .that(resource)
@ -1379,9 +1381,9 @@ public class DatabaseHelper {
return transactIfJpaTm(() -> tm().loadByEntitiesIfPresent(entities)); return transactIfJpaTm(() -> tm().loadByEntitiesIfPresent(entities));
} }
/** Returns whether or not the given entity exists in the database. */ /** Returns whether or not the given entity exists in Cloud SQL. */
public static boolean existsInDatabase(Object object) { public static boolean existsInDb(ImmutableObject object) {
return transactIfJpaTm(() -> tm().exists(object)); return jpaTm().transact(() -> jpaTm().exists(object));
} }
/** Inserts the given entity/entities into Cloud SQL in a single transaction. */ /** Inserts the given entity/entities into Cloud SQL in a single transaction. */
@ -1394,6 +1396,26 @@ public class DatabaseHelper {
jpaTm().transact(() -> jpaTm().insertAll(entities)); jpaTm().transact(() -> jpaTm().insertAll(entities));
} }
/** Puts the given entity/entities into Cloud SQL in a single transaction. */
public static <T extends ImmutableObject> void putInDb(T... entities) {
jpaTm().transact(() -> jpaTm().putAll(entities));
}
/** Puts the given entities into Cloud SQL in a single transaction. */
public static <T extends ImmutableObject> void putInDb(ImmutableCollection<T> entities) {
jpaTm().transact(() -> jpaTm().putAll(entities));
}
/** Updates the given entities in Cloud SQL in a single transaction. */
public static <T extends ImmutableObject> void updateInDb(T... entities) {
jpaTm().transact(() -> jpaTm().updateAll(entities));
}
/** Updates the given entities in Cloud SQL in a single transaction. */
public static <T extends ImmutableObject> void updateInDb(ImmutableCollection<T> entities) {
jpaTm().transact(() -> jpaTm().updateAll(entities));
}
/** /**
* In JPA mode, asserts that the given entity is detached from the current entity manager. * In JPA mode, asserts that the given entity is detached from the current entity manager.
* *

View file

@ -22,6 +22,7 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static google.registry.reporting.spec11.Spec11RegistrarThreatMatchesParserTest.sampleThreatMatches; import static google.registry.reporting.spec11.Spec11RegistrarThreatMatchesParserTest.sampleThreatMatches;
import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.deleteResource; import static google.registry.testing.DatabaseHelper.deleteResource;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newDomainBase; import static google.registry.testing.DatabaseHelper.newDomainBase;
import static google.registry.testing.DatabaseHelper.persistActiveDomain; import static google.registry.testing.DatabaseHelper.persistActiveDomain;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
@ -175,7 +176,7 @@ public class BackfillSpec11ThreatMatchesCommandTest
.setRegistrarId("TheRegistrar") .setRegistrarId("TheRegistrar")
.setThreatTypes(ImmutableSet.of(ThreatType.MALWARE)) .setThreatTypes(ImmutableSet.of(ThreatType.MALWARE))
.build(); .build();
jpaTm().transact(() -> jpaTm().put(previous)); insertInDb(previous);
runCommandForced(); runCommandForced();
ImmutableList<Spec11ThreatMatch> threatMatches = ImmutableList<Spec11ThreatMatch> threatMatches =
@ -197,7 +198,7 @@ public class BackfillSpec11ThreatMatchesCommandTest
.setRegistrarId("TheRegistrar") .setRegistrarId("TheRegistrar")
.setThreatTypes(ImmutableSet.of(ThreatType.MALWARE)) .setThreatTypes(ImmutableSet.of(ThreatType.MALWARE))
.build(); .build();
jpaTm().transact(() -> jpaTm().put(previous)); insertInDb(previous);
runCommandForced("--overwrite_existing_dates"); runCommandForced("--overwrite_existing_dates");
verifyExactlyThreeEntriesInDbFromLastDay(); verifyExactlyThreeEntriesInDbFromLastDay();
@ -214,7 +215,7 @@ public class BackfillSpec11ThreatMatchesCommandTest
assertThrows(RuntimeException.class, this::runCommandForced); assertThrows(RuntimeException.class, this::runCommandForced);
assertThat(runtimeException.getCause().getClass()).isEqualTo(IOException.class); assertThat(runtimeException.getCause().getClass()).isEqualTo(IOException.class);
assertThat(runtimeException).hasCauseThat().hasMessageThat().isEqualTo("hi"); assertThat(runtimeException).hasCauseThat().hasMessageThat().isEqualTo("hi");
jpaTm().transact(() -> assertThat(jpaTm().loadAllOf(Spec11ThreatMatch.class)).isEmpty()); assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(Spec11ThreatMatch.class))).isEmpty();
} }
@TestOfyAndSql @TestOfyAndSql