diff --git a/java/google/registry/cron/TldFanoutAction.java b/java/google/registry/cron/TldFanoutAction.java index 6df61a35b..c6a69f58d 100644 --- a/java/google/registry/cron/TldFanoutAction.java +++ b/java/google/registry/cron/TldFanoutAction.java @@ -23,12 +23,14 @@ import static com.google.common.collect.Iterables.concat; import static com.google.common.collect.Iterables.getFirst; import static com.google.common.collect.Multimaps.filterKeys; import static com.google.common.collect.Sets.difference; +import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; import static google.registry.model.registry.Registries.getTldsOfType; import static google.registry.model.registry.Registry.TldType.REAL; import static google.registry.model.registry.Registry.TldType.TEST; import static java.util.concurrent.TimeUnit.SECONDS; import com.google.appengine.api.taskqueue.Queue; +import com.google.appengine.api.taskqueue.TaskHandle; import com.google.appengine.api.taskqueue.TaskOptions; import com.google.common.base.Optional; import com.google.common.collect.ImmutableListMultimap; @@ -38,6 +40,7 @@ import google.registry.request.Action; import google.registry.request.Parameter; import google.registry.request.ParameterMap; import google.registry.request.RequestParameters; +import google.registry.request.Response; import google.registry.util.TaskEnqueuer; import java.util.Random; import java.util.Set; @@ -93,6 +96,7 @@ public final class TldFanoutAction implements Runnable { private static final Random random = new Random(); @Inject TaskEnqueuer taskEnqueuer; + @Inject Response response; @Inject @Parameter(ENDPOINT_PARAM) String endpoint; @Inject @Parameter(QUEUE_PARAM) String queue; @Inject @Parameter(FOR_EACH_REAL_TLD_PARAM) boolean forEachRealTld; @@ -105,15 +109,26 @@ public final class TldFanoutAction implements Runnable { @Override public void run() { - Set namespaces = ImmutableSet.copyOf(concat( - runInEmpty ? ImmutableSet.of("") : ImmutableSet.of(), - forEachRealTld ? getTldsOfType(REAL) : ImmutableSet.of(), - forEachTestTld ? getTldsOfType(TEST) : ImmutableSet.of())); + Set tlds = + difference( + ImmutableSet.copyOf( + concat( + runInEmpty ? ImmutableSet.of("") : ImmutableSet.of(), + forEachRealTld ? getTldsOfType(REAL) : ImmutableSet.of(), + forEachTestTld ? getTldsOfType(TEST) : ImmutableSet.of())), + excludes); Multimap flowThruParams = filterKeys(params, not(in(CONTROL_PARAMS))); Queue taskQueue = getQueue(queue); - for (String namespace : difference(namespaces, excludes)) { - taskEnqueuer.enqueue(taskQueue, createTaskOptions(namespace, flowThruParams)); + String outputPayload = String.format( + "OK: Launched the following %d tasks in queue %s\n", tlds.size(), queue); + for (String tld : tlds) { + TaskOptions taskOptions = createTaskOptions(tld, flowThruParams); + TaskHandle taskHandle = taskEnqueuer.enqueue(taskQueue, taskOptions); + outputPayload += String.format( + "- Task: %s, tld: %s, endpoint: %s\n", taskHandle.getName(), tld, taskOptions.getUrl()); } + response.setContentType(PLAIN_TEXT_UTF_8); + response.setPayload(outputPayload); } private TaskOptions createTaskOptions(String tld, Multimap params) { diff --git a/javatests/google/registry/cron/BUILD b/javatests/google/registry/cron/BUILD index 15989e30b..6d203e217 100644 --- a/javatests/google/registry/cron/BUILD +++ b/javatests/google/registry/cron/BUILD @@ -17,6 +17,8 @@ java_library( "//javatests/google/registry/testing", "//third_party/java/objectify:objectify-v4_1", "@com_google_appengine_api_1_0_sdk//:testonly", + "@com_google_appengine_api_stubs", + "@com_google_appengine_testing", "@com_google_guava", "@com_google_truth", "@javax_servlet_api", diff --git a/javatests/google/registry/cron/TldFanoutActionTest.java b/javatests/google/registry/cron/TldFanoutActionTest.java index d8245bd06..641f46254 100644 --- a/javatests/google/registry/cron/TldFanoutActionTest.java +++ b/javatests/google/registry/cron/TldFanoutActionTest.java @@ -16,12 +16,15 @@ package google.registry.cron; import static com.google.common.collect.Iterables.getLast; import static com.google.common.collect.Lists.transform; +import static com.google.common.truth.Truth.assertThat; import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued; import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued; import static java.util.Arrays.asList; +import com.google.appengine.api.taskqueue.dev.QueueStateInfo.TaskStateInfo; +import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Optional; @@ -32,9 +35,11 @@ import google.registry.model.registry.Registry; import google.registry.model.registry.Registry.TldType; import google.registry.testing.AppEngineRule; import google.registry.testing.ExceptionRule; +import google.registry.testing.FakeResponse; import google.registry.testing.TaskQueueHelper.TaskMatcher; import google.registry.util.Retrier; import google.registry.util.TaskEnqueuer; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -47,6 +52,7 @@ public class TldFanoutActionTest { private static final String ENDPOINT = "/the/servlet"; private static final String QUEUE = "the-queue"; + private final FakeResponse response = new FakeResponse(); @Rule public final ExceptionRule thrown = new ExceptionRule(); @@ -74,7 +80,7 @@ public class TldFanoutActionTest { return params.build(); } - private static void run(ImmutableListMultimap params) throws Exception { + private void run(ImmutableListMultimap params) throws Exception { TldFanoutAction action = new TldFanoutAction(); action.params = params; action.endpoint = getLast(params.get("endpoint")); @@ -83,6 +89,7 @@ public class TldFanoutActionTest { ? ImmutableSet.copyOf(Splitter.on(',').split(params.get("exclude").get(0))) : ImmutableSet.of(); action.taskEnqueuer = new TaskEnqueuer(new Retrier(null, 1)); + action.response = response; action.runInEmpty = params.containsKey("runInEmpty"); action.forEachRealTld = params.containsKey("forEachRealTld"); action.forEachTestTld = params.containsKey("forEachTestTld"); @@ -208,4 +215,23 @@ public class TldFanoutActionTest { assertTasksEnqueued(QUEUE, new TaskMatcher().url("/the/servlet").param("newkey", "newval")); } + + @Test + public void testSuccess_returnHttpResponse() throws Exception { + run(getParamsMap("forEachRealTld", "", "endpoint", "/the/servlet/:tld")); + + List taskList = + LocalTaskQueueTestConfig.getLocalTaskQueue().getQueueStateInfo().get(QUEUE).getTaskInfo(); + + assertThat(taskList).hasSize(3); + String expectedResponse = String.format( + "OK: Launched the following 3 tasks in queue the-queue\n" + + "- Task: %s, tld: com, endpoint: /the/servlet/com\n" + + "- Task: %s, tld: net, endpoint: /the/servlet/net\n" + + "- Task: %s, tld: org, endpoint: /the/servlet/org\n", + taskList.get(0).getTaskName(), + taskList.get(1).getTaskName(), + taskList.get(2).getTaskName()); + assertThat(response.getPayload()).isEqualTo(expectedResponse); + } }