Repository: searchbox-io/Jest
Pull request #500 (2017-04-20 16:46:59)

Add support for checking for existence of aliases
https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-aliases.html#alias-retrieving


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/indices/aliases/AliasExists.java b/jest-common/src/main/java/io/searchbox/indices/aliases/AliasExists.java
new file mode 100644
index 00000000..36b8c423
--- /dev/null
+++ b/jest-common/src/main/java/io/searchbox/indices/aliases/AliasExists.java
@@ -0,0 +1,38 @@
+package io.searchbox.indices.aliases;
+
+import io.searchbox.action.AbstractMultiIndexActionBuilder;
+import io.searchbox.action.GenericResultAbstractAction;
+
+public class AliasExists extends GenericResultAbstractAction {
+    private String alias;
+
+    protected AliasExists(Builder builder, String alias) {
+        super(builder);
+        this.alias = alias;
+        setURI(buildURI());
+    }
+
+    @Override
+    public String getRestMethodName() {
+        return "HEAD";
+    }
+
+    @Override
+    protected String buildURI() {
+        return super.buildURI() + "/_alias/" + alias;
+    }
+
+    public static class Builder extends AbstractMultiIndexActionBuilder<AliasExists, Builder> {
+        protected String alias = "*";
+
+        public Builder alias(String alias) {
+            this.alias = alias;
+            return this;
+        }
+
+        @Override
+        public AliasExists build() {
+            return new AliasExists(this, alias);
+        }
+    }
+}


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/indices/aliases/AliasExistsTest.java b/jest-common/src/test/java/io/searchbox/indices/aliases/AliasExistsTest.java
new file mode 100644
index 00000000..4ac76aa5
--- /dev/null
+++ b/jest-common/src/test/java/io/searchbox/indices/aliases/AliasExistsTest.java
@@ -0,0 +1,39 @@
+package io.searchbox.indices.aliases;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class AliasExistsTest {
+    @Test
+    public void testBasicUriGeneration() {
+        AliasExists aliasExists = new AliasExists.Builder().build();
+
+        assertEquals("HEAD", aliasExists.getRestMethodName());
+        assertEquals("_all/_alias/*", aliasExists.getURI());
+    }
+
+    @Test
+    public void testBasicUriGenerationWithIndex() {
+        AliasExists aliasExists = new AliasExists.Builder().addIndex("indexName").build();
+
+        assertEquals("HEAD", aliasExists.getRestMethodName());
+        assertEquals("indexName/_alias/*", aliasExists.getURI());
+    }
+
+    @Test
+    public void testBasicUriGenerationWithAlias() {
+        AliasExists aliasExists = new AliasExists.Builder().alias("aliasName").build();
+
+        assertEquals("HEAD", aliasExists.getRestMethodName());
+        assertEquals("_all/_alias/aliasName", aliasExists.getURI());
+    }
+
+    @Test
+    public void testBasicUriGenerationWithAliasAndIndex() {
+        AliasExists aliasExists = new AliasExists.Builder().addIndex("indexName").alias("aliasName").build();
+
+        assertEquals("HEAD", aliasExists.getRestMethodName());
+        assertEquals("indexName/_alias/aliasName", aliasExists.getURI());
+    }
+}
\ No newline at end of file
diff --git a/jest/src/test/java/io/searchbox/indices/aliases/AliasExistsIntegrationTest.java b/jest/src/test/java/io/searchbox/indices/aliases/AliasExistsIntegrationTest.java
new file mode 100644
index 00000000..0a313bfb
--- /dev/null
+++ b/jest/src/test/java/io/searchbox/indices/aliases/AliasExistsIntegrationTest.java
@@ -0,0 +1,111 @@
+package io.searchbox.indices.aliases;
+
+import io.searchbox.client.JestResult;
+import io.searchbox.common.AbstractIntegrationTest;
+import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
+import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
+import org.elasticsearch.test.ESIntegTestCase;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
+
+@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1)
+public class AliasExistsIntegrationTest extends AbstractIntegrationTest {
+    private static final String INDEX_NAME_1 = "aliases_test_index1";
+    private static final String INDEX_NAME_2 = "aliases_test_index2";
+
+    @Before
+    public void setup() {
+        createIndex(INDEX_NAME_1, INDEX_NAME_2);
+    }
+
+    @Test
+    public void testAliasesExists() throws IOException {
+        String alias = "myAlias000";
+
+        createAlias(INDEX_NAME_1, alias);
+
+        AliasExists aliasExists = new AliasExists.Builder().build();
+        JestResult result = client.execute(aliasExists);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testAliasDoesNotExist() throws IOException {
+        AliasExists aliasExists = new AliasExists.Builder().build();
+        JestResult result = client.execute(aliasExists);
+        assertFalse(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testIndexDoesNotExist() throws IOException {
+        AliasExists aliasExists = new AliasExists.Builder().addIndex("does_not_exist").build();
+        JestResult result = client.execute(aliasExists);
+        assertFalse(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testAliasAndIndexDoesNotExist() throws IOException {
+        AliasExists aliasExists = new AliasExists.Builder().addIndex("does_not_exist").alias("abc").build();
+        JestResult result = client.execute(aliasExists);
+        assertFalse(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testAliasesExistsForSpecificIndex() throws IOException {
+        String alias = "myAlias000";
+
+        createAlias(INDEX_NAME_1, alias);
+
+        AliasExists aliasExists = new AliasExists.Builder().addIndex(INDEX_NAME_1).build();
+        JestResult result = client.execute(aliasExists);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testAliasesExistsForMultipleIndices() throws IOException {
+        String alias = "myAlias000";
+
+        createAlias(INDEX_NAME_1, alias);
+
+        AliasExists aliasExists = new AliasExists.Builder().addIndex(INDEX_NAME_1).addIndex(INDEX_NAME_2).build();
+        JestResult result = client.execute(aliasExists);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testAliasesExistsForSpecificAlias() throws IOException {
+        String alias = "myAlias000";
+
+        createAlias(INDEX_NAME_1, alias);
+
+        AliasExists aliasExists = new AliasExists.Builder().alias(alias).build();
+        JestResult result = client.execute(aliasExists);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    @Test
+    public void testAliasesExistsForSpecificAliasAndIndex() throws IOException {
+        String alias = "myAlias000";
+
+        createAlias(INDEX_NAME_1, alias);
+
+        AliasExists aliasExists = new AliasExists.Builder().addIndex(INDEX_NAME_1).alias(alias).build();
+        JestResult result = client.execute(aliasExists);
+        assertTrue(result.getErrorMessage(), result.isSucceeded());
+    }
+
+    private void createAlias(String index, String alias) {
+        final IndicesAliasesRequest.AliasActions aliasAction = IndicesAliasesRequest.AliasActions.add()
+                .index(index)
+                .alias(alias);
+        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest().addAliasAction(aliasAction);
+        IndicesAliasesResponse indicesAliasesResponse =
+                client().admin().indices().aliases(indicesAliasesRequest).actionGet(10, TimeUnit.SECONDS);
+        assertAcked(indicesAliasesResponse);
+    }
+}