From fbec19c0228551ac3929375cb949a36c21d4b2b0 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Sat, 25 Jan 2020 01:19:55 +0100 Subject: [PATCH] Centralize mocks initialization in ILM steps tests (#51384) (#51453) * Centralize mocks initialization in ILM steps tests This change centralizes initialization of `Client`, `AdminClient` and `IndicesAdminClient` for all classes extending `AbstractStepTestCase`. This removes a lot of code duplication and make it easier to write tests. This also removes need for `AsyncActionStep#setClient` * Unused imports removed * Added missed tests * Fix OpenFollowerIndexStepTests --- .../xpack/core/ilm/AsyncActionStep.java | 5 - .../AbstractStepMasterTimeoutTestCase.java | 12 +- .../xpack/core/ilm/AbstractStepTestCase.java | 19 +++ .../AbstractUnfollowIndexStepTestCase.java | 12 +- .../core/ilm/CloseFollowerIndexStepTests.java | 18 +- .../xpack/core/ilm/DeleteStepTests.java | 46 +---- .../xpack/core/ilm/ForceMergeStepTests.java | 15 -- .../xpack/core/ilm/FreezeStepTests.java | 24 +-- .../core/ilm/OpenFollowerIndexStepTests.java | 36 ++-- .../core/ilm/PauseFollowerIndexStepTests.java | 11 +- .../xpack/core/ilm/RolloverStepTests.java | 65 ++------ .../xpack/core/ilm/SegmentCountStepTests.java | 26 --- .../ilm/SetSingleNodeAllocateStepTests.java | 70 ++------ .../core/ilm/ShrinkSetAliasStepTests.java | 20 --- .../xpack/core/ilm/ShrinkStepTests.java | 66 ++------ .../ilm/UnfollowFollowIndexStepTests.java | 23 +-- .../core/ilm/UpdateSettingsStepTests.java | 24 +-- .../ilm/WaitForFollowShardTasksStepTests.java | 21 +-- .../core/ilm/WaitForNoFollowersStepTests.java | 22 +-- .../ilm/WaitForRolloverReadyStepTests.java | 157 ++++++------------ 20 files changed, 168 insertions(+), 524 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java index 712caab5082..a3e06c6eafd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java @@ -29,11 +29,6 @@ public abstract class AsyncActionStep extends Step { return client; } - //visible for testing - void setClient(Client client){ - this.client = client; - } - public static TimeValue getMasterTimeout(ClusterState clusterState){ Objects.requireNonNull(clusterState, "cannot determine master timeout when cluster state is null"); return LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING.get(clusterState.metaData().settings()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java index f93d07d580b..32012156d21 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java @@ -22,6 +22,8 @@ import org.elasticsearch.threadpool.ThreadPool; import org.junit.After; import org.junit.Before; +import java.util.concurrent.atomic.AtomicBoolean; + import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT; import static org.hamcrest.Matchers.equalTo; @@ -51,18 +53,19 @@ public abstract class AbstractStepMasterTimeoutTestCase void doExecute(ActionType action, Request request, ActionListener listener) { if (request instanceof MasterNodeRequest) { assertThat(((MasterNodeRequest) request).masterNodeTimeout(), equalTo(timeValue)); + timeoutChecked.set(true); } } - }); - instance.performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() { + }; + createRandomInstance().performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { @@ -73,6 +76,7 @@ public abstract class AbstractStepMasterTimeoutTestCase extends ESTestCase { + protected Client client; + protected AdminClient adminClient; + protected IndicesAdminClient indicesClient; + + @Before + public void setupClient() { + client = Mockito.mock(Client.class); + adminClient = Mockito.mock(AdminClient.class); + indicesClient = Mockito.mock(IndicesAdminClient.class); + + Mockito.when(client.admin()).thenReturn(adminClient); + Mockito.when(adminClient.indices()).thenReturn(indicesClient); + } + protected static final int NUMBER_OF_TEST_RUNS = 20; protected static final TimeValue MASTER_TIMEOUT = TimeValue.timeValueSeconds(30); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStepTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStepTestCase.java index d79b7c563f1..cb2a333cf68 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStepTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStepTestCase.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.Version; -import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.mockito.Mockito; @@ -19,7 +18,7 @@ public abstract class AbstractUnfollowIndexStepTestCase { CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0]; assertThat(closeIndexRequest.indices()[0], equalTo("follower-index")); @@ -75,12 +66,6 @@ public class CloseFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCa IndexMetaData indexMetadata = getIndexMetaData(); // Mock pause follow api call: - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Exception error = new RuntimeException(); Mockito.doAnswer(invocation -> { CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0]; @@ -118,7 +103,6 @@ public class CloseFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCa .numberOfShards(1) .numberOfReplicas(0) .build(); - Client client = Mockito.mock(Client.class); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { @Override @@ -138,7 +122,7 @@ public class CloseFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCa protected CloseFollowerIndexStep createRandomInstance() { Step.StepKey stepKey = randomStepKey(); Step.StepKey nextStepKey = randomStepKey(); - return new CloseFollowerIndexStep(stepKey, nextStepKey, Mockito.mock(Client.class)); + return new CloseFollowerIndexStep(stepKey, nextStepKey, client); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java index 379c42469d7..8ebae30860f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java @@ -11,27 +11,14 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.xpack.core.ilm.Step.StepKey; -import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import static org.hamcrest.Matchers.equalTo; public class DeleteStepTests extends AbstractStepMasterTimeoutTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override public DeleteStep createRandomInstance() { StepKey stepKey = randomStepKey(); @@ -77,11 +64,6 @@ public class DeleteStepTests extends AbstractStepMasterTimeoutTestCase { DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") @@ -119,25 +101,15 @@ public class DeleteStepTests extends AbstractStepMasterTimeoutTestCase() { - - @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(indexMetaData.getIndex().getName(), request.indices()[0]); - listener.onFailure(exception); - return null; - } - + 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(indexMetaData.getIndex().getName(), request.indices()[0]); + listener.onFailure(exception); + return null; }).when(indicesClient).delete(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java index 634ce77394b..22bf7b40c0b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java @@ -11,9 +11,6 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -21,8 +18,6 @@ import org.mockito.Mockito; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class ForceMergeStepTests extends AbstractStepTestCase { @@ -70,11 +65,6 @@ public class ForceMergeStepTests extends AbstractStepTestCase { Step.StepKey stepKey = randomStepKey(); StepKey nextStepKey = randomStepKey(); int maxNumSegments = randomIntBetween(1, 10); - Client client = mock(Client.class); - AdminClient adminClient = mock(AdminClient.class); - IndicesAdminClient indicesClient = mock(IndicesAdminClient.class); - when(client.admin()).thenReturn(adminClient); - when(adminClient.indices()).thenReturn(indicesClient); ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class); Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK); Mockito.doAnswer(invocationOnMock -> { @@ -109,11 +99,6 @@ public class ForceMergeStepTests extends AbstractStepTestCase { Step.StepKey stepKey = randomStepKey(); StepKey nextStepKey = randomStepKey(); int maxNumSegments = randomIntBetween(1, 10); - Client client = mock(Client.class); - AdminClient adminClient = mock(AdminClient.class); - IndicesAdminClient indicesClient = mock(IndicesAdminClient.class); - when(client.admin()).thenReturn(adminClient); - when(adminClient.indices()).thenReturn(indicesClient); ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class); Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK); Mockito.doAnswer(invocationOnMock -> { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java index c9755edbe3c..8cd8a2ef0b3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java @@ -10,28 +10,16 @@ import org.apache.lucene.util.SetOnce; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.protocol.xpack.frozen.FreezeRequest; import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction; import org.elasticsearch.xpack.core.ilm.Step.StepKey; -import org.junit.Before; import org.mockito.Mockito; -import org.mockito.stubbing.Answer; import static org.hamcrest.Matchers.equalTo; public class FreezeStepTests extends AbstractStepMasterTimeoutTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override public FreezeStep createRandomInstance() { StepKey stepKey = randomStepKey(); @@ -77,11 +65,6 @@ public class FreezeStepTests extends AbstractStepMasterTimeoutTestCase { assertSame(invocation.getArguments()[0], FreezeIndexAction.INSTANCE); FreezeRequest request = (FreezeRequest) invocation.getArguments()[1]; @@ -120,12 +103,7 @@ public class FreezeStepTests extends AbstractStepMasterTimeoutTestCase) invocation -> { + Mockito.doAnswer(invocation -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[2]; listener.onFailure(exception); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java index b2cf4d542e9..f9b5d213359 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java @@ -9,9 +9,6 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.mockito.Mockito; @@ -29,7 +26,7 @@ public class OpenFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCas protected OpenFollowerIndexStep createRandomInstance() { Step.StepKey stepKey = randomStepKey(); Step.StepKey nextStepKey = randomStepKey(); - return new OpenFollowerIndexStep(stepKey, nextStepKey, Mockito.mock(Client.class)); + return new OpenFollowerIndexStep(stepKey, nextStepKey, client); } @Override @@ -56,15 +53,20 @@ public class OpenFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCas return IndexMetaData.builder("follower-index") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) - .state(IndexMetaData.State.OPEN) + .state(IndexMetaData.State.CLOSE) .numberOfShards(1) .numberOfReplicas(0) .build(); } public void testOpenFollowerIndexIsNoopForAlreadyOpenIndex() { - IndexMetaData indexMetadata = getIndexMetaData(); - Client client = Mockito.mock(Client.class); + IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) + .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) + .state(IndexMetaData.State.OPEN) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); OpenFollowerIndexStep step = new OpenFollowerIndexStep(randomStepKey(), randomStepKey(), client); step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { @Override @@ -81,19 +83,7 @@ public class OpenFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCas } public void testOpenFollowingIndex() { - IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") - .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) - .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) - .state(IndexMetaData.State.CLOSE) - .numberOfShards(1) - .numberOfReplicas(0) - .build(); - - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); + IndexMetaData indexMetadata = getIndexMetaData(); Mockito.doAnswer(invocation -> { OpenIndexRequest closeIndexRequest = (OpenIndexRequest) invocation.getArguments()[0]; @@ -131,12 +121,6 @@ public class OpenFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCas .numberOfReplicas(0) .build(); - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Exception error = new RuntimeException(); Mockito.doAnswer(invocation -> { OpenIndexRequest closeIndexRequest = (OpenIndexRequest) invocation.getArguments()[0]; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java index a18f905d6c7..b3b693f165d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java @@ -8,9 +8,6 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction; import org.mockito.Mockito; @@ -26,7 +23,7 @@ import static org.hamcrest.Matchers.sameInstance; public class PauseFollowerIndexStepTests extends AbstractUnfollowIndexStepTestCase { @Override - protected PauseFollowerIndexStep newInstance(Step.StepKey key, Step.StepKey nextKey, Client client) { + protected PauseFollowerIndexStep newInstance(Step.StepKey key, Step.StepKey nextKey) { return new PauseFollowerIndexStep(key, nextKey, client); } @@ -38,11 +35,6 @@ public class PauseFollowerIndexStepTests extends AbstractUnfollowIndexStepTestCa .numberOfReplicas(0) .build(); - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); Mockito.doAnswer(invocation -> { PauseFollowAction.Request request = (PauseFollowAction.Request) invocation.getArguments()[1]; @@ -80,7 +72,6 @@ public class PauseFollowerIndexStepTests extends AbstractUnfollowIndexStepTestCa .build(); // Mock pause follow api call: - Client client = Mockito.mock(Client.class); Exception error = new RuntimeException(); Mockito.doAnswer(invocation -> { PauseFollowAction.Request request = (PauseFollowAction.Request) invocation.getArguments()[1]; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java index a83117e2c4d..ec56439e952 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java @@ -12,17 +12,11 @@ import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition; import org.elasticsearch.action.admin.indices.rollover.RolloverInfo; import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.xpack.core.ilm.Step.StepKey; -import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.util.Collections; import java.util.Locale; @@ -32,13 +26,6 @@ import static org.hamcrest.core.Is.is; public class RolloverStepTests extends AbstractStepMasterTimeoutTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override public RolloverStep createRandomInstance() { StepKey stepKey = randomStepKey(); @@ -99,23 +86,13 @@ public class RolloverStepTests extends AbstractStepMasterTimeoutTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertRolloverIndexRequest(request, alias); - listener.onResponse(new RolloverResponse(null, null, Collections.emptyMap(), request.isDryRun(), true, true, true)); - return null; - } - + Mockito.doAnswer(invocation -> { + RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertRolloverIndexRequest(request, alias); + listener.onResponse(new RolloverResponse(null, null, Collections.emptyMap(), request.isDryRun(), true, true, true)); + return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); @@ -179,10 +156,6 @@ public class RolloverStepTests extends AbstractStepMasterTimeoutTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertRolloverIndexRequest(request, alias); - listener.onFailure(exception); - return null; - } - + Mockito.doAnswer(invocation -> { + RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertRolloverIndexRequest(request, alias); + listener.onFailure(exception); + return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java index c4ddd9baae5..08182207f78 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java @@ -13,9 +13,6 @@ import org.elasticsearch.action.admin.indices.segments.IndexShardSegments; import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse; import org.elasticsearch.action.admin.indices.segments.ShardSegments; import org.elasticsearch.action.support.DefaultShardOperationFailedException; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; @@ -85,9 +82,6 @@ public class SegmentCountStepTests extends AbstractStepTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override protected SetSingleNodeAllocateStep createRandomInstance() { return new SetSingleNodeAllocateStep(randomStepKey(), randomStepKey(), client); @@ -249,25 +236,15 @@ public class SetSingleNodeAllocateStepTests extends AbstractStepTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertSettingsRequestContainsValueFrom(request, - IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", validNodeIds, true, - indexMetaData.getIndex().getName()); - listener.onFailure(exception); - return null; - } - + Mockito.doAnswer(invocation -> { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertSettingsRequestContainsValueFrom(request, + IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", validNodeIds, true, + indexMetaData.getIndex().getName()); + listener.onFailure(exception); + return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); @@ -537,26 +514,15 @@ public class SetSingleNodeAllocateStepTests extends AbstractStepTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertSettingsRequestContainsValueFrom(request, - IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", validNodeIds, true, - indexMetaData.getIndex().getName()); - listener.onResponse(new AcknowledgedResponse(true)); - return null; - } - + Mockito.doAnswer(invocation -> { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertSettingsRequestContainsValueFrom(request, + IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", validNodeIds, true, + indexMetaData.getIndex().getName()); + listener.onResponse(new AcknowledgedResponse(true)); + return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java index 58d1659535a..c2cc1efea12 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java @@ -11,14 +11,10 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.xpack.core.ilm.AsyncActionStep.Listener; import org.elasticsearch.xpack.core.ilm.Step.StepKey; -import org.junit.Before; import org.mockito.Mockito; import org.mockito.stubbing.Answer; @@ -30,13 +26,6 @@ import static org.hamcrest.Matchers.equalTo; public class ShrinkSetAliasStepTests extends AbstractStepTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override public ShrinkSetAliasStep createRandomInstance() { StepKey stepKey = randomStepKey(); @@ -99,11 +88,7 @@ public class ShrinkSetAliasStepTests extends AbstractStepTestCase { IndicesAliasesRequest request = (IndicesAliasesRequest) invocation.getArguments()[0]; assertThat(request.getAliasActions(), equalTo(expectedAliasActions)); @@ -140,11 +125,6 @@ public class ShrinkSetAliasStepTests extends AbstractStepTestCase) invocation -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java index 4021112533f..9496f90086d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java @@ -11,18 +11,12 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xpack.core.ilm.AsyncActionStep.Listener; import org.elasticsearch.xpack.core.ilm.Step.StepKey; -import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.util.Collections; @@ -32,13 +26,6 @@ import static org.hamcrest.Matchers.equalTo; public class ShrinkStepTests extends AbstractStepTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override public ShrinkStep createRandomInstance() { StepKey stepKey = randomStepKey(); @@ -98,33 +85,22 @@ public class ShrinkStepTests extends AbstractStepTestCase { .putAlias(AliasMetaData.builder("my_alias")) .build(); - - 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 { - ResizeRequest request = (ResizeRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.getSourceIndex(), equalTo(sourceIndexMetaData.getIndex().getName())); - assertThat(request.getTargetIndexRequest().aliases(), equalTo(Collections.emptySet())); - assertThat(request.getTargetIndexRequest().settings(), equalTo(Settings.builder() - .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, step.getNumberOfShards()) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetaData.getNumberOfReplicas()) - .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) - .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null) - .build())); - assertThat(request.getTargetIndexRequest().settings() - .getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1), equalTo(step.getNumberOfShards())); - listener.onResponse(new ResizeResponse(true, true, sourceIndexMetaData.getIndex().getName())); - return null; - } - + Mockito.doAnswer(invocation -> { + ResizeRequest request = (ResizeRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertThat(request.getSourceIndex(), equalTo(sourceIndexMetaData.getIndex().getName())); + assertThat(request.getTargetIndexRequest().aliases(), equalTo(Collections.emptySet())); + assertThat(request.getTargetIndexRequest().settings(), equalTo(Settings.builder() + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, step.getNumberOfShards()) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetaData.getNumberOfReplicas()) + .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) + .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null) + .build())); + assertThat(request.getTargetIndexRequest().settings() + .getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1), equalTo(step.getNumberOfShards())); + listener.onResponse(new ResizeResponse(true, true, sourceIndexMetaData.getIndex().getName())); + return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); @@ -156,11 +132,6 @@ public class ShrinkStepTests extends AbstractStepTestCase { .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); ShrinkStep step = createRandomInstance(); - 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 -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; @@ -198,11 +169,6 @@ public class ShrinkStepTests extends AbstractStepTestCase { Exception exception = new RuntimeException(); ShrinkStep step = createRandomInstance(); - 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 -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowIndexStepTests.java index 6ba0116426c..9cdaad0dbac 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowIndexStepTests.java @@ -9,9 +9,6 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.xpack.core.ccr.action.UnfollowAction; import org.mockito.Mockito; @@ -29,7 +26,7 @@ import static org.hamcrest.Matchers.sameInstance; public class UnfollowFollowIndexStepTests extends AbstractUnfollowIndexStepTestCase { @Override - protected UnfollowFollowIndexStep newInstance(Step.StepKey key, Step.StepKey nextKey, Client client) { + protected UnfollowFollowIndexStep newInstance(Step.StepKey key, Step.StepKey nextKey) { return new UnfollowFollowIndexStep(key, nextKey, client); } @@ -41,12 +38,6 @@ public class UnfollowFollowIndexStepTests extends AbstractUnfollowIndexStepTestC .numberOfReplicas(0) .build(); - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(invocation -> { UnfollowAction.Request request = (UnfollowAction.Request) invocation.getArguments()[1]; assertThat(request.getFollowerIndex(), equalTo("follower-index")); @@ -82,12 +73,6 @@ public class UnfollowFollowIndexStepTests extends AbstractUnfollowIndexStepTestC .numberOfReplicas(0) .build(); - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - // Mock unfollow api call: Exception error = new RuntimeException(); Mockito.doAnswer(invocation -> { @@ -124,12 +109,6 @@ public class UnfollowFollowIndexStepTests extends AbstractUnfollowIndexStepTestC .numberOfReplicas(0) .build(); - Client client = Mockito.mock(Client.class); - AdminClient adminClient = Mockito.mock(AdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - // Mock unfollow api call: ElasticsearchException error = new ElasticsearchException("text exception"); error.addMetadata("es.failed_to_remove_retention_leases", randomAlphaOfLength(10)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java index 0400feb60d0..d1cfcc17099 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java @@ -11,27 +11,16 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xpack.core.ilm.AsyncActionStep.Listener; import org.elasticsearch.xpack.core.ilm.Step.StepKey; -import org.junit.Before; import org.mockito.Mockito; import static org.hamcrest.Matchers.equalTo; public class UpdateSettingsStepTests extends AbstractStepMasterTimeoutTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override public UpdateSettingsStep createRandomInstance() { StepKey stepKey = randomStepKey(); @@ -75,17 +64,11 @@ public class UpdateSettingsStepTests extends AbstractStepMasterTimeoutTestCase { UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") @@ -123,11 +106,6 @@ public class UpdateSettingsStepTests extends AbstractStepMasterTimeoutTestCase { UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java index a4443259fda..80508ff762f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; @@ -31,7 +30,7 @@ public class WaitForFollowShardTasksStepTests extends AbstractStepTestCase statsResponses = Arrays.asList( new FollowStatsAction.StatsResponse(createShardFollowTaskStatus(0, 9, 9)), new FollowStatsAction.StatsResponse(createShardFollowTaskStatus(1, 3, 3)) ); - mockFollowStatsCall(client, indexMetadata.getIndex().getName(), statsResponses); + mockFollowStatsCall(indexMetadata.getIndex().getName(), statsResponses); - WaitForFollowShardTasksStep step = new WaitForFollowShardTasksStep(randomStepKey(), randomStepKey(), client); final boolean[] conditionMetHolder = new boolean[1]; final ToXContentObject[] informationContextHolder = new ToXContentObject[1]; final Exception[] exceptionHolder = new Exception[1]; - step.evaluateCondition(indexMetadata, new AsyncWaitStep.Listener() { + createRandomInstance().evaluateCondition(indexMetadata, new AsyncWaitStep.Listener() { @Override public void onResponse(boolean conditionMet, ToXContentObject informationContext) { conditionMetHolder[0] = conditionMet; @@ -96,18 +93,16 @@ public class WaitForFollowShardTasksStepTests extends AbstractStepTestCase statsResponses = Arrays.asList( new FollowStatsAction.StatsResponse(createShardFollowTaskStatus(0, 9, 9)), new FollowStatsAction.StatsResponse(createShardFollowTaskStatus(1, 8, 3)) ); - mockFollowStatsCall(client, indexMetadata.getIndex().getName(), statsResponses); + mockFollowStatsCall(indexMetadata.getIndex().getName(), statsResponses); - WaitForFollowShardTasksStep step = new WaitForFollowShardTasksStep(randomStepKey(), randomStepKey(), client); final boolean[] conditionMetHolder = new boolean[1]; final ToXContentObject[] informationContextHolder = new ToXContentObject[1]; final Exception[] exceptionHolder = new Exception[1]; - step.evaluateCondition(indexMetadata, new AsyncWaitStep.Listener() { + createRandomInstance().evaluateCondition(indexMetadata, new AsyncWaitStep.Listener() { @Override public void onResponse(boolean conditionMet, ToXContentObject informationContext) { conditionMetHolder[0] = conditionMet; @@ -136,13 +131,11 @@ public class WaitForFollowShardTasksStepTests extends AbstractStepTestCase statsResponses) { + private void mockFollowStatsCall(String expectedIndexName, List statsResponses) { Mockito.doAnswer(invocationOnMock -> { FollowStatsAction.StatsRequest request = (FollowStatsAction.StatsRequest) invocationOnMock.getArguments()[1]; assertThat(request.indices().length, equalTo(1)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java index 36824af47a6..d2458eafb39 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java @@ -13,9 +13,6 @@ import org.elasticsearch.action.admin.indices.stats.IndexStats; import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.action.admin.indices.stats.ShardStats; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.ToXContentObject; @@ -43,7 +40,7 @@ public class WaitForNoFollowersStepTests extends AbstractStepTestCase conditionMetHolder = new SetOnce<>(); final SetOnce stepInfoHolder = new SetOnce<>(); @@ -110,7 +107,7 @@ public class WaitForNoFollowersStepTests extends AbstractStepTestCase conditionMetHolder = new SetOnce<>(); final SetOnce stepInfoHolder = new SetOnce<>(); @@ -147,7 +144,7 @@ public class WaitForNoFollowersStepTests extends AbstractStepTestCase conditionMetHolder = new SetOnce<>(); final SetOnce stepInfoHolder = new SetOnce<>(); @@ -182,11 +179,6 @@ public class WaitForNoFollowersStepTests extends AbstractStepTestCase { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; @@ -211,11 +203,7 @@ public class WaitForNoFollowersStepTests extends AbstractStepTestCase { IndicesStatsRequest request = (IndicesStatsRequest) invocationOnMock.getArguments()[0]; assertThat(request.indices().length, equalTo(1)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java index 583d540cca3..06dc0eff35a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java @@ -16,19 +16,13 @@ import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition; import org.elasticsearch.action.admin.indices.rollover.RolloverInfo; import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; -import org.elasticsearch.client.AdminClient; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContentObject; -import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.util.Collections; import java.util.HashSet; @@ -42,13 +36,6 @@ import static org.hamcrest.Matchers.is; public class WaitForRolloverReadyStepTests extends AbstractStepTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override protected WaitForRolloverReadyStep createRandomInstance() { Step.StepKey stepKey = randomStepKey(); @@ -124,35 +111,25 @@ public class WaitForRolloverReadyStepTests extends AbstractStepTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - Set> expectedConditions = new HashSet<>(); - if (step.getMaxAge() != null) { - expectedConditions.add(new MaxAgeCondition(step.getMaxAge())); - } - if (step.getMaxSize() != null) { - expectedConditions.add(new MaxSizeCondition(step.getMaxSize())); - } - if (step.getMaxDocs() != null) { - expectedConditions.add(new MaxDocsCondition(step.getMaxDocs())); - } - assertRolloverIndexRequest(request, alias, expectedConditions); - Map conditionResults = expectedConditions.stream() - .collect(Collectors.toMap(Condition::toString, condition -> true)); - listener.onResponse(new RolloverResponse(null, null, conditionResults, request.isDryRun(), false, false, false)); - return null; + Mockito.doAnswer(invocation -> { + RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + Set> expectedConditions = new HashSet<>(); + if (step.getMaxAge() != null) { + expectedConditions.add(new MaxAgeCondition(step.getMaxAge())); } - + if (step.getMaxSize() != null) { + expectedConditions.add(new MaxSizeCondition(step.getMaxSize())); + } + if (step.getMaxDocs() != null) { + expectedConditions.add(new MaxDocsCondition(step.getMaxDocs())); + } + assertRolloverIndexRequest(request, alias, expectedConditions); + Map conditionResults = expectedConditions.stream() + .collect(Collectors.toMap(Condition::toString, condition -> true)); + listener.onResponse(new RolloverResponse(null, null, conditionResults, request.isDryRun(), false, false, false)); + return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce conditionsMet = new SetOnce<>(); @@ -186,7 +163,6 @@ public class WaitForRolloverReadyStepTests extends AbstractStepTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - Set> expectedConditions = new HashSet<>(); - if (step.getMaxAge() != null) { - expectedConditions.add(new MaxAgeCondition(step.getMaxAge())); - } - if (step.getMaxSize() != null) { - expectedConditions.add(new MaxSizeCondition(step.getMaxSize())); - } - if (step.getMaxDocs() != null) { - expectedConditions.add(new MaxDocsCondition(step.getMaxDocs())); - } - assertRolloverIndexRequest(request, alias, expectedConditions); - Map conditionResults = expectedConditions.stream() - .collect(Collectors.toMap(Condition::toString, condition -> false)); - listener.onResponse(new RolloverResponse(null, null, conditionResults, request.isDryRun(), false, false, false)); - return null; + Mockito.doAnswer(invocation -> { + RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + Set> expectedConditions = new HashSet<>(); + if (step.getMaxAge() != null) { + expectedConditions.add(new MaxAgeCondition(step.getMaxAge())); } - + if (step.getMaxSize() != null) { + expectedConditions.add(new MaxSizeCondition(step.getMaxSize())); + } + if (step.getMaxDocs() != null) { + expectedConditions.add(new MaxDocsCondition(step.getMaxDocs())); + } + assertRolloverIndexRequest(request, alias, expectedConditions); + Map conditionResults = expectedConditions.stream() + .collect(Collectors.toMap(Condition::toString, condition -> false)); + listener.onResponse(new RolloverResponse(null, null, conditionResults, request.isDryRun(), false, false, false)); + return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); @@ -360,33 +325,23 @@ public class WaitForRolloverReadyStepTests extends AbstractStepTestCase() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - Set> expectedConditions = new HashSet<>(); - if (step.getMaxAge() != null) { - expectedConditions.add(new MaxAgeCondition(step.getMaxAge())); - } - if (step.getMaxSize() != null) { - expectedConditions.add(new MaxSizeCondition(step.getMaxSize())); - } - if (step.getMaxDocs() != null) { - expectedConditions.add(new MaxDocsCondition(step.getMaxDocs())); - } - assertRolloverIndexRequest(request, alias, expectedConditions); - listener.onFailure(exception); - return null; + Mockito.doAnswer(invocation -> { + RolloverRequest request = (RolloverRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + Set> expectedConditions = new HashSet<>(); + if (step.getMaxAge() != null) { + expectedConditions.add(new MaxAgeCondition(step.getMaxAge())); } - + if (step.getMaxSize() != null) { + expectedConditions.add(new MaxSizeCondition(step.getMaxSize())); + } + if (step.getMaxDocs() != null) { + expectedConditions.add(new MaxDocsCondition(step.getMaxDocs())); + } + assertRolloverIndexRequest(request, alias, expectedConditions); + listener.onFailure(exception); + return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); @@ -460,12 +415,4 @@ public class WaitForRolloverReadyStepTests extends AbstractStepTestCase