Add loadOnlyOf method to tm() (#1162)

* Add loadOnlyOf method to tm()

In addition there's a bit of a refator of SqlReplayCheckpoint to make it
more in line with the other singletons. This method is useful for the
singleton classes where we expect at most one entity to exist, e.g.
ServerSecret.
This commit is contained in:
gbrodman
2021-05-20 10:59:01 -04:00
committed by GitHub
parent dc7f21ca68
commit a7e8ae5a2c
13 changed files with 4012 additions and 3955 deletions
@@ -17,7 +17,9 @@ package google.registry.persistence.transaction;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableList;
@@ -372,6 +374,27 @@ public class TransactionManagerTest {
() -> tm().transact(() -> tm().loadAllOf(TestEntity.class)));
}
@TestOfyAndSql
void loadSingleton_returnsValue_orEmpty() {
assertEntityNotExist(theEntity);
assertThat(transactIfJpaTm(() -> tm().loadSingleton(TestEntity.class))).isEmpty();
tm().transact(() -> tm().insert(theEntity));
assertThat(transactIfJpaTm(() -> tm().loadSingleton(TestEntity.class))).hasValue(theEntity);
}
@TestOfyAndSql
void loadSingleton_exceptionOnMultiple() {
assertAllEntitiesNotExist(moreEntities);
tm().transact(() -> tm().insertAll(moreEntities));
assertThat(
assertThrows(
IllegalArgumentException.class,
() -> transactIfJpaTm(() -> tm().loadSingleton(TestEntity.class))))
.hasMessageThat()
.isEqualTo("Expected at most one entity of type TestEntity, found at least two");
}
private static void assertEntityExists(TestEntity entity) {
assertThat(tm().transact(() -> tm().exists(entity))).isTrue();
}