Stop writing ReservedList to Datastore (#1163)

This commit is contained in:
sarahcaseybot
2021-05-17 17:46:21 -04:00
committed by GitHub
parent 16641e05a1
commit c1f0c29134
19 changed files with 53 additions and 692 deletions
@@ -28,8 +28,8 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Unit tests for {@link ReservedListSqlDao}. */
public class ReservedListSqlDaoTest {
/** Unit tests for {@link ReservedListDao}. */
public class ReservedListDaoTest {
private final FakeClock fakeClock = new FakeClock();
@@ -65,7 +65,7 @@ public class ReservedListSqlDaoTest {
@Test
void save_worksSuccessfully() {
ReservedListSqlDao.save(testReservedList);
ReservedListDao.save(testReservedList);
jpaTm()
.transact(
() -> {
@@ -82,31 +82,31 @@ public class ReservedListSqlDaoTest {
@Test
void delete_worksSuccessfully() {
ReservedListSqlDao.save(testReservedList);
assertThat(ReservedListSqlDao.checkExists("testlist")).isTrue();
ReservedListSqlDao.delete(testReservedList);
assertThat(ReservedListSqlDao.checkExists("testlist")).isFalse();
ReservedListDao.save(testReservedList);
assertThat(ReservedListDao.checkExists("testlist")).isTrue();
ReservedListDao.delete(testReservedList);
assertThat(ReservedListDao.checkExists("testlist")).isFalse();
}
@Test
void delete_listNotInDatabase() {
assertThat(ReservedListSqlDao.checkExists("testlist")).isFalse();
ReservedListSqlDao.delete(testReservedList);
assertThat(ReservedListSqlDao.checkExists("testlist")).isFalse();
assertThat(ReservedListDao.checkExists("testlist")).isFalse();
ReservedListDao.delete(testReservedList);
assertThat(ReservedListDao.checkExists("testlist")).isFalse();
}
@Test
void checkExists_worksSuccessfully() {
assertThat(ReservedListSqlDao.checkExists("testlist")).isFalse();
ReservedListSqlDao.save(testReservedList);
assertThat(ReservedListSqlDao.checkExists("testlist")).isTrue();
assertThat(ReservedListDao.checkExists("testlist")).isFalse();
ReservedListDao.save(testReservedList);
assertThat(ReservedListDao.checkExists("testlist")).isTrue();
}
@Test
void getLatestRevision_worksSuccessfully() {
assertThat(ReservedListSqlDao.getLatestRevision("testlist").isPresent()).isFalse();
ReservedListSqlDao.save(testReservedList);
ReservedList persistedList = ReservedListSqlDao.getLatestRevision("testlist").get();
assertThat(ReservedListDao.getLatestRevision("testlist").isPresent()).isFalse();
ReservedListDao.save(testReservedList);
ReservedList persistedList = ReservedListDao.getLatestRevision("testlist").get();
assertThat(persistedList.getRevisionId()).isNotNull();
assertThat(persistedList.getLastUpdateTime()).isEqualTo(fakeClock.nowUtc());
assertThat(persistedList.getName()).isEqualTo("testlist");
@@ -116,7 +116,7 @@ public class ReservedListSqlDaoTest {
@Test
void getLatestRevision_returnsLatestRevision() {
ReservedListSqlDao.save(
ReservedListDao.save(
new ReservedList.Builder()
.setName("testlist")
.setLastUpdateTime(fakeClock.nowUtc())
@@ -127,8 +127,8 @@ public class ReservedListSqlDaoTest {
ReservedListEntry.create(
"old", ReservationType.RESERVED_FOR_SPECIFIC_USE, null)))
.build());
ReservedListSqlDao.save(testReservedList);
ReservedList persistedList = ReservedListSqlDao.getLatestRevision("testlist").get();
ReservedListDao.save(testReservedList);
ReservedList persistedList = ReservedListDao.getLatestRevision("testlist").get();
assertThat(persistedList.getRevisionId()).isNotNull();
assertThat(persistedList.getLastUpdateTime()).isEqualTo(fakeClock.nowUtc());
assertThat(persistedList.getName()).isEqualTo("testlist");
@@ -1,117 +0,0 @@
// Copyright 2021 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.registry.label;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.persistence.transaction.TransactionManagerFactory.ofyTm;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import google.registry.persistence.VKey;
import google.registry.testing.AppEngineExtension;
import google.registry.testing.FakeClock;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Unit tests for {@link ReservedListDatastoreDao}. */
public class ReservedListDatastoreDaoTest {
@RegisterExtension
final AppEngineExtension appEngine =
AppEngineExtension.builder().withDatastoreAndCloudSql().build();
private final FakeClock fakeClock = new FakeClock();
private ImmutableMap<String, ReservedListEntry> reservations;
private ReservedList reservedList;
@BeforeEach
void setUp() {
reservations =
ImmutableMap.of(
"food",
ReservedListEntry.create("food", ReservationType.RESERVED_FOR_SPECIFIC_USE, null),
"music",
ReservedListEntry.create("music", ReservationType.FULLY_BLOCKED, "fully blocked"));
reservedList =
new ReservedList.Builder()
.setName("testlist")
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(reservations)
.build();
}
@Test
void save_worksSuccessfully() {
ReservedListDatastoreDao.save(reservedList);
Optional<ReservedList> savedList =
ofyTm()
.loadByKeyIfPresent(
VKey.createOfy(
ReservedList.class,
Key.create(getCrossTldKey(), ReservedList.class, reservedList.name)));
assertThat(savedList.get()).isEqualTo(reservedList);
}
@Test
void delete_worksSuccessfully() {
ReservedListDatastoreDao.save(reservedList);
assertThat(ReservedListDatastoreDao.getLatestRevision("testlist").isPresent()).isTrue();
ReservedListDatastoreDao.delete(reservedList);
assertThat(ReservedListDatastoreDao.getLatestRevision("testlist").isPresent()).isFalse();
}
@Test
void delete_listNotInDatastore() {
assertThat(ReservedListDatastoreDao.getLatestRevision("testlist").isPresent()).isFalse();
ReservedListDatastoreDao.delete(reservedList);
assertThat(ReservedListDatastoreDao.getLatestRevision("testlist").isPresent()).isFalse();
}
@Test
void getLatestRevision_worksSuccessfully() {
assertThat(ReservedListDatastoreDao.getLatestRevision("testlist").isPresent()).isFalse();
ReservedListDatastoreDao.save(reservedList);
ReservedList persistedList = ReservedListDatastoreDao.getLatestRevision("testlist").get();
assertThat(persistedList).isEqualTo(reservedList);
}
@Test
void getLatestRevision_returnsLatestRevision() {
ReservedListDatastoreDao.save(
new ReservedList.Builder()
.setName("testlist")
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(
ImmutableMap.of(
"old",
ReservedListEntry.create(
"old", ReservationType.RESERVED_FOR_SPECIFIC_USE, null)))
.build());
assertThat(ReservedListDatastoreDao.getLatestRevision("testlist").get())
.isNotEqualTo(reservedList);
ReservedListDatastoreDao.save(reservedList);
ReservedList persistedList = ReservedListDatastoreDao.getLatestRevision("testlist").get();
assertThat(persistedList).isEqualTo(reservedList);
}
}
@@ -1,143 +0,0 @@
// Copyright 2021 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.registry.label;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableMap;
import google.registry.model.EntityTestCase;
import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ReservedListDualDatabaseDaoTest extends EntityTestCase {
private ImmutableMap<String, ReservedListEntry> reservations;
private ReservedList reservedList;
@BeforeEach
void setUp() {
fakeClock.setAutoIncrementByOneMilli();
reservations =
ImmutableMap.of(
"food",
ReservedListEntry.create("food", ReservationType.RESERVED_FOR_SPECIFIC_USE, null),
"music",
ReservedListEntry.create("music", ReservationType.FULLY_BLOCKED, "fully blocked"));
reservedList =
new ReservedList.Builder()
.setName("testlist")
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(reservations)
.build();
}
@Test
void testSave_success() {
ReservedListDualDatabaseDao.save(reservedList);
Optional<ReservedList> savedList =
ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName());
assertThat(savedList.get()).isEqualTo(reservedList);
}
@Test
void testDelete_success() {
ReservedListDualDatabaseDao.save(reservedList);
assertThat(ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName()).isPresent())
.isTrue();
ReservedListDualDatabaseDao.delete(reservedList);
assertThat(ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName()).isPresent())
.isFalse();
}
@Test
void testSaveAndLoad_emptyList() {
ReservedList list =
new ReservedList.Builder()
.setName("empty")
.setLastUpdateTime(fakeClock.nowUtc())
.setReservedListMap(ImmutableMap.of())
.build();
ReservedListDualDatabaseDao.save(list);
Optional<ReservedList> savedList = ReservedListDualDatabaseDao.getLatestRevision("empty");
assertThat(savedList.get()).isEqualTo(list);
}
@Test
void testSave_multipleVersions() {
ReservedListDualDatabaseDao.save(reservedList);
assertThat(
ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName())
.get()
.getReservedListEntries())
.isEqualTo(reservations);
ImmutableMap<String, ReservedListEntry> newReservations =
ImmutableMap.of(
"food",
ReservedListEntry.create("food", ReservationType.RESERVED_FOR_SPECIFIC_USE, null));
ReservedList secondList =
new ReservedList.Builder()
.setName("testlist2")
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(newReservations)
.build();
ReservedListDualDatabaseDao.save(secondList);
assertThat(
ReservedListDualDatabaseDao.getLatestRevision(secondList.getName())
.get()
.getReservedListEntries())
.isEqualTo(newReservations);
}
@Test
void testLoad_unequalLists() {
ReservedListDualDatabaseDao.save(reservedList);
ReservedList secondList =
new ReservedList.Builder()
.setName(reservedList.name)
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(
ImmutableMap.of(
"food",
ReservedListEntry.create(
"food", ReservationType.RESERVED_FOR_SPECIFIC_USE, null)))
.build();
ReservedListSqlDao.save(secondList);
IllegalStateException thrown =
assertThrows(
IllegalStateException.class,
() -> ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName()));
assertThat(thrown)
.hasMessageThat()
.contains("Domain label music has entry in Datastore, but not in Cloud SQL.");
}
@Test
void testLoad_noListInDatastore() {
ReservedListSqlDao.save(reservedList);
IllegalStateException thrown =
assertThrows(
IllegalStateException.class,
() -> ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName()));
assertThat(thrown).hasMessageThat().contains("Reserved list in Datastore is empty.");
}
}
@@ -28,7 +28,7 @@ import google.registry.model.poll.PollMessageTest;
import google.registry.model.rde.RdeRevisionTest;
import google.registry.model.registry.RegistryLockDaoTest;
import google.registry.model.registry.RegistryTest;
import google.registry.model.registry.label.ReservedListSqlDaoTest;
import google.registry.model.registry.label.ReservedListDaoTest;
import google.registry.model.reporting.Spec11ThreatMatchTest;
import google.registry.model.server.KmsSecretRevisionSqlDaoTest;
import google.registry.model.server.LockTest;
@@ -96,7 +96,7 @@ import org.junit.runner.RunWith;
RdeRevisionTest.class,
RegistrarDaoTest.class,
RegistryTest.class,
ReservedListSqlDaoTest.class,
ReservedListDaoTest.class,
RegistryLockDaoTest.class,
ServerSecretTest.class,
SignedMarkRevocationListDaoTest.class,
@@ -99,7 +99,7 @@ import google.registry.model.registry.Registry.TldType;
import google.registry.model.registry.label.PremiumList;
import google.registry.model.registry.label.PremiumList.PremiumListEntry;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.registry.label.ReservedListDualDatabaseDao;
import google.registry.model.registry.label.ReservedListDao;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.reporting.HistoryEntryDao;
import google.registry.model.transfer.ContactTransferData;
@@ -346,9 +346,8 @@ public class DatabaseHelper {
}
public static ReservedList persistReservedList(ReservedList reservedList) {
ReservedListDualDatabaseDao.save(reservedList);
ReservedListDao.save(reservedList);
maybeAdvanceClock();
tm().clearSessionCache();
return reservedList;
}
@@ -1,115 +0,0 @@
// Copyright 2021 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.tools;
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.persistReservedList;
import com.google.common.collect.ImmutableMap;
import google.registry.model.registry.label.ReservationType;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import google.registry.model.registry.label.ReservedListDatastoreDao;
import google.registry.model.registry.label.ReservedListDualDatabaseDao;
import google.registry.model.registry.label.ReservedListSqlDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Unit tests for {@link CompareReservedListsCommand}. */
public class CompareReservedListCommandTest extends CommandTestCase<CompareReservedListsCommand> {
@BeforeEach
void setUp() {
persistReservedList(
"testlist", "food, RESERVED_FOR_SPECIFIC_USE", "music, FULLY_BLOCKED # fully blocked");
persistReservedList("testlist2", "candy, ALLOWED_IN_SUNRISE");
}
@Test
void test_success() throws Exception {
runCommand();
assertThat(getStdoutAsString()).isEqualTo("Found 0 unequal list(s).\n");
}
@Test
void test_listMissingFromCloudSql() throws Exception {
jpaTm().transact(() -> jpaTm().delete(ReservedListSqlDao.getLatestRevision("testlist").get()));
runCommand();
assertThat(getStdoutAsString())
.isEqualTo(
"ReservedList 'testlist' is present in Datastore, but not in Cloud SQL.\n"
+ "Found 1 unequal list(s).\n");
}
@Test
void test_listMissingFromDatastore() throws Exception {
ofyTm()
.transact(
() -> ofyTm().delete(ReservedListDatastoreDao.getLatestRevision("testlist").get()));
runCommand();
assertThat(getStdoutAsString())
.isEqualTo(
"ReservedList 'testlist' is present in Cloud SQL, but not in Datastore.\n"
+ "Found 1 unequal list(s).\n");
}
@Test
void test_listsDiffer() throws Exception {
ImmutableMap<String, ReservedListEntry> newReservations =
ImmutableMap.of(
"food",
ReservedListEntry.create("food", ReservationType.RESERVED_FOR_SPECIFIC_USE, null));
ReservedList secondList =
new ReservedList.Builder()
.setName("testlist")
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(newReservations)
.build();
ReservedListDatastoreDao.save(secondList);
runCommand();
assertThat(getStdoutAsString())
.isEqualTo(
"ReservedList 'testlist' has different entries in each database.\n"
+ "Found 1 unequal list(s).\n");
}
@Test
void test_listsDifferAndMissing() throws Exception {
ofyTm()
.transact(
() -> ofyTm().delete(ReservedListDualDatabaseDao.getLatestRevision("testlist2").get()));
ImmutableMap<String, ReservedListEntry> newReservations =
ImmutableMap.of(
"food",
ReservedListEntry.create("food", ReservationType.RESERVED_FOR_SPECIFIC_USE, null));
ReservedList secondList =
new ReservedList.Builder()
.setName("testlist")
.setLastUpdateTime(fakeClock.nowUtc())
.setShouldPublish(false)
.setReservedListMap(newReservations)
.build();
ReservedListDatastoreDao.save(secondList);
runCommand();
assertThat(getStdoutAsString())
.isEqualTo(
"ReservedList 'testlist2' is present in Cloud SQL, but not in Datastore.\n"
+ "ReservedList 'testlist' has different entries in each database.\n"
+ "Found 2 unequal list(s).\n");
}
}
@@ -27,7 +27,7 @@ import com.google.common.io.Files;
import com.google.common.truth.Truth8;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import google.registry.model.registry.label.ReservedListSqlDao;
import google.registry.model.registry.label.ReservedListDao;
import java.io.File;
import java.io.IOException;
import org.joda.time.DateTime;
@@ -135,7 +135,7 @@ abstract class CreateOrUpdateReservedListCommandTestCase<
}
void verifyXnq9jyb4cInCloudSql() {
assertThat(ReservedListSqlDao.checkExists("xn--q9jyb4c_common-reserved")).isTrue();
assertThat(ReservedListDao.checkExists("xn--q9jyb4c_common-reserved")).isTrue();
ReservedList persistedList = getCloudSqlReservedList("xn--q9jyb4c_common-reserved");
assertThat(persistedList.getName()).isEqualTo("xn--q9jyb4c_common-reserved");
assertThat(persistedList.getShouldPublish()).isTrue();
@@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import google.registry.model.registry.Registry;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.registry.label.ReservedListDualDatabaseDao;
import google.registry.model.registry.label.ReservedListDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -41,7 +41,7 @@ class DeleteReservedListCommandTest extends CommandTestCase<DeleteReservedListCo
void testSuccess() throws Exception {
assertThat(reservedList.getReservedListEntries()).hasSize(1);
runCommandForced("--name=common");
assertThat(ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName())).isEmpty();
assertThat(ReservedListDao.getLatestRevision(reservedList.getName())).isEmpty();
}
@Test
@@ -63,7 +63,7 @@ class DeleteReservedListCommandTest extends CommandTestCase<DeleteReservedListCo
assertThrows(
IllegalArgumentException.class,
() -> runCommandForced("--name=" + reservedList.getName()));
assertThat(ReservedListDualDatabaseDao.getLatestRevision(reservedList.getName())).isPresent();
assertThat(ReservedListDao.getLatestRevision(reservedList.getName())).isPresent();
assertThat(thrown)
.hasMessageThat()
.isEqualTo("Cannot delete reserved list because it is used on these tld(s): xn--q9jyb4c");
@@ -28,7 +28,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.registry.label.ReservedList.ReservedListEntry;
import google.registry.model.registry.label.ReservedListSqlDao;
import google.registry.model.registry.label.ReservedListDao;
import java.io.File;
import java.nio.file.Paths;
import org.junit.jupiter.api.BeforeEach;
@@ -55,7 +55,7 @@ class UpdateReservedListCommandTest
}
private void populateInitialReservedListInCloudSql(boolean shouldPublish) {
ReservedListSqlDao.save(
ReservedListDao.save(
createCloudSqlReservedList(
"xn--q9jyb4c_common-reserved",
fakeClock.nowUtc(),
@@ -128,7 +128,7 @@ class UpdateReservedListCommandTest
// Datastore when we update it.
runCommandForced("--name=xn--q9jyb4c_common-reserved", "--input=" + reservedTermsPath);
verifyXnq9jyb4cInDatastore();
assertThat(ReservedListSqlDao.checkExists("xn--q9jyb4c_common-reserved")).isTrue();
assertThat(ReservedListDao.checkExists("xn--q9jyb4c_common-reserved")).isTrue();
}
@Test