Repository: searchbox-io/Jest
Pull request #472 (2017-03-20 10:07:09)

Add Cat Segments 


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 0b9593aa..5f34a79b 100644
--- a/jest-common/src/main/java/io/searchbox/core/Cat.java
+++ b/jest-common/src/main/java/io/searchbox/core/Cat.java
@@ -133,6 +133,29 @@ public class Cat extends AbstractAction<CatResult> {
         }
     }
 
+    public static class SegmentsBuilder extends AbstractMultiIndexActionBuilder<Cat, SegmentsBuilder> implements CatBuilder {
+        private static final String operationPath = "segments";
+        public SegmentsBuilder() {
+            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() {
+            return indexNames.size() > 0 ? StringUtils.join(indexNames, ",") : null;
+        }
+    }
+
     public static class NodesBuilder extends AbstractAction.Builder<Cat, NodesBuilder> implements CatBuilder {
         private static final String operationPath = "nodes";
         public NodesBuilder() {


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/core/CatSegmentsBuilderTest.java b/jest-common/src/test/java/io/searchbox/core/CatSegmentsBuilderTest.java
new file mode 100644
index 00000000..cf5fb4f1
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/core/CatSegmentsBuilderTest.java
@@ -0,0 +1,49 @@
+package io.searchbox.core;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Created by Eran Shahar on 16/03/2017.
+ */
+public class CatSegmentsBuilderTest {
+
+    @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.SegmentsBuilder().build();
+        assertEquals("_cat/segments", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenSingleIndexGiven() {
+        Cat cat = new Cat.SegmentsBuilder().addIndex("testIndex").build();
+        assertEquals("_cat/segments/testIndex", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenIndicesGiven() {
+        Cat cat = new Cat.SegmentsBuilder().addIndex("testIndex1").addIndex("testIndex2").build();
+        assertEquals("_cat/segments/testIndex1%2CtestIndex2", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenParameterGiven() {
+        Cat cat = new Cat.SegmentsBuilder().setParameter("v", "true").build();
+        assertEquals("_cat/segments?v=true", cat.getURI());
+    }
+
+    @Test
+    public void shouldGenerateValidUriWhenHeadersParameterGiven() {
+        Cat cat = new Cat.SegmentsBuilder().setParameter("h", "index,shard,prirep,segment,docs.count").build();
+        assertEquals("_cat/segments?h=index%2Cshard%2Cprirep%2Csegment%2Cdocs.count", cat.getURI());
+    }
+
+}