Change premium list command to be based off of mutating command (#1123)

* Change premium list command to be based off of mutating command

* Modify test cases and add comments for better readability

* Fix typo
This commit is contained in:
Rachel Guan
2021-05-14 08:40:03 -04:00
committed by GitHub
parent 2bb0e7305d
commit 27f431b9cf
6 changed files with 401 additions and 259 deletions
@@ -14,47 +14,64 @@
package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.registry.Registry.TldState.GENERAL_AVAILABILITY;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.newRegistry;
import static google.registry.testing.DatabaseHelper.persistPremiumList;
import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import com.google.common.collect.ImmutableMap;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.io.Files;
import com.google.common.net.MediaType;
import google.registry.testing.UriParameters;
import google.registry.dns.writer.VoidDnsWriter;
import google.registry.model.pricing.StaticPremiumListPricingEngine;
import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldType;
import java.io.File;
import java.nio.charset.StandardCharsets;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
/** Base class for common testing setup for create and update commands for Premium Lists. */
abstract class CreateOrUpdatePremiumListCommandTestCase<T extends CreateOrUpdatePremiumListCommand>
extends CommandTestCase<T> {
@Captor
ArgumentCaptor<ImmutableMap<String, String>> urlParamCaptor;
protected static final String TLD_TEST = "prime";
protected String premiumTermsPath;
protected String initialPremiumListData;
@Captor
ArgumentCaptor<byte[]> requestBodyCaptor;
static String generateInputData(String premiumTermsPath) throws Exception {
return Files.asCharSource(new File(premiumTermsPath), StandardCharsets.UTF_8).read();
@BeforeEach
void beforeEachCreateOrUpdatePremiumListCommandTestCase() throws IOException {
// initial set up for both CreatePremiumListCommand and UpdatePremiumListCommand test cases;
initialPremiumListData = "doge,USD 9090";
File premiumTermsFile = tmpDir.resolve(TLD_TEST + ".txt").toFile();
Files.asCharSink(premiumTermsFile, UTF_8).write(initialPremiumListData);
premiumTermsPath = premiumTermsFile.getPath();
}
void verifySentParams(
AppEngineConnection connection, String path, ImmutableMap<String, String> parameterMap)
throws Exception {
verify(connection)
.sendPostRequest(
eq(path),
urlParamCaptor.capture(),
eq(MediaType.FORM_DATA),
requestBodyCaptor.capture());
assertThat(new ImmutableMap.Builder<String, String>()
.putAll(urlParamCaptor.getValue())
.putAll(UriParameters.parse(new String(requestBodyCaptor.getValue(), UTF_8)).entries())
.build())
.containsExactlyEntriesIn(parameterMap);
Registry createRegistry(String tldStr, String premiumListInput) {
Registry registry;
if (premiumListInput != null) {
registry =
newRegistry(
tldStr,
Ascii.toUpperCase(tldStr),
ImmutableSortedMap.of(START_OF_TIME, GENERAL_AVAILABILITY),
TldType.TEST);
persistPremiumList(tldStr, premiumListInput);
persistResource(registry);
} else {
registry =
new Registry.Builder()
.setTldStr(tldStr)
.setPremiumPricingEngine(StaticPremiumListPricingEngine.NAME)
.setDnsWriters(ImmutableSet.of(VoidDnsWriter.NAME))
.setPremiumList(null)
.build();
tm().transact(() -> tm().put(registry));
}
return registry;
}
}
@@ -15,105 +15,125 @@
package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.request.JsonResponse.JSON_SAFETY_PREFIX;
import static google.registry.testing.TestDataHelper.loadFile;
import static google.registry.testing.DatabaseHelper.createTld;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
import com.beust.jcommander.ParameterException;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.server.CreatePremiumListAction;
import com.google.common.io.Files;
import google.registry.model.registry.Registry;
import google.registry.schema.tld.PremiumListSqlDao;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
/** Unit tests for {@link CreatePremiumListCommand}. */
class CreatePremiumListCommandTest<C extends CreatePremiumListCommand>
extends CreateOrUpdatePremiumListCommandTestCase<C> {
@Mock AppEngineConnection connection;
private String premiumTermsPath;
String premiumTermsCsv;
private String servletPath;
Registry registry;
@BeforeEach
void beforeEach() throws Exception {
command.setConnection(connection);
premiumTermsPath =
writeToNamedTmpFile(
"example_premium_terms.csv",
loadFile(CreatePremiumListCommandTest.class, "example_premium_terms.csv"));
servletPath = "/_dr/admin/createPremiumList";
when(connection.sendPostRequest(
eq(CreatePremiumListAction.PATH),
ArgumentMatchers.<String, String>anyMap(),
any(MediaType.class),
any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"success\",\"lines\":[]}");
void beforeEach() {
registry = createRegistry(TLD_TEST, null);
}
@Test
void testRun() throws Exception {
runCommandForced("-i=" + premiumTermsPath, "-n=foo");
assertInStdout("Successfully");
verifySentParams(
connection,
servletPath,
ImmutableMap.of("name", "foo", "inputData", generateInputData(premiumTermsPath)));
void verify_registryIsSetUpCorrectly() {
// ensure that no premium list is created before running the command
// this check also implicitly verifies the TLD is successfully created;
assertThat(PremiumListSqlDao.getLatestRevision(TLD_TEST).isPresent()).isFalse();
}
@Test
void testRun_noProvidedName_usesBasenameOfInputFile() throws Exception {
runCommandForced("-i=" + premiumTermsPath);
assertInStdout("Successfully");
verifySentParams(
connection,
servletPath,
ImmutableMap.of(
"name", "example_premium_terms", "inputData", generateInputData(premiumTermsPath)));
void commandRun_successCreateList() throws Exception {
runCommandForced("--name=" + TLD_TEST, "--input=" + premiumTermsPath);
assertThat(registry.getTld().toString()).isEqualTo(TLD_TEST);
assertThat(PremiumListSqlDao.getLatestRevision(TLD_TEST).isPresent()).isTrue();
}
@Test
void testRun_errorResponse() throws Exception {
reset(connection);
command.setConnection(connection);
when(connection.sendPostRequest(
eq(CreatePremiumListAction.PATH), anyMap(), any(MediaType.class), any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"error\",\"error\":\"foo already exists\"}");
VerifyException thrown =
assertThrows(
VerifyException.class, () -> runCommandForced("-i=" + premiumTermsPath, "-n=foo"));
assertThat(thrown).hasMessageThat().contains("Server error:");
// since the old entity is always null and file cannot be empty, the prompt will NOT be "No entity
// changes to apply."
void commandInit_successStageNewEntity() throws Exception {
CreatePremiumListCommand command = new CreatePremiumListCommand();
command.inputFile = Paths.get(premiumTermsPath);
command.init();
assertThat(command.prompt()).contains("Create PremiumList@");
assertThat(command.prompt()).contains(String.format("name=%s", TLD_TEST));
}
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void testRun_noInputFileSpecified_throwsException() {
ParameterException thrown = assertThrows(ParameterException.class, this::runCommand);
assertThat(thrown).hasMessageThat().contains("The following option is required");
void commandInit_successStageNewEntityWithOverride() throws Exception {
CreatePremiumListCommand command = new CreatePremiumListCommand();
String alterTld = "override";
command.inputFile = Paths.get(premiumTermsPath);
command.override = true;
command.name = alterTld;
command.init();
assertThat(command.prompt()).contains("Create PremiumList@");
assertThat(command.prompt()).contains(String.format("name=%s", alterTld));
}
@Test
@MockitoSettings(strictness = Strictness.LENIENT)
void testRun_invalidInputData() throws Exception {
premiumTermsPath =
writeToNamedTmpFile(
"tmp_file2",
loadFile(CreatePremiumListCommandTest.class, "example_invalid_premium_terms.csv"));
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> runCommandForced("-i=" + premiumTermsPath, "-n=foo"));
assertThat(thrown).hasMessageThat().contains("Could not parse line in premium list");
void commandInit_failureNoInputFile() {
CreatePremiumListCommand command = new CreatePremiumListCommand();
assertThrows(NullPointerException.class, command::init);
}
@Test
void commandInit_failurePremiumListAlreadyExists() {
String randomStr = "random";
createTld(randomStr);
CreatePremiumListCommand command = new CreatePremiumListCommand();
command.name = randomStr;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown).hasMessageThat().isEqualTo("A premium list already exists by this name");
}
@Test
void commandInit_failureMismatchedTldFileName_noOverride() throws Exception {
CreatePremiumListCommand command = new CreatePremiumListCommand();
String fileName = "random";
Path tmpPath = tmpDir.resolve(String.format("%s.txt", fileName));
Files.write(new byte[0], tmpPath.toFile());
command.inputFile = tmpPath;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.contains(
String.format(
"Premium names must match the name of the TLD they are "
+ "intended to be used on (unless --override is specified), "
+ "yet TLD %s does not exist",
fileName));
}
@Test
void commandInit_failureMismatchedTldName_noOverride() {
CreatePremiumListCommand command = new CreatePremiumListCommand();
String fileName = "random";
command.name = fileName;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.contains(
String.format(
"Premium names must match the name of the TLD they are "
+ "intended to be used on (unless --override is specified), "
+ "yet TLD %s does not exist",
fileName));
}
@Test
void commandInit_failureUseEmptyFile() throws Exception {
CreatePremiumListCommand command = new CreatePremiumListCommand();
String fileName = "empty";
Path tmpPath = tmpDir.resolve(String.format("%s.txt", fileName));
Files.write(new byte[0], tmpPath.toFile());
command.inputFile = tmpPath;
command.name = TLD_TEST;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.contains("The Cloud SQL schema requires exactly one currency");
}
}
@@ -14,60 +14,154 @@
package google.registry.tools;
import static google.registry.request.JsonResponse.JSON_SAFETY_PREFIX;
import static google.registry.testing.TestDataHelper.loadFile;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.server.UpdatePremiumListAction;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import google.registry.model.registry.Registry;
import google.registry.schema.tld.PremiumListSqlDao;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
/** Unit tests for {@link UpdatePremiumListCommand}. */
class UpdatePremiumListCommandTest<C extends UpdatePremiumListCommand>
extends CreateOrUpdatePremiumListCommandTestCase<C> {
@Mock AppEngineConnection connection;
private String premiumTermsPath;
String premiumTermsCsv;
private String servletPath;
Registry registry;
@BeforeEach
void beforeEach() throws Exception {
command.setConnection(connection);
servletPath = "/_dr/admin/updatePremiumList";
premiumTermsPath =
writeToNamedTmpFile(
"example_premium_terms.csv",
loadFile(UpdatePremiumListCommandTest.class, "example_premium_terms.csv"));
when(connection.sendPostRequest(
eq(UpdatePremiumListAction.PATH), anyMap(), any(MediaType.class), any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"success\",\"lines\":[]}");
void beforeEach() {
registry = createRegistry(TLD_TEST, initialPremiumListData);
}
@Test
void testRun() throws Exception {
runCommandForced("-i=" + premiumTermsPath, "-n=foo");
verifySentParams(
connection,
servletPath,
ImmutableMap.of("name", "foo", "inputData", generateInputData(premiumTermsPath)));
void verify_registryIsSetUpCorrectly() {
// ensure that no premium list is created before running the command
assertThat(PremiumListSqlDao.getLatestRevision(TLD_TEST).isPresent()).isTrue();
// ensure that there's value in existing premium list;
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
ImmutableSet<String> entries = command.getExistingPremiumListEntry(TLD_TEST);
assertThat(entries.size()).isEqualTo(1);
// data from @beforeEach of CreateOrUpdatePremiumListCommandTestCase.java
assertThat(entries.contains("doge,USD 9090.00")).isTrue();
}
@Test
void testRun_noProvidedName_usesBasenameOfInputFile() throws Exception {
runCommandForced("-i=" + premiumTermsPath);
assertInStdout("Successfully");
verifySentParams(
connection,
servletPath,
ImmutableMap.of(
"name", "example_premium_terms", "inputData", generateInputData(premiumTermsPath)));
void commandInit_successStageNoEntityChange() throws Exception {
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
command.inputFile = Paths.get(premiumTermsPath);
command.name = TLD_TEST;
command.init();
assertThat(command.prompt()).contains("No entity changes to apply.");
}
@Test
void commandInit_successStageEntityChange() throws Exception {
File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile();
String newPremiumListData = "omg,JPY 1234";
Files.asCharSink(tmpFile, UTF_8).write(newPremiumListData);
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
command.inputFile = Paths.get(tmpFile.getPath());
command.name = TLD_TEST;
command.init();
assertThat(command.prompt()).contains("Update PremiumList@");
}
@Test
void commandRun_successUpdateList() throws Exception {
File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile();
String newPremiumListData = "eth,USD 9999";
Files.asCharSink(tmpFile, UTF_8).write(newPremiumListData);
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
// data come from @beforeEach of CreateOrUpdatePremiumListCommandTestCase.java
command.inputFile = Paths.get(tmpFile.getPath());
runCommandForced("--name=" + TLD_TEST, "--input=" + command.inputFile);
ImmutableSet<String> entries = command.getExistingPremiumListEntry(TLD_TEST);
assertThat(entries.size()).isEqualTo(1);
// verify that list is updated; cannot use only string since price is formatted;
assertThat(entries.contains("eth,USD 9999.00")).isTrue();
}
@Test
void commandRun_successUpdateMultiLineList() throws Exception {
File tmpFile = tmpDir.resolve(TLD_TEST + ".txt").toFile();
String premiumTerms = "foo,USD 9000\ndoge,USD 100\nelon,USD 2021";
Files.asCharSink(tmpFile, UTF_8).write(premiumTerms);
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
command.inputFile = Paths.get(tmpFile.getPath());
runCommandForced("--name=" + TLD_TEST, "--input=" + command.inputFile);
// assert all three lines from premiumTerms are added
ImmutableSet<String> entries = command.getExistingPremiumListEntry(TLD_TEST);
assertThat(entries.size()).isEqualTo(3);
assertThat(entries.contains("foo,USD 9000.00")).isTrue();
assertThat(entries.contains("doge,USD 100.00")).isTrue();
assertThat(entries.contains("elon,USD 2021.00")).isTrue();
}
@Test
void commandInit_failureUpdateEmptyList() throws Exception {
Path tmpPath = tmpDir.resolve(String.format("%s.txt", TLD_TEST));
Files.write(new byte[0], tmpPath.toFile());
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
command.inputFile = tmpPath;
command.name = TLD_TEST;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.contains("The Cloud SQL schema requires exactly one currency");
}
@Test
void commandInit_failureNoPreviousVersion() {
String fileName = "random";
registry = createRegistry(fileName, null);
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
command.name = fileName;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
String.format("Could not update premium list %s because it doesn't exist.", fileName));
}
@Test
void commandInit_failureNoInputFile() {
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
assertThrows(NullPointerException.class, command::init);
}
@Test
void commandInit_failureTldFromNameDoesNotExist() {
String fileName = "random";
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
command.name = fileName;
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
String.format("Could not update premium list %s because it doesn't exist.", fileName));
}
@Test
void commandInit_failureTldFromInputFileDoesNotExist() {
String fileName = "random";
UpdatePremiumListCommand command = new UpdatePremiumListCommand();
// using tld extracted from file name but this tld is not part of the registry
command.inputFile =
Paths.get(tmpDir.resolve(String.format("%s.txt", fileName)).toFile().getPath());
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, command::init);
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
String.format("Could not update premium list %s because it doesn't exist.", fileName));
}
}