Repository: searchbox-io/Jest
Pull request #448 (2017-01-10 12:32:04)

cat-shards, cat-nodes endpoints
Add cat-shards (_cat/shards) and cat-nodes (_cat/nodes) endpoints


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/core/Cat.java b/jest-common/src/main/java/io/searchbox/core/Cat.java
index d34e67fc..0b9593aa 100644
--- a/jest-common/src/main/java/io/searchbox/core/Cat.java
+++ b/jest-common/src/main/java/io/searchbox/core/Cat.java
@@ -9,6 +9,7 @@ import com.google.gson.JsonSyntaxException;
 import io.searchbox.action.AbstractAction;
 import io.searchbox.action.AbstractMultiIndexActionBuilder;
 import io.searchbox.action.AbstractMultiTypeActionBuilder;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.builder.HashCodeBuilder;
 
 /**
@@ -105,6 +106,51 @@ public class Cat extends AbstractAction<CatResult> {
         }
     }
 
+    public static class ShardsBuilder extends AbstractMultiIndexActionBuilder<Cat, ShardsBuilder> implements CatBuilder {
+        private static final String operationPath = "shards";
+        public ShardsBuilder() {
+            setHeader("accept", "application/json");
+            setHeader("content-type", "application/json");
+        }
+
+        @Override
+        public Cat build() {
+            return new Cat(this);
+        }
+
+        @Override
+        public String operationPath() {
+            return operationPath;
+        }
+
+        @Override
+        public String getJoinedIndices() {
+            if (indexNames.size() > 0) {
+                return StringUtils.join(indexNames, ",");
+            } else {
+                return null;
+            }
+        }
+    }
+
+    public static class NodesBuilder extends AbstractAction.Builder<Cat, NodesBuilder> implements CatBuilder {
+        private static final String operationPath = "nodes";
+        public NodesBuilder() {
+            setHeader("accept", "application/json");
+            setHeader("content-type", "application/json");
+        }
+
+        @Override
+        public Cat build() {
+            return new Cat(this);
+        }
+
+        @Override
+        public String operationPath() {
+            return operationPath;
+        }
+    }
+
     protected interface CatBuilder {
         String operationPath();
     }


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/core/CatNodesBuilderTest.java b/jest-common/src/test/java/io/searchbox/core/CatNodesBuilderTest.java
new file mode 100644
index 00000000..d91b2ae4
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/core/CatNodesBuilderTest.java
@@ -0,0 +1,26 @@
+package io.searchbox.core;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class CatNodesBuilderTest {
+    @Test
+    public void shouldSetApplicationJsonHeader() {
+        Cat cat = new Cat.NodesBuilder().build();
+        assertEquals("application/json", cat.getHeader("accept"));
+        assertEquals("application/json", cat.getHeader("content-type"));
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenIndexNotGiven() {
+        Cat cat = new Cat.NodesBuilder().build();
+        assertEquals("_cat/nodes", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenParameterGiven() {
+        Cat cat = new Cat.NodesBuilder().setParameter("v", "true").build();
+        assertEquals("_cat/nodes?v=true", cat.getURI());
+    }
+}
diff --git a/jest-common/src/test/java/io/searchbox/core/CatShardsBuilderTest.java b/jest-common/src/test/java/io/searchbox/core/CatShardsBuilderTest.java
new file mode 100644
index 00000000..7b54f44c
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/core/CatShardsBuilderTest.java
@@ -0,0 +1,32 @@
+package io.searchbox.core;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class CatShardsBuilderTest {
+    @Test
+    public void shouldSetApplicationJsonHeader() {
+        Cat cat = new Cat.ShardsBuilder().build();
+        assertEquals("application/json", cat.getHeader("accept"));
+        assertEquals("application/json", cat.getHeader("content-type"));
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenIndexNotGiven() {
+        Cat cat = new Cat.ShardsBuilder().build();
+        assertEquals("_cat/shards", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenIndexGiven() {
+        Cat cat = new Cat.ShardsBuilder().addIndex("testIndex").build();
+        assertEquals("_cat/shards/testIndex", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenParameterGiven() {
+        Cat cat = new Cat.ShardsBuilder().setParameter("v", "true").build();
+        assertEquals("_cat/shards?v=true", cat.getURI());
+    }
+}
diff --git a/jest/src/test/java/io/searchbox/core/CatIntegrationTest.java b/jest/src/test/java/io/searchbox/core/CatIntegrationTest.java
index a7202948..f66379d1 100644
--- a/jest/src/test/java/io/searchbox/core/CatIntegrationTest.java
+++ b/jest/src/test/java/io/searchbox/core/CatIntegrationTest.java
@@ -1,7 +1,11 @@
 package io.searchbox.core;
 
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 
+import com.google.gson.JsonElement;
 import org.elasticsearch.action.admin.indices.alias.IndicesAliasesAction;
 import org.elasticsearch.test.ESIntegTestCase;
 import org.json.JSONException;
@@ -11,6 +15,8 @@ import org.skyscreamer.jsonassert.JSONAssert;
 import com.google.gson.JsonArray;
 
 import io.searchbox.common.AbstractIntegrationTest;
+import org.skyscreamer.jsonassert.JSONCompare;
+import org.skyscreamer.jsonassert.JSONCompareMode;
 
 /**
  * @author Bartosz Polnik
@@ -81,4 +87,59 @@ public class CatIntegrationTest extends AbstractIntegrationTest {
         }, result.getPlainText());
         JSONAssert.assertEquals("[{\"index\":\"catintegrationindex\",\"alias\":\"catintegrationalias\"}]", result.getSourceAsString(), false);
     }
+
+    @Test
+    public void catAllShards() throws IOException, JSONException {
+        createIndex(INDEX);
+        createIndex(INDEX2);
+        ensureSearchable(INDEX);
+        ensureSearchable(INDEX2);
+
+        CatResult catResult = client.execute(new Cat.ShardsBuilder().setParameter("h", "index,docs").build());
+        JsonArray shards = catResult.getJsonObject().get("result").getAsJsonArray();
+
+        assertEquals(shards.size(), getNumShards(INDEX).totalNumShards + getNumShards(INDEX2).totalNumShards);
+
+        int index1Count = 0;
+        int index2Count = 0;
+        for (JsonElement shard : shards) {
+            index1Count += JSONCompare.compareJSON("{\"index\":\"" + INDEX + "\",\"docs\":\"0\"}", shard.toString(), JSONCompareMode.LENIENT).passed() ? 1 : 0;
+            index2Count += JSONCompare.compareJSON("{\"index\":\"" + INDEX2 + "\",\"docs\":\"0\"}", shard.toString(), JSONCompareMode.LENIENT).passed() ? 1 : 0;
+        }
+
+        assertTrue(index1Count > 0);
+        assertTrue(index2Count > 0);
+        assertEquals(index1Count + index2Count, shards.size());
+    }
+
+    @Test
+    public void catShardsSingleIndex() throws IOException, JSONException {
+        createIndex(INDEX);
+        createIndex(INDEX2);
+        ensureSearchable(INDEX);
+        ensureSearchable(INDEX2);
+
+        CatResult catResult = client.execute(new Cat.ShardsBuilder().addIndex(INDEX).setParameter("h", "index,docs").build());
+        JsonArray shards = catResult.getJsonObject().get("result").getAsJsonArray();
+
+        assertEquals(shards.size(), getNumShards(INDEX).totalNumShards);
+
+        for (JsonElement shard : shards) {
+            JSONAssert.assertEquals("{\"index\":\"catintegrationindex\",\"docs\":\"0\"}", shard.toString(), false);
+        }
+    }
+
+    @Test
+    public void catNodes() throws IOException {
+        CatResult catResult = client.execute(new Cat.NodesBuilder().setParameter("h", "name").build());
+        JsonArray nodes = catResult.getJsonObject().get("result").getAsJsonArray();
+
+        Set<String> expectedNodeNames = new HashSet<>(Arrays.asList(internalCluster().getNodeNames()));
+        Set<String> actualNodeNames = new HashSet<>();
+        for (JsonElement node : nodes) {
+            actualNodeNames.add(node.getAsJsonObject().get("name").getAsString());
+        }
+
+        assertEquals(actualNodeNames, expectedNodeNames);
+    }
 }