Clean up tx manager insert() signature and add convenience helper method (#1325)

* Clean up tx manager insert() signature and add convenience helper method

This is the first of a series of PRs to clean up the type signatures on the
TransactionManager methods (which are way too generic), along with creating some
helper methods for use in tests only that don't require creating transactions
all over the place, thus reducing visual noise at callsites. This first method
is DatabaseHelper.insertInDb(), but there will be plenty of others. Note that
this is only for the Cloud SQL transaction manager -- I'm not bothering to
migrate any Datastore-only code, as that will be going away soon enough.
This commit is contained in:
Ben McIlwain 2021-09-17 14:45:07 -04:00 committed by GitHub
parent 742eff0b0a
commit b4f6280d6f
40 changed files with 173 additions and 110 deletions

View file

@ -32,6 +32,7 @@ import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import com.googlecode.objectify.cmd.Query;
import google.registry.model.ImmutableObject;
import google.registry.model.annotations.InCrossTld;
import google.registry.model.contact.ContactHistory;
import google.registry.model.domain.DomainHistory;
@ -127,6 +128,11 @@ public class DatastoreTransactionManager implements TransactionManager {
putAll(entities);
}
@Override
public void insertAll(ImmutableObject... entities) {
putAll(entities);
}
@Override
public void insertWithoutBackup(Object entity) {
putWithoutBackup(entity);
@ -143,7 +149,7 @@ public class DatastoreTransactionManager implements TransactionManager {
}
@Override
public void putAll(Object... entities) {
public void putAll(ImmutableObject... entities) {
syncIfTransactionless(
getOfy().save().entities(toDatastoreEntities(ImmutableList.copyOf(entities))));
}

View file

@ -312,6 +312,11 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
entities.forEach(this::insert);
}
@Override
public void insertAll(ImmutableObject... entities) {
insertAll(ImmutableSet.copyOf(entities));
}
@Override
public void insertWithoutBackup(Object entity) {
insert(entity);
@ -335,7 +340,7 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
}
@Override
public void putAll(Object... entities) {
public void putAll(ImmutableObject... entities) {
checkArgumentNotNull(entities, "entities must be specified");
assertInTransaction();
for (Object entity : entities) {

View file

@ -17,6 +17,7 @@ package google.registry.persistence.transaction;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.model.ImmutableObject;
import google.registry.model.annotations.InCrossTld;
import google.registry.persistence.VKey;
import java.util.NoSuchElementException;
@ -97,6 +98,9 @@ public interface TransactionManager {
/** Persists all new entities in the database, throws exception if any entity already exists. */
void insertAll(ImmutableCollection<?> entities);
/** Persists all new entities in the database, throws exception if any entity already exists. */
void insertAll(ImmutableObject... entities);
/**
* Persists a new entity in the database without writing any backup if the underlying database is
* Datastore.
@ -125,7 +129,7 @@ public interface TransactionManager {
void put(Object entity);
/** Persists all new entities or updates the existing entities in the database. */
void putAll(Object... entities);
void putAll(ImmutableObject... entities);
/** Persists all new entities or updates the existing entities in the database. */
void putAll(ImmutableCollection<?> entities);

View file

@ -16,6 +16,7 @@ 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.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newRegistry;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
@ -210,7 +211,6 @@ public class RegistryJpaReadTest {
null,
100L))
.build();
jpaTm()
.transact(() -> jpaTm().insertAll(ImmutableList.of(registry, registrar, contact, domain)));
insertInDb(registry, registrar, contact, domain);
}
}

View file

@ -22,6 +22,7 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static google.registry.testing.ContactResourceSubject.assertAboutContacts;
import static google.registry.testing.DatabaseHelper.cloneAndSetAutoTimestamps;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
import static google.registry.testing.SqlHelper.saveRegistrar;
@ -136,7 +137,7 @@ public class ContactResourceTest extends EntityTestCase {
@Test
void testCloudSqlPersistence_failWhenViolateForeignKeyConstraint() {
assertThrowForeignKeyViolation(() -> jpaTm().transact(() -> jpaTm().insert(originalContact)));
assertThrowForeignKeyViolation(() -> insertInDb(originalContact));
}
@Test
@ -146,7 +147,7 @@ public class ContactResourceTest extends EntityTestCase {
saveRegistrar("registrar3");
saveRegistrar("gaining");
saveRegistrar("losing");
jpaTm().transact(() -> jpaTm().insert(originalContact));
insertInDb(originalContact);
ContactResource persisted =
jpaTm()
.transact(

View file

@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newContactResource;
import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid;
import static google.registry.testing.SqlHelper.saveRegistrar;
@ -45,11 +46,11 @@ public class ContactHistoryTest extends EntityTestCase {
saveRegistrar("TheRegistrar");
ContactResource contact = newContactResourceWithRoid("contactId", "contact1");
jpaTm().transact(() -> jpaTm().insert(contact));
insertInDb(contact);
VKey<ContactResource> contactVKey = contact.createVKey();
ContactResource contactFromDb = jpaTm().transact(() -> jpaTm().loadByKey(contactVKey));
ContactHistory contactHistory = createContactHistory(contactFromDb);
jpaTm().transact(() -> jpaTm().insert(contactHistory));
insertInDb(contactHistory);
jpaTm()
.transact(
() -> {
@ -64,12 +65,12 @@ public class ContactHistoryTest extends EntityTestCase {
saveRegistrar("TheRegistrar");
ContactResource contact = newContactResourceWithRoid("contactId", "contact1");
jpaTm().transact(() -> jpaTm().insert(contact));
insertInDb(contact);
VKey<ContactResource> contactVKey = contact.createVKey();
ContactResource contactFromDb = jpaTm().transact(() -> jpaTm().loadByKey(contactVKey));
ContactHistory contactHistory =
createContactHistory(contactFromDb).asBuilder().setContact(null).build();
jpaTm().transact(() -> jpaTm().insert(contactHistory));
insertInDb(contactHistory);
jpaTm()
.transact(

View file

@ -22,6 +22,7 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.AppEngineExtension.makeRegistrar2;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid;
import static google.registry.testing.DatabaseHelper.newDomainBase;
import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid;
@ -74,7 +75,7 @@ public class DomainHistoryTest extends EntityTestCase {
void testPersistence() {
DomainBase domain = addGracePeriodForSql(createDomainWithContactsAndHosts());
DomainHistory domainHistory = createDomainHistory(domain);
jpaTm().transact(() -> jpaTm().insert(domainHistory));
insertInDb(domainHistory);
jpaTm()
.transact(
@ -89,7 +90,7 @@ public class DomainHistoryTest extends EntityTestCase {
void testLegacyPersistence_nullResource() {
DomainBase domain = addGracePeriodForSql(createDomainWithContactsAndHosts());
DomainHistory domainHistory = createDomainHistory(domain).asBuilder().setDomain(null).build();
jpaTm().transact(() -> jpaTm().insert(domainHistory));
insertInDb(domainHistory);
jpaTm()
.transact(
@ -175,7 +176,7 @@ public class DomainHistoryTest extends EntityTestCase {
.setNameservers(host.createVKey())
.build();
tm().transact(() -> tm().insert(domain));
jpaTm().transact(() -> jpaTm().insert(domain));
insertInDb(domain);
DomainHistory domainHistory = createDomainHistory(domain);
tm().transact(() -> tm().insert(domainHistory));
@ -185,7 +186,7 @@ public class DomainHistoryTest extends EntityTestCase {
DomainHistory domainHistoryFromDb = tm().transact(() -> tm().loadByKey(domainHistoryVKey));
// attempt to write to SQL.
jpaTm().transact(() -> jpaTm().insert(domainHistoryFromDb));
insertInDb(domainHistoryFromDb);
// Reload and rewrite.
DomainHistory domainHistoryFromDb2 = tm().transact(() -> tm().loadByKey(domainHistoryVKey));
@ -249,7 +250,7 @@ public class DomainHistoryTest extends EntityTestCase {
.setDsData(ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {0, 1, 2})))
.setDnsRefreshRequestTime(Optional.of(DateTime.parse("2020-03-09T16:40:00Z")))
.build();
jpaTm().transact(() -> jpaTm().insert(domain));
insertInDb(domain);
return domain;
}

View file

@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newHostResource;
import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid;
import static google.registry.testing.SqlHelper.saveRegistrar;
@ -45,12 +46,12 @@ public class HostHistoryTest extends EntityTestCase {
saveRegistrar("TheRegistrar");
HostResource host = newHostResourceWithRoid("ns1.example.com", "host1");
jpaTm().transact(() -> jpaTm().insert(host));
insertInDb(host);
VKey<HostResource> hostVKey =
VKey.create(HostResource.class, "host1", Key.create(HostResource.class, "host1"));
HostResource hostFromDb = jpaTm().transact(() -> jpaTm().loadByKey(hostVKey));
HostHistory hostHistory = createHostHistory(hostFromDb);
jpaTm().transact(() -> jpaTm().insert(hostHistory));
insertInDb(hostHistory);
jpaTm()
.transact(
() -> {
@ -64,11 +65,11 @@ public class HostHistoryTest extends EntityTestCase {
void testLegacyPersistence_nullHostBase() {
saveRegistrar("TheRegistrar");
HostResource host = newHostResourceWithRoid("ns1.example.com", "host1");
jpaTm().transact(() -> jpaTm().insert(host));
insertInDb(host);
HostResource hostFromDb = jpaTm().transact(() -> jpaTm().loadByKey(host.createVKey()));
HostHistory hostHistory = createHostHistory(hostFromDb).asBuilder().setHost(null).build();
jpaTm().transact(() -> jpaTm().insert(hostHistory));
insertInDb(hostHistory);
jpaTm()
.transact(

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.ofyTm;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newContactResourceWithRoid;
import static google.registry.testing.DatabaseHelper.newHostResourceWithRoid;
import static google.registry.util.CollectionUtils.nullToEmpty;
@ -128,7 +129,7 @@ public class LegacyHistoryObjectTest extends EntityTestCase {
DomainHistory legacyDomainHistory = (DomainHistory) fromObjectify;
// Next, save that from-Datastore object in SQL and verify we can load it back in
jpaTm().transact(() -> jpaTm().insert(legacyDomainHistory));
insertInDb(legacyDomainHistory);
jpaTm()
.transact(
() -> {

View file

@ -18,6 +18,7 @@ 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.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.DatabaseHelper.newDomainBase;
import static google.registry.testing.DatabaseHelper.persistActiveContact;
import static google.registry.testing.DatabaseHelper.persistResource;
@ -92,14 +93,14 @@ public class PollMessageTest extends EntityTestCase {
@TestSqlOnly
void testCloudSqlSupportForPolymorphicVKey() {
jpaTm().transact(() -> jpaTm().insert(oneTime));
insertInDb(oneTime);
PollMessage persistedOneTime =
jpaTm()
.transact(() -> jpaTm().loadByKey(VKey.createSql(PollMessage.class, oneTime.getId())));
assertThat(persistedOneTime).isInstanceOf(PollMessage.OneTime.class);
assertThat(persistedOneTime).isEqualTo(oneTime);
jpaTm().transact(() -> jpaTm().insert(autoRenew));
insertInDb(autoRenew);
PollMessage persistedAutoRenew =
jpaTm()
.transact(

View file

@ -17,6 +17,7 @@ package google.registry.model.replay;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.LogsSubject.assertAboutLogs;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.junit.Assert.assertThrows;
@ -118,7 +119,7 @@ public class ReplicateToDatastoreActionTest {
TestObject bar = TestObject.create("bar");
// Write a transaction containing "foo".
jpaTm().transact(() -> jpaTm().insert(foo));
insertInDb(foo);
task.run();
// Verify that it propagated to datastore, then remove "foo" directly from datastore.
@ -126,7 +127,7 @@ public class ReplicateToDatastoreActionTest {
ofyTm().transact(() -> ofyTm().delete(foo.key()));
// Write "bar"
jpaTm().transact(() -> jpaTm().insert(bar));
insertInDb(bar);
task.run();
// If we replayed only the most recent transaction, we should have "bar" but not "foo".
@ -140,12 +141,12 @@ public class ReplicateToDatastoreActionTest {
TestObject bar = TestObject.create("bar");
// Write a transaction and run just the batch fetch.
jpaTm().transact(() -> jpaTm().insert(foo));
insertInDb(foo);
List<TransactionEntity> txns1 = task.getTransactionBatch();
assertThat(txns1).hasSize(1);
// Write a second transaction and do another batch fetch.
jpaTm().transact(() -> jpaTm().insert(bar));
insertInDb(bar);
List<TransactionEntity> txns2 = task.getTransactionBatch();
assertThat(txns2).hasSize(2);
@ -173,7 +174,7 @@ public class ReplicateToDatastoreActionTest {
void testMissingTransactions() {
// Write a transaction (should have a transaction id of 1).
TestObject foo = TestObject.create("foo");
jpaTm().transact(() -> jpaTm().insert(foo));
insertInDb(foo);
// Force the last transaction id back to -1 so that we look for transaction 0.
ofyTm().transact(() -> ofyTm().insert(new LastSqlTransaction(-1)));
@ -189,7 +190,7 @@ public class ReplicateToDatastoreActionTest {
void testMissingTransactions_fullTask() {
// Write a transaction (should have a transaction id of 1).
TestObject foo = TestObject.create("foo");
jpaTm().transact(() -> jpaTm().insert(foo));
insertInDb(foo);
// Force the last transaction id back to -1 so that we look for transaction 0.
ofyTm().transact(() -> ofyTm().insert(new LastSqlTransaction(-1)));
@ -229,7 +230,7 @@ public class ReplicateToDatastoreActionTest {
.build()));
fakeClock.advanceBy(Duration.standardDays(1));
jpaTm().transact(() -> jpaTm().insert(TestObject.create("foo")));
insertInDb(TestObject.create("foo"));
task.run();
// Replication shouldn't have happened
assertThat(ofyTm().loadAllOf(TestObject.class)).isEmpty();

View file

@ -18,8 +18,10 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableSet;
import google.registry.model.ImmutableObject;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestExtension;
import java.lang.reflect.Method;
@ -49,7 +51,7 @@ class EntityCallbacksListenerTest {
@Test
void verifyAllCallbacks_executedExpectedTimes() {
TestEntity testPersist = new TestEntity();
jpaTm().transact(() -> jpaTm().insert(testPersist));
insertInDb(testPersist);
checkAll(testPersist, 1, 0, 0, 0);
TestEntity testUpdate = new TestEntity();
@ -100,8 +102,7 @@ class EntityCallbacksListenerTest {
@Test
void verifyCallbacksNotCalledOnCommit() {
TestEntity testEntity = new TestEntity();
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(new TestEntity());
TestEntity testLoad =
jpaTm().transact(() -> jpaTm().loadByKey(VKey.createSql(TestEntity.class, "id")));
@ -263,7 +264,7 @@ class EntityCallbacksListenerTest {
}
@MappedSuperclass
private static class ParentEntity {
private static class ParentEntity extends ImmutableObject {
@Embedded ParentEmbedded parentEmbedded = new ParentEmbedded();
@Transient int parentPostLoad = 0;
@Transient int parentPrePersist = 0;

View file

@ -19,6 +19,7 @@ import static google.registry.model.domain.token.AllocationToken.TokenStatus.END
import static google.registry.model.domain.token.AllocationToken.TokenStatus.NOT_STARTED;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.VALID;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.collect.ImmutableSortedMap;
@ -58,7 +59,7 @@ public class AllocationTokenStatusTransitionConverterTest {
TimedTransitionProperty.fromValueMap(values, TokenStatusTransition.class);
AllocationTokenStatusTransitionConverterTestEntity testEntity =
new AllocationTokenStatusTransitionConverterTestEntity(timedTransitionProperty);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
AllocationTokenStatusTransitionConverterTestEntity persisted =
jpaTm()
.transact(

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.money.CurrencyUnit.USD;
@ -53,7 +54,7 @@ public class BillingCostTransitionConverterTest {
TimedTransitionProperty<Money, BillingCostTransition> timedTransitionProperty =
TimedTransitionProperty.fromValueMap(values, BillingCostTransition.class);
TestEntity testEntity = new TestEntity(timedTransitionProperty);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.timedTransitionProperty).containsExactlyEntriesIn(timedTransitionProperty);

View file

@ -17,6 +17,7 @@ import static com.google.common.base.Charsets.US_ASCII;
import static com.google.common.hash.Funnels.stringFunnel;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.BloomFilter;
@ -41,7 +42,7 @@ class BloomFilterConverterTest {
BloomFilter<String> bloomFilter = BloomFilter.create(stringFunnel(US_ASCII), 3);
ImmutableSet.of("foo", "bar", "baz").forEach(bloomFilter::put);
TestEntity entity = new TestEntity(bloomFilter);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.bloomFilter).isEqualTo(bloomFilter);

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableList;
import google.registry.model.ImmutableObject;
@ -45,7 +46,7 @@ public class CidrAddressBlockListConverterTest {
CidrAddressBlock.create("8000::/1"),
CidrAddressBlock.create("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
TestEntity testEntity = new TestEntity(addresses);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.addresses).isEqualTo(addresses);

View file

@ -15,6 +15,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
@ -45,7 +46,7 @@ public class CreateAutoTimestampConverterTest {
CreateAutoTimestamp ts = CreateAutoTimestamp.create(DateTime.parse("2019-09-9T11:39:00Z"));
TestEntity ent = new TestEntity("myinst", ts);
jpaTm().transact(() -> jpaTm().insert(ent));
insertInDb(ent);
TestEntity result =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "myinst"));
assertThat(result).isEqualTo(new TestEntity("myinst", ts));
@ -56,7 +57,7 @@ public class CreateAutoTimestampConverterTest {
CreateAutoTimestamp ts = CreateAutoTimestamp.create(null);
TestEntity ent = new TestEntity("autoinit", ts);
jpaTm().transact(() -> jpaTm().insert(ent));
insertInDb(ent);
TestEntity result =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "autoinit"));

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableMap;
import google.registry.model.ImmutableObject;
@ -47,7 +48,7 @@ public class CurrencyToBillingConverterTest {
CurrencyUnit.of("CNY"),
new BillingAccountEntry(CurrencyUnit.of("CNY"), "accountId2"));
TestEntity testEntity = new TestEntity(currencyToBilling);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.currencyToBilling).containsExactlyEntriesIn(currencyToBilling);

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static org.junit.jupiter.api.Assertions.assertThrows;
import google.registry.model.ImmutableObject;
@ -39,7 +40,7 @@ public class CurrencyUnitConverterTest {
@Test
void roundTripConversion() {
TestEntity entity = new TestEntity(CurrencyUnit.EUR);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
assertThat(
jpaTm()
.transact(

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.collect.ImmutableSortedMap;
@ -59,7 +60,7 @@ public class DatabaseMigrationScheduleTransitionConverterTest {
TimedTransitionProperty.fromValueMap(values, MigrationStateTransition.class);
DatabaseMigrationScheduleTransitionConverterTestEntity testEntity =
new DatabaseMigrationScheduleTransitionConverterTestEntity(timedTransitionProperty);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
DatabaseMigrationScheduleTransitionConverterTestEntity persisted =
jpaTm()
.transact(

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.persistence.transaction.JpaTestRules;
@ -69,7 +70,7 @@ public class DateTimeConverterTest {
void converter_generatesTimestampWithNormalizedZone() {
DateTime dt = parseDateTime("2019-09-01T01:01:01Z");
TestEntity entity = new TestEntity("normalized_utc_time", dt);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm()
.transact(
@ -82,7 +83,7 @@ public class DateTimeConverterTest {
DateTime dt = parseDateTime("2019-09-01T01:01:01-05:00");
TestEntity entity = new TestEntity("new_york_time", dt);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "new_york_time"));
assertThat(retrievedEntity.dt.toString()).isEqualTo("2019-09-01T06:01:01.000Z");

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.model.replay.EntityTest.EntityForTesting;
@ -78,7 +79,7 @@ public class DurationConverterTest {
private void assertPersistedEntityHasSameDuration(Duration duration) {
DurationTestEntity entity = new DurationTestEntity(duration);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
DurationTestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(DurationTestEntity.class, "id"));
assertThat(persisted.duration.getMillis()).isEqualTo(duration.getMillis());

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
@ -63,7 +64,7 @@ public class InetAddressSetConverterTest {
private void verifySaveAndLoad(@Nullable Set<InetAddress> inetAddresses) {
InetAddressSetTestEntity testEntity = new InetAddressSetTestEntity(inetAddresses);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
InetAddressSetTestEntity persisted =
jpaTm()
.transact(

View file

@ -15,6 +15,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableMap;
import google.registry.model.ImmutableObject;
@ -71,7 +72,7 @@ public class JodaMoneyConverterTest {
void roundTripConversion() {
Money money = Money.of(CurrencyUnit.USD, 100);
TestEntity entity = new TestEntity(money);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
List<?> result =
jpaTm()
.transact(
@ -101,7 +102,7 @@ public class JodaMoneyConverterTest {
"dos", Money.ofMajor(CurrencyUnit.JPY, 2000),
"tres", Money.of(CurrencyUnit.GBP, 20));
ComplexTestEntity entity = new ComplexTestEntity(moneyMap, myMoney, yourMoney);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
List<?> result =
jpaTm()
.transact(

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.model.replay.EntityTest;
@ -54,7 +55,7 @@ public class LocalDateConverterTest {
private LocalDateConverterTestEntity persistAndLoadTestEntity(LocalDate date) {
LocalDateConverterTestEntity entity = new LocalDateConverterTestEntity(date);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
return jpaTm()
.transact(
() -> jpaTm().loadByKey(VKey.createSql(LocalDateConverterTestEntity.class, "id")));

View file

@ -16,7 +16,9 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.persistence.VKey;
import google.registry.persistence.WithLongVKey;
import google.registry.testing.AppEngineExtension;
@ -46,7 +48,7 @@ public class LongVKeyConverterTest {
new TestLongEntity(
VKey.createSql(TestLongEntity.class, 10L),
VKey.createSql(CompositeKeyTestLongEntity.class, 20L));
jpaTm().transact(() -> jpaTm().insert(original));
insertInDb(original);
TestLongEntity retrieved =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestLongEntity.class, "id"));
@ -60,7 +62,7 @@ public class LongVKeyConverterTest {
@Entity(name = "TestLongEntity")
@com.googlecode.objectify.annotation.Entity
@WithLongVKey(classNameSuffix = "LongType")
static class TestLongEntity {
static class TestLongEntity extends ImmutableObject {
@com.googlecode.objectify.annotation.Id @Id String id = "id";
VKey<TestLongEntity> number;

View file

@ -16,8 +16,10 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableSet;
import google.registry.model.ImmutableObject;
import google.registry.model.eppcommon.StatusValue;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestExtension;
@ -39,14 +41,14 @@ public class StatusValueSetConverterTest {
Set<StatusValue> enums = ImmutableSet.of(StatusValue.INACTIVE, StatusValue.PENDING_DELETE);
TestEntity obj = new TestEntity("foo", enums);
jpaTm().transact(() -> jpaTm().insert(obj));
insertInDb(obj);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "foo"));
assertThat(persisted.data).isEqualTo(enums);
}
@Entity(name = "TestEntity")
static class TestEntity {
static class TestEntity extends ImmutableObject {
@Id String name;
Set<StatusValue> data;

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
@ -40,7 +41,7 @@ public class StringListConverterTest {
void roundTripConversion_returnsSameStringList() {
List<String> tlds = ImmutableList.of("app", "dev", "how");
TestEntity testEntity = new TestEntity(tlds);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.tlds).containsExactly("app", "dev", "how");
@ -50,7 +51,7 @@ public class StringListConverterTest {
void testMerge_succeeds() {
List<String> tlds = ImmutableList.of("app", "dev", "how");
TestEntity testEntity = new TestEntity(tlds);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
persisted.tlds = ImmutableList.of("com", "gov");
@ -63,7 +64,7 @@ public class StringListConverterTest {
@Test
void testNullValue_writesAndReadsNullSuccessfully() {
TestEntity testEntity = new TestEntity(null);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.tlds).isNull();
@ -72,7 +73,7 @@ public class StringListConverterTest {
@Test
void testEmptyCollection_writesAndReadsEmptyCollectionSuccessfully() {
TestEntity testEntity = new TestEntity(ImmutableList.of());
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.tlds).isEmpty();

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableMap;
@ -49,7 +50,7 @@ public class StringMapConverterBaseTest {
@Test
void roundTripConversion_returnsSameMap() {
TestEntity testEntity = new TestEntity(MAP);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.map).containsExactlyEntriesIn(MAP);
@ -58,7 +59,7 @@ public class StringMapConverterBaseTest {
@Test
void testUpdateColumn_succeeds() {
TestEntity testEntity = new TestEntity(MAP);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.map).containsExactlyEntriesIn(MAP);
@ -72,7 +73,7 @@ public class StringMapConverterBaseTest {
@Test
void testNullValue_writesAndReadsNullSuccessfully() {
TestEntity testEntity = new TestEntity(null);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.map).isNull();
@ -81,7 +82,7 @@ public class StringMapConverterBaseTest {
@Test
void testEmptyMap_writesAndReadsEmptyCollectionSuccessfully() {
TestEntity testEntity = new TestEntity(ImmutableMap.of());
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.map).isEmpty();

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import com.google.common.collect.ImmutableSet;
import google.registry.model.ImmutableObject;
@ -38,7 +39,7 @@ public class StringSetConverterTest {
void roundTripConversion_returnsSameStringList() {
Set<String> tlds = ImmutableSet.of("app", "dev", "how");
TestEntity testEntity = new TestEntity(tlds);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.tlds).containsExactly("app", "dev", "how");
@ -47,7 +48,7 @@ public class StringSetConverterTest {
@Test
void testNullValue_writesAndReadsNullSuccessfully() {
TestEntity testEntity = new TestEntity(null);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.tlds).isNull();
@ -56,7 +57,7 @@ public class StringSetConverterTest {
@Test
void testEmptyCollection_writesAndReadsEmptyCollectionSuccessfully() {
TestEntity testEntity = new TestEntity(ImmutableSet.of());
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.tlds).isEmpty();

View file

@ -16,7 +16,9 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.persistence.VKey;
import google.registry.persistence.WithStringVKey;
import google.registry.testing.AppEngineExtension;
@ -47,7 +49,7 @@ public class StringVKeyConverterTest {
"TheRealSpartacus",
VKey.createSql(TestStringEntity.class, "ImSpartacus!"),
VKey.createSql(CompositeKeyTestStringEntity.class, "NoImSpartacus!"));
jpaTm().transact(() -> jpaTm().insert(original));
insertInDb(original);
TestStringEntity retrieved =
jpaTm()
@ -63,7 +65,7 @@ public class StringVKeyConverterTest {
@Entity(name = "TestStringEntity")
@com.googlecode.objectify.annotation.Entity
@WithStringVKey(classNameSuffix = "StringType")
static class TestStringEntity {
static class TestStringEntity extends ImmutableObject {
@com.googlecode.objectify.annotation.Id @Id String id;
VKey<TestStringEntity> other;

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.model.registrar.Registrar.State;
@ -38,7 +39,7 @@ public class StringValueEnumeratedTest {
@Test
void roundTripConversion_returnsSameEnum() {
TestEntity testEntity = new TestEntity(State.ACTIVE);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.state).isEqualTo(State.ACTIVE);
@ -47,7 +48,7 @@ public class StringValueEnumeratedTest {
@Test
void testNativeQuery_succeeds() {
TestEntity testEntity = new TestEntity(State.DISABLED);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
assertThat(
jpaTm()

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -59,7 +60,7 @@ class TimedTransitionPropertyConverterBaseTest {
@Test
void roundTripConversion_returnsSameTimedTransitionProperty() {
TestEntity testEntity = new TestEntity(TIMED_TRANSITION_PROPERTY);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.property).containsExactlyEntriesIn(TIMED_TRANSITION_PROPERTY);
@ -68,7 +69,7 @@ class TimedTransitionPropertyConverterBaseTest {
@Test
void testUpdateColumn_succeeds() {
TestEntity testEntity = new TestEntity(TIMED_TRANSITION_PROPERTY);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.property).containsExactlyEntriesIn(TIMED_TRANSITION_PROPERTY);
@ -83,7 +84,7 @@ class TimedTransitionPropertyConverterBaseTest {
@Test
void testNullValue_writesAndReadsNullSuccessfully() {
TestEntity testEntity = new TestEntity(null);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.property).isNull();

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.collect.ImmutableSortedMap;
@ -56,7 +57,7 @@ class TldStateTransitionConverterTest {
TimedTransitionProperty<TldState, TldStateTransition> timedTransitionProperty =
TimedTransitionProperty.fromValueMap(values, TldStateTransition.class);
TestEntity testEntity = new TestEntity(timedTransitionProperty);
jpaTm().transact(() -> jpaTm().insert(testEntity));
insertInDb(testEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.timedTransitionProperty).containsExactlyEntriesIn(timedTransitionProperty);

View file

@ -16,6 +16,7 @@ package google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import google.registry.model.ImmutableObject;
import google.registry.persistence.transaction.JpaTestRules;
@ -66,7 +67,7 @@ public class ZonedDateTimeConverterTest {
void converter_generatesTimestampWithNormalizedZone() {
ZonedDateTime zdt = ZonedDateTime.parse("2019-09-01T01:01:01Z");
TestEntity entity = new TestEntity("normalized_utc_time", zdt);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm()
.transact(
@ -79,7 +80,7 @@ public class ZonedDateTimeConverterTest {
ZonedDateTime zdt = ZonedDateTime.parse("2019-09-01T01:01:01Z[UTC]");
TestEntity entity = new TestEntity("non_normalized_utc_time", zdt);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm()
.transact(
@ -92,7 +93,7 @@ public class ZonedDateTimeConverterTest {
ZonedDateTime zdt = ZonedDateTime.parse("2019-09-01T01:01:01+05:00");
TestEntity entity = new TestEntity("new_york_time", zdt);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
TestEntity retrievedEntity =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "new_york_time"));
assertThat(retrievedEntity.zdt.toString()).isEqualTo("2019-08-31T20:01:01Z");

View file

@ -18,6 +18,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.assertDetachedFromEntityManager;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.TestDataHelper.fileClassPath;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
@ -139,7 +140,7 @@ class JpaTransactionManagerImplTest {
@Test
void insert_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse();
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().loadByKey(theEntityKey))).isEqualTo(theEntity);
}
@ -258,16 +259,16 @@ class JpaTransactionManagerImplTest {
@Test
void insert_throwsExceptionIfEntityExists() {
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse();
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().loadByKey(theEntityKey))).isEqualTo(theEntity);
assertThrows(RollbackException.class, () -> jpaTm().transact(() -> jpaTm().insert(theEntity)));
assertThrows(RollbackException.class, () -> insertInDb(theEntity));
}
@Test
void createCompoundIdEntity_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isFalse();
jpaTm().transact(() -> jpaTm().insert(compoundIdEntity));
insertInDb(compoundIdEntity);
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().loadByKey(compoundIdEntityKey)))
.isEqualTo(compoundIdEntity);
@ -277,7 +278,7 @@ class JpaTransactionManagerImplTest {
void createNamedCompoundIdEntity_succeeds() {
// Compound IDs should also work even if the field names don't match up exactly
TestNamedCompoundIdEntity entity = new TestNamedCompoundIdEntity("foo", 1);
jpaTm().transact(() -> jpaTm().insert(entity));
insertInDb(entity);
jpaTm()
.transact(
() -> {
@ -295,7 +296,7 @@ class JpaTransactionManagerImplTest {
void saveAllNew_succeeds() {
moreEntities.forEach(
entity -> assertThat(jpaTm().transact(() -> jpaTm().exists(entity))).isFalse());
jpaTm().transact(() -> jpaTm().insertAll(moreEntities));
insertInDb(moreEntities);
moreEntities.forEach(
entity -> assertThat(jpaTm().transact(() -> jpaTm().exists(entity))).isTrue());
assertThat(jpaTm().transact(() -> jpaTm().loadAllOf(TestEntity.class)))
@ -306,9 +307,8 @@ class JpaTransactionManagerImplTest {
void saveAllNew_rollsBackWhenFailure() {
moreEntities.forEach(
entity -> assertThat(jpaTm().transact(() -> jpaTm().exists(entity))).isFalse());
jpaTm().transact(() -> jpaTm().insert(moreEntities.get(0)));
assertThrows(
RollbackException.class, () -> jpaTm().transact(() -> jpaTm().insertAll(moreEntities)));
insertInDb(moreEntities.get(0));
assertThrows(RollbackException.class, () -> insertInDb(moreEntities));
assertThat(jpaTm().transact(() -> jpaTm().exists(moreEntities.get(0)))).isTrue();
assertThat(jpaTm().transact(() -> jpaTm().exists(moreEntities.get(1)))).isFalse();
assertThat(jpaTm().transact(() -> jpaTm().exists(moreEntities.get(2)))).isFalse();
@ -324,7 +324,7 @@ class JpaTransactionManagerImplTest {
@Test
void put_updatesExistingEntity() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
TestEntity persisted = jpaTm().transact(() -> jpaTm().loadByKey(theEntityKey));
assertThat(persisted.data).isEqualTo("foo");
theEntity.data = "bar";
@ -346,7 +346,7 @@ class JpaTransactionManagerImplTest {
@Test
void update_succeeds() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
TestEntity persisted =
jpaTm().transact(() -> jpaTm().loadByKey(VKey.createSql(TestEntity.class, "theEntity")));
assertThat(persisted.data).isEqualTo("foo");
@ -358,7 +358,7 @@ class JpaTransactionManagerImplTest {
@Test
void updateCompoundIdEntity_succeeds() {
jpaTm().transact(() -> jpaTm().insert(compoundIdEntity));
insertInDb(compoundIdEntity);
TestCompoundIdEntity persisted = jpaTm().transact(() -> jpaTm().loadByKey(compoundIdEntityKey));
assertThat(persisted.data).isEqualTo("foo");
compoundIdEntity.data = "bar";
@ -377,7 +377,7 @@ class JpaTransactionManagerImplTest {
@Test
void updateAll_succeeds() {
jpaTm().transact(() -> jpaTm().insertAll(moreEntities));
insertInDb(moreEntities);
ImmutableList<TestEntity> updated =
ImmutableList.of(
new TestEntity("entity1", "foo_updated"),
@ -390,7 +390,7 @@ class JpaTransactionManagerImplTest {
@Test
void updateAll_rollsBackWhenFailure() {
jpaTm().transact(() -> jpaTm().insertAll(moreEntities));
insertInDb(moreEntities);
ImmutableList<TestEntity> updated =
ImmutableList.of(
new TestEntity("entity1", "foo_updated"),
@ -406,7 +406,7 @@ class JpaTransactionManagerImplTest {
@Test
void load_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse();
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
TestEntity persisted =
jpaTm().transact(() -> assertDetachedFromEntityManager(jpaTm().loadByKey(theEntityKey)));
assertThat(persisted.name).isEqualTo("theEntity");
@ -423,7 +423,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadByEntity_succeeds() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
TestEntity persisted =
jpaTm().transact(() -> assertDetachedFromEntityManager(jpaTm().loadByEntity(theEntity)));
assertThat(persisted.name).isEqualTo("theEntity");
@ -433,7 +433,7 @@ class JpaTransactionManagerImplTest {
@Test
void maybeLoad_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse();
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
TestEntity persisted =
jpaTm()
.transact(
@ -454,7 +454,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadCompoundIdEntity_succeeds() {
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isFalse();
jpaTm().transact(() -> jpaTm().insert(compoundIdEntity));
insertInDb(compoundIdEntity);
TestCompoundIdEntity persisted =
jpaTm()
.transact(
@ -466,7 +466,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadByKeysIfPresent() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
jpaTm()
.transact(
() -> {
@ -483,7 +483,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadByKeys_succeeds() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
jpaTm()
.transact(
() -> {
@ -496,7 +496,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadByEntitiesIfPresent_succeeds() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
jpaTm()
.transact(
() -> {
@ -511,7 +511,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadByEntities_succeeds() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
jpaTm()
.transact(
() -> {
@ -524,7 +524,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadAll_succeeds() {
jpaTm().transact(() -> jpaTm().insertAll(moreEntities));
insertInDb(moreEntities);
ImmutableList<TestEntity> persisted =
jpaTm()
.transact(
@ -537,7 +537,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadSingleton_detaches() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
jpaTm()
.transact(
() ->
@ -550,7 +550,7 @@ class JpaTransactionManagerImplTest {
@Test
void delete_succeeds() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isTrue();
jpaTm().transact(() -> jpaTm().delete(theEntityKey));
assertThat(jpaTm().transact(() -> jpaTm().exists(theEntity))).isFalse();
@ -565,7 +565,7 @@ class JpaTransactionManagerImplTest {
@Test
void deleteCompoundIdEntity_succeeds() {
jpaTm().transact(() -> jpaTm().insert(compoundIdEntity));
insertInDb(compoundIdEntity);
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isTrue();
jpaTm().transact(() -> jpaTm().delete(compoundIdEntityKey));
assertThat(jpaTm().transact(() -> jpaTm().exists(compoundIdEntity))).isFalse();
@ -597,7 +597,7 @@ class JpaTransactionManagerImplTest {
@Test
void loadAfterUpdate_fails() {
jpaTm().transact(() -> jpaTm().insert(theEntity));
insertInDb(theEntity);
assertThat(
assertThrows(
IllegalStateException.class,
@ -614,7 +614,7 @@ class JpaTransactionManagerImplTest {
@Test
void cqQuery_detaches() {
jpaTm().transact(() -> jpaTm().insertAll(moreEntities));
insertInDb(moreEntities);
jpaTm()
.transact(
() ->
@ -653,7 +653,7 @@ class JpaTransactionManagerImplTest {
@Test
void query_detachesResults() {
jpaTm().transact(() -> jpaTm().insertAll(moreEntities));
insertInDb(moreEntities);
jpaTm()
.transact(
() ->

View file

@ -16,6 +16,7 @@ package google.registry.persistence.transaction;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static org.junit.jupiter.api.Assertions.assertThrows;
import google.registry.model.ImmutableObject;
@ -66,7 +67,7 @@ public class JpaTransactionManagerRuleTest {
// This test verifies that 1) withEntityClass() has registered TestEntity and 2) The table
// has been created, implying withProperty(HBM2DDL_AUTO, "update") worked.
TestEntity original = new TestEntity("key", "value");
jpaTm().transact(() -> jpaTm().insert(original));
insertInDb(original);
TestEntity retrieved =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "key"));
assertThat(retrieved).isEqualTo(original);

View file

@ -17,6 +17,7 @@ package google.registry.schema.registrar;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.registrar.RegistrarContact.Type.WHOIS;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static google.registry.testing.SqlHelper.saveRegistrar;
import com.google.common.collect.ImmutableSet;
@ -65,7 +66,7 @@ class RegistrarContactTest {
@Test
void testPersistence_succeeds() {
jpaTm().transact(() -> jpaTm().insert(testRegistrarPoc));
insertInDb(testRegistrarPoc);
RegistrarContact persisted =
jpaTm().transact(() -> jpaTm().loadByKey(testRegistrarPoc.createVKey()));
assertThat(persisted).isEqualTo(testRegistrarPoc);

View file

@ -16,6 +16,7 @@ package google.registry.schema.registrar;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.insertInDb;
import static org.joda.time.DateTimeZone.UTC;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -71,13 +72,13 @@ public class RegistrarDaoTest {
@Test
void saveNew_worksSuccessfully() {
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isFalse();
jpaTm().transact(() -> jpaTm().insert(testRegistrar));
insertInDb(testRegistrar);
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isTrue();
}
@Test
void update_worksSuccessfully() {
jpaTm().transact(() -> jpaTm().insert(testRegistrar));
insertInDb(testRegistrar);
Registrar persisted = jpaTm().transact(() -> jpaTm().loadByKey(registrarKey));
assertThat(persisted.getRegistrarName()).isEqualTo("registrarName");
jpaTm()
@ -101,7 +102,7 @@ public class RegistrarDaoTest {
@Test
void load_worksSuccessfully() {
assertThat(jpaTm().transact(() -> jpaTm().exists(testRegistrar))).isFalse();
jpaTm().transact(() -> jpaTm().insert(testRegistrar));
insertInDb(testRegistrar);
Registrar persisted = jpaTm().transact(() -> jpaTm().loadByKey(registrarKey));
assertThat(persisted.getRegistrarId()).isEqualTo("registrarId");

View file

@ -68,6 +68,7 @@ import google.registry.model.Buildable;
import google.registry.model.EppResource;
import google.registry.model.EppResource.ForeignKeyedEppResource;
import google.registry.model.EppResourceUtils;
import google.registry.model.ImmutableObject;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingEvent.Flag;
import google.registry.model.billing.BillingEvent.Reason;
@ -1375,6 +1376,16 @@ public class DatabaseHelper {
return transactIfJpaTm(() -> tm().exists(object));
}
/** Inserts the given entity/entities into Cloud SQL in a single transaction. */
public static <T extends ImmutableObject> void insertInDb(T... entities) {
jpaTm().transact(() -> jpaTm().insertAll(entities));
}
/** Inserts the given entities into Cloud SQL in a single transaction. */
public static <T extends ImmutableObject> void insertInDb(ImmutableCollection<T> entities) {
jpaTm().transact(() -> jpaTm().insertAll(entities));
}
/**
* In JPA mode, asserts that the given entity is detached from the current entity manager.
*