diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStep.java index 5181c79d885..8829a9a461b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStep.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.core.indexlifecycle; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.client.Client; import org.elasticsearch.index.Index; @@ -17,8 +18,9 @@ public class DeleteStep extends AsyncActionStep { @Override public void performAction(Index index, Listener listener) { - getClient().admin().indices().prepareDelete(index.getName()) - .execute(ActionListener.wrap(response -> listener.onResponse(true) , listener::onFailure)); + getClient().admin().indices() + .delete(new DeleteIndexRequest(index.getName()), + ActionListener.wrap(response -> listener.onResponse(true) , listener::onFailure)); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteActionTests.java index 5b62c3ba323..84bfe983a19 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteActionTests.java @@ -5,20 +5,9 @@ */ package org.elasticsearch.xpack.core.indexlifecycle; -import org.apache.lucene.util.SetOnce; -import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.Index; import org.elasticsearch.test.AbstractSerializingTestCase; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.IOException; @@ -38,102 +27,4 @@ public class DeleteActionTests extends AbstractSerializingTestCase protected Reader instanceReader() { return DeleteAction::new; } - -// public void testExecute() throws Exception { -// Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); -// -// Client client = Mockito.mock(Client.class); -// AdminClient adminClient = Mockito.mock(AdminClient.class); -// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); -// -// Mockito.when(client.admin()).thenReturn(adminClient); -// Mockito.when(adminClient.indices()).thenReturn(indicesClient); -// Mockito.doAnswer(new Answer() { -// -// @Override -// public Void answer(InvocationOnMock invocation) throws Throwable { -// DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; -// @SuppressWarnings("unchecked") -// ActionListener listener = (ActionListener) invocation.getArguments()[1]; -// assertNotNull(request); -// assertEquals(1, request.indices().length); -// assertEquals(index.getName(), request.indices()[0]); -// listener.onResponse(null); -// return null; -// } -// -// }).when(indicesClient).delete(Mockito.any(), Mockito.any()); -// -// SetOnce actionCompleted = new SetOnce<>(); -// DeleteAction action = new DeleteAction(); -// action.execute(index, client, null, new Listener() { -// -// @Override -// public void onSuccess(boolean completed) { -// actionCompleted.set(completed); -// } -// -// @Override -// public void onFailure(Exception e) { -// throw new AssertionError("Unexpected method call", e); -// } -// }); -// -// assertEquals(true, actionCompleted.get()); -// -// Mockito.verify(client, Mockito.only()).admin(); -// Mockito.verify(adminClient, Mockito.only()).indices(); -// Mockito.verify(indicesClient, Mockito.only()).delete(Mockito.any(), Mockito.any()); -// } -// -// public void testExecuteFailure() throws Exception { -// Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); -// Exception exception = new RuntimeException(); -// -// Client client = Mockito.mock(Client.class); -// AdminClient adminClient = Mockito.mock(AdminClient.class); -// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); -// -// Mockito.when(client.admin()).thenReturn(adminClient); -// Mockito.when(adminClient.indices()).thenReturn(indicesClient); -// Mockito.doAnswer(new Answer() { -// -// @Override -// public Void answer(InvocationOnMock invocation) throws Throwable { -// DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; -// @SuppressWarnings("unchecked") -// ActionListener listener = (ActionListener) invocation.getArguments()[1]; -// assertNotNull(request); -// assertEquals(1, request.indices().length); -// assertEquals(index.getName(), request.indices()[0]); -// listener.onFailure(exception); -// ; -// return null; -// } -// -// }).when(indicesClient).delete(Mockito.any(), Mockito.any()); -// -// SetOnce exceptionThrown = new SetOnce<>(); -// DeleteAction action = new DeleteAction(); -// action.execute(index, client, null, new Listener() { -// -// @Override -// public void onSuccess(boolean completed) { -// throw new AssertionError("Unexpected method call"); -// } -// -// @Override -// public void onFailure(Exception e) { -// assertEquals(exception, e); -// exceptionThrown.set(true); -// } -// }); -// -// assertEquals(true, exceptionThrown.get()); -// -// Mockito.verify(client, Mockito.only()).admin(); -// Mockito.verify(adminClient, Mockito.only()).indices(); -// Mockito.verify(indicesClient, Mockito.only()).delete(Mockito.any(), Mockito.any()); -// } - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStepTests.java index 54f70f8b688..ca073501a88 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteStepTests.java @@ -6,10 +6,112 @@ package org.elasticsearch.xpack.core.indexlifecycle; +import org.apache.lucene.util.SetOnce; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.client.AdminClient; +import org.elasticsearch.client.Client; +import org.elasticsearch.client.IndicesAdminClient; +import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import static org.hamcrest.Matchers.equalTo; public class DeleteStepTests extends ESTestCase { - public void test() { + public void testIndexSurvives() { + assertFalse(new DeleteStep(null, null, null).indexSurvives()); + } + + public void testDeleted() { + Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(invocation -> { + DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(index.getName(), request.indices()[0]); + listener.onResponse(null); + return null; + }).when(indicesClient).delete(Mockito.any(), Mockito.any()); + + SetOnce actionCompleted = new SetOnce<>(); + + DeleteStep step = new DeleteStep(null, null, client); + step.performAction(index, new AsyncActionStep.Listener() { + @Override + public void onResponse(boolean complete) { + actionCompleted.set(complete); + } + + @Override + public void onFailure(Exception e) { + throw new AssertionError(e); + } + }); + + assertThat(actionCompleted.get(), equalTo(true)); + + Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(adminClient, Mockito.only()).indices(); + Mockito.verify(indicesClient, Mockito.only()).delete(Mockito.any(), Mockito.any()); + } + + public void testExceptionThrown() { + + Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); + Exception exception = new RuntimeException(); + + Client client = Mockito.mock(Client.class); + AdminClient adminClient = Mockito.mock(AdminClient.class); + IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + Mockito.doAnswer(new Answer() { + + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(index.getName(), request.indices()[0]); + listener.onFailure(exception); + ; + return null; + } + + }).when(indicesClient).delete(Mockito.any(), Mockito.any()); + + SetOnce exceptionThrown = new SetOnce<>(); + DeleteStep step = new DeleteStep(null, null, client); + step.performAction(index, new AsyncActionStep.Listener() { + @Override + public void onResponse(boolean complete) { + throw new AssertionError("Unexpected method call"); + } + + @Override + public void onFailure(Exception e) { + assertEquals(exception, e); + exceptionThrown.set(true); + } + }); + + assertThat(exceptionThrown.get(), equalTo(true)); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ReadOnlyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ReadOnlyStepTests.java index 40a73983ed7..8efb0813301 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ReadOnlyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ReadOnlyStepTests.java @@ -6,10 +6,35 @@ package org.elasticsearch.xpack.core.indexlifecycle; +import org.elasticsearch.Version; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; +import static org.hamcrest.Matchers.equalTo; + public class ReadOnlyStepTests extends ESTestCase { - public void test() { + public void testPerformAction() { + Settings.Builder indexSettingsBuilder = settings(Version.CURRENT); + if (randomBoolean()) { + indexSettingsBuilder.put(IndexMetaData.SETTING_BLOCKS_WRITE, randomBoolean()); + } + IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(1).numberOfReplicas(0).build(); + MetaData metaData = MetaData.builder() + .persistentSettings(settings(Version.CURRENT).build()) + .put(IndexMetaData.builder(indexMetadata)) + .build(); + Index index = indexMetadata.getIndex(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); + ReadOnlyStep step = new ReadOnlyStep(null, null); + ClusterState newState = step.performAction(index, clusterState); + assertThat(newState.metaData().index(index).getSettings().get(IndexMetaData.SETTING_BLOCKS_WRITE), equalTo("true")); } }