]> source.dussan.org Git - sonarqube.git/commitdiff
Use EsTester in all ProxyXXXTest classes
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 13 Feb 2015 08:57:55 +0000 (09:57 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 13 Feb 2015 14:55:43 +0000 (15:55 +0100)
20 files changed:
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyBulkRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClearCacheRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterHealthRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStateRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyClusterStatsRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCountRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyCreateIndexRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteByQueryRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyDeleteRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyFlushRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyGetRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndexRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesExistsRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndicesStatsRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyMultiGetRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyNodesStatsRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyPutMappingRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyRefreshRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchScrollRequestBuilderTest.java

index d56df24ef34fec610cbdeabf4b97f4168d007f9b..f96e451dd0b6f80ba33a781bb044e67e3918fd29 100644 (file)
@@ -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
   }
 
index 6b31392269c4e2f0ddd1affab784a001827cad75..63909a98c4efc4792843d4961c9c69d19ed3edbb 100644 (file)
@@ -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
index 6abbd47e057a1277f3d18be6517584f27e7d85c1..3e2d2cd59e70da9c91d02c31230d6f4f1feb056b 100644 (file)
@@ -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();
index 6f220a693f83ad848e63f6d26969287d07de449b..d33e2258ebfba2a60a8b3797b132aace155cca34 100644 (file)
@@ -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");
index 937abf83ba5309c1008edf01f702a89593314ce5..d20f6d777173a28b78ab23480178c27fa3274eca 100644 (file)
@@ -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");
index 01f73380d16cbf0657073e54cf2840363f9fe24a..2defc836a787cd83334c3c5b7d1ab4f7586ef1f8 100644 (file)
 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");
index fbf7338e9519ab12a30e0bcc3f07dcaf82582756..6f749f26a1c170c1cef98eeef4cba1704ea3e15e 100644 (file)
@@ -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());
+  }
+
 }
index 2c81fff5231c8f620c8a6ac4a126909c2a9d4724..b43698ea88271fe2418cc80558cb1e3b3118cb9a 100644 (file)
@@ -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");
index f475542b0915ae9d0efbc78a27540692de1f573d..e8bcca0b7dfe08cd6e621576aaecc7f9e4527c2f 100644 (file)
 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");
index 489915668408c6f82db4adbae6a37858c86161ad..cd0ee9935a75bad0dd18bf65b1544bdd893a2387 100644 (file)
 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");
index c7307039880ae737f3ebe97078689396f9d9812e..66d4ea9bd05255ce1dbf5831ffd7f0054c1b7c9b 100644 (file)
@@ -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");
index d9c0527d7921d1595a2f1e101e4929ab7718aab9..236dab71931a0745fc460f6f778bacb0791e778a 100644 (file)
@@ -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();
index 4446bbe9c9e178ee6b65019972608c7e0daf17bb..ab868d82e85f1f6d1bcf9d5343b737eda61d8e61 100644 (file)
 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");
index 65110596a1a2fb19da92e3d56a3806e3f4dd781e..6d0bfe6cdc41c1d8725adc9aea7c58718f5b0fcc 100644 (file)
 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");
index eba934555b4b21316e58f8b2aaeb9d822680af34..7a29e3bb51ea46caf3d0ae589f71f4b6ac089ff5 100644 (file)
@@ -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");
index e4c89d4fb7beb109921ba70f0aa34a51450ee06f..e40abca7b13f6a62d8ff90d8fedaa1a2d8dfd333 100644 (file)
 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");
index 817b6af5cb0f5edc3f1fb00161a3bbded92f10d8..13f43287fbb397aeec2d43cfffb303dc20a32a8f 100644 (file)
@@ -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");
index 0f6cdc487af541459e702fa4e74043e63c5e4f57..87e6379d00f5e2e374a86d3c89f4dbd5c13a522d 100644 (file)
@@ -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");
index 202c9a60f34c548f52b72af14a247129f75b5802..8c033b6da8703343566e24f1b605e031fdd22e66 100644 (file)
 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");
index 87a83c1adcf64ac937322e77a8ba19bf21f822bb..496139b38b83a0fd32965fe664d096cbc6c82a6c 100644 (file)
 
 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");