]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7613 EsTester must index without doc id
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 23 May 2016 15:57:20 +0000 (17:57 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 24 May 2016 21:12:58 +0000 (23:12 +0200)
Remove the method EsTester#putDocuments(String index, String type, Map<String, Object>... docs)

server/sonar-server/src/test/java/org/sonar/server/es/BulkIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/es/EsTester.java
server/sonar-server/src/test/java/org/sonar/server/es/FakeDoc.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/es/FakeIndexDefinition.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyBulkRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxyIndexRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexDefinitionTest.java
server/sonar-server/src/test/java/org/sonar/server/search/FacetsMediumTest.java

index 0554699ecdeb33b840197c2e616263da4b0e67d6..3616ff99dd3182c44f139331b2e9c4c5420defaf 100644 (file)
@@ -29,8 +29,6 @@ import org.elasticsearch.index.query.QueryBuilders;
 import org.junit.Rule;
 import org.junit.Test;
 
-import java.util.Map;
-
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class BulkIndexerTest {
@@ -92,9 +90,9 @@ public class BulkIndexerTest {
   public void bulk_delete() throws Exception {
     int max = 500;
     int removeFrom = 200;
-    Map[] docs = new Map[max];
+    FakeDoc[] docs = new FakeDoc[max];
     for (int i = 0; i < max; i++) {
-      docs[i] = ImmutableMap.of(FakeIndexDefinition.INT_FIELD, i);
+      docs[i] = FakeIndexDefinition.newDoc(i);
     }
     esTester.putDocuments(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, docs);
     assertThat(count()).isEqualTo(max);
@@ -124,7 +122,6 @@ public class BulkIndexerTest {
     assertThat(count()).isEqualTo(2);
   }
 
-
   private long count() {
     return esTester.countDocuments("fakes", "fake");
   }
index 559206a559ffbd5be4c203e234cb1b22c7ad24a5..5961d334abe8dc99e0cdbc4313423ec296ced978 100644 (file)
@@ -135,16 +135,7 @@ public class EsTester extends ExternalResource {
     assertThat(response.hasFailures()).as(response.buildFailureMessage()).isFalse();
   }
 
-  public void putDocuments(String index, String type, Map<String, Object>... docs) throws Exception {
-    BulkRequestBuilder bulk = client.prepareBulk().setRefresh(true);
-    for (Map<String, Object> doc : docs) {
-      bulk.add(new IndexRequest(index, type).source(doc));
-    }
-    BulkResponse response = bulk.get();
-    assertThat(response.hasFailures()).as(response.buildFailureMessage()).isFalse();
-  }
-
-  public void index(String indexName, String typeName, String id, Map<String,Object> source) {
+  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();
   }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/FakeDoc.java b/server/sonar-server/src/test/java/org/sonar/server/es/FakeDoc.java
new file mode 100644 (file)
index 0000000..734c997
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.es;
+
+import com.google.common.collect.Maps;
+import java.util.Map;
+
+import static org.sonar.server.es.FakeIndexDefinition.INT_FIELD;
+
+public class FakeDoc extends BaseDoc {
+  public FakeDoc(Map<String, Object> fields) {
+    super(fields);
+  }
+
+  public FakeDoc() {
+    super(Maps.<String, Object>newHashMap());
+  }
+
+  public int getInt() {
+    return getField(INT_FIELD);
+  }
+
+  public FakeDoc setInt(int i) {
+    setField(INT_FIELD, i);
+    return this;
+  }
+}
index a90f527dcfff5670864f10050c6f050c815f6ea8..cbd9bd4c0013eb9cd75d0de974c911feca5e66f4 100644 (file)
  */
 package org.sonar.server.es;
 
-import com.google.common.collect.ImmutableMap;
 import org.elasticsearch.cluster.metadata.IndexMetaData;
 
-import java.util.Map;
-
 public class FakeIndexDefinition implements IndexDefinition {
 
   public static final String INDEX = "fakes";
@@ -46,7 +43,7 @@ public class FakeIndexDefinition implements IndexDefinition {
     type.createIntegerField(INT_FIELD);
   }
 
-  public static Map<String,Object> newDoc(int value) {
-    return ImmutableMap.<String,Object>of(INT_FIELD, value);
+  public static FakeDoc newDoc(int value) {
+    return new FakeDoc().setInt(value);
   }
 }
index e27e7606732941b88209bd29c298aa18e4748043..5f958488d9872f20f91d7e71e4b9865559596892 100644 (file)
@@ -60,10 +60,10 @@ public class ProxyBulkRequestBuilderTest {
   private void testBulk() {
     BulkRequestBuilder req = esTester.client().prepareBulk();
     req.add(new UpdateRequest(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "key1")
-      .doc(FakeIndexDefinition.newDoc(1)));
+      .doc(FakeIndexDefinition.newDoc(1).getFields()));
     req.add(new DeleteRequest(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "key2"));
     req.add(new IndexRequest(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "key3")
-      .source(FakeIndexDefinition.newDoc(3)));
+      .source(FakeIndexDefinition.newDoc(3).getFields()));
 
     assertThat(req.toString()).isEqualTo(
       "Bulk[1 update request(s) on index fakes and type fake, 1 delete request(s) on index fakes and type fake, 1 index request(s) on index fakes and type fake]");
index 263eb1ebbd03e51aff6ed03342864ab1813885d3..2e29c37abc466d440074dfbd7245136ceccf55a1 100644 (file)
@@ -44,7 +44,7 @@ public class ProxyIndexRequestBuilderTest {
   @Test
   public void index_with_index_type_and_id() {
     IndexResponse response = esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE)
-      .setSource(FakeIndexDefinition.newDoc(42))
+      .setSource(FakeIndexDefinition.newDoc(42).getFields())
       .get();
     assertThat(response.isCreated()).isTrue();
   }
@@ -53,7 +53,7 @@ public class ProxyIndexRequestBuilderTest {
   public void trace_logs() {
     logTester.setLevel(LoggerLevel.TRACE);
     IndexResponse response = esTester.client().prepareIndex(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE)
-      .setSource(FakeIndexDefinition.newDoc(42))
+      .setSource(FakeIndexDefinition.newDoc(42).getFields())
       .get();
     assertThat(response.isCreated()).isTrue();
     assertThat(logTester.logs()).hasSize(1);
index f617522b853b951f8d7c54c5d64176ed33804db1..eec29c730fe0e9bb975db344b728593a9ad2569b 100644 (file)
@@ -62,10 +62,12 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.server.issue.index.IssueIndexDefinition.FIELD_AUTHORIZATION_PROJECT_UUID;
+import static org.sonar.server.issue.index.IssueIndexDefinition.TYPE_AUTHORIZATION;
+import static org.sonar.server.issue.index.IssueIndexDefinition.TYPE_ISSUE;
 import static org.sonar.server.project.ws.BulkDeleteAction.PARAM_IDS;
 import static org.sonar.server.project.ws.BulkDeleteAction.PARAM_KEYS;
 
-
 public class BulkDeleteActionTest {
 
   private static final String ACTION = "bulk_delete";
@@ -153,9 +155,9 @@ public class BulkDeleteActionTest {
       .setParam(PARAM_KEYS, "project-key-1, project-key-3, project-key-4").execute();
 
     String remainingProjectUuid = "project-uuid-2";
-    assertThat(es.getDocumentFieldValues(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_ISSUE, IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID))
+    assertThat(es.getDocumentFieldValues(IssueIndexDefinition.INDEX, TYPE_ISSUE, IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID))
       .containsOnly(remainingProjectUuid);
-    assertThat(es.getDocumentFieldValues(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_AUTHORIZATION, IssueIndexDefinition.FIELD_AUTHORIZATION_PROJECT_UUID))
+    assertThat(es.getDocumentFieldValues(IssueIndexDefinition.INDEX, TYPE_AUTHORIZATION, FIELD_AUTHORIZATION_PROJECT_UUID))
       .containsOnly(remainingProjectUuid);
     assertThat(es.getDocumentFieldValues(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, TestIndexDefinition.FIELD_PROJECT_UUID))
       .containsOnly(remainingProjectUuid);
@@ -221,9 +223,8 @@ public class BulkDeleteActionTest {
     dbClient.componentDao().insert(dbSession, project);
     dbSession.commit();
 
-    es.putDocuments(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_ISSUE, IssueTesting.newDoc("issue-key-" + suffix, project));
-    es.putDocuments(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_AUTHORIZATION,
-      ImmutableMap.<String, Object>of(IssueIndexDefinition.FIELD_AUTHORIZATION_PROJECT_UUID, project.uuid()));
+    es.putDocuments(IssueIndexDefinition.INDEX, TYPE_ISSUE, IssueTesting.newDoc("issue-key-" + suffix, project));
+    es.index(IssueIndexDefinition.INDEX, TYPE_AUTHORIZATION, project.uuid(), ImmutableMap.<String, Object>of(FIELD_AUTHORIZATION_PROJECT_UUID, project.uuid()));
 
     TestDoc testDoc = new TestDoc().setUuid("test-uuid-" + suffix).setProjectUuid(project.uuid()).setFileUuid(project.uuid());
     es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, testDoc);
index 8ca823216c7a2c5497f1be0a6e7b05d2be2b6c6c..9642a3b5daf784fd3e9450c0b1735a872f6e7baa 100644 (file)
@@ -64,7 +64,6 @@ import static org.mockito.Mockito.when;
 import static org.sonar.server.project.ws.DeleteAction.PARAM_ID;
 import static org.sonar.server.project.ws.DeleteAction.PARAM_KEY;
 
-
 public class DeleteActionTest {
 
   private static final String ACTION = "delete";
@@ -244,8 +243,7 @@ public class DeleteActionTest {
     dbSession.commit();
 
     es.putDocuments(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_ISSUE, IssueTesting.newDoc("issue-key-" + suffix, project));
-    es.putDocuments(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_AUTHORIZATION,
-      ImmutableMap.<String, Object>of(IssueIndexDefinition.FIELD_AUTHORIZATION_PROJECT_UUID, project.uuid()));
+    es.index(IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_AUTHORIZATION, project.uuid(), ImmutableMap.<String, Object>of(IssueIndexDefinition.FIELD_AUTHORIZATION_PROJECT_UUID, project.uuid()));
 
     TestDoc testDoc = new TestDoc().setUuid("test-uuid-" + suffix).setProjectUuid(project.uuid()).setFileUuid(project.uuid());
     es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, testDoc);
index 6810d5f073aff8c4dbb55ea5eeaa55e0d08c965c..6aa48078555a555ff7d3e99d4ce892c8597692eb 100644 (file)
@@ -74,10 +74,10 @@ public class RuleIndexDefinitionTest {
   public void support_long_html_description() throws Exception {
     String longText = StringUtils.repeat("hello  ", 10_000);
     // the following method fails if PUT fails
-    tester.putDocuments(INDEX, RuleIndexDefinition.TYPE_RULE, ImmutableMap.<String, Object>of(
+    tester.putDocuments(INDEX, RuleIndexDefinition.TYPE_RULE, new RuleDoc(ImmutableMap.<String, Object>of(
       FIELD_RULE_HTML_DESCRIPTION, longText,
       FIELD_RULE_REPOSITORY, "squid",
-      FIELD_RULE_KEY, "S001"));
+      FIELD_RULE_KEY, "S001")));
     assertThat(tester.countDocuments(INDEX, RuleIndexDefinition.TYPE_RULE)).isEqualTo(1);
 
     List<AnalyzeResponse.AnalyzeToken> tokens = analyzeIndexedTokens(longText);
index f6c5163cc87fd8ae746cb118e94555f531ef9256..d4b12a50a31d0b1c4faf40504e1fd80f63f8cc18 100644 (file)
@@ -63,12 +63,11 @@ public class FacetsMediumTest {
 
   @Test
   public void should_ignore_unknown_aggregation_type() throws Exception {
-    esTester.putDocuments(INDEX, TYPE,
-      newTagsDocument("noTags"),
-      newTagsDocument("oneTag", "tag1"),
-      newTagsDocument("twoTags", "tag1", "tag2"),
-      newTagsDocument("twoTags", "tag1", "tag2", "tag3"),
-      newTagsDocument("twoTags", "tag1", "tag2", "tag3", "tag4"));
+    esTester.index(INDEX, TYPE, "noTags", newTagsDocument("noTags"));
+    esTester.index(INDEX, TYPE, "oneTag", newTagsDocument("oneTag", "tag1"));
+    esTester.index(INDEX, TYPE, "twoTags", newTagsDocument("twoTags", "tag1", "tag2"));
+    esTester.index(INDEX, TYPE, "threeTags", newTagsDocument("threeTags", "tag1", "tag2", "tag3"));
+    esTester.index(INDEX, TYPE, "fourTags", newTagsDocument("fourTags", "tag1", "tag2", "tag3", "tag4"));
     SearchRequestBuilder search = esTester.client().prepareSearch(INDEX).setTypes(TYPE)
       .addAggregation(AggregationBuilders.cardinality(FIELD_TAGS).field(FIELD_TAGS));
 
@@ -79,12 +78,9 @@ public class FacetsMediumTest {
 
   @Test
   public void should_process_result_with_nested_missing_and_terms_aggregations() throws Exception {
-    esTester.putDocuments(INDEX, TYPE,
-      newTagsDocument("noTags"),
-      newTagsDocument("oneTag", "tag1"),
-      newTagsDocument("twoTags", "tag1", "tag2"),
-      newTagsDocument("twoTags", "tag1", "tag2", "tag3"),
-      newTagsDocument("twoTags", "tag1", "tag2", "tag3", "tag4"));
+    esTester.index(INDEX, TYPE, "noTags", newTagsDocument("noTags"));
+    esTester.index(INDEX, TYPE, "oneTag", newTagsDocument("oneTag", "tag1"));
+    esTester.index(INDEX, TYPE, "fourTags", newTagsDocument("fourTags", "tag1", "tag2", "tag3", "tag4"));
 
     SearchRequestBuilder search = esTester.client().prepareSearch(INDEX).setTypes(TYPE)
       .addAggregation(AggregationBuilders.global("tags__global")
@@ -108,11 +104,10 @@ public class FacetsMediumTest {
 
   @Test
   public void should_ignore_empty_missing_aggregation() throws Exception {
-    esTester.putDocuments(INDEX, TYPE,
-      newTagsDocument("oneTag", "tag1"),
-      newTagsDocument("twoTags", "tag1", "tag2"),
-      newTagsDocument("twoTags", "tag1", "tag2", "tag3"),
-      newTagsDocument("twoTags", "tag1", "tag2", "tag3", "tag4"));
+    esTester.index(INDEX, TYPE, "oneTag", newTagsDocument("oneTag", "tag1"));
+    esTester.index(INDEX, TYPE, "twoTags", newTagsDocument("twoTags", "tag1", "tag2"));
+    esTester.index(INDEX, TYPE, "threeTags", newTagsDocument("threeTags", "tag1", "tag2", "tag3"));
+    esTester.index(INDEX, TYPE, "fourTags", newTagsDocument("fourTags", "tag1", "tag2", "tag3", "tag4"));
 
     SearchRequestBuilder search = esTester.client().prepareSearch(INDEX).setTypes(TYPE)
       .addAggregation(AggregationBuilders.global("tags__global")
@@ -129,8 +124,9 @@ public class FacetsMediumTest {
 
   @Test
   public void should_process_result_with_date_histogram() throws Exception {
-    esTester.putDocuments(INDEX, TYPE,
-      newTagsDocument("first"), newTagsDocument("second"), newTagsDocument("third"));
+    esTester.index(INDEX, TYPE, "first", newTagsDocument("first"));
+    esTester.index(INDEX, TYPE, "second", newTagsDocument("second"));
+    esTester.index(INDEX, TYPE, "third", newTagsDocument("third"));
 
     SearchRequestBuilder search = esTester.client().prepareSearch(INDEX).setTypes(TYPE)
       .addAggregation(
@@ -162,7 +158,6 @@ public class FacetsMediumTest {
     @Override
     public void define(IndexDefinitionContext context) {
       NewIndexType newType = context.create(INDEX).createType(TYPE);
-      newType.setAttribute("_id", ImmutableMap.of("path", FIELD_KEY));
       newType.stringFieldBuilder(FIELD_KEY).build();
       newType.stringFieldBuilder(FIELD_TAGS).build();
       newType.createDateTimeField(FIELD_CREATED_AT);