Separate JPA rules for unit and integration tests (#420)

* Separate JPA rules for unit and integration tests

Define two subclasses of JpaTransactionManagerRule, one for unit
tests and the other for integration tests. The difference is that
the former does not need nomulus schema and need not be included
in server/schema compatibility tests.

* Separate JPA rules for unit and integration tests

Define two subclasses of JpaTransactionManagerRule, one for unit
tests and the other for integration tests. The difference is that
the former does not need nomulus schema and need not be included
in server/schema compatibility tests.
This commit is contained in:
Weimin Yu 2019-12-19 14:49:54 -05:00 committed by GitHub
parent f76030f7f0
commit 24d671c070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 222 additions and 163 deletions

View file

@ -19,7 +19,8 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.JUnitBackports.assertThrows;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.schema.domain.RegistryLock;
import google.registry.schema.domain.RegistryLock.Action;
import google.registry.testing.AppEngineRule;
@ -37,8 +38,8 @@ public final class RegistryLockDaoTest {
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
@Test
public void testSaveAndLoad_success() {
@ -64,13 +65,13 @@ public final class RegistryLockDaoTest {
public void testSaveTwiceAndLoad_returnsLatest() {
RegistryLock lock = createLock();
jpaTm().transact(() -> RegistryLockDao.save(lock));
jpaTmRule.getTxnClock().advanceOneMilli();
jpaRule.getTxnClock().advanceOneMilli();
jpaTm()
.transact(
() -> {
RegistryLock updatedLock =
RegistryLockDao.getByVerificationCode(lock.getVerificationCode());
updatedLock.setCompletionTimestamp(jpaTmRule.getTxnClock().nowUtc());
updatedLock.setCompletionTimestamp(jpaRule.getTxnClock().nowUtc());
RegistryLockDao.save(updatedLock);
});
jpaTm()
@ -79,16 +80,16 @@ public final class RegistryLockDaoTest {
RegistryLock fromDatabase =
RegistryLockDao.getByVerificationCode(lock.getVerificationCode());
assertThat(fromDatabase.getCompletionTimestamp().get())
.isEqualTo(jpaTmRule.getTxnClock().nowUtc());
.isEqualTo(jpaRule.getTxnClock().nowUtc());
});
}
@Test
public void testUpdateLock_usingSamePrimaryKey() {
RegistryLock lock = RegistryLockDao.save(createLock());
jpaTmRule.getTxnClock().advanceOneMilli();
jpaRule.getTxnClock().advanceOneMilli();
RegistryLock updatedLock =
lock.asBuilder().setCompletionTimestamp(jpaTmRule.getTxnClock().nowUtc()).build();
lock.asBuilder().setCompletionTimestamp(jpaRule.getTxnClock().nowUtc()).build();
jpaTm().transact(() -> RegistryLockDao.save(updatedLock));
jpaTm()
.transact(
@ -96,7 +97,7 @@ public final class RegistryLockDaoTest {
RegistryLock fromDatabase =
RegistryLockDao.getByVerificationCode(lock.getVerificationCode());
assertThat(fromDatabase.getCompletionTimestamp())
.isEqualTo(Optional.of(jpaTmRule.getTxnClock().nowUtc()));
.isEqualTo(Optional.of(jpaRule.getTxnClock().nowUtc()));
});
}
@ -123,10 +124,10 @@ public final class RegistryLockDaoTest {
@Test
public void testLoad_byRepoId() {
RegistryLock completedLock =
createLock().asBuilder().setCompletionTimestamp(jpaTmRule.getTxnClock().nowUtc()).build();
createLock().asBuilder().setCompletionTimestamp(jpaRule.getTxnClock().nowUtc()).build();
RegistryLockDao.save(completedLock);
jpaTmRule.getTxnClock().advanceOneMilli();
jpaRule.getTxnClock().advanceOneMilli();
RegistryLock inProgressLock = createLock();
RegistryLockDao.save(inProgressLock);

View file

@ -0,0 +1,109 @@
// Copyright 2019 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.model.transaction;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Holds specialized JUnit rules that start a test database server and provide {@link
* JpaTransactionManager} instances.
*/
public class JpaTestRules {
private static final String GOLDEN_SCHEMA_SQL_PATH = "sql/schema/nomulus.golden.sql";
/**
* Junit rule for integration tests with JPA framework, when the underlying database is populated
* with the Nomulus Cloud SQL schema.
*
* <p>Test classes that instantiate this class should be included in {@link
* google.registry.schema.integration.SqlIntegrationTestSuite}. This enforced by {@link
* google.registry.schema.integration.SqlIntegrationMembershipTest}.
*/
public static class JpaIntegrationTestRule extends JpaTransactionManagerRule {
private JpaIntegrationTestRule(
ImmutableList<Class> extraEntityClasses, ImmutableMap<String, String> userProperties) {
super(Optional.of(GOLDEN_SCHEMA_SQL_PATH), extraEntityClasses, userProperties);
}
}
/**
* Junit rule for unit tests with JPA framework, when the underlying database is populated by the
* optional init script.
*/
public static class JpaUnitTestRule extends JpaTransactionManagerRule {
private JpaUnitTestRule(
Optional<String> initScriptPath,
ImmutableList<Class> extraEntityClasses,
ImmutableMap<String, String> userProperties) {
super(initScriptPath, extraEntityClasses, userProperties);
}
}
/** Builder of test rules that provide {@link JpaTransactionManager}. */
public static class Builder {
private String initScript;
private List<Class> extraEntityClasses = new ArrayList<Class>();
private Map<String, String> userProperties = new HashMap<String, String>();
/**
* Sets the SQL script to be used to initialize the database. If not set,
* sql/schema/nomulus.golden.sql will be used.
*
* <p>The {@code initScript} is only accepted when building {@link JpaUnitTestRule}.
*/
public Builder withInitScript(String initScript) {
this.initScript = initScript;
return this;
}
/** Adds annotated class(es) to the known entities for the database. */
public Builder withEntityClass(Class... classes) {
this.extraEntityClasses.addAll(ImmutableSet.copyOf(classes));
return this;
}
/** Adds the specified property to those used to initialize the transaction manager. */
public Builder withProperty(String name, String value) {
this.userProperties.put(name, value);
return this;
}
/** Builds a {@link JpaIntegrationTestRule} instance. */
public JpaIntegrationTestRule buildIntegrationTestRule() {
checkState(initScript == null, "JpaNomulusIntegrationTestRule does not accept initScript");
return new JpaIntegrationTestRule(
ImmutableList.copyOf(extraEntityClasses), ImmutableMap.copyOf(userProperties));
}
/** Builds a {@link JpaUnitTestRule} instance. */
public JpaUnitTestRule buildUnitTestRule() {
return new JpaUnitTestRule(
Optional.ofNullable(initScript),
ImmutableList.copyOf(extraEntityClasses),
ImmutableMap.copyOf(userProperties));
}
}
}

View file

@ -19,6 +19,7 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.TestDataHelper.fileClassPath;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import google.registry.testing.FakeClock;
import java.math.BigInteger;
import javax.persistence.EntityManager;
@ -32,10 +33,10 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JpaTransactionManagerImplTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder()
.withInitScript(fileClassPath(getClass(), "test_schema.sql"))
.build();
.buildUnitTestRule();
@Test
public void inTransaction_returnsCorrespondingResult() {
@ -53,7 +54,7 @@ public class JpaTransactionManagerImplTest {
@Test
public void getTransactionTime_throwsExceptionWhenNotInTransaction() {
FakeClock txnClock = jpaTmRule.getTxnClock();
FakeClock txnClock = jpaRule.getTxnClock();
txnClock.advanceOneMilli();
assertThrows(PersistenceException.class, () -> jpaTm().getTransactionTime());
jpaTm().transact(() -> assertThat(jpaTm().getTransactionTime()).isEqualTo(txnClock.nowUtc()));

View file

@ -21,9 +21,9 @@ import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.io.Resources;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.persistence.HibernateSchemaExporter;
import google.registry.persistence.NomulusPostgreSql;
import google.registry.persistence.PersistenceModule;
@ -39,10 +39,8 @@ import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.hibernate.cfg.Environment;
@ -54,15 +52,16 @@ import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.PostgreSQLContainer;
/**
* JUnit Rule to provision {@link JpaTransactionManagerImpl} backed by {@link PostgreSQLContainer}.
* Base class of JUnit Rules to provision {@link JpaTransactionManagerImpl} backed by {@link
* PostgreSQLContainer}. This class is not for direct use. Use specialized subclasses, {@link
* JpaIntegrationTestRule} or {@link JpaTestRules.JpaUnitTestRule} as befits the use case.
*
* <p>This rule also replaces the {@link JpaTransactionManagerImpl} provided by {@link
* TransactionManagerFactory} with the {@link JpaTransactionManagerImpl} generated by the rule
* itself, so that all SQL queries will be sent to the database instance created by {@link
* PostgreSQLContainer} to achieve test purpose.
*/
public class JpaTransactionManagerRule extends ExternalResource {
private static final String GOLDEN_SCHEMA_SQL_PATH = "sql/schema/nomulus.golden.sql";
abstract class JpaTransactionManagerRule extends ExternalResource {
private static final String DB_CLEANUP_SQL_PATH =
"google/registry/model/transaction/cleanup_database.sql";
private static final String MANAGEMENT_DB_NAME = "management";
@ -70,7 +69,7 @@ public class JpaTransactionManagerRule extends ExternalResource {
private final DateTime now = DateTime.now(UTC);
private final FakeClock clock = new FakeClock(now);
private final String initScriptPath;
private final Optional<String> initScriptPath;
private final ImmutableList<Class> extraEntityClasses;
private final ImmutableMap userProperties;
@ -84,8 +83,8 @@ public class JpaTransactionManagerRule extends ExternalResource {
private EntityManagerFactory emf;
private JpaTransactionManager cachedTm;
private JpaTransactionManagerRule(
String initScriptPath,
protected JpaTransactionManagerRule(
Optional<String> initScriptPath,
ImmutableList<Class> extraEntityClasses,
ImmutableMap<String, String> userProperties) {
this.initScriptPath = initScriptPath;
@ -105,7 +104,7 @@ public class JpaTransactionManagerRule extends ExternalResource {
@Override
public void before() throws Exception {
executeSql(MANAGEMENT_DB_NAME, readSqlInClassPath(DB_CLEANUP_SQL_PATH));
executeSql(POSTGRES_DB_NAME, readSqlInClassPath(initScriptPath));
initScriptPath.ifPresent(path -> executeSql(POSTGRES_DB_NAME, readSqlInClassPath(path)));
if (!extraEntityClasses.isEmpty()) {
File tempSqlFile = File.createTempFile("tempSqlFile", ".sql");
tempSqlFile.deleteOnExit();
@ -232,42 +231,4 @@ public class JpaTransactionManagerRule extends ExternalResource {
return clock;
}
/** Builder for {@link JpaTransactionManagerRule}. */
public static class Builder {
private String initScript;
private List<Class> extraEntityClasses = new ArrayList<Class>();
private Map<String, String> userProperties = new HashMap<String, String>();
/**
* Sets the SQL script to be used to initialize the database. If not set,
* sql/schema/nomulus.golden.sql will be used.
*/
public Builder withInitScript(String initScript) {
this.initScript = initScript;
return this;
}
/** Adds annotated class(es) to the known entities for the database. */
public Builder withEntityClass(Class... classes) {
this.extraEntityClasses.addAll(ImmutableSet.copyOf(classes));
return this;
}
/** Adds the specified property to those used to initialize the transaction manager. */
public Builder withProperty(String name, String value) {
this.userProperties.put(name, value);
return this;
}
/** Builds a {@link JpaTransactionManagerRule} instance. */
public JpaTransactionManagerRule build() {
if (initScript == null) {
initScript = GOLDEN_SCHEMA_SQL_PATH;
}
return new JpaTransactionManagerRule(
initScript,
ImmutableList.copyOf(extraEntityClasses),
ImmutableMap.copyOf(userProperties));
}
}
}

View file

@ -19,6 +19,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.JUnitBackports.assertThrows;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import google.registry.schema.tmch.ClaimsList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
@ -28,15 +30,15 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** JUnit test for {@link JpaTransactionManagerRule} */
/** JUnit test for {@link JpaTransactionManagerRule}, with {@link JpaUnitTestRule} as proxy. */
@RunWith(JUnit4.class)
public class JpaTransactionManagerRuleTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder()
.withEntityClass(ClaimsList.class, TestEntity.class)
.buildUnitTestRule();
@Test
public void verifiesRuleWorks() {

View file

@ -21,7 +21,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.BloomFilter;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Rule;
@ -34,10 +35,8 @@ import org.junit.runners.JUnit4;
public class BloomFilterConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
@Test
public void roundTripConversion_returnsSameBloomFilter() {

View file

@ -18,7 +18,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.joda.time.DateTime;
@ -32,10 +33,8 @@ import org.junit.runners.JUnit4;
public class CreateAutoTimestampConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
@Test
public void testTypeConversion() {
@ -57,7 +56,7 @@ public class CreateAutoTimestampConverterTest {
TestEntity result =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "autoinit"));
assertThat(result.cat.getTimestamp()).isEqualTo(jpaTmRule.getTxnClock().nowUtc());
assertThat(result.cat.getTimestamp()).isEqualTo(jpaRule.getTxnClock().nowUtc());
}
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.

View file

@ -18,7 +18,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.JUnitBackports.assertThrows;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PersistenceException;
@ -33,10 +34,8 @@ import org.junit.runners.JUnit4;
public class CurrencyUnitConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
@Test
public void roundTripConversion() {

View file

@ -18,7 +18,8 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.Id;
@ -34,8 +35,8 @@ import org.junit.runners.JUnit4;
public class DateTimeConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().withEntityClass(TestEntity.class).build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
private final DateTimeConverter converter = new DateTimeConverter();

View file

@ -18,7 +18,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import com.google.common.collect.ImmutableMap;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
@ -63,11 +64,11 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JodaMoneyConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder()
.withEntityClass(TestEntity.class, ComplexTestEntity.class)
.withProperty(Environment.HBM2DDL_AUTO, "update")
.build();
.buildUnitTestRule();
@Test
public void roundTripConversion() {

View file

@ -18,7 +18,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.ImmutableObject;
import google.registry.model.UpdateAutoTimestamp;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Rule;
@ -31,10 +32,8 @@ import org.junit.runners.JUnit4;
public class UpdateAutoTimestampConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
@Test
public void testTypeConversion() {
@ -46,7 +45,7 @@ public class UpdateAutoTimestampConverterTest {
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "myinst"));
assertThat(result.name).isEqualTo("myinst");
assertThat(result.uat.getTimestamp()).isEqualTo(jpaTmRule.getTxnClock().nowUtc());
assertThat(result.uat.getTimestamp()).isEqualTo(jpaRule.getTxnClock().nowUtc());
}
@Test
@ -58,7 +57,7 @@ public class UpdateAutoTimestampConverterTest {
TestEntity result1 =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "myinst1"));
jpaTmRule.getTxnClock().advanceOneMilli();
jpaRule.getTxnClock().advanceOneMilli();
TestEntity ent2 = new TestEntity("myinst2", result1.uat);
@ -68,7 +67,7 @@ public class UpdateAutoTimestampConverterTest {
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "myinst2"));
assertThat(result1.uat.getTimestamp()).isNotEqualTo(result2.uat.getTimestamp());
assertThat(result2.uat.getTimestamp()).isEqualTo(jpaTmRule.getTxnClock().nowUtc());
assertThat(result2.uat.getTimestamp()).isEqualTo(jpaRule.getTxnClock().nowUtc());
}
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.

View file

@ -18,7 +18,8 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.ImmutableObject;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaUnitTestRule;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZonedDateTime;
@ -34,10 +35,8 @@ import org.junit.runners.JUnit4;
public class ZonedDateTimeConverterTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder()
.withEntityClass(TestEntity.class)
.build();
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
private final ZonedDateTimeConverter converter = new ZonedDateTimeConverter();

View file

@ -17,7 +17,8 @@ package google.registry.schema.cursor;
import static com.google.common.truth.Truth.assertThat;
import google.registry.model.common.Cursor.CursorType;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.testing.FakeClock;
import java.util.List;
import org.junit.Rule;
@ -32,8 +33,8 @@ public class CursorDaoTest {
private FakeClock fakeClock = new FakeClock();
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
@Test
public void save_worksSuccessfullyOnNewCursor() {

View file

@ -19,7 +19,7 @@ import static com.google.common.truth.Truth.assertWithMessage;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import java.lang.reflect.Field;
@ -35,7 +35,7 @@ import org.junit.runners.Suite.SuiteClasses;
* system property as a comma-separated string.
*
* <p>A test is deemed dependent on the SQL schema iff it has a field with type {@link
* JpaTransactionManagerRule}.
* JpaIntegrationTestRule}.
*/
// TODO(weiminyu): consider generating a TestSuite class instead.
@RunWith(JUnit4.class)
@ -74,7 +74,7 @@ public class SqlIntegrationMembershipTest {
for (Class<?> clazz = testClass; clazz != null; clazz = clazz.getSuperclass()) {
if (Stream.of(clazz.getDeclaredFields())
.map(Field::getType)
.anyMatch(JpaTransactionManagerRule.class::equals)) {
.anyMatch(JpaIntegrationTestRule.class::equals)) {
return true;
}
}

View file

@ -15,15 +15,7 @@
package google.registry.schema.integration;
import google.registry.model.registry.RegistryLockDaoTest;
import google.registry.model.transaction.JpaTransactionManagerImplTest;
import google.registry.model.transaction.JpaTransactionManagerRuleTest;
import google.registry.persistence.BloomFilterConverterTest;
import google.registry.persistence.CreateAutoTimestampConverterTest;
import google.registry.persistence.CurrencyUnitConverterTest;
import google.registry.persistence.DateTimeConverterTest;
import google.registry.persistence.JodaMoneyConverterTest;
import google.registry.persistence.UpdateAutoTimestampConverterTest;
import google.registry.persistence.ZonedDateTimeConverterTest;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.schema.cursor.CursorDaoTest;
import google.registry.schema.tld.PremiumListDaoTest;
import google.registry.schema.tld.ReservedListDaoTest;
@ -39,30 +31,20 @@ import org.junit.runners.Suite.SuiteClasses;
* Groups all tests that may depends on Cloud SQL schema. They will be run for server-schema
* compatibility check.
*
* <p>Schema dependency is approximated by the use of {@link
* google.registry.model.transaction.JpaTransactionManagerRule}.
* <p>Schema dependency is approximated by the use of {@link JpaIntegrationTestRule}.
*
* @see SqlIntegrationMembershipTest
*/
// TODO(weiminyu): refactor JpaTransactionManagerRule to eliminate false positives.
@RunWith(Suite.class)
@SuiteClasses({
BloomFilterConverterTest.class,
ClaimsListDaoTest.class,
CreateAutoTimestampConverterTest.class,
CreateReservedListCommandTest.class,
CurrencyUnitConverterTest.class,
CursorDaoTest.class,
DateTimeConverterTest.class,
JodaMoneyConverterTest.class,
JpaTransactionManagerImplTest.class,
JpaTransactionManagerRuleTest.class,
PremiumListDaoTest.class,
RegistryLockDaoTest.class,
RegistryLockGetActionTest.class,
ReservedListDaoTest.class,
UpdateAutoTimestampConverterTest.class,
UpdateReservedListCommandTest.class,
ZonedDateTimeConverterTest.class
UpdateReservedListCommandTest.class
})
public class SqlIntegrationTestSuite {}

View file

@ -28,7 +28,8 @@ import static org.joda.money.CurrencyUnit.USD;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import google.registry.model.registry.Registry;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.testing.AppEngineRule;
import java.math.BigDecimal;
import java.util.List;
@ -44,8 +45,8 @@ import org.junit.runners.JUnit4;
public class PremiumListDaoTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@ -74,7 +75,7 @@ public class PremiumListDaoTest {
.getSingleResult();
assertThat(persistedList.getLabelsToPrices()).containsExactlyEntriesIn(TEST_PRICES);
assertThat(persistedList.getCreationTimestamp())
.isEqualTo(jpaTmRule.getTxnClock().nowUtc());
.isEqualTo(jpaRule.getTxnClock().nowUtc());
});
}
@ -100,7 +101,7 @@ public class PremiumListDaoTest {
assertThat(persistedLists.get(1).getLabelsToPrices())
.containsExactlyEntriesIn(TEST_PRICES);
assertThat(persistedLists.get(1).getCreationTimestamp())
.isEqualTo(jpaTmRule.getTxnClock().nowUtc());
.isEqualTo(jpaRule.getTxnClock().nowUtc());
});
}

View file

@ -19,7 +19,8 @@ import static google.registry.model.transaction.TransactionManagerFactory.jpaTm;
import com.google.common.collect.ImmutableMap;
import google.registry.model.registry.label.ReservationType;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.schema.tld.ReservedList.ReservedEntry;
import org.junit.Rule;
import org.junit.Test;
@ -30,8 +31,8 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ReservedListDaoTest {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
private static final ImmutableMap<String, ReservedEntry> TEST_RESERVATIONS =
ImmutableMap.of(
@ -56,7 +57,7 @@ public class ReservedListDaoTest {
assertThat(persistedList.getLabelsToReservations())
.containsExactlyEntriesIn(TEST_RESERVATIONS);
assertThat(persistedList.getCreationTimestamp())
.isEqualTo(jpaTmRule.getTxnClock().nowUtc());
.isEqualTo(jpaRule.getTxnClock().nowUtc());
});
}

View file

@ -18,7 +18,8 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import com.google.common.collect.ImmutableMap;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.testing.FakeClock;
import javax.persistence.NoResultException;
import org.junit.Rule;
@ -33,8 +34,8 @@ public class ClaimsListDaoTest {
private FakeClock fakeClock = new FakeClock();
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
@Test
public void trySave_insertsClaimsListSuccessfully() {
@ -43,8 +44,7 @@ public class ClaimsListDaoTest {
ClaimsListDao.trySave(claimsList);
ClaimsList insertedClaimsList = ClaimsListDao.getCurrent();
assertClaimsListEquals(claimsList, insertedClaimsList);
assertThat(insertedClaimsList.getCreationTimestamp())
.isEqualTo(jpaTmRule.getTxnClock().nowUtc());
assertThat(insertedClaimsList.getCreationTimestamp()).isEqualTo(jpaRule.getTxnClock().nowUtc());
}
@Test

View file

@ -19,7 +19,7 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.collect.ImmutableList;
import com.google.common.net.HostAndPort;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.testing.AppEngineRule;
import google.registry.testing.UserInfo;
import google.registry.tools.params.HostAndPortParameter;
@ -170,8 +170,8 @@ public final class RegistryTestServerMain {
.apply(runner, Description.EMPTY);
System.out.printf("%sLoading SQL fixtures and AppEngineRule...%s\n", BLUE, RESET);
new JpaTransactionManagerRule.Builder()
.build()
new JpaTestRules.Builder()
.buildIntegrationTestRule()
.apply(withAppEngine, Description.EMPTY)
.evaluate();
}

View file

@ -25,7 +25,8 @@ import com.beust.jcommander.ParameterException;
import com.google.common.io.Files;
import com.google.common.truth.Truth8;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.schema.tld.ReservedList.ReservedEntry;
import google.registry.schema.tld.ReservedListDao;
import java.io.File;
@ -45,8 +46,8 @@ public abstract class CreateOrUpdateReservedListCommandTestCase
<T extends CreateOrUpdateReservedListCommand> extends CommandTestCase<T> {
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
String reservedTermsPath;
String invalidReservedTermsPath;
@ -111,7 +112,7 @@ public abstract class CreateOrUpdateReservedListCommandTestCase
getCloudSqlReservedList("xn--q9jyb4c_common-reserved");
assertThat(persistedList.getName()).isEqualTo("xn--q9jyb4c_common-reserved");
assertThat(persistedList.getShouldPublish()).isTrue();
assertThat(persistedList.getCreationTimestamp()).isEqualTo(jpaTmRule.getTxnClock().nowUtc());
assertThat(persistedList.getCreationTimestamp()).isEqualTo(jpaRule.getTxnClock().nowUtc());
assertThat(persistedList.getLabelsToReservations())
.containsExactly(
"baddies",

View file

@ -30,7 +30,8 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.gson.Gson;
import google.registry.model.registry.RegistryLockDao;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.request.Action.Method;
import google.registry.request.auth.AuthLevel;
import google.registry.request.auth.AuthResult;
@ -61,8 +62,8 @@ public final class RegistryLockGetActionTest {
@Rule public final AppEngineRule appEngineRule = AppEngineRule.builder().withDatastore().build();
@Rule
public final JpaTransactionManagerRule jpaTmRule =
new JpaTransactionManagerRule.Builder().build();
public final JpaIntegrationTestRule jpaRule =
new JpaTestRules.Builder().buildIntegrationTestRule();
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@ -75,7 +76,7 @@ public final class RegistryLockGetActionTest {
@Before
public void setup() {
jpaTmRule.getTxnClock().setTo(DateTime.parse("2000-06-08T22:00:00.0Z"));
jpaRule.getTxnClock().setTo(DateTime.parse("2000-06-08T22:00:00.0Z"));
authResult = AuthResult.create(AuthLevel.USER, UserAuthInfo.create(user, false));
accessor =
AuthenticatedRegistrarAccessor.createForTesting(
@ -97,9 +98,9 @@ public final class RegistryLockGetActionTest {
.setAction(Action.LOCK)
.setVerificationCode(UUID.randomUUID().toString())
.setRegistrarPocId("johndoe@theregistrar.com")
.setCompletionTimestamp(jpaTmRule.getTxnClock().nowUtc())
.setCompletionTimestamp(jpaRule.getTxnClock().nowUtc())
.build();
jpaTmRule.getTxnClock().advanceOneMilli();
jpaRule.getTxnClock().advanceOneMilli();
RegistryLock adminLock =
new RegistryLock.Builder()
.setRepoId("repoId")
@ -108,7 +109,7 @@ public final class RegistryLockGetActionTest {
.setAction(Action.LOCK)
.setVerificationCode(UUID.randomUUID().toString())
.isSuperuser(true)
.setCompletionTimestamp(jpaTmRule.getTxnClock().nowUtc())
.setCompletionTimestamp(jpaRule.getTxnClock().nowUtc())
.build();
RegistryLock incompleteLock =
new RegistryLock.Builder()

View file

@ -23,7 +23,8 @@ import static google.registry.util.NetworkUtils.pickUnusedPort;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.model.transaction.JpaTestRules;
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
import google.registry.request.auth.AuthenticatedRegistrarAccessor;
import google.registry.server.Fixture;
import google.registry.server.Route;
@ -54,7 +55,7 @@ public final class TestServerRule extends ExternalResource {
private final ImmutableList<Fixture> fixtures;
private final AppEngineRule appEngineRule;
private final JpaTransactionManagerRule jpaTransactionManagerRule;
private final JpaIntegrationTestRule jpaTransactionManagerRule;
private final BlockingQueue<FutureTask<?>> jobs = new LinkedBlockingDeque<>();
private final ImmutableMap<String, Path> runfiles;
private final ImmutableList<Route> routes;
@ -82,7 +83,7 @@ public final class TestServerRule extends ExternalResource {
.withTaskQueue()
.withUserService(UserInfo.createAdmin(email, THE_REGISTRAR_GAE_USER_ID))
.build();
this.jpaTransactionManagerRule = new JpaTransactionManagerRule.Builder().build();
this.jpaTransactionManagerRule = new JpaTestRules.Builder().buildIntegrationTestRule();
}
@Override