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; package google.registry.tools;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.CollectionUtils.isNullOrEmpty;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
@ -29,9 +30,7 @@ import org.joda.time.DateTime;
@Parameters(separators = " =", commandDescription = "Modifies cursor timestamps used by LRC tasks") @Parameters(separators = " =", commandDescription = "Modifies cursor timestamps used by LRC tasks")
final class UpdateCursorsCommand extends MutatingCommand { final class UpdateCursorsCommand extends MutatingCommand {
@Parameter( @Parameter(description = "TLDs on which to operate. Omit for global cursors.")
description = "TLDs on which to operate.",
required = true)
private List<String> tlds; private List<String> tlds;
@Parameter( @Parameter(
@ -49,12 +48,17 @@ final class UpdateCursorsCommand extends MutatingCommand {
@Override @Override
protected void init() throws Exception { protected void init() throws Exception {
for (String tld : tlds) { if (isNullOrEmpty(tlds)) {
Registry registry = Registry.get(tld); Cursor cursor = ofy().load().key(Cursor.createGlobalKey(cursorType)).now();
Cursor cursor = ofy().load().key(Cursor.createKey(cursorType, registry)).now(); stageEntityChange(cursor, Cursor.createGlobal(cursorType, newTimestamp));
stageEntityChange( } else {
cursor, for (String tld : tlds) {
Cursor.create(cursorType, newTimestamp, registry)); Registry registry = Registry.get(tld);
Cursor cursor = ofy().load().key(Cursor.createKey(cursorType, registry)).now();
stageEntityChange(
cursor,
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.createTld;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import com.beust.jcommander.ParameterException;
import google.registry.model.common.Cursor; import google.registry.model.common.Cursor;
import google.registry.model.common.Cursor.CursorType; import google.registry.model.common.Cursor.CursorType;
import google.registry.model.registry.Registry; import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.RegistryNotFoundException;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -43,15 +45,78 @@ public class UpdateCursorsCommandTest extends CommandTestCase<UpdateCursorsComma
.isEqualTo(DateTime.parse("1984-12-18TZ")); .isEqualTo(DateTime.parse("1984-12-18TZ"));
} }
@Test void doGlobalUpdateTest() throws Exception {
public void testUpdateCursors_oldValueIsAbsent() throws Exception { runCommandForced("--type=recurring_billing", "--timestamp=1984-12-18T00:00:00Z");
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry)).now()).isNull(); assertThat(
doUpdateTest(); ofy()
} .load()
.key(Cursor.createGlobalKey(CursorType.RECURRING_BILLING))
.now()
.getCursorTime())
.isEqualTo(DateTime.parse("1984-12-18TZ"));
}
@Test @Test
public void testUpdateCursors_hasOldValue() throws Exception { public void testSuccess_oldValueIsAbsent() throws Exception {
assertThat(ofy().load().key(Cursor.createKey(CursorType.BRDA, registry)).now()).isNull();
doUpdateTest();
}
@Test
public void testSuccess_hasOldValue() throws Exception {
persistResource(Cursor.create(CursorType.BRDA, DateTime.parse("1950-12-18TZ"), registry)); persistResource(Cursor.create(CursorType.BRDA, DateTime.parse("1950-12-18TZ"), registry));
doUpdateTest(); 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");
}
} }