Remove the method EsTester#putDocuments(String index, String type, Map<String, Object>... docs)
import org.junit.Rule;
import org.junit.Test;
-import java.util.Map;
-
import static org.assertj.core.api.Assertions.assertThat;
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);
assertThat(count()).isEqualTo(2);
}
-
private long count() {
return esTester.countDocuments("fakes", "fake");
}
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();
}
--- /dev/null
+/*
+ * 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;
+ }
+}
*/
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";
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);
}
}
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]");
@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();
}
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);
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";
.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);
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);
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";
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);
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);
@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));
@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")
@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")
@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(
@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);