diff --git a/core/src/main/java/google/registry/tools/ConfigureTldCommand.java b/core/src/main/java/google/registry/tools/ConfigureTldCommand.java index f7302625f..23d0c05c3 100644 --- a/core/src/main/java/google/registry/tools/ConfigureTldCommand.java +++ b/core/src/main/java/google/registry/tools/ConfigureTldCommand.java @@ -72,9 +72,9 @@ public class ConfigureTldCommand extends MutatingCommand { boolean breakglass; @Parameter( - names = {"-d", "--dryrun"}, + names = {"-d", "--dry_run"}, description = "Does not execute the entity mutation") - boolean dryrun; + boolean dryRun; @Inject ObjectMapper mapper; @@ -138,7 +138,7 @@ public class ConfigureTldCommand extends MutatingCommand { @Override protected boolean dontRunCommand() { - if (dryrun) { + if (dryRun) { return true; } if (!newDiff) { diff --git a/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java b/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java index 0c11bccb4..2d9cc4eae 100644 --- a/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java +++ b/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static google.registry.util.ListNamingUtils.convertFilePathToName; import static java.nio.charset.StandardCharsets.UTF_8; +import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.common.base.Strings; import google.registry.model.tld.label.PremiumList; @@ -29,6 +30,14 @@ import java.nio.file.Files; @Parameters(separators = " =", commandDescription = "Update a PremiumList in Database.") class UpdatePremiumListCommand extends CreateOrUpdatePremiumListCommand { + @Parameter( + names = {"-d", "--dry_run"}, + description = "Does not execute the entity mutation") + boolean dryRun; + + // indicates if there is a new change made by this command + private boolean newChange = false; + @Override protected String prompt() throws Exception { name = Strings.isNullOrEmpty(name) ? convertFilePathToName(inputFile) : name; @@ -43,8 +52,23 @@ class UpdatePremiumListCommand extends CreateOrUpdatePremiumListCommand { checkArgument(!inputData.isEmpty(), "New premium list data cannot be empty"); currency = existingList.getCurrency(); PremiumList updatedPremiumList = PremiumListUtils.parseToPremiumList(name, currency, inputData); - return String.format( - "Update premium list for %s?\n Old List: %s\n New List: %s", - name, existingList, updatedPremiumList); + if (!existingList + .getLabelsToPrices() + .entrySet() + .equals(updatedPremiumList.getLabelsToPrices().entrySet())) { + newChange = true; + return String.format( + "Update premium list for %s?\n Old List: %s\n New List: %s", + name, existingList, updatedPremiumList); + } else { + return String.format( + "This update contains no changes to the premium list for %s.\n List Contents: %s", + name, existingList); + } + } + + @Override + protected boolean dontRunCommand() { + return dryRun || !newChange; } } diff --git a/core/src/test/java/google/registry/tools/ConfigureTldCommandTest.java b/core/src/test/java/google/registry/tools/ConfigureTldCommandTest.java index cbac74db9..9ffa2ce14 100644 --- a/core/src/test/java/google/registry/tools/ConfigureTldCommandTest.java +++ b/core/src/test/java/google/registry/tools/ConfigureTldCommandTest.java @@ -593,7 +593,7 @@ public class ConfigureTldCommandTest extends CommandTestCase Tld.get("tld")); } diff --git a/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java b/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java index 8a8e653fe..d043a7f2e 100644 --- a/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java +++ b/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java @@ -63,10 +63,19 @@ class UpdatePremiumListCommandTest UpdatePremiumListCommand command = new UpdatePremiumListCommand(); command.inputFile = Paths.get(tmpFile.getPath()); command.name = TLD_TEST; - command.prompt(); assertThat(command.prompt()).contains("Update premium list for prime?"); } + @Test + void commandPrompt_successStageNoChange() throws Exception { + File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile(); + UpdatePremiumListCommand command = new UpdatePremiumListCommand(); + command.inputFile = Paths.get(tmpFile.getPath()); + command.name = TLD_TEST; + assertThat(command.prompt()) + .contains("This update contains no changes to the premium list for prime."); + } + @Test void commandRun_successUpdateList() throws Exception { File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile(); @@ -83,6 +92,18 @@ class UpdatePremiumListCommandTest .containsExactly(PremiumEntry.create(0L, new BigDecimal("9999.00"), "eth")); } + @Test + void commandRun_successNoChange() throws Exception { + File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile(); + + UpdatePremiumListCommand command = new UpdatePremiumListCommand(); + command.inputFile = Paths.get(tmpFile.getPath()); + runCommandForced("--name=" + TLD_TEST, "--input=" + command.inputFile); + + assertThat(PremiumListDao.loadAllPremiumEntries(TLD_TEST)) + .containsExactly(PremiumEntry.create(0L, new BigDecimal("9090.00"), "doge")); + } + @Test void commandRun_successUpdateList_whenExistingListIsEmpty() throws Exception { File existingPremiumFile = tmpDir.resolve(TLD_TEST + ".txt").toFile(); @@ -169,4 +190,19 @@ class UpdatePremiumListCommandTest .hasMessageThat() .isEqualTo("Could not update premium list random3 because it doesn't exist"); } + + @Test + void commandDryRun_noChangesMade() 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(); + command.inputFile = Paths.get(tmpFile.getPath()); + runCommandForced("--name=" + TLD_TEST, "--input=" + command.inputFile, "--dry_run"); + + assertThat(PremiumListDao.loadAllPremiumEntries(TLD_TEST)) + .comparingElementsUsing(immutableObjectCorrespondence("revisionId")) + .containsExactly(PremiumEntry.create(0L, new BigDecimal("9090.00"), "doge")); + } }