add more tests

This commit is contained in:
Tal Levy 2018-04-05 18:00:41 -07:00
parent b58159ddad
commit 47ed2e5b23
4 changed files with 133 additions and 113 deletions

View File

@ -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

View File

@ -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<DeleteAction>
protected Reader<DeleteAction> 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<Void>() {
//
// @Override
// public Void answer(InvocationOnMock invocation) throws Throwable {
// DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
// @SuppressWarnings("unchecked")
// ActionListener<DeleteIndexResponse> listener = (ActionListener<DeleteIndexResponse>) 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<Boolean> 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<Void>() {
//
// @Override
// public Void answer(InvocationOnMock invocation) throws Throwable {
// DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
// @SuppressWarnings("unchecked")
// ActionListener<DeleteIndexResponse> listener = (ActionListener<DeleteIndexResponse>) 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<Boolean> 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());
// }
}

View File

@ -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<DeleteIndexResponse> listener = (ActionListener<DeleteIndexResponse>) 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<Boolean> 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<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
@SuppressWarnings("unchecked")
ActionListener<DeleteIndexResponse> listener = (ActionListener<DeleteIndexResponse>) 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<Boolean> 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));
}
}

View File

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