Repository: searchbox-io/Jest
Pull request #527 (2017-07-20 13:13:28)

Add task information action


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/cluster/TasksInformation.java b/jest-common/src/main/java/io/searchbox/cluster/TasksInformation.java
new file mode 100644
index 00000000..0cfab1fe
--- /dev/null
+++ b/jest-common/src/main/java/io/searchbox/cluster/TasksInformation.java
@@ -0,0 +1,47 @@
+package io.searchbox.cluster;
+
+import io.searchbox.action.GenericResultAbstractAction;
+
+/**
+ * Currently only supports retrieving information for a particular task
+ */
+public class TasksInformation extends GenericResultAbstractAction {
+
+    protected String task;
+
+    protected TasksInformation(Builder builder) {
+        super(builder);
+        task = builder.task;
+        setURI(buildURI());
+    }
+
+    @Override
+    protected String buildURI() {
+        String uri = super.buildURI() + "_tasks";
+        if (task != null) {
+            uri += "/" + task;
+        }
+        return uri;
+    }
+
+    @Override
+    public String getRestMethodName() {
+        return "GET";
+    }
+
+    public static class Builder extends GenericResultAbstractAction.Builder<TasksInformation, Builder> {
+
+        protected String task;
+
+        public Builder task(String task) {
+            this.task = task;
+            return this;
+        }
+
+        @Override
+        public TasksInformation build() {
+            return new TasksInformation(this);
+        }
+    }
+
+}


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/cluster/TasksInformationTest.java b/jest-common/src/test/java/io/searchbox/cluster/TasksInformationTest.java
new file mode 100644
index 00000000..238cc263
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/cluster/TasksInformationTest.java
@@ -0,0 +1,21 @@
+package io.searchbox.cluster;
+
+import io.searchbox.action.Action;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class TasksInformationTest {
+
+    @Test
+    public void testUriGeneration() {
+        Action action = new TasksInformation.Builder().build();
+        assertEquals("_tasks", action.getURI());
+    }
+
+    @Test
+    public void testUriGenerationSpecificTask() {
+        Action action = new TasksInformation.Builder().task("node_id:task_id").build();
+        assertEquals("_tasks/node_id:task_id", action.getURI());
+    }
+}
\ No newline at end of file
diff --git a/jest/src/test/java/io/searchbox/cluster/TaskInformationIntegrationTest.java b/jest/src/test/java/io/searchbox/cluster/TaskInformationIntegrationTest.java
new file mode 100644
index 00000000..07b566f4
--- /dev/null
+++ b/jest/src/test/java/io/searchbox/cluster/TaskInformationIntegrationTest.java
@@ -0,0 +1,49 @@
+package io.searchbox.cluster;
+
+import com.google.common.collect.ImmutableMap;
+import io.searchbox.client.JestResult;
+import io.searchbox.common.AbstractIntegrationTest;
+import io.searchbox.indices.reindex.Reindex;
+import org.elasticsearch.index.reindex.ReindexPlugin;
+import org.elasticsearch.plugins.Plugin;
+import org.elasticsearch.test.ESIntegTestCase;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 1)
+public class TaskInformationIntegrationTest extends AbstractIntegrationTest {
+
+    @Override
+    protected Collection<Class<? extends Plugin>> nodePlugins() {
+        final ArrayList<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
+        plugins.add(ReindexPlugin.class);
+        return plugins;
+    }
+
+    @Test
+    public void shouldReturnTaskInformation() throws IOException {
+
+        String sourceIndex = "test_source_index";
+        String destIndex = "test_dest_index";
+        String documentType = "test_type";
+        String documentId = "test_id";
+
+        createIndex(sourceIndex);
+        index(sourceIndex, documentType, documentId, "{}");
+        flushAndRefresh(sourceIndex);
+
+        ImmutableMap<String, Object> source = ImmutableMap.of("index", sourceIndex);
+        ImmutableMap<String, Object> dest = ImmutableMap.of("index", destIndex);
+        Reindex reindex = new Reindex.Builder(source, dest).refresh(true).waitForCompletion(false).build();
+        JestResult result = client.execute(reindex);
+
+        String task = (String) result.getValue("task");
+        assertNotNull(task);
+        JestResult taskInformation = client.execute(new TasksInformation.Builder().task(task).build());
+        Boolean completed = (Boolean) taskInformation.getValue("completed");
+        assertNotNull(completed);
+    }
+}