Convert TmchCrl and ServerSecret to cleaner tm() impls (#1068)

* Convert TmchCrl and ServerSecret to cleaner tm() impls

When I implemented this originally I knew a lot less than I know now
about how we'll be storing and retrieving these singletons from SQL. The
optimal way here is to use the single SINGLETON_ID as the primary key,
that way we always know how to create the key that we can use in the
tm() retrieval.

This allows us to use generic tm() methods and to remove the handcrafted
SQL queries.
This commit is contained in:
gbrodman 2021-04-13 20:50:07 -04:00 committed by GitHub
parent 245e2ea5a8
commit d35460f14c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 285 additions and 343 deletions

View file

@ -15,16 +15,19 @@
package google.registry.model.server;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.DatabaseHelper.persistResource;
import google.registry.model.EntityTestCase;
import google.registry.model.ofy.RequestCapturingAsyncDatastoreService;
import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import google.registry.testing.TestOfyOnly;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link ServerSecret}. */
@DualDatabaseTest
public class ServerSecretTest extends EntityTestCase {
ServerSecretTest() {
@ -36,24 +39,22 @@ public class ServerSecretTest extends EntityTestCase {
ServerSecret.resetCache();
}
@Test
@TestOfyAndSql
void testGet_bootstrapping_savesSecretToDatastore() {
ServerSecret secret = ServerSecret.get();
assertThat(secret).isNotNull();
assertThat(ofy().load().entity(new ServerSecret()).now()).isEqualTo(secret);
assertThat(loadFromSql()).isEqualTo(secret);
assertThat(loadByEntity(new ServerSecret())).isEqualTo(secret);
}
@Test
@TestOfyAndSql
void testGet_existingSecret_returned() {
ServerSecret secret = ServerSecret.create(new UUID(123, 456));
ofy().saveWithoutBackup().entity(secret).now();
persistResource(secret);
assertThat(ServerSecret.get()).isEqualTo(secret);
assertThat(ofy().load().entity(new ServerSecret()).now()).isEqualTo(secret);
assertThat(loadFromSql()).isEqualTo(secret);
assertThat(loadByEntity(new ServerSecret())).isEqualTo(secret);
}
@Test
@TestOfyOnly // relies on request-capturing datastore
void testGet_cachedSecret() {
int numInitialReads = RequestCapturingAsyncDatastoreService.getReads().size();
ServerSecret secret = ServerSecret.get();
@ -63,21 +64,9 @@ public class ServerSecretTest extends EntityTestCase {
assertThat(RequestCapturingAsyncDatastoreService.getReads()).hasSize(numReads);
}
@Test
@TestOfyAndSql
void testAsBytes() {
byte[] bytes = ServerSecret.create(new UUID(123, 0x456)).asBytes();
assertThat(bytes).isEqualTo(new byte[] {0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0x4, 0x56});
}
private static ServerSecret loadFromSql() {
return jpaTm()
.transact(
() ->
jpaTm()
.query("FROM ServerSecret", ServerSecret.class)
.setMaxResults(1)
.getResultStream()
.findFirst()
.get());
}
}

View file

@ -15,61 +15,43 @@
package google.registry.model.tmch;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import google.registry.model.EntityTestCase;
import google.registry.testing.DualDatabaseTest;
import google.registry.testing.TestOfyAndSql;
import java.util.Optional;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link TmchCrl}. */
@DualDatabaseTest
public class TmchCrlTest extends EntityTestCase {
TmchCrlTest() {
super(JpaEntityCoverageCheck.ENABLED);
}
@Test
@TestOfyAndSql
void testSuccess() {
assertThat(TmchCrl.get()).isEqualTo(Optional.empty());
TmchCrl.set("lolcat", "https://lol.cat");
assertThat(TmchCrl.get().get().getCrl()).isEqualTo("lolcat");
}
@Test
@TestOfyAndSql
void testDualWrite() {
TmchCrl expected = new TmchCrl();
expected.crl = "lolcat";
expected.url = "https://lol.cat";
expected.updated = fakeClock.nowUtc();
TmchCrl.set("lolcat", "https://lol.cat");
assertThat(ofy().load().entity(new TmchCrl()).now()).isEqualTo(expected);
assertThat(loadFromSql()).isEqualTo(expected);
assertThat(loadByEntity(new TmchCrl())).isEqualTo(expected);
}
@Test
@TestOfyAndSql
void testMultipleWrites() {
TmchCrl.set("first", "https://first.cat");
assertThat(TmchCrl.get().get().getCrl()).isEqualTo("first");
TmchCrl.set("second", "https://second.cat");
assertThat(TmchCrl.get().get().getCrl()).isEqualTo("second");
jpaTm()
.transact(
() ->
assertThat(
jpaTm().query("SELECT COUNT(*) FROM TmchCrl", Long.class).getSingleResult())
.isEqualTo(1L));
}
private static TmchCrl loadFromSql() {
return jpaTm()
.transact(
() ->
jpaTm()
.query("FROM TmchCrl", TmchCrl.class)
.setMaxResults(1)
.getResultStream()
.findFirst()
.get());
}
}