From: Julien Lancelot Date: Fri, 13 Feb 2015 08:57:55 +0000 (+0100) Subject: Use EsTester in all ProxyXXXTest classes X-Git-Tag: 5.1-RC1~274 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=56c6d32be1457b85dd277797206e7db805e55e4c;p=sonarqube.git Use EsTester in all ProxyXXXTest classes --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyBulkRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyBulkRequestBuilderTest.java index d56df24ef34..f96e451dd0b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyBulkRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyBulkRequestBuilderTest.java @@ -26,7 +26,8 @@ import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.unit.TimeValue; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.core.profiling.Profiling; import org.sonar.server.es.EsTester; @@ -36,8 +37,13 @@ import static org.assertj.core.api.Assertions.assertThat; public class ProxyBulkRequestBuilderTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); + + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); + } @Test public void bulk() { @@ -45,8 +51,8 @@ public class ProxyBulkRequestBuilderTest { } @Test - public void with_profiling_basic() { - testBulk(Profiling.Level.BASIC); + public void with_profiling_full() { + testBulk(Profiling.Level.FULL); // TODO assert profiling } diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClearCacheRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClearCacheRequestBuilderTest.java index 6b31392269c..63909a98c4e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClearCacheRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClearCacheRequestBuilderTest.java @@ -22,20 +22,24 @@ package org.sonar.server.es.request; import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; import org.sonar.server.es.EsTester; -import org.sonar.server.search.SearchClient; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyClearCacheRequestBuilderTest { - @Rule - public EsTester esTester = new EsTester(); + @ClassRule + public static EsTester esTester = new EsTester(); + + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); + } @Test public void clear_cache() { @@ -55,15 +59,12 @@ public class ProxyClearCacheRequestBuilderTest { } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); - + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); ClearIndicesCacheRequestBuilder requestBuilder = esTester.client().prepareClearCache(); requestBuilder.get(); // TODO assert profiling - searchClient.stop(); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterHealthRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterHealthRequestBuilderTest.java index 6abbd47e057..3e2d2cd59e7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterHealthRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterHealthRequestBuilderTest.java @@ -24,7 +24,8 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.unit.TimeValue; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.core.profiling.Profiling; import org.sonar.server.es.EsTester; @@ -34,8 +35,13 @@ import static org.junit.Assert.fail; public class ProxyClusterHealthRequestBuilderTest { - @Rule - public EsTester esTester = new EsTester(); + @ClassRule + public static EsTester esTester = new EsTester(); + + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); + } @Test public void state() { @@ -51,8 +57,8 @@ public class ProxyClusterHealthRequestBuilderTest { } @Test - public void with_profiling_basic() { - esTester.setProfilingLevel(Profiling.Level.BASIC); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); ClusterHealthRequestBuilder requestBuilder = esTester.client().prepareHealth(); ClusterHealthResponse state = requestBuilder.get(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStateRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStateRequestBuilderTest.java index 6f220a693f8..d33e2258ebf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStateRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStateRequestBuilderTest.java @@ -22,67 +22,51 @@ package org.sonar.server.es.request; import org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyClusterStateRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester(); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void state() { - try { - ClusterStateRequestBuilder requestBuilder = searchClient.prepareState(); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES cluster state request"); - } + ClusterStateRequestBuilder requestBuilder = esTester.client().prepareState(); + requestBuilder.get(); } @Test public void to_string() { - assertThat(searchClient.prepareState().setIndices("rules").toString()).isEqualTo("ES cluster state request on indices 'rules'"); - assertThat(searchClient.prepareState().toString()).isEqualTo("ES cluster state request"); + assertThat(esTester.client().prepareState().setIndices("rules").toString()).isEqualTo("ES cluster state request on indices 'rules'"); + assertThat(esTester.client().prepareState().toString()).isEqualTo("ES cluster state request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); - try { - - ClusterStateRequestBuilder requestBuilder = searchClient.prepareState(); - requestBuilder.get(); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES cluster state request"); - } + ClusterStateRequestBuilder requestBuilder = esTester.client().prepareState(); + requestBuilder.get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareState().get("1"); + esTester.client().prepareState().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -92,7 +76,7 @@ public class ProxyClusterStateRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareState().get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareState().get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -102,7 +86,7 @@ public class ProxyClusterStateRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareState().execute(); + esTester.client().prepareState().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStatsRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStatsRequestBuilderTest.java index 937abf83ba5..d20f6d77717 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStatsRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStatsRequestBuilderTest.java @@ -22,67 +22,51 @@ package org.sonar.server.es.request; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyClusterStatsRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester(); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void stats() { - try { - ClusterStatsRequestBuilder requestBuilder = searchClient.prepareClusterStats(); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES cluster stats request"); - } + ClusterStatsRequestBuilder requestBuilder = esTester.client().prepareClusterStats(); + requestBuilder.get(); } @Test public void to_string() { - assertThat(searchClient.prepareClusterStats().setNodesIds("node1").toString()).isEqualTo("ES cluster stats request on nodes 'node1'"); - assertThat(searchClient.prepareClusterStats().toString()).isEqualTo("ES cluster stats request"); + assertThat(esTester.client().prepareClusterStats().setNodesIds("node1").toString()).isEqualTo("ES cluster stats request on nodes 'node1'"); + assertThat(esTester.client().prepareClusterStats().toString()).isEqualTo("ES cluster stats request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - ClusterStatsRequestBuilder requestBuilder = searchClient.prepareClusterStats(); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES cluster stats request"); - } + ClusterStatsRequestBuilder requestBuilder = esTester.client().prepareClusterStats(); + requestBuilder.get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareClusterStats().get("1"); + esTester.client().prepareClusterStats().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -92,7 +76,7 @@ public class ProxyClusterStatsRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareClusterStats().get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareClusterStats().get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -102,7 +86,7 @@ public class ProxyClusterStatsRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareClusterStats().execute(); + esTester.client().prepareClusterStats().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCountRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCountRequestBuilderTest.java index 01f73380d16..2defc836a78 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCountRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCountRequestBuilderTest.java @@ -21,67 +21,51 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyCountRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void count() { - try { - searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES count request on indices 'rules'"); - } + esTester.client().prepareCount(FakeIndexDefinition.INDEX).get(); } @Test public void to_string() { - assertThat(searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).setTypes("rule").toString()).isEqualTo("ES count request on indices 'rules' on types 'rule'"); - assertThat(searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).toString()).isEqualTo("ES count request on indices 'rules'"); - assertThat(searchClient.prepareCount().toString()).isEqualTo("ES count request"); + assertThat(esTester.client().prepareCount(FakeIndexDefinition.INDEX).setTypes(FakeIndexDefinition.TYPE).toString()).isEqualTo("ES count request on indices 'fakes' on types 'fake'"); + assertThat(esTester.client().prepareCount(FakeIndexDefinition.INDEX).toString()).isEqualTo("ES count request on indices 'fakes'"); + assertThat(esTester.client().prepareCount().toString()).isEqualTo("ES count request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES count request on indices 'rules'"); - } + esTester.client().prepareCount(FakeIndexDefinition.INDEX).get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_count_bad_query() throws Exception { try { - searchClient.prepareCount("unknown_index1, unknown_index2").setTypes("unknown_type").get(); + esTester.client().prepareCount("unknown_index1, unknown_index2").setTypes("unknown_type").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -92,7 +76,7 @@ public class ProxyCountRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).get("1"); + esTester.client().prepareCount(FakeIndexDefinition.INDEX).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -102,7 +86,7 @@ public class ProxyCountRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareCount(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -112,7 +96,7 @@ public class ProxyCountRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareCount(IndexDefinition.RULE.getIndexName()).execute(); + esTester.client().prepareCount(FakeIndexDefinition.INDEX).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCreateIndexRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCreateIndexRequestBuilderTest.java index fbf7338e951..6f749f26a1c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCreateIndexRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCreateIndexRequestBuilderTest.java @@ -22,66 +22,52 @@ package org.sonar.server.es.request; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; +import org.sonar.api.utils.System2; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyCreateIndexRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester(); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void create_index() { - try { - CreateIndexRequestBuilder requestBuilder = searchClient.prepareCreate("new"); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES create index 'new'"); - } + CreateIndexRequestBuilder requestBuilder = esTester.client().prepareCreate(generateNewIndexName()); + requestBuilder.get(); } @Test public void to_string() { - assertThat(searchClient.prepareCreate("new").toString()).isEqualTo("ES create index 'new'"); + String indexName = generateNewIndexName(); + assertThat(esTester.client().prepareCreate(indexName).toString()).contains("ES create index '" + indexName + "'"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); - - try { - CreateIndexRequestBuilder requestBuilder = searchClient.prepareCreate("new"); - requestBuilder.get(); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES create index 'new'"); - } + CreateIndexRequestBuilder requestBuilder = esTester.client().prepareCreate(generateNewIndexName()); + requestBuilder.get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareCreate("new").get("1"); + esTester.client().prepareCreate(generateNewIndexName()).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -91,7 +77,7 @@ public class ProxyCreateIndexRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareCreate("new").get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareCreate(generateNewIndexName()).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -101,11 +87,15 @@ public class ProxyCreateIndexRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareCreate("new").execute(); + esTester.client().prepareCreate(generateNewIndexName()).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); } } + private static String generateNewIndexName(){ + return "index_" + Long.toString(System2.INSTANCE.now()); + } + } diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteByQueryRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteByQueryRequestBuilderTest.java index 2c81fff5231..b43698ea882 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteByQueryRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteByQueryRequestBuilderTest.java @@ -22,66 +22,50 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyDeleteByQueryRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test - public void expect_failure() { - try { - searchClient.prepareDeleteByQuery(IndexDefinition.RULE.getIndexName()).setQuery(QueryBuilders.matchAllQuery()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES delete by query request on indices 'rules'"); - } + public void delete_by_query() { + esTester.client().prepareDeleteByQuery(FakeIndexDefinition.INDEX).setQuery(QueryBuilders.matchAllQuery()).get(); } @Test public void to_string() { - assertThat(searchClient.prepareDeleteByQuery(IndexDefinition.RULE.getIndexName()).toString()).isEqualTo("ES delete by query request on indices 'rules'"); - assertThat(searchClient.prepareDeleteByQuery().toString()).isEqualTo("ES delete by query request"); + assertThat(esTester.client().prepareDeleteByQuery(FakeIndexDefinition.INDEX).toString()).isEqualTo("ES delete by query request on indices 'fakes'"); + assertThat(esTester.client().prepareDeleteByQuery().toString()).isEqualTo("ES delete by query request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareDeleteByQuery(IndexDefinition.RULE.getIndexName()).setQuery(QueryBuilders.matchAllQuery()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES delete by query request on indices 'rules'"); - } + esTester.client().prepareDeleteByQuery(FakeIndexDefinition.INDEX).setQuery(QueryBuilders.matchAllQuery()).get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareDeleteByQuery().get("1"); + esTester.client().prepareDeleteByQuery().get("1"); fail(); } catch (UnsupportedOperationException e) { assertThat(e).hasMessage("Not yet implemented"); @@ -91,7 +75,7 @@ public class ProxyDeleteByQueryRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareDeleteByQuery().get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareDeleteByQuery().get(TimeValue.timeValueMinutes(1)); fail(); } catch (UnsupportedOperationException e) { assertThat(e).hasMessage("Not yet implemented"); @@ -101,7 +85,7 @@ public class ProxyDeleteByQueryRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareDeleteByQuery().execute(); + esTester.client().prepareDeleteByQuery().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteRequestBuilderTest.java index f475542b091..e8bcca0b7df 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteRequestBuilderTest.java @@ -20,64 +20,47 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyDeleteRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester(); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test - public void expect_failure() { - try { - searchClient.prepareDelete("fakes", "fake", "the_id").get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES delete request of doc the_id in index fakes/fake"); - } + public void delete() { + esTester.client().prepareDelete("fakes", "fake", "the_id").get(); } @Test public void to_string() { - assertThat(searchClient.prepareDelete("fakes", "fake", "the_id").toString()).isEqualTo("ES delete request of doc the_id in index fakes/fake"); + assertThat(esTester.client().prepareDelete("fakes", "fake", "the_id").toString()).isEqualTo("ES delete request of doc the_id in index fakes/fake"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); - - try { - searchClient.prepareDelete("fakes", "fake", "the_id").get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES delete request of doc the_id in index fakes/fake"); - } + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); + esTester.client().prepareDelete("fakes", "fake", "the_id").get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareDelete("fakes", "fake", "the_id").get("1"); + esTester.client().prepareDelete("fakes", "fake", "the_id").get("1"); fail(); } catch (UnsupportedOperationException e) { assertThat(e).hasMessage("Not yet implemented"); @@ -87,7 +70,7 @@ public class ProxyDeleteRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareDelete("fakes", "fake", "the_id").get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareDelete("fakes", "fake", "the_id").get(TimeValue.timeValueMinutes(1)); fail(); } catch (UnsupportedOperationException e) { assertThat(e).hasMessage("Not yet implemented"); @@ -97,7 +80,7 @@ public class ProxyDeleteRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareDelete("fakes", "fake", "the_id").execute(); + esTester.client().prepareDelete("fakes", "fake", "the_id").execute(); fail(); } catch (UnsupportedOperationException e) { assertThat(e).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyFlushRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyFlushRequestBuilderTest.java index 48991566840..cd0ee9935a7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyFlushRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyFlushRequestBuilderTest.java @@ -21,66 +21,50 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyFlushRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void flush() { - try { - searchClient.prepareFlush(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES flush request on indices 'rules'"); - } + esTester.client().prepareFlush(FakeIndexDefinition.INDEX).get(); } @Test public void to_string() { - assertThat(searchClient.prepareFlush(IndexDefinition.RULE.getIndexName()).toString()).isEqualTo("ES flush request on indices 'rules'"); - assertThat(searchClient.prepareFlush().toString()).isEqualTo("ES flush request"); + assertThat(esTester.client().prepareFlush(FakeIndexDefinition.INDEX).toString()).isEqualTo("ES flush request on indices 'fakes'"); + assertThat(esTester.client().prepareFlush().toString()).isEqualTo("ES flush request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareFlush(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES flush request on indices 'rules'"); - } + esTester.client().prepareFlush(FakeIndexDefinition.INDEX).get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_refresh() throws Exception { try { - searchClient.prepareFlush("unknown").get(); + esTester.client().prepareFlush("unknown").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -91,7 +75,7 @@ public class ProxyFlushRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareFlush(IndexDefinition.RULE.getIndexName()).get("1"); + esTester.client().prepareFlush(FakeIndexDefinition.INDEX).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -101,7 +85,7 @@ public class ProxyFlushRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareFlush(IndexDefinition.RULE.getIndexName()).get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareFlush(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -111,7 +95,7 @@ public class ProxyFlushRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareFlush(IndexDefinition.RULE.getIndexName()).execute(); + esTester.client().prepareFlush(FakeIndexDefinition.INDEX).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyGetRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyGetRequestBuilderTest.java index c7307039880..66d4ea9bd05 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyGetRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyGetRequestBuilderTest.java @@ -22,67 +22,51 @@ package org.sonar.server.es.request; import org.elasticsearch.action.get.GetRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyGetRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void get() { - try { - searchClient.prepareGet() - .setIndex(IndexDefinition.RULE.getIndexName()) - .setType(IndexDefinition.RULE.getIndexType()) - .setId("ruleKey") - .get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES get request for key 'ruleKey' on index 'rules' on type 'rule'"); - } + esTester.client().prepareGet() + .setIndex(FakeIndexDefinition.INDEX) + .setType(FakeIndexDefinition.TYPE) + .setId("ruleKey") + .get(); } @Test public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareGet() - .setIndex(IndexDefinition.RULE.getIndexName()) - .setType(IndexDefinition.RULE.getIndexType()) - .setId("ruleKey") - .get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES get request for key 'ruleKey' on index 'rules' on type 'rule'"); - } + esTester.client().prepareGet() + .setIndex(FakeIndexDefinition.INDEX) + .setType(FakeIndexDefinition.TYPE) + .setId("ruleKey") + .get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_get_bad_query() throws Exception { - GetRequestBuilder requestBuilder = searchClient.prepareGet() + GetRequestBuilder requestBuilder = esTester.client().prepareGet() .setIndex("unknown") .setType("test") .setId("rule1"); @@ -98,7 +82,7 @@ public class ProxyGetRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareGet().get("1"); + esTester.client().prepareGet().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -108,7 +92,7 @@ public class ProxyGetRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareGet().get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareGet().get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -118,7 +102,7 @@ public class ProxyGetRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareGet().execute(); + esTester.client().prepareGet().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndexRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndexRequestBuilderTest.java index d9c0527d792..236dab71931 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndexRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndexRequestBuilderTest.java @@ -23,7 +23,8 @@ package org.sonar.server.es.request; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.common.unit.TimeValue; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.core.profiling.Profiling; import org.sonar.server.es.EsTester; @@ -34,12 +35,16 @@ import static org.junit.Assert.fail; public class ProxyIndexRequestBuilderTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); + + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); + } @Test public void index_with_index_type_and_id() { - esTester.setProfilingLevel(Profiling.Level.FULL); IndexResponse response = esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE) .setSource(FakeIndexDefinition.newDoc(42)) .get(); @@ -47,8 +52,8 @@ public class ProxyIndexRequestBuilderTest { } @Test - public void with_profiling_basic() { - esTester.setProfilingLevel(Profiling.Level.BASIC); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); IndexResponse response = esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE) .setSource(FakeIndexDefinition.newDoc(42)) .get(); @@ -58,7 +63,6 @@ public class ProxyIndexRequestBuilderTest { @Test public void fail_if_bad_query() throws Exception { - esTester.setProfilingLevel(Profiling.Level.FULL); IndexRequestBuilder requestBuilder = esTester.client().prepareIndex("unknownIndex", "unknownType"); try { requestBuilder.get(); @@ -71,7 +75,6 @@ public class ProxyIndexRequestBuilderTest { @Test public void fail_if_bad_query_with_basic_profiling() throws Exception { - esTester.setProfilingLevel(Profiling.Level.BASIC); IndexRequestBuilder requestBuilder = esTester.client().prepareIndex("unknownIndex", "unknownType"); try { requestBuilder.get(); @@ -84,7 +87,6 @@ public class ProxyIndexRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { - esTester.setProfilingLevel(Profiling.Level.FULL); try { esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE).get("1"); fail(); @@ -95,7 +97,6 @@ public class ProxyIndexRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { - esTester.setProfilingLevel(Profiling.Level.FULL); try { esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE).get(TimeValue.timeValueMinutes(1)); fail(); @@ -106,7 +107,6 @@ public class ProxyIndexRequestBuilderTest { @Test public void do_not_support_execute_method() throws Exception { - esTester.setProfilingLevel(Profiling.Level.FULL); try { esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE).execute(); fail(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesExistsRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesExistsRequestBuilderTest.java index 4446bbe9c9e..ab868d82e85 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesExistsRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesExistsRequestBuilderTest.java @@ -21,60 +21,45 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyIndicesExistsRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void exists() { - try { - searchClient.prepareIndicesExist(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES indices exists request on indices 'rules'"); - } + assertThat(esTester.client().prepareIndicesExist(FakeIndexDefinition.INDEX).get().isExists()).isTrue(); + assertThat(esTester.client().prepareIndicesExist("unknown").get().isExists()).isFalse(); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareIndicesExist(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES indices exists request on indices 'rules'"); - } + esTester.client().prepareIndicesExist(FakeIndexDefinition.INDEX).get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_exists() throws Exception { try { - searchClient.prepareIndicesExist().get(); + esTester.client().prepareIndicesExist().get(); // expected to fail because elasticsearch is not correctly configured, but that does not matter fail(); @@ -86,13 +71,13 @@ public class ProxyIndicesExistsRequestBuilderTest { @Test public void to_string() { - assertThat(searchClient.prepareIndicesExist(IndexDefinition.RULE.getIndexName()).toString()).isEqualTo("ES indices exists request on indices 'rules'"); + assertThat(esTester.client().prepareIndicesExist(FakeIndexDefinition.INDEX).toString()).isEqualTo("ES indices exists request on indices 'fakes'"); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareIndicesExist().get("1"); + esTester.client().prepareIndicesExist().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -102,7 +87,7 @@ public class ProxyIndicesExistsRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareIndicesExist().get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareIndicesExist().get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -112,7 +97,7 @@ public class ProxyIndicesExistsRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareIndicesExist().execute(); + esTester.client().prepareIndicesExist().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesStatsRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesStatsRequestBuilderTest.java index 65110596a1a..6d0bfe6cdc4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesStatsRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesStatsRequestBuilderTest.java @@ -21,66 +21,50 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyIndicesStatsRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void stats() { - try { - searchClient.prepareStats(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES indices stats request on indices 'rules'"); - } + esTester.client().prepareStats(FakeIndexDefinition.INDEX).get(); } @Test public void to_string() { - assertThat(searchClient.prepareStats(IndexDefinition.RULE.getIndexName()).setIndices("rules").toString()).isEqualTo("ES indices stats request on indices 'rules'"); - assertThat(searchClient.prepareStats().toString()).isEqualTo("ES indices stats request"); + assertThat(esTester.client().prepareStats(FakeIndexDefinition.INDEX).setIndices("rules").toString()).isEqualTo("ES indices stats request on indices 'rules'"); + assertThat(esTester.client().prepareStats().toString()).isEqualTo("ES indices stats request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareStats(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (IllegalStateException e) { - assertThat(e.getMessage()).isEqualTo("Fail to execute ES indices stats request on indices 'rules'"); - } + esTester.client().prepareStats(FakeIndexDefinition.INDEX).get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_stats() throws Exception { try { - searchClient.prepareStats("unknown").get(); + esTester.client().prepareStats("unknown").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -91,7 +75,7 @@ public class ProxyIndicesStatsRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareStats(IndexDefinition.RULE.getIndexName()).get("1"); + esTester.client().prepareStats(FakeIndexDefinition.INDEX).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -101,7 +85,7 @@ public class ProxyIndicesStatsRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareStats(IndexDefinition.RULE.getIndexName()).get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareStats(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -111,7 +95,7 @@ public class ProxyIndicesStatsRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareStats(IndexDefinition.RULE.getIndexName()).execute(); + esTester.client().prepareStats(FakeIndexDefinition.INDEX).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyMultiGetRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyMultiGetRequestBuilderTest.java index eba934555b4..7a29e3bb51e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyMultiGetRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyMultiGetRequestBuilderTest.java @@ -24,77 +24,60 @@ import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.get.MultiGetRequestBuilder; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.search.fetch.source.FetchSourceContext; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyMultiGetRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void multi_get() { - try { - MultiGetRequestBuilder request = searchClient.prepareMultiGet(); - request.add(new MultiGetRequest.Item(IndexDefinition.RULE.getIndexName(), IndexDefinition.RULE.getIndexType(), "ruleKey") - .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)); - request.get(); + MultiGetRequestBuilder request = esTester.client().prepareMultiGet(); + request.add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "ruleKey") + .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)); + request.get(); - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES multi get request"); - } } @Test public void to_string() { - assertThat(searchClient.prepareMultiGet().toString()).isEqualTo("ES multi get request"); - assertThat(searchClient.prepareMultiGet().add(new MultiGetRequest.Item(IndexDefinition.RULE.getIndexName(), null, "ruleKey") - .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)).toString()).isEqualTo("ES multi get request [key 'ruleKey', index 'rules'],"); - assertThat(searchClient.prepareMultiGet().add(new MultiGetRequest.Item(IndexDefinition.RULE.getIndexName(), IndexDefinition.RULE.getIndexType(), "ruleKey") - .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)).toString()).isEqualTo("ES multi get request [key 'ruleKey', index 'rules', type 'rule'],"); + assertThat(esTester.client().prepareMultiGet().toString()).isEqualTo("ES multi get request"); + assertThat(esTester.client().prepareMultiGet().add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, null, "fake1") + .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)).toString()).isEqualTo("ES multi get request [key 'fake1', index 'fakes'],"); + assertThat(esTester.client().prepareMultiGet().add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "fake1") + .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)).toString()).isEqualTo("ES multi get request [key 'fake1', index 'fakes', type 'fake'],"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - MultiGetRequestBuilder request = searchClient.prepareMultiGet(); - request.add(new MultiGetRequest.Item(IndexDefinition.RULE.getIndexName(), IndexDefinition.RULE.getIndexType(), "ruleKey") - .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)); - request.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES multi get request"); - } + MultiGetRequestBuilder request = esTester.client().prepareMultiGet(); + request.add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "ruleKey") + .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)); + request.get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareMultiGet().get("1"); + esTester.client().prepareMultiGet().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -104,7 +87,7 @@ public class ProxyMultiGetRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareMultiGet().get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareMultiGet().get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -114,7 +97,7 @@ public class ProxyMultiGetRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareMultiGet().execute(); + esTester.client().prepareMultiGet().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyNodesStatsRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyNodesStatsRequestBuilderTest.java index e4c89d4fb7b..e40abca7b13 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyNodesStatsRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyNodesStatsRequestBuilderTest.java @@ -21,68 +21,50 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyNodesStatsRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester(); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void stats() { - try { - searchClient.prepareNodesStats().get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES nodes stats request"); - } + esTester.client().prepareNodesStats().get(); } @Test public void to_string() { - assertThat(searchClient.prepareNodesStats().setNodesIds("node1").toString()).isEqualTo("ES nodes stats request on nodes 'node1'"); - assertThat(searchClient.prepareNodesStats().toString()).isEqualTo("ES nodes stats request"); + assertThat(esTester.client().prepareNodesStats().setNodesIds("node1").toString()).isEqualTo("ES nodes stats request on nodes 'node1'"); + assertThat(esTester.client().prepareNodesStats().toString()).isEqualTo("ES nodes stats request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareNodesStats().get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES nodes stats request"); - } + esTester.client().prepareNodesStats().get(); // TODO assert profiling - searchClient.stop(); } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareNodesStats(IndexDefinition.RULE.getIndexName()).get("1"); + esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -92,7 +74,7 @@ public class ProxyNodesStatsRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareNodesStats(IndexDefinition.RULE.getIndexName()).get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -102,7 +84,7 @@ public class ProxyNodesStatsRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareNodesStats(IndexDefinition.RULE.getIndexName()).execute(); + esTester.client().prepareNodesStats(FakeIndexDefinition.INDEX).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyPutMappingRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyPutMappingRequestBuilderTest.java index 817b6af5cb0..13f43287fbb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyPutMappingRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyPutMappingRequestBuilderTest.java @@ -23,12 +23,12 @@ package org.sonar.server.es.request; import com.google.common.collect.ImmutableMap; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import java.util.HashMap; import java.util.Map; @@ -38,66 +38,48 @@ import static org.junit.Assert.fail; public class ProxyPutMappingRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void put_mapping() { - try { - PutMappingRequestBuilder requestBuilder = searchClient.preparePutMapping(IndexDefinition.RULE.getIndexName()) - .setType(IndexDefinition.RULE.getIndexType()) - .setIgnoreConflicts(true) - .setSource(mapDomain()); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES put mapping request"); - } + PutMappingRequestBuilder requestBuilder = esTester.client().preparePutMapping(FakeIndexDefinition.INDEX) + .setType(FakeIndexDefinition.TYPE) + .setIgnoreConflicts(true) + .setSource(mapDomain()); + requestBuilder.get(); } @Test public void to_string() { - assertThat(searchClient.preparePutMapping(IndexDefinition.RULE.getIndexName()).setSource(mapDomain()).toString()) - .isEqualTo("ES put mapping request on indices 'rules' with source '{\"dynamic\":false,\"_all\":{\"enabled\":false}}'"); - assertThat(searchClient.preparePutMapping(IndexDefinition.RULE.getIndexName()).setType(IndexDefinition.RULE.getIndexType()).setSource(mapDomain()).toString()) - .isEqualTo("ES put mapping request on indices 'rules' on type 'rule' with source '{\"dynamic\":false,\"_all\":{\"enabled\":false}}'"); + assertThat(esTester.client().preparePutMapping(FakeIndexDefinition.INDEX).setSource(mapDomain()).toString()) + .isEqualTo("ES put mapping request on indices 'fakes' with source '{\"dynamic\":false,\"_all\":{\"enabled\":false}}'"); + assertThat(esTester.client().preparePutMapping(FakeIndexDefinition.INDEX).setType(FakeIndexDefinition.TYPE).setSource(mapDomain()).toString()) + .isEqualTo("ES put mapping request on indices 'fakes' on type 'fake' with source '{\"dynamic\":false,\"_all\":{\"enabled\":false}}'"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - PutMappingRequestBuilder requestBuilder = searchClient.preparePutMapping(IndexDefinition.RULE.getIndexName()) - .setType(IndexDefinition.RULE.getIndexType()) - .setIgnoreConflicts(true) - .setSource(mapDomain()); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES put mapping request"); - } + PutMappingRequestBuilder requestBuilder = esTester.client().preparePutMapping(FakeIndexDefinition.INDEX) + .setType(FakeIndexDefinition.TYPE) + .setIgnoreConflicts(true) + .setSource(mapDomain()); + requestBuilder.get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_on_bad_query() throws Exception { try { - searchClient.preparePutMapping().get(); + esTester.client().preparePutMapping().get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -108,7 +90,7 @@ public class ProxyPutMappingRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.preparePutMapping().get("1"); + esTester.client().preparePutMapping().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -118,7 +100,7 @@ public class ProxyPutMappingRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.preparePutMapping().get(TimeValue.timeValueMinutes(1)); + esTester.client().preparePutMapping().get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -128,7 +110,7 @@ public class ProxyPutMappingRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.preparePutMapping().execute(); + esTester.client().preparePutMapping().execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyRefreshRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyRefreshRequestBuilderTest.java index 0f6cdc487af..87e6379d00f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyRefreshRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyRefreshRequestBuilderTest.java @@ -22,70 +22,52 @@ package org.sonar.server.es.request; import org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxyRefreshRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void refresh() { - try { - RefreshRequestBuilder requestBuilder = searchClient.prepareRefresh(IndexDefinition.RULE.getIndexName()); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES refresh request on indices 'rules'"); - } + RefreshRequestBuilder requestBuilder = esTester.client().prepareRefresh(FakeIndexDefinition.INDEX); + requestBuilder.get(); } @Test public void to_string() { - assertThat(searchClient.prepareRefresh(IndexDefinition.RULE.getIndexName()).toString()).isEqualTo("ES refresh request on indices 'rules'"); - assertThat(searchClient.prepareRefresh().toString()).isEqualTo("ES refresh request"); + assertThat(esTester.client().prepareRefresh(FakeIndexDefinition.INDEX).toString()).isEqualTo("ES refresh request on indices 'fakes'"); + assertThat(esTester.client().prepareRefresh().toString()).isEqualTo("ES refresh request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - RefreshRequestBuilder requestBuilder = searchClient.prepareRefresh(IndexDefinition.RULE.getIndexName()); - requestBuilder.get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES refresh request on indices 'rules'"); - } + RefreshRequestBuilder requestBuilder = esTester.client().prepareRefresh(FakeIndexDefinition.INDEX); + requestBuilder.get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_refresh() throws Exception { try { - searchClient.prepareRefresh("unknown").get(); + esTester.client().prepareRefresh("unknown").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -96,7 +78,7 @@ public class ProxyRefreshRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareRefresh(IndexDefinition.RULE.getIndexName()).get("1"); + esTester.client().prepareRefresh(FakeIndexDefinition.INDEX).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -106,7 +88,7 @@ public class ProxyRefreshRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareRefresh(IndexDefinition.RULE.getIndexName()).get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareRefresh(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -116,7 +98,7 @@ public class ProxyRefreshRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareRefresh(IndexDefinition.RULE.getIndexName()).execute(); + esTester.client().prepareRefresh(FakeIndexDefinition.INDEX).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchRequestBuilderTest.java index 202c9a60f34..8c033b6da87 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchRequestBuilderTest.java @@ -21,81 +21,63 @@ package org.sonar.server.es.request; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.IndexDefinition; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxySearchRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void search() { - try { - searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES search request '{").contains("}' on indices '[rules]'"); - } + esTester.client().prepareSearch(FakeIndexDefinition.INDEX).get(); } @Test public void to_string() { - assertThat(searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).setTypes("rule").toString()).contains("ES search request '").contains( - "' on indices '[rules]' on types '[rule]'"); - assertThat(searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).toString()).contains("ES search request '").contains("' on indices '[rules]'"); - assertThat(searchClient.prepareSearch().toString()).contains("ES search request"); + assertThat(esTester.client().prepareSearch(FakeIndexDefinition.INDEX).setTypes(FakeIndexDefinition.TYPE).toString()).contains("ES search request '").contains( + "' on indices '[fakes]' on types '[fake]'"); + assertThat(esTester.client().prepareSearch(FakeIndexDefinition.INDEX).toString()).contains("ES search request '").contains("' on indices '[fakes]'"); + assertThat(esTester.client().prepareSearch().toString()).contains("ES search request"); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - try { - searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES search request '{").contains("}' on indices '[rules]'"); - } + esTester.client().prepareSearch(FakeIndexDefinition.INDEX).get(); // TODO assert profiling - searchClient.stop(); } @Test public void fail_to_search_bad_query() throws Exception { try { - searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).setQuery("bad query").get(); + esTester.client().prepareSearch(FakeIndexDefinition.INDEX).setQuery("bad query").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES search request '{").contains("}' on indices '[rules]'"); + assertThat(e.getMessage()).contains("Fail to execute ES search request '{").contains("}' on indices '[fakes]'"); } } @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).get("1"); + esTester.client().prepareSearch(FakeIndexDefinition.INDEX).get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -105,7 +87,7 @@ public class ProxySearchRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareSearch(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -115,7 +97,7 @@ public class ProxySearchRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareSearch(IndexDefinition.RULE.getIndexName()).execute(); + esTester.client().prepareSearch(FakeIndexDefinition.INDEX).execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchScrollRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchScrollRequestBuilderTest.java index 87a83c1adcf..496139b38b8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchScrollRequestBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchScrollRequestBuilderTest.java @@ -20,59 +20,55 @@ package org.sonar.server.es.request; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.common.unit.TimeValue; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; -import org.sonar.api.config.Settings; import org.sonar.core.profiling.Profiling; -import org.sonar.server.search.SearchClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.es.FakeIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class ProxySearchScrollRequestBuilderTest { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.NONE.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition()); - @After - public void tearDown() throws Exception { - searchClient.stop(); + @Before + public void setUp() throws Exception { + esTester.setProfilingLevel(Profiling.Level.NONE); } @Test public void search_scroll() { - try { - searchClient.prepareSearchScroll("scrollId").get(); - - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES search scroll request for scroll id 'null'"); - } + SearchResponse response = esTester.client().prepareSearch(FakeIndexDefinition.INDEX) + .setSearchType(SearchType.SCAN) + .setScroll(TimeValue.timeValueMinutes(1)) + .get(); + esTester.client().prepareSearchScroll(response.getScrollId()).get(); } @Test - public void with_profiling_basic() { - Profiling profiling = new Profiling(new Settings().setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.BASIC.name())); - SearchClient searchClient = new SearchClient(new Settings(), profiling); - try { - searchClient.prepareSearchScroll("scrollId").get(); + public void with_profiling_full() { + esTester.setProfilingLevel(Profiling.Level.FULL); - // expected to fail because elasticsearch is not correctly configured, but that does not matter - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalStateException.class); - assertThat(e.getMessage()).contains("Fail to execute ES search scroll request for scroll id 'null'"); - } - searchClient.stop(); + SearchResponse response = esTester.client().prepareSearch(FakeIndexDefinition.INDEX) + .setSearchType(SearchType.SCAN) + .setScroll(TimeValue.timeValueMinutes(1)) + .get(); + esTester.client().prepareSearchScroll(response.getScrollId()).get(); + + // TODO assert profiling } @Test public void fail_to_search_bad_query() throws Exception { try { - searchClient.prepareSearchScroll("unknown").get(); + esTester.client().prepareSearchScroll("unknown").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -83,7 +79,7 @@ public class ProxySearchScrollRequestBuilderTest { @Test public void get_with_string_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareSearchScroll("scrollId").get("1"); + esTester.client().prepareSearchScroll("scrollId").get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -93,7 +89,7 @@ public class ProxySearchScrollRequestBuilderTest { @Test public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception { try { - searchClient.prepareSearchScroll("scrollId").get(TimeValue.timeValueMinutes(1)); + esTester.client().prepareSearchScroll("scrollId").get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); @@ -103,7 +99,7 @@ public class ProxySearchScrollRequestBuilderTest { @Test public void execute_should_throw_an_unsupported_operation_exception() throws Exception { try { - searchClient.prepareSearchScroll("scrollId").execute(); + esTester.client().prepareSearchScroll("scrollId").execute(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous");