]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7613 refactor ViewIndexTest
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 23 May 2016 15:22:02 +0000 (17:22 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 24 May 2016 21:12:58 +0000 (23:12 +0200)
to prepare removal of _id path (required for ES 2.3)

server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java
server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java
server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view1.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view2.json [deleted file]

index d98d86590deccc6ad212a358898828388ba037f5..559206a559ffbd5be4c203e234cb1b22c7ad24a5 100644 (file)
@@ -24,13 +24,10 @@ import com.google.common.base.Throwables;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Iterables;
-import java.io.File;
-import java.io.FileInputStream;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.Nonnull;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.math.RandomUtils;
 import org.apache.commons.lang.reflect.ConstructorUtils;
 import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
@@ -51,7 +48,6 @@ import org.elasticsearch.search.SearchHit;
 import org.junit.rules.ExternalResource;
 import org.sonar.api.config.Settings;
 import org.sonar.core.platform.ComponentContainer;
-import org.sonar.test.TestUtils;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -130,19 +126,6 @@ public class EsTester extends ExternalResource {
     }
   }
 
-  public void putDocuments(String index, String type, Class<?> testClass, String... jsonPaths) throws Exception {
-    BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
-    for (String path : jsonPaths) {
-      File file = TestUtils.getResource(testClass, path);
-      if (file == null) {
-        throw new IllegalArgumentException(String.format("File '%s' hasn't been found in folder '%s'", path, testClass.getSimpleName()));
-      }
-      bulk.add(new IndexRequest(index, type).source(IOUtils.toString(new FileInputStream(file))));
-    }
-    BulkResponse response = bulk.get();
-    assertThat(response.hasFailures()).as(response.buildFailureMessage()).isFalse();
-  }
-
   public void putDocuments(String index, String type, BaseDoc... docs) throws Exception {
     BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
     for (BaseDoc doc : docs) {
@@ -161,6 +144,11 @@ public class EsTester extends ExternalResource {
     assertThat(response.hasFailures()).as(response.buildFailureMessage()).isFalse();
   }
 
+  public void index(String indexName, String typeName, String id, Map<String,Object> source) {
+    client.prepareIndex(indexName, typeName).setId(id).setSource(source).get();
+    client.prepareRefresh(indexName).get();
+  }
+
   public long countDocuments(String indexName, String typeName) {
     return client().prepareCount(indexName).setTypes(typeName).get().getCount();
   }
index 5b734e07cc6d53bc5f2b3f7d03ae3089ee042567..e57623751559aff45cd5a7c21bf7eebc5e812c57 100644 (file)
  */
 package org.sonar.server.view.index;
 
+import java.util.List;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.server.es.EsTester;
 
-import java.util.List;
-
 import static com.google.common.collect.Lists.newArrayList;
+import static java.util.Arrays.asList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.view.index.ViewIndexDefinition.INDEX;
+import static org.sonar.server.view.index.ViewIndexDefinition.TYPE_VIEW;
 
 public class ViewIndexTest {
 
@@ -45,11 +47,14 @@ public class ViewIndexTest {
 
   @Test
   public void find_all_view_uuids() throws Exception {
-    esTester.putDocuments(ViewIndexDefinition.INDEX, ViewIndexDefinition.TYPE_VIEW, this.getClass(), "view1.json", "view2.json");
+    ViewDoc view1 = new ViewDoc().setUuid("UUID1").setProjects(asList("P1"));
+    ViewDoc view2 = new ViewDoc().setUuid("UUID2").setProjects(asList("P2"));
+    esTester.index(INDEX, TYPE_VIEW, view1.uuid(), view1.getFields());
+    esTester.index(INDEX, TYPE_VIEW, view2.uuid(), view2.getFields());
 
     List<String> result = newArrayList(index.findAllViewUuids());
 
-    assertThat(result).containsOnly("fed0a543-9d9c-4af5-a4ec-450a8fe78ce7", "8d0bc2a5-bfba-464b-92de-bb170e9d978e");
+    assertThat(result).containsOnly(view1.uuid(), view2.uuid());
   }
 
   @Test
@@ -61,11 +66,16 @@ public class ViewIndexTest {
 
   @Test
   public void delete_views() throws Exception {
-    esTester.putDocuments(ViewIndexDefinition.INDEX, ViewIndexDefinition.TYPE_VIEW, this.getClass(), "view1.json", "view2.json");
+    ViewDoc view1 = new ViewDoc().setUuid("UUID1").setProjects(asList("P1"));
+    ViewDoc view2 = new ViewDoc().setUuid("UUID2").setProjects(asList("P2", "P3", "P4"));
+    ViewDoc view3 = new ViewDoc().setUuid("UUID3").setProjects(asList("P2", "P3", "P4"));
+    esTester.index(INDEX, TYPE_VIEW, view1.uuid(), view1.getFields());
+    esTester.index(INDEX, TYPE_VIEW, view2.uuid(), view2.getFields());
+    esTester.index(INDEX, TYPE_VIEW, view3.uuid(), view3.getFields());
 
-    index.delete(newArrayList("fed0a543-9d9c-4af5-a4ec-450a8fe78ce7", "8d0bc2a5-bfba-464b-92de-bb170e9d978e"));
+    index.delete(asList(view1.uuid(), view2.uuid()));
 
-    assertThat(esTester.countDocuments(ViewIndexDefinition.INDEX, ViewIndexDefinition.TYPE_VIEW)).isEqualTo(0L);
+    assertThat(esTester.getDocumentFieldValues(INDEX, TYPE_VIEW, ViewIndexDefinition.FIELD_UUID)).containsOnly(view3.uuid());
   }
 
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view1.json b/server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view1.json
deleted file mode 100644 (file)
index 246e7ad..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-  "uuid": "fed0a543-9d9c-4af5-a4ec-450a8fe78ce7",
-  "projects": [
-    "548415bc-6626-45b1-a99a-ca77aedec45f"
-  ]
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view2.json b/server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view2.json
deleted file mode 100644 (file)
index b884626..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-{
-  "uuid": "8d0bc2a5-bfba-464b-92de-bb170e9d978e",
-  "projects": [
-    "e5dccc4f-431a-46ba-ab55-47318c332af7",
-    "6432a311-4d1f-41fd-b90a-826130f6f890",
-    "bf93ca9b-18f6-4f5d-848b-722ca7bd67d1"
-  ]
-}