Improved index template tests, among other to test for getting all templates by not specifying a name.

This commit is contained in:
Boaz Leskes 2013-10-01 15:02:58 +02:00
parent b5ae1fb51b
commit 4ddfe89bdb
2 changed files with 50 additions and 40 deletions

View File

@ -33,6 +33,8 @@ import java.util.Arrays;
import java.util.List;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertThrows;
import static org.hamcrest.Matchers.*;
/**
@ -40,8 +42,17 @@ import static org.hamcrest.Matchers.*;
*/
public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
@Test
public void simpleIndexTemplateTests() throws Exception {
// clean all templates setup by the framework.
client().admin().indices().prepareDeleteTemplate("*").get();
// check get all templates on an empty index.
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), empty());
client().admin().indices().preparePutTemplate("template_1")
.setTemplate("te*")
.setOrder(0)
@ -49,7 +60,7 @@ public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
.startObject("field1").field("type", "string").field("store", "yes").endObject()
.startObject("field2").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
.get();
client().admin().indices().preparePutTemplate("template_2")
.setTemplate("test*")
@ -57,24 +68,21 @@ public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field2").field("type", "string").field("store", "no").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
.get();
// test create param
try {
client().admin().indices().preparePutTemplate("template_2")
.setTemplate("test*")
.setCreate(true)
.setOrder(1)
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field2").field("type", "string").field("store", "no").endObject()
.endObject().endObject().endObject())
.execute().actionGet();
assertThat(false, equalTo(true));
} catch (IndexTemplateAlreadyExistsException e) {
// OK
} catch (Exception e) {
assertThat(false, equalTo(true));
}
assertThrows(client().admin().indices().preparePutTemplate("template_2")
.setTemplate("test*")
.setCreate(true)
.setOrder(1)
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
.startObject("field2").field("type", "string").field("store", "no").endObject()
.endObject().endObject().endObject())
, IndexTemplateAlreadyExistsException.class
);
response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), hasSize(2));
// index something into test_index, will match on both templates
@ -86,12 +94,8 @@ public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
.setQuery(termQuery("field1", "value1"))
.addField("field1").addField("field2")
.execute().actionGet();
if (searchResponse.getFailedShards() > 0) {
logger.warn("failed search " + Arrays.toString(searchResponse.getShardFailures()));
}
assertThat(searchResponse.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertThat(searchResponse.getHits().hits().length, equalTo(1));
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).field("field1").value().toString(), equalTo("value1"));
assertThat(searchResponse.getHits().getAt(0).field("field2").value().toString(), equalTo("value 2")); // this will still be loaded because of the source feature
@ -107,9 +111,7 @@ public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
if (searchResponse.getFailedShards() > 0) {
logger.warn("failed search " + Arrays.toString(searchResponse.getShardFailures()));
}
assertThat(searchResponse.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertThat(searchResponse.getHits().hits().length, equalTo(1));
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).field("field1").value().toString(), equalTo("value1"));
assertThat(searchResponse.getHits().getAt(0).field("field2").value().toString(), equalTo("value 2"));
}
@ -137,7 +139,7 @@ public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
logger.info("--> explicitly delete template_1");
admin().indices().prepareDeleteTemplate("template_1").execute().actionGet();
assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().size(), equalTo(1+existingTemplates));
assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().size(), equalTo(1 + existingTemplates));
assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().containsKey("template_2"), equalTo(true));
assertThat(admin().cluster().prepareState().execute().actionGet().getState().metaData().templates().containsKey("template_1"), equalTo(false));
@ -262,12 +264,9 @@ public class SimpleIndexTemplateTests extends AbstractIntegrationTest {
}
private void testExpectActionRequestValidationException(String... names) {
try {
client().admin().indices().prepareGetTemplates(names).execute().actionGet();
fail("We should have raised an ActionRequestValidationException for " + names);
} catch (ActionRequestValidationException e) {
// That's fine!
}
assertThrows(client().admin().indices().prepareGetTemplates(names),
ActionRequestValidationException.class,
"get template with " + Arrays.toString(names));
}
}

View File

@ -55,7 +55,7 @@ public class ElasticSearchAssertions {
public static void assertAcked(PutMappingRequestBuilder builder) {
assertAcked(builder.get());
}
private static void assertAcked(PutMappingResponse response) {
assertThat("Put Mapping failed - not acked", response.isAcknowledged(), equalTo(true));
@ -114,11 +114,11 @@ public class ElasticSearchAssertions {
}
assertThat("Expected ids: " + Arrays.toString(idsSet.toArray(new String[0])) + " in the result - result size differs." + shardStatus, idsSet.size(), equalTo(0));
}
public static void assertOrderedSearchHits(SearchResponse searchResponse, String... ids) {
String shardStatus = formatShardStatus(searchResponse);
assertThat("Expected different hit count. " + shardStatus, searchResponse.getHits().hits().length, equalTo(ids.length));
for (int i=0; i<ids.length; i++) {
for (int i = 0; i < ids.length; i++) {
SearchHit hit = searchResponse.getHits().hits()[i];
assertThat("Expected id: " + ids[i] + " at position " + i + " but wasn't." + shardStatus, hit.getId(), equalTo(ids[i]));
}
@ -200,7 +200,7 @@ public class ElasticSearchAssertions {
*/
public static void assertSuggestion(Suggest searchSuggest, int entry, String key, int size, String... text) {
assertSuggestionSize(searchSuggest, entry, size, key);
for( int i = 0; i < text.length; i++) {
for (int i = 0; i < text.length; i++) {
assertSuggestion(searchSuggest, entry, i, key, text[i]);
}
}
@ -232,20 +232,31 @@ public class ElasticSearchAssertions {
assertThrows(builder.execute(), exceptionClass);
}
public static <E extends Throwable> void assertThrows(ActionRequestBuilder<?, ?, ?> builder, Class<E> exceptionClass, String extraInfo) {
assertThrows(builder.execute(), exceptionClass, extraInfo);
}
public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass) {
assertThrows(future, exceptionClass, null);
}
public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass, String extraInfo) {
boolean fail = false;
extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": ";
extraInfo += "expected a " + exceptionClass + " exception to be thrown";
try {
future.actionGet();
fail = true;
} catch (ElasticSearchException esException) {
assertThat(esException.unwrapCause(), instanceOf(exceptionClass));
assertThat(extraInfo, esException.unwrapCause(), instanceOf(exceptionClass));
} catch (Throwable e) {
assertThat(e, instanceOf(exceptionClass));
assertThat(extraInfo, e, instanceOf(exceptionClass));
}
// has to be outside catch clause to get a proper message
if (fail) {
throw new AssertionError("Expected a " + exceptionClass + " exception to be thrown");
throw new AssertionError(extraInfo);
}
}