Repository: searchbox-io/Jest
Pull request #523 (2017-07-03 13:17:03)

Add rollover 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
deleted file mode 100644
index 0cfab1fe..00000000
--- a/jest-common/src/main/java/io/searchbox/cluster/TasksInformation.java
+++ /dev/null
@@ -1,47 +0,0 @@
-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);
-        }
-    }
-
-}
diff --git a/jest-common/src/main/java/io/searchbox/indices/Rollover.java b/jest-common/src/main/java/io/searchbox/indices/Rollover.java
new file mode 100644
index 00000000..cc9d3df5
--- /dev/null
+++ b/jest-common/src/main/java/io/searchbox/indices/Rollover.java
@@ -0,0 +1,69 @@
+package io.searchbox.indices;
+
+import io.searchbox.action.AbstractAction;
+import io.searchbox.action.GenericResultAbstractAction;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Rollover extends GenericResultAbstractAction {
+
+    protected Rollover(Rollover.Builder builder) {
+        super(builder);
+
+        this.indexName = builder.index;
+        Map<String, Object> rolloverConditions = new HashMap<>();
+        if (builder.conditions != null) {
+            rolloverConditions.put("conditions", builder.conditions);
+        }
+        if (builder.settings != null) {
+            rolloverConditions.put("settings", builder.settings);
+        }
+        this.payload = rolloverConditions;
+
+        setURI(buildURI() + (builder.isDryRun ? "?dry_run" : ""));
+    }
+
+    @Override
+    public String getRestMethodName() {
+        return "POST";
+    }
+
+    @Override
+    protected String buildURI() {
+        return super.buildURI() + "/_rollover";
+    }
+
+
+    public static class Builder extends AbstractAction.Builder<Rollover, Rollover.Builder> {
+
+        private String index;
+        private Object conditions;
+        private Object settings;
+        private boolean isDryRun;
+
+        public Builder(String index) {
+            this.index = index;
+        }
+
+        public Rollover.Builder conditions(Object conditions) {
+            this.conditions = conditions;
+            return this;
+        }
+
+        public Rollover.Builder setDryRun(boolean dryRun) {
+            this.isDryRun = dryRun;
+            return this;
+        }
+
+        public Rollover.Builder settings(Object settings) {
+            this.settings = settings;
+            return this;
+        }
+
+        @Override
+        public Rollover build() {
+            return new Rollover(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
deleted file mode 100644
index 238cc263..00000000
--- a/jest-common/src/test/java/io/searchbox/cluster/TasksInformationTest.java
+++ /dev/null
@@ -1,21 +0,0 @@
-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-common/src/test/java/io/searchbox/indices/RolloverTest.java b/jest-common/src/test/java/io/searchbox/indices/RolloverTest.java
new file mode 100644
index 00000000..c1f4be73
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/indices/RolloverTest.java
@@ -0,0 +1,61 @@
+package io.searchbox.indices;
+
+import com.google.gson.Gson;
+import org.elasticsearch.common.collect.MapBuilder;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+public class RolloverTest {
+
+    Map<String, Object> rolloverConditions = new MapBuilder<String, Object>()
+                    .put("max_docs", "10000")
+                    .put("max_age", "1d")
+                    .immutableMap();
+    Map<String, Object> rolloverSettings = new MapBuilder<String, Object>()
+            .put("index.number_of_shards", "2")
+            .immutableMap();
+
+
+    @Test
+    public void testBasicUriGeneration() {
+        Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
+        assertEquals("POST", rollover.getRestMethodName());
+        assertEquals("twitter/_rollover", rollover.getURI());
+        assertEquals("{\"conditions\":{\"max_age\":\"1d\",\"max_docs\":\"10000\"}}", rollover.getData(new Gson()));
+    }
+
+    @Test
+    public void testBasicUriWithSettingsGeneration() {
+        Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).settings(rolloverSettings).build();
+        assertEquals("POST", rollover.getRestMethodName());
+        assertEquals("twitter/_rollover", rollover.getURI());
+        assertEquals("{\"settings\":{\"index.number_of_shards\":\"2\"},\"conditions\":{\"max_age\":\"1d\",\"max_docs\":\"10000\"}}", rollover.getData(new Gson()));
+    }
+
+    @Test
+    public void testDryRunUriGeneration() {
+        Rollover rollover = new Rollover.Builder("twitter").conditions(rolloverConditions).setDryRun(true).build();
+        assertEquals("POST", rollover.getRestMethodName());
+        assertEquals("twitter/_rollover?dry_run", rollover.getURI());
+    }
+
+    @Test
+    public void equalsReturnsTrueForSameDestination() {
+        Rollover indexRollover1 = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
+        Rollover indexRollover2 = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
+
+        assertEquals(indexRollover1, indexRollover2);
+    }
+
+    @Test
+    public void equalsReturnsFalseForDifferentIndex() {
+        Rollover indexRollover1 = new Rollover.Builder("twitter").conditions(rolloverConditions).build();
+        Rollover indexRollover2 = new Rollover.Builder("myspace").conditions(rolloverConditions).build();
+
+        assertNotEquals(indexRollover1, indexRollover2);
+    }
+}
diff --git a/jest/src/test/java/io/searchbox/cluster/TaskInformationIntegrationTest.java b/jest/src/test/java/io/searchbox/cluster/TaskInformationIntegrationTest.java
deleted file mode 100644
index 07b566f4..00000000
--- a/jest/src/test/java/io/searchbox/cluster/TaskInformationIntegrationTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-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);
-    }
-}
diff --git a/jest/src/test/java/io/searchbox/indices/RolloverIntegrationTest.java b/jest/src/test/java/io/searchbox/indices/RolloverIntegrationTest.java
new file mode 100644
index 00000000..fa2d336e
--- /dev/null
+++ b/jest/src/test/java/io/searchbox/indices/RolloverIntegrationTest.java
@@ -0,0 +1,33 @@
+package io.searchbox.indices;
+
+import io.searchbox.client.JestResult;
+import io.searchbox.common.AbstractIntegrationTest;
+import org.elasticsearch.common.collect.MapBuilder;
+import org.elasticsearch.test.ESIntegTestCase;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Map;
+
+@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
+public class RolloverIntegrationTest extends AbstractIntegrationTest {
+
+    private final Map<String, Object> rolloverConditions = new MapBuilder<String, Object>()
+            .put("max_docs", "1")
+            .put("max_age", "1d")
+            .immutableMap();
+
+    @Test
+    public void testRollover() throws IOException {
+        String aliasSetting = "{ \"aliases\": { \"rollover-test-index\": {} } }";
+        CreateIndex createIndex = new CreateIndex.Builder("rollover-test-index-000001").settings(aliasSetting).build();
+
+        JestResult result = client.execute(createIndex);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+
+        Rollover rollover = new Rollover.Builder("rollover-test-index").conditions(rolloverConditions).build();
+
+        result = client.execute(rollover);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+    }
+}