diff --git a/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java b/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java index 33afbc5ef27..a87b19facee 100644 --- a/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java +++ b/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java @@ -19,39 +19,16 @@ package org.elasticsearch.index.aliases; -import org.elasticsearch.cache.recycler.CacheRecyclerModule; -import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.common.compress.CompressedString; -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.Injector; -import org.elasticsearch.common.inject.ModulesBuilder; -import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.IndexNameModule; -import org.elasticsearch.index.analysis.AnalysisModule; -import org.elasticsearch.index.cache.IndexCacheModule; -import org.elasticsearch.index.codec.CodecModule; -import org.elasticsearch.index.engine.IndexEngineModule; import org.elasticsearch.index.query.FilterBuilder; -import org.elasticsearch.index.query.IndexQueryParserModule; -import org.elasticsearch.index.query.IndexQueryParserService; -import org.elasticsearch.index.query.functionscore.FunctionScoreModule; -import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.index.similarity.SimilarityModule; +import org.elasticsearch.index.service.IndexService; import org.elasticsearch.indices.InvalidAliasNameException; -import org.elasticsearch.indices.fielddata.breaker.NoneCircuitBreakerService; -import org.elasticsearch.indices.query.IndicesQueriesModule; -import org.elasticsearch.script.ScriptModule; -import org.elasticsearch.indices.fielddata.breaker.CircuitBreakerService; -import org.elasticsearch.indices.query.IndicesQueriesModule; -import org.elasticsearch.script.ScriptModule; -import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.junit.Test; import java.io.IOException; @@ -63,37 +40,12 @@ import static org.hamcrest.Matchers.nullValue; /** * */ -public class IndexAliasesServiceTests extends ElasticsearchTestCase { - public static IndexAliasesService newIndexAliasesService() { +public class IndexAliasesServiceTests extends ElasticsearchSingleNodeTest { + public IndexAliasesService newIndexAliasesService() { Settings settings = ImmutableSettings.builder().put("name", "IndexAliasesServiceTests").build(); - return new IndexAliasesService(new Index("test"), settings, newIndexQueryParserService(settings)); - } - - public static IndexQueryParserService newIndexQueryParserService(Settings settings) { - Injector injector = new ModulesBuilder().add( - new IndicesQueriesModule(), - new CacheRecyclerModule(settings), - new CodecModule(settings), - new IndexSettingsModule(new Index("test"), settings), - new IndexNameModule(new Index("test")), - new IndexQueryParserModule(settings), - new AnalysisModule(settings), - new SimilarityModule(settings), - new ScriptModule(settings), - new SettingsModule(settings), - new IndexEngineModule(settings), - new IndexCacheModule(settings), - new FunctionScoreModule(), - new AbstractModule() { - @Override - protected void configure() { - bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); - bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class); - } - } - ).createInjector(); - return injector.getInstance(IndexQueryParserService.class); + IndexService indexService = createIndex("test", settings); + return indexService.aliasesService(); } public static CompressedString filter(FilterBuilder filterBuilder) throws IOException { diff --git a/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterCachingTests.java b/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterCachingTests.java index 39eee314603..11a53626883 100644 --- a/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterCachingTests.java +++ b/src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterCachingTests.java @@ -22,48 +22,22 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Query; -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.NumericUtils; -import org.elasticsearch.cache.recycler.CacheRecyclerModule; -import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedString; -import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; -import org.elasticsearch.common.inject.ModulesBuilder; -import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.lucene.search.AndFilter; import org.elasticsearch.common.lucene.search.CachedFilter; import org.elasticsearch.common.lucene.search.NoCacheFilter; import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.IndexNameModule; -import org.elasticsearch.index.analysis.AnalysisModule; -import org.elasticsearch.index.cache.IndexCacheModule; -import org.elasticsearch.index.codec.CodecModule; -import org.elasticsearch.index.engine.IndexEngineModule; -import org.elasticsearch.index.fielddata.IndexFieldDataModule; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.MapperServiceModule; -import org.elasticsearch.index.query.functionscore.FunctionScoreModule; import org.elasticsearch.index.search.child.TestSearchContext; -import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.index.similarity.SimilarityModule; -import org.elasticsearch.indices.fielddata.breaker.CircuitBreakerService; -import org.elasticsearch.indices.fielddata.breaker.NoneCircuitBreakerService; -import org.elasticsearch.indices.query.IndicesQueriesModule; -import org.elasticsearch.script.ScriptModule; import org.elasticsearch.search.internal.SearchContext; -import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.elasticsearch.test.index.service.StubIndexService; -import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.threadpool.ThreadPoolModule; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import java.io.IOException; @@ -76,44 +50,18 @@ import static org.hamcrest.Matchers.is; /** * */ -public class IndexQueryParserFilterCachingTests extends ElasticsearchTestCase { +public class IndexQueryParserFilterCachingTests extends ElasticsearchSingleNodeTest { - private static Injector injector; + private Injector injector; + private IndexQueryParserService queryParser; - private static IndexQueryParserService queryParser; - - @BeforeClass - public static void setupQueryParser() throws IOException { + @Before + public void setup() throws IOException { Settings settings = ImmutableSettings.settingsBuilder() .put("index.cache.filter.type", "weighted") .put("name", "IndexQueryParserFilterCachingTests") .build(); - Index index = new Index("test"); - injector = new ModulesBuilder().add( - new CacheRecyclerModule(settings), - new CodecModule(settings), - new SettingsModule(settings), - new ThreadPoolModule(settings), - new IndicesQueriesModule(), - new ScriptModule(settings), - new MapperServiceModule(), - new IndexSettingsModule(index, settings), - new IndexCacheModule(settings), - new AnalysisModule(settings), - new IndexEngineModule(settings), - new SimilarityModule(settings), - new IndexQueryParserModule(settings), - new IndexNameModule(index), - new FunctionScoreModule(), - new IndexFieldDataModule(settings), - new AbstractModule() { - @Override - protected void configure() { - bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); - bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class); - } - } - ).createInjector(); + injector = createIndex("test", settings).injector(); injector.getInstance(IndexFieldDataService.class).setIndexService((new StubIndexService(injector.getInstance(MapperService.class)))); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); @@ -124,24 +72,10 @@ public class IndexQueryParserFilterCachingTests extends ElasticsearchTestCase { queryParser = injector.getInstance(IndexQueryParserService.class); } - @AfterClass - public static void close() { - injector.getInstance(ThreadPool.class).shutdownNow(); - queryParser = null; - injector = null; - } - private IndexQueryParserService queryParser() throws IOException { return this.queryParser; } - private BytesRef longToPrefixCoded(long val, int shift) { - BytesRef bytesRef = new BytesRef(); - NumericUtils.longToPrefixCoded(val, shift, bytesRef); - return bytesRef; - } - - @Test public void testNoFilterParsing() throws IOException { IndexQueryParserService queryParser = queryParser(); diff --git a/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java b/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java index 9898fd27961..b3b117629ad 100644 --- a/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java +++ b/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java @@ -31,54 +31,30 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.NumericUtils; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.get.MultiGetRequest; -import org.elasticsearch.cache.recycler.CacheRecyclerModule; -import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedString; -import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; -import org.elasticsearch.common.inject.ModulesBuilder; -import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.lucene.search.*; import org.elasticsearch.common.lucene.search.function.BoostScoreFunction; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.IndexNameModule; -import org.elasticsearch.index.analysis.AnalysisModule; -import org.elasticsearch.index.cache.IndexCacheModule; import org.elasticsearch.index.cache.filter.support.CacheKeyFilter; -import org.elasticsearch.index.codec.CodecModule; -import org.elasticsearch.index.engine.IndexEngineModule; -import org.elasticsearch.index.fielddata.IndexFieldDataModule; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.MapperServiceModule; import org.elasticsearch.index.mapper.core.NumberFieldMapper; -import org.elasticsearch.index.query.functionscore.FunctionScoreModule; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import org.elasticsearch.index.search.geo.GeoDistanceFilter; import org.elasticsearch.index.search.geo.GeoPolygonFilter; import org.elasticsearch.index.search.geo.InMemoryGeoBoundingBoxFilter; import org.elasticsearch.index.search.morelikethis.MoreLikeThisFetchService; import org.elasticsearch.index.search.morelikethis.MoreLikeThisFetchService.LikeText; -import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.index.similarity.SimilarityModule; -import org.elasticsearch.indices.fielddata.breaker.CircuitBreakerService; -import org.elasticsearch.indices.fielddata.breaker.NoneCircuitBreakerService; -import org.elasticsearch.indices.query.IndicesQueriesModule; -import org.elasticsearch.script.ScriptModule; -import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.elasticsearch.test.index.service.StubIndexService; -import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.threadpool.ThreadPoolModule; import org.hamcrest.Matchers; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import java.io.IOException; @@ -99,44 +75,18 @@ import static org.hamcrest.Matchers.*; /** * */ -public class SimpleIndexQueryParserTests extends ElasticsearchTestCase { +public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { - private static Injector injector; + private Injector injector; + private IndexQueryParserService queryParser; - private static IndexQueryParserService queryParser; - - @BeforeClass - public static void setupQueryParser() throws IOException { + @Before + public void setup() throws IOException { Settings settings = ImmutableSettings.settingsBuilder() .put("index.cache.filter.type", "none") .put("name", "SimpleIndexQueryParserTests") .build(); - Index index = new Index("test"); - injector = new ModulesBuilder().add( - new CacheRecyclerModule(settings), - new CodecModule(settings), - new SettingsModule(settings), - new ThreadPoolModule(settings), - new IndicesQueriesModule(), - new ScriptModule(settings), - new MapperServiceModule(), - new IndexSettingsModule(index, settings), - new IndexCacheModule(settings), - new AnalysisModule(settings), - new IndexEngineModule(settings), - new SimilarityModule(settings), - new IndexQueryParserModule(settings), - new IndexFieldDataModule(settings), - new IndexNameModule(index), - new FunctionScoreModule(), - new AbstractModule() { - @Override - protected void configure() { - bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); - bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class); - } - } - ).createInjector(); + injector = createIndex("test", settings).injector(); injector.getInstance(IndexFieldDataService.class).setIndexService((new StubIndexService(injector.getInstance(MapperService.class)))); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); @@ -146,13 +96,6 @@ public class SimpleIndexQueryParserTests extends ElasticsearchTestCase { queryParser = injector.getInstance(IndexQueryParserService.class); } - @AfterClass - public static void close() { - injector.getInstance(ThreadPool.class).shutdownNow(); - queryParser = null; - injector = null; - } - private IndexQueryParserService queryParser() throws IOException { return this.queryParser; } diff --git a/src/test/java/org/elasticsearch/index/similarity/SimilarityTests.java b/src/test/java/org/elasticsearch/index/similarity/SimilarityTests.java index b292fd5a06b..10c63253464 100644 --- a/src/test/java/org/elasticsearch/index/similarity/SimilarityTests.java +++ b/src/test/java/org/elasticsearch/index/similarity/SimilarityTests.java @@ -20,23 +20,11 @@ package org.elasticsearch.index.similarity; import org.apache.lucene.search.similarities.*; -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.Injector; -import org.elasticsearch.common.inject.ModulesBuilder; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.IndexNameModule; -import org.elasticsearch.index.analysis.AnalysisModule; -import org.elasticsearch.index.codec.CodecModule; import org.elasticsearch.index.mapper.DocumentMapper; -import org.elasticsearch.index.mapper.MapperServiceModule; -import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.fielddata.breaker.CircuitBreakerService; -import org.elasticsearch.indices.fielddata.breaker.NoneCircuitBreakerService; -import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.junit.Test; import java.io.IOException; @@ -44,11 +32,11 @@ import java.io.IOException; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; -public class SimilarityTests extends ElasticsearchTestCase { +public class SimilarityTests extends ElasticsearchSingleNodeTest { @Test public void testResolveDefaultSimilarities() { - SimilarityLookupService similarityLookupService = similarityService().similarityLookupService(); + SimilarityLookupService similarityLookupService = createIndex("foo").similarityService().similarityLookupService(); assertThat(similarityLookupService.similarity("default"), instanceOf(PreBuiltSimilarityProvider.class)); assertThat(similarityLookupService.similarity("default").get(), instanceOf(DefaultSimilarity.class)); assertThat(similarityLookupService.similarity("BM25"), instanceOf(PreBuiltSimilarityProvider.class)); @@ -67,7 +55,7 @@ public class SimilarityTests extends ElasticsearchTestCase { .put("index.similarity.my_similarity.type", "default") .put("index.similarity.my_similarity.discount_overlaps", false) .build(); - SimilarityService similarityService = similarityService(indexSettings); + SimilarityService similarityService = createIndex("foo", indexSettings).similarityService(); DocumentMapper documentMapper = similarityService.mapperService().documentMapperParser().parse(mapping); assertThat(documentMapper.mappers().name("field1").mapper().similarity(), instanceOf(DefaultSimilarityProvider.class)); @@ -89,7 +77,7 @@ public class SimilarityTests extends ElasticsearchTestCase { .put("index.similarity.my_similarity.b", 1.5f) .put("index.similarity.my_similarity.discount_overlaps", false) .build(); - SimilarityService similarityService = similarityService(indexSettings); + SimilarityService similarityService = createIndex("foo", indexSettings).similarityService(); DocumentMapper documentMapper = similarityService.mapperService().documentMapperParser().parse(mapping); assertThat(documentMapper.mappers().name("field1").mapper().similarity(), instanceOf(BM25SimilarityProvider.class)); @@ -114,7 +102,7 @@ public class SimilarityTests extends ElasticsearchTestCase { .put("index.similarity.my_similarity.normalization", "h2") .put("index.similarity.my_similarity.normalization.h2.c", 3f) .build(); - SimilarityService similarityService = similarityService(indexSettings); + SimilarityService similarityService = createIndex("foo", indexSettings).similarityService(); DocumentMapper documentMapper = similarityService.mapperService().documentMapperParser().parse(mapping); assertThat(documentMapper.mappers().name("field1").mapper().similarity(), instanceOf(DFRSimilarityProvider.class)); @@ -140,7 +128,7 @@ public class SimilarityTests extends ElasticsearchTestCase { .put("index.similarity.my_similarity.normalization", "h2") .put("index.similarity.my_similarity.normalization.h2.c", 3f) .build(); - SimilarityService similarityService = similarityService(indexSettings); + SimilarityService similarityService = createIndex("foo", indexSettings).similarityService(); DocumentMapper documentMapper = similarityService.mapperService().documentMapperParser().parse(mapping); assertThat(documentMapper.mappers().name("field1").mapper().similarity(), instanceOf(IBSimilarityProvider.class)); @@ -163,7 +151,7 @@ public class SimilarityTests extends ElasticsearchTestCase { .put("index.similarity.my_similarity.type", "LMDirichlet") .put("index.similarity.my_similarity.mu", 3000f) .build(); - SimilarityService similarityService = similarityService(indexSettings); + SimilarityService similarityService = createIndex("foo", indexSettings).similarityService(); DocumentMapper documentMapper = similarityService.mapperService().documentMapperParser().parse(mapping); assertThat(documentMapper.mappers().name("field1").mapper().similarity(), instanceOf(LMDirichletSimilarityProvider.class)); @@ -183,36 +171,11 @@ public class SimilarityTests extends ElasticsearchTestCase { .put("index.similarity.my_similarity.type", "LMJelinekMercer") .put("index.similarity.my_similarity.lambda", 0.7f) .build(); - SimilarityService similarityService = similarityService(indexSettings); + SimilarityService similarityService = createIndex("foo", indexSettings).similarityService(); DocumentMapper documentMapper = similarityService.mapperService().documentMapperParser().parse(mapping); assertThat(documentMapper.mappers().name("field1").mapper().similarity(), instanceOf(LMJelinekMercerSimilarityProvider.class)); LMJelinekMercerSimilarity similarity = (LMJelinekMercerSimilarity) documentMapper.mappers().name("field1").mapper().similarity().get(); assertThat(similarity.getLambda(), equalTo(0.7f)); } - - - private static SimilarityService similarityService() { - return similarityService(ImmutableSettings.Builder.EMPTY_SETTINGS); - } - - private static SimilarityService similarityService(Settings settings) { - Index index = new Index("test"); - Injector injector = new ModulesBuilder() - .add(new SettingsModule(settings)) - .add(new IndexNameModule(index)) - .add(new IndexSettingsModule(index, settings)) - .add(new CodecModule(settings)) - .add(new MapperServiceModule()) - .add(new AnalysisModule(settings)) - .add(new SimilarityModule(settings)) - .add(new AbstractModule() { - @Override - protected void configure() { - bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class); - } - }) - .createInjector(); - return injector.getInstance(SimilarityService.class); - } } diff --git a/src/test/java/org/elasticsearch/test/ElasticsearchSingleNodeTest.java b/src/test/java/org/elasticsearch/test/ElasticsearchSingleNodeTest.java new file mode 100644 index 00000000000..ef4a637e8ce --- /dev/null +++ b/src/test/java/org/elasticsearch/test/ElasticsearchSingleNodeTest.java @@ -0,0 +1,97 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.test; + +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; +import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; +import org.elasticsearch.client.Requests; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.Priority; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.concurrent.EsExecutors; +import org.elasticsearch.index.service.IndexService; +import org.elasticsearch.indices.IndicesService; +import org.elasticsearch.node.Node; +import org.elasticsearch.node.NodeBuilder; +import org.elasticsearch.node.internal.InternalNode; +import org.junit.After; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; + +/** + * A test that keep a single node started for all tests that can be used to get + * references to Guice injectors in unit tests. + */ +public class ElasticsearchSingleNodeTest extends ElasticsearchTestCase { + + private static final Node node = node(); + + @After + public void after() { + node.client().admin().indices().prepareDelete("*").get(); + MetaData metaData = node.client().admin().cluster().prepareState().get().getState().getMetaData(); + assertThat("test leaves persistent cluster metadata behind: " + metaData.persistentSettings().getAsMap(), + metaData.persistentSettings().getAsMap().size(), equalTo(0)); + assertThat("test leaves transient cluster metadata behind: " + metaData.transientSettings().getAsMap(), + metaData.transientSettings().getAsMap().size(), equalTo(0)); + } + + /** + * Same as {@link #node(Settings) node(ImmutableSettings.EMPTY)}. + */ + public static Node node() { + return node(ImmutableSettings.EMPTY); + } + + public static Node node(Settings settings) { + return NodeBuilder.nodeBuilder().local(true).data(true).settings(ImmutableSettings.builder() + .put(ClusterName.SETTING, ElasticsearchSingleNodeTest.class.getName()) + .put("node.name", ElasticsearchSingleNodeTest.class.getName()) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) + .put(settings) + .put(EsExecutors.PROCESSORS, 1) // limit the number of threads created + .put("http.enabled", false) + .put("index.store.type", "ram") + .put("gateway.type", "none")).build().start(); + } + + public static T getInstanceFromNode(Class clazz, Node node) { + return ((InternalNode) node).injector().getInstance(clazz); + } + + public static IndexService createIndex(String index) { + return createIndex(index, ImmutableSettings.EMPTY); + } + + public static IndexService createIndex(String index, Settings settings) { + node.client().admin().indices().prepareCreate(index).setSettings(settings).get(); + // Wait for the index to be allocated so that cluster state updates don't override + // changes that would have been done locally + ClusterHealthResponse health = node.client().admin().cluster() + .health(Requests.clusterHealthRequest(index).waitForYellowStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet(); + assertThat(health.getStatus(), lessThanOrEqualTo(ClusterHealthStatus.YELLOW)); + IndicesService instanceFromNode = getInstanceFromNode(IndicesService.class, node); + return instanceFromNode.indexService(index); + } +}