Tidies up some of the unit tests

Adds some missing tests including checking the hashcode and equals methods of `DeleteStep`, `StepKey`, and `TerminalPolicyStep` as well as adding a test for `DeleteAction.toSteps()`
This commit is contained in:
Colin Goodheart-Smithe 2018-04-11 12:38:55 +01:00
parent 66b4e8c4aa
commit 482de191f2
4 changed files with 108 additions and 7 deletions

View File

@ -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<DeleteAction> {
@ -27,4 +29,18 @@ public class DeleteActionTests extends AbstractSerializingTestCase<DeleteAction>
protected Reader<DeleteAction> 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<Step> 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());
}
}

View File

@ -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<Boolean> 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<Boolean> 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) {

View File

@ -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);
}
}

View File

@ -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());
}
}