Repository: searchbox-io/Jest
Pull request #496 (2017-04-19 13:47:28)

Add support for cluster stats API
https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-stats.html


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/cluster/Stats.java b/jest-common/src/main/java/io/searchbox/cluster/Stats.java
new file mode 100644
index 00000000..84ab0153
--- /dev/null
+++ b/jest-common/src/main/java/io/searchbox/cluster/Stats.java
@@ -0,0 +1,28 @@
+package io.searchbox.cluster;
+
+import io.searchbox.action.AbstractMultiINodeActionBuilder;
+import io.searchbox.action.GenericResultAbstractAction;
+
+public class Stats extends GenericResultAbstractAction {
+    protected Stats(Builder builder) {
+        super(builder);
+        setURI(buildURI());
+    }
+
+    @Override
+    protected String buildURI() {
+        return super.buildURI() + "/_cluster/stats/nodes/" + nodes;
+    }
+
+    @Override
+    public String getRestMethodName() {
+        return "GET";
+    }
+
+    public static class Builder extends AbstractMultiINodeActionBuilder<Stats, Builder> {
+        @Override
+        public Stats build() {
+            return new Stats(this);
+        }
+    }
+}


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/cluster/StatsTest.java b/jest-common/src/test/java/io/searchbox/cluster/StatsTest.java
new file mode 100644
index 00000000..de90a7a5
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/cluster/StatsTest.java
@@ -0,0 +1,23 @@
+package io.searchbox.cluster;
+
+import io.searchbox.action.Action;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class StatsTest {
+    @Test
+    public void testUriGeneration() {
+        Action action = new Stats.Builder().build();
+        assertEquals("/_cluster/stats/nodes/_all", action.getURI());
+    }
+
+    @Test
+    public void testUriGenerationWithSpecificNodes() {
+        Action action = new Stats.Builder()
+                .addNode("test1")
+                .addNode("test2")
+                .build();
+        assertEquals("/_cluster/stats/nodes/test1,test2", action.getURI());
+    }
+}
diff --git a/jest/src/test/java/io/searchbox/cluster/StatsIntegrationTest.java b/jest/src/test/java/io/searchbox/cluster/StatsIntegrationTest.java
new file mode 100644
index 00000000..9121dd15
--- /dev/null
+++ b/jest/src/test/java/io/searchbox/cluster/StatsIntegrationTest.java
@@ -0,0 +1,43 @@
+package io.searchbox.cluster;
+
+import com.google.gson.JsonObject;
+import io.searchbox.client.JestResult;
+import io.searchbox.common.AbstractIntegrationTest;
+import org.elasticsearch.test.ESIntegTestCase;
+import org.junit.Test;
+
+import java.io.IOException;
+
+@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
+public class StatsIntegrationTest extends AbstractIntegrationTest {
+    @Test
+    public void clusterStats() throws IOException {
+        JestResult result = client.execute(new Stats.Builder().build());
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+
+        JsonObject resultJson = result.getJsonObject();
+        assertNotNull(resultJson);
+        assertNotNull(resultJson.getAsJsonPrimitive("timestamp"));
+        assertNotNull(resultJson.getAsJsonPrimitive("cluster_name"));
+        assertNotNull(resultJson.getAsJsonPrimitive("status"));
+        assertNotNull(resultJson.getAsJsonObject("indices"));
+        assertNotNull(resultJson.getAsJsonObject("nodes"));
+        assertEquals(internalCluster().size(), resultJson.getAsJsonObject("nodes").getAsJsonObject("count").get("total").getAsInt());
+
+    }
+
+    @Test
+    public void clusterStatsWithSpecificNodes() throws IOException {
+        final String localNodeName = clusterService().localNode().name();
+        JestResult result = client.execute(new Stats.Builder().addNode(localNodeName).build());
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+
+        JsonObject resultJson = result.getJsonObject();
+        assertNotNull(resultJson);
+        assertNotNull(resultJson.getAsJsonPrimitive("timestamp"));
+        assertNotNull(resultJson.getAsJsonPrimitive("cluster_name"));
+        assertNotNull(resultJson.getAsJsonObject("indices"));
+        assertNotNull(resultJson.getAsJsonObject("nodes"));
+        assertEquals(1, resultJson.getAsJsonObject("nodes").getAsJsonObject("count").get("total").getAsInt());
+    }
+}