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 84bfe983a19..919df2b4f73 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 @@ -8,8 +8,10 @@ package org.elasticsearch.xpack.core.indexlifecycle; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; import java.io.IOException; +import java.util.List; public class DeleteActionTests extends AbstractSerializingTestCase { @@ -27,4 +29,18 @@ public class DeleteActionTests extends AbstractSerializingTestCase protected Reader instanceReader() { return DeleteAction::new; } + + public void testToSteps() { + DeleteAction action = createTestInstance(); + String phase = randomAlphaOfLengthBetween(1, 10); + StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10)); + List steps = action.toSteps(null, phase, nextStepKey); + assertNotNull(steps); + assertEquals(1, steps.size()); + StepKey expectedFirstStepKey = new StepKey(phase, AllocateAction.NAME, AllocateAction.NAME); + DeleteStep firstStep = (DeleteStep) steps.get(0); + assertEquals(expectedFirstStepKey, firstStep.getKey()); + assertEquals(nextStepKey, firstStep.getNextStepKey()); + } } 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 a31e2769845..def66657d8f 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 @@ -16,6 +16,9 @@ import org.elasticsearch.client.Client; import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; +import org.junit.Before; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -24,15 +27,51 @@ import static org.hamcrest.Matchers.equalTo; public class DeleteStepTests extends ESTestCase { + private Client client; + + @Before + public void setup() { + client = Mockito.mock(Client.class); + } + + public DeleteStep createRandomInstance() { + StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10)); + StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10)); + + return new DeleteStep(stepKey, nextStepKey, client); + } + + public DeleteStep mutateInstance(DeleteStep instance) { + StepKey key = instance.getKey(); + StepKey nextKey = instance.getNextStepKey(); + + switch (between(0, 1)) { + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + + return new DeleteStep(key, nextKey, instance.getClient()); + } + + public void testHashcodeAndEquals() { + EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), + instance -> new DeleteStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()), this::mutateInstance); + } + public void testIndexSurvives() { - assertFalse(new DeleteStep(null, null, null).indexSurvives()); + assertFalse(createRandomInstance().indexSurvives()); } public void testDeleted() { IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - Client client = Mockito.mock(Client.class); AdminClient adminClient = Mockito.mock(AdminClient.class); IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); @@ -51,7 +90,7 @@ public class DeleteStepTests extends ESTestCase { SetOnce actionCompleted = new SetOnce<>(); - DeleteStep step = new DeleteStep(null, null, client); + DeleteStep step = createRandomInstance(); step.performAction(indexMetaData, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { @@ -76,7 +115,6 @@ public class DeleteStepTests extends ESTestCase { .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); Exception exception = new RuntimeException(); - Client client = Mockito.mock(Client.class); AdminClient adminClient = Mockito.mock(AdminClient.class); IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); @@ -99,7 +137,7 @@ public class DeleteStepTests extends ESTestCase { }).when(indicesClient).delete(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); - DeleteStep step = new DeleteStep(null, null, client); + DeleteStep step = createRandomInstance(); step.performAction(indexMetaData, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/StepKeyTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/StepKeyTests.java new file mode 100644 index 00000000000..aee735c15ea --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/StepKeyTests.java @@ -0,0 +1,45 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.core.indexlifecycle; + + +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; + +public class StepKeyTests extends ESTestCase { + + public StepKey createRandomInstance() { + return new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10)); + } + + public StepKey mutateInstance(StepKey instance) { + String phase = instance.getPhase(); + String action = instance.getAction(); + String step = instance.getName(); + + switch (between(0, 2)) { + case 0: + phase += randomAlphaOfLength(5); + break; + case 1: + action += randomAlphaOfLength(5); + break; + case 2: + step += randomAlphaOfLength(5); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + + return new StepKey(phase, action, step); + } + + public void testHashcodeAndEquals() { + EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), + instance -> new StepKey(instance.getPhase(), instance.getAction(), instance.getName()), this::mutateInstance); + } +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/StepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStepTests.java similarity index 55% rename from x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/StepTests.java rename to x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStepTests.java index 28296d8f63a..e5ee34b85bc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/StepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStepTests.java @@ -8,8 +8,10 @@ package org.elasticsearch.xpack.core.indexlifecycle; import org.elasticsearch.test.ESTestCase; -public class StepTests extends ESTestCase { +public class TerminalPolicyStepTests extends ESTestCase { - public void test() { + public void testKeys() { + assertEquals(new Step.StepKey("completed", "completed", "completed"), TerminalPolicyStep.INSTANCE.getKey()); + assertEquals(null, TerminalPolicyStep.INSTANCE.getNextStepKey()); } }