Add metadata parameters to CreateLrpTokensCommand

As part of this change, built out a KeyValueMapParameter from the existing TransitionListParameter in order to enable other Map types in commands (two of which are used in CreateLrpTokensCommand).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137476564
This commit is contained in:
ctingue 2016-10-27 21:16:37 -07:00 committed by Ben McIlwain
parent 1dbc5f6bb0
commit c89a902b72
6 changed files with 326 additions and 52 deletions

View file

@ -55,7 +55,20 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
public void testSuccess_oneAssignee() throws Exception {
runCommand("--assignee=domain.tld", "--tlds=tld");
assertLrpTokens(
createToken("LRP_abcdefghijklmnop", "domain.tld", ImmutableSet.of("tld"), null));
createToken("LRP_abcdefghijklmnop", "domain.tld", ImmutableSet.of("tld"), null, null));
assertInStdout("domain.tld,LRP_abcdefghijklmnop");
}
@Test
public void testSuccess_oneAssignee_withMetadata() throws Exception {
runCommand("--assignee=domain.tld", "--tlds=tld", "--metadata=key=foo,key2=bar");
assertLrpTokens(
createToken(
"LRP_abcdefghijklmnop",
"domain.tld",
ImmutableSet.of("tld"),
null,
ImmutableMap.of("key", "foo", "key2", "bar")));
assertInStdout("domain.tld,LRP_abcdefghijklmnop");
}
@ -69,7 +82,7 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
runCommand("--assignee=domain.tld", "--tlds=tld");
assertLrpTokens(
existingToken,
createToken("LRP_qrstuvwxyzabcdef", "domain.tld", ImmutableSet.of("tld"), null));
createToken("LRP_qrstuvwxyzabcdef", "domain.tld", ImmutableSet.of("tld"), null, null));
assertInStdout("domain.tld,LRP_qrstuvwxyzabcdef");
}
@ -78,7 +91,21 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
Files.write("domain.tld", assigneeFile, UTF_8);
runCommand("--input=" + assigneeFilePath, "--tlds=tld");
assertLrpTokens(
createToken("LRP_abcdefghijklmnop", "domain.tld", ImmutableSet.of("tld"), null));
createToken("LRP_abcdefghijklmnop", "domain.tld", ImmutableSet.of("tld"), null, null));
assertInStdout("domain.tld,LRP_abcdefghijklmnop");
}
@Test
public void testSuccess_oneAssignee_byFile_withMetadata() throws Exception {
Files.write("domain.tld,foo,bar", assigneeFile, UTF_8);
runCommand("--input=" + assigneeFilePath, "--tlds=tld", "--metadata_columns=key=1,key2=2");
assertLrpTokens(
createToken(
"LRP_abcdefghijklmnop",
"domain.tld",
ImmutableSet.of("tld"),
null,
ImmutableMap.of("key", "foo", "key2", "bar")));
assertInStdout("domain.tld,LRP_abcdefghijklmnop");
}
@ -96,9 +123,9 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
runCommand("--input=" + assigneeFilePath, "--tlds=tld");
assertLrpTokens(
createToken("LRP_abcdefghijklmnop", "domain1.tld", ImmutableSet.of("tld"), null),
createToken("LRP_qrstuvwxyzabcdef", "domain2.tld", ImmutableSet.of("tld"), null),
createToken("LRP_ghijklmnopqrstuv", "domain3.tld", ImmutableSet.of("tld"), null));
createToken("LRP_abcdefghijklmnop", "domain1.tld", ImmutableSet.of("tld"), null, null),
createToken("LRP_qrstuvwxyzabcdef", "domain2.tld", ImmutableSet.of("tld"), null, null),
createToken("LRP_ghijklmnopqrstuv", "domain3.tld", ImmutableSet.of("tld"), null, null));
assertInStdout("domain1.tld,LRP_abcdefghijklmnop");
assertInStdout("domain2.tld,LRP_qrstuvwxyzabcdef");
@ -110,15 +137,15 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
Files.write("domain1.tld\n\ndomain2.tld", assigneeFile, UTF_8);
runCommand("--input=" + assigneeFilePath, "--tlds=tld");
assertLrpTokens(
createToken("LRP_abcdefghijklmnop", "domain1.tld", ImmutableSet.of("tld"), null),
createToken("LRP_abcdefghijklmnop", "domain1.tld", ImmutableSet.of("tld"), null, null),
// Second deterministic token (LRP_qrstuvwxyzabcdef) still consumed but not assigned
createToken("LRP_ghijklmnopqrstuv", "domain2.tld", ImmutableSet.of("tld"), null));
createToken("LRP_ghijklmnopqrstuv", "domain2.tld", ImmutableSet.of("tld"), null, null));
assertInStdout("domain1.tld,LRP_abcdefghijklmnop");
assertInStdout("domain2.tld,LRP_ghijklmnopqrstuv");
}
@Test
public void testSuccess_largeFile() throws Exception {
public void testSuccess_largeFile_withMetadata() throws Exception {
int numberOfTokens = 67;
LrpTokenEntity[] expectedTokens = new LrpTokenEntity[numberOfTokens];
// Prepend a counter to avoid collisions, 16-char alphabet will always generate the same string.
@ -127,16 +154,17 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
command.stringGenerator = stringGenerator;
StringBuilder assigneeFileBuilder = new StringBuilder();
for (int i = 0; i < numberOfTokens; i++) {
assigneeFileBuilder.append(String.format("domain%d.tld\n", i));
assigneeFileBuilder.append(String.format("domain%d.tld,%d,%d\n", i, i * 2, i * 3));
expectedTokens[i] =
createToken(
String.format("LRP_%04d_abcdefghijklmnop", i),
String.format("domain%d.tld", i),
ImmutableSet.of("tld"),
null);
null,
ImmutableMap.of("key", Integer.toString(i * 2), "key2", Integer.toString(i * 3)));
}
Files.write(assigneeFileBuilder, assigneeFile, UTF_8);
runCommand("--input=" + assigneeFilePath, "--tlds=tld");
runCommand("--input=" + assigneeFilePath, "--tlds=tld", "--metadata_columns=key=1,key2=2");
assertLrpTokens(expectedTokens);
for (int i = 0; i < numberOfTokens; i++) {
assertInStdout(String.format("domain%d.tld,LRP_%04d_abcdefghijklmnop", i, i));
@ -159,12 +187,36 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
runCommand("--assignee=domain.tld", "--tlds=tld", "--input=" + assigneeFilePath);
}
@Test
public void testFailure_bothMetadataAndFile() throws Exception {
thrown.expect(
IllegalArgumentException.class,
"Metadata cannot be specified along with a filename.");
runCommand("--tlds=tld", "--input=" + assigneeFilePath, "--metadata=key=foo");
}
@Test
public void testFailure_bothAssigneeAndMetadataColumns() throws Exception {
thrown.expect(
IllegalArgumentException.class,
"Metadata columns cannot be specified along with an assignee.");
runCommand("--assignee=domain.tld", "--tlds=tld", "--metadata_columns=foo=1");
}
@Test
public void testFailure_badTld() throws Exception {
thrown.expect(IllegalArgumentException.class, "TLD foo does not exist");
runCommand("--assignee=domain.tld", "--tlds=foo");
}
@Test
public void testFailure_oneAssignee_byFile_insufficientMetadata() throws Exception {
Files.write("domain.tld,foo", assigneeFile, UTF_8);
thrown.expect(IllegalArgumentException.class,
"Entry for domain.tld does not have a value for key2 (index 2)");
runCommand("--input=" + assigneeFilePath, "--tlds=tld", "--metadata_columns=key=1,key2=2");
}
private void assertLrpTokens(LrpTokenEntity... expected) throws Exception {
// Using ImmutableObject comparison here is tricky because updateTimestamp is not set on the
// expected LrpToken objects and will cause the assert to fail.
@ -190,7 +242,8 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
String token,
String assignee,
Set<String> validTlds,
@Nullable Key<HistoryEntry> redemptionHistoryEntry) {
@Nullable Key<HistoryEntry> redemptionHistoryEntry,
@Nullable ImmutableMap<String, String> metadata) {
LrpTokenEntity.Builder tokenBuilder = new LrpTokenEntity.Builder()
.setAssignee(assignee)
.setValidTlds(validTlds)
@ -198,6 +251,9 @@ public class CreateLrpTokensCommandTest extends CommandTestCase<CreateLrpTokensC
if (redemptionHistoryEntry != null) {
tokenBuilder.setRedemptionHistoryEntry(redemptionHistoryEntry);
}
if (metadata != null) {
tokenBuilder.setMetadata(metadata);
}
return tokenBuilder.build();
}
}

View file

@ -11,6 +11,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//java/com/google/common/base",
"//java/com/google/common/collect",
"//third_party/java/hamcrest",
"//third_party/java/jcommander",
"//third_party/java/joda_money",

View file

@ -0,0 +1,101 @@
// Copyright 2016 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.params;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableMap;
import google.registry.testing.ExceptionRule;
import google.registry.tools.params.KeyValueMapParameter.StringToIntegerMap;
import google.registry.tools.params.KeyValueMapParameter.StringToStringMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link KeyValueMapParameter}. */
@RunWith(JUnit4.class)
public class KeyValueMapParameterTest {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
private final StringToStringMap stringToStringInstance = new StringToStringMap();
private final StringToIntegerMap stringToIntegerInstance = new StringToIntegerMap();
@Test
public void testSuccess_convertStringToString_singleEntry() throws Exception {
assertThat(stringToStringInstance.convert("key=foo"))
.isEqualTo(ImmutableMap.of("key", "foo"));
}
@Test
public void testSuccess_convertStringToInteger_singleEntry() throws Exception {
assertThat(stringToIntegerInstance.convert("key=1"))
.isEqualTo(ImmutableMap.of("key", 1));
}
@Test
public void testSuccess_convertStringToString() throws Exception {
assertThat(stringToStringInstance.convert("key=foo,key2=bar"))
.isEqualTo(ImmutableMap.of("key", "foo", "key2", "bar"));
}
@Test
public void testSuccess_convertStringToInteger() throws Exception {
assertThat(stringToIntegerInstance.convert("key=1,key2=2"))
.isEqualTo(ImmutableMap.of("key", 1, "key2", 2));
}
@Test
public void testSuccess_convertStringToString_empty() throws Exception {
assertThat(stringToStringInstance.convert("")).isEmpty();
}
@Test
public void testSuccess_convertStringToInteger_empty() throws Exception {
assertThat(stringToIntegerInstance.convert("")).isEmpty();
}
@Test
public void testFailure_convertStringToInteger_badType() throws Exception {
thrown.expect(NumberFormatException.class);
stringToIntegerInstance.convert("key=1,key2=foo");
}
@Test
public void testFailure_convertStringToString_badSeparator() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToStringInstance.convert("key=foo&key2=bar");
}
@Test
public void testFailure_convertStringToInteger_badSeparator() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToIntegerInstance.convert("key=1&key2=2");
}
@Test
public void testFailure_convertStringToString_badFormat() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToStringInstance.convert("foo");
}
@Test
public void testFailure_convertStringToInteger_badFormat() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToIntegerInstance.convert("foo");
}
}