Add global cursor functionality to UpdateCursorsCommand

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143965268
This commit is contained in:
ctingue 2017-01-09 08:28:29 -08:00 committed by Ben McIlwain
parent 3a5a7e030d
commit bca2169c68
2 changed files with 84 additions and 15 deletions

View file

@ -15,6 +15,7 @@
package google.registry.tools;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.isNullOrEmpty;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
@ -29,9 +30,7 @@ import org.joda.time.DateTime;
@Parameters(separators = " =", commandDescription = "Modifies cursor timestamps used by LRC tasks")
final class UpdateCursorsCommand extends MutatingCommand {
@Parameter(
description = "TLDs on which to operate.",
required = true)
@Parameter(description = "TLDs on which to operate. Omit for global cursors.")
private List<String> tlds;
@Parameter(
@ -49,6 +48,10 @@ final class UpdateCursorsCommand extends MutatingCommand {
@Override
protected void init() throws Exception {
if (isNullOrEmpty(tlds)) {
Cursor cursor = ofy().load().key(Cursor.createGlobalKey(cursorType)).now();
stageEntityChange(cursor, Cursor.createGlobal(cursorType, newTimestamp));
} else {
for (String tld : tlds) {
Registry registry = Registry.get(tld);
Cursor cursor = ofy().load().key(Cursor.createKey(cursorType, registry)).now();
@ -57,4 +60,5 @@ final class UpdateCursorsCommand extends MutatingCommand {
Cursor.create(cursorType, newTimestamp, registry));
}
}
}
}

View file

@ -19,9 +19,11 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistResource;
import com.beust.jcommander.ParameterException;
import google.registry.model.common.Cursor;
import google.registry.model.common.Cursor.CursorType;
import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.RegistryNotFoundException;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
@ -43,15 +45,78 @@ public class UpdateCursorsCommandTest extends CommandTestCase<UpdateCursorsComma
.isEqualTo(DateTime.parse("1984-12-18TZ"));
}
void doGlobalUpdateTest() throws Exception {
runCommandForced("--type=recurring_billing", "--timestamp=1984-12-18T00:00:00Z");
assertThat(
ofy()
.load()
.key(Cursor.createGlobalKey(CursorType.RECURRING_BILLING))
.now()
.getCursorTime())
.isEqualTo(DateTime.parse("1984-12-18TZ"));
}
@Test
public void testUpdateCursors_oldValueIsAbsent() throws Exception {
public void testSuccess_oldValueIsAbsent() throws Exception {
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry)).now()).isNull();
doUpdateTest();
}
@Test
public void testUpdateCursors_hasOldValue() throws Exception {
public void testSuccess_hasOldValue() throws Exception {
persistResource(Cursor.create(CursorType.BRDA, DateTime.parse("1950-12-18TZ"), registry));
doUpdateTest();
}
@Test
public void testSuccess_global_hasOldValue() throws Exception {
persistResource(
Cursor.createGlobal(CursorType.RECURRING_BILLING, DateTime.parse("1950-12-18TZ")));
doGlobalUpdateTest();
}
@Test
public void testSuccess_global_oldValueIsAbsent() throws Exception {
assertThat(ofy().load().key(Cursor.createGlobalKey(CursorType.RECURRING_BILLING)).now())
.isNull();
doGlobalUpdateTest();
}
@Test
public void testSuccess_multipleTlds_hasOldValue() throws Exception {
createTld("bar");
Registry registry2 = Registry.get("bar");
persistResource(Cursor.create(CursorType.BRDA, DateTime.parse("1950-12-18TZ"), registry));
persistResource(Cursor.create(CursorType.BRDA, DateTime.parse("1950-12-18TZ"), registry2));
runCommandForced("--type=brda", "--timestamp=1984-12-18T00:00:00Z", "foo", "bar");
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry)).now().getCursorTime())
.isEqualTo(DateTime.parse("1984-12-18TZ"));
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry2)).now().getCursorTime())
.isEqualTo(DateTime.parse("1984-12-18TZ"));
}
@Test
public void testSuccess_multipleTlds_oldValueIsAbsent() throws Exception {
createTld("bar");
Registry registry2 = Registry.get("bar");
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry)).now()).isNull();
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry2)).now()).isNull();
runCommandForced("--type=brda", "--timestamp=1984-12-18T00:00:00Z", "foo", "bar");
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry)).now().getCursorTime())
.isEqualTo(DateTime.parse("1984-12-18TZ"));
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry2)).now().getCursorTime())
.isEqualTo(DateTime.parse("1984-12-18TZ"));
}
@Test
public void testFailure_badTld() throws Exception {
thrown.expect(RegistryNotFoundException.class);
runCommandForced("--type=brda", "--timestamp=1984-12-18T00:00:00Z", "bar");
}
@Test
public void testFailure_badCursorType() throws Exception {
thrown.expect(ParameterException.class, "Invalid value for --type parameter");
runCommandForced("--type=rbda", "--timestamp=1984-12-18T00:00:00Z", "foo");
}
}