aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-05-23 17:22:02 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-05-24 23:12:58 +0200
commitbe5ca7eeb25f49cfb8ed656698e21fd2569c56db (patch)
tree24c05665f57db2419863c578b5ccaba73f5e85fc /server
parent3e2cf4f39b085260c82e9c7765306d6b29ec267f (diff)
downloadsonarqube-be5ca7eeb25f49cfb8ed656698e21fd2569c56db.tar.gz
sonarqube-be5ca7eeb25f49cfb8ed656698e21fd2569c56db.zip
SONAR-7613 refactor ViewIndexTest
to prepare removal of _id path (required for ES 2.3)
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java22
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java24
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view1.json6
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view2.json8
4 files changed, 22 insertions, 38 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java b/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java
index d98d86590de..559206a559f 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java
@@ -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();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java
index 5b734e07cc6..e5762375155 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java
@@ -19,16 +19,18 @@
*/
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
index 246e7ad41a6..00000000000
--- a/server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view1.json
+++ /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
index b88462641d2..00000000000
--- a/server/sonar-server/src/test/resources/org/sonar/server/view/index/ViewIndexTest/view2.json
+++ /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"
- ]
-}