]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4923 Try to get all ids from profile rule query (Not work for the moment)
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 8 Jan 2014 13:44:28 +0000 (14:44 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 8 Jan 2014 13:44:28 +0000 (14:44 +0100)
sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java
sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java
sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java
sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java

index ede48fd0127c9b4e3915ad67266e2405b3487970..0402d82a117fbf4a8c15ad4de2a62249ecce18d6 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.server.rule;
 
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
 import org.apache.commons.lang.StringUtils;
 import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.get.MultiGetItemResponse;
@@ -39,10 +41,13 @@ import org.sonar.server.qualityprofile.QProfileRule;
 import org.sonar.server.qualityprofile.QProfileRuleResult;
 import org.sonar.server.search.SearchIndex;
 
+import javax.annotation.Nullable;
+
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
+import static com.google.common.collect.Lists.newArrayList;
 import static org.elasticsearch.index.query.FilterBuilders.*;
 import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
 import static org.sonar.server.rule.RuleRegistry.*;
@@ -107,6 +112,23 @@ public class ProfileRules implements ServerExtension {
     return new QProfileRuleResult(result, PagingResult.create(paging.pageSize(), paging.pageIndex(), hits.getTotalHits()));
   }
 
+  // FIXME Due to a bug in E/S, As the query filter contain a filter with has_parent, nothing will be returned
+  public List<Integer> searchProfileRuleIds(ProfileRuleQuery query) {
+    BoolFilterBuilder filter = activeRuleFilter(query);
+
+    SearchRequestBuilder builder = index.client()
+      .prepareSearch(INDEX_RULES)
+      .setTypes(TYPE_ACTIVE_RULE)
+      .setFilter(filter);
+    List<String> documentIds = index.findDocumentIds(builder, 2);
+    return newArrayList(Iterables.transform(documentIds, new Function<String, Integer>() {
+      @Override
+      public Integer apply(@Nullable String input) {
+        return Integer.valueOf(input);
+      }
+    }));
+  }
+
   public QProfileRuleResult searchInactiveProfileRules(ProfileRuleQuery query, Paging paging) {
     BoolFilterBuilder filter = parentRuleFilter(query);
     addMustTermOrTerms(filter, RuleDocument.FIELD_SEVERITY, query.severities());
index a1b6af0f0f2862452e2cd5c83a69da9c0cfad6c1..bff6f252c90fcc3f1df127d87965d32ee414d9d7 100644 (file)
@@ -151,7 +151,6 @@ public class SearchIndex {
     }
   }
 
-
   public void addMappingFromClasspath(String index, String type, String resourcePath) {
     try {
       URL resource = getClass().getResource(resourcePath);
@@ -191,15 +190,19 @@ public class SearchIndex {
   }
 
   public List<String> findDocumentIds(SearchQuery searchQuery) {
+    SearchRequestBuilder builder = searchQuery.toBuilder(client);
+    return findDocumentIds(builder, searchQuery.scrollSize());
+  }
+
+  public List<String> findDocumentIds(SearchRequestBuilder builder, int scrollSize) {
     List<String> result = Lists.newArrayList();
     final int scrollTime = 100;
 
-    SearchRequestBuilder builder = searchQuery.toBuilder(client);
     StopWatch watch = createWatch();
     SearchResponse scrollResp = builder.addField("_id")
-            .setSearchType(SearchType.SCAN)
-            .setScroll(new TimeValue(scrollTime))
-            .setSize(searchQuery.scrollSize()).execute().actionGet();
+      .setSearchType(SearchType.SCAN)
+      .setScroll(new TimeValue(scrollTime))
+      .setSize(scrollSize).execute().actionGet();
     //Scroll until no hits are returned
     while (true) {
       scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(scrollTime)).execute().actionGet();
index 6ed7a80107dd6305151faccb6ee87b887a6e747c..8f24536f6da14a9314d9bf4cacf00155c7239cbb 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.commons.io.IOUtils;
 import org.elasticsearch.client.Requests;
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.api.rules.RulePriority;
@@ -78,8 +79,7 @@ public class ProfileRulesTest {
   }
 
   @Test
-  public void should_find_active_rules() throws Exception {
-
+  public void find_profile_rules() {
     Paging paging = Paging.create(10, 1);
 
     // All rules for profile 1
@@ -116,12 +116,20 @@ public class ProfileRulesTest {
   }
 
   @Test
-  public void should_get_from_active_rule() {
+  @Ignore("bug in E/S : fail to do a scroll when filter contain has_parent -> return good total_hits but hits is empty")
+  public void find_profile_rule_ids() {
+    // All rules for profile 1
+    List<Integer> result = profileRules.searchProfileRuleIds(ProfileRuleQuery.create(1));
+    assertThat(result).hasSize(3);
+    assertThat(result.get(0)).isEqualTo(1);  }
+
+  @Test
+  public void get_from_active_rule() {
     assertThat(profileRules.getFromActiveRuleId(391)).isNotNull();
   }
 
   @Test
-  public void should_get_from_rule() {
+  public void get_from_rule() {
     assertThat(profileRules.getFromActiveRuleId(25)).isNotNull();
   }
 
index 7ee79bb5d6871a781a7491cb070513c8fe7dcb61..439635a93583e01fa5ed35d8225a65051bd444bb 100644 (file)
@@ -33,9 +33,7 @@ import org.sonar.core.profiling.Profiling;
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 public class SearchIndexTest {
 
@@ -130,6 +128,23 @@ public class SearchIndexTest {
     assertThat(docIds).hasSize(numberOfDocuments);
   }
 
+  @Test
+  public void should_iterate_over_small_dataset() throws Exception {
+    final int numberOfDocuments = 3;
+
+    searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json");
+    String[] ids = new String[numberOfDocuments];
+    BytesStream[] sources = new BytesStream[numberOfDocuments];
+    for (int i=0; i<numberOfDocuments; i++) {
+      ids[i] = Integer.toString(i);
+      sources[i] = XContentFactory.jsonBuilder().startObject().field("value", Integer.toString(i)).endObject();
+    }
+    searchIndex.bulkIndex("index", "type1", ids, sources);
+
+    List<String> docIds = searchIndex.findDocumentIds(SearchQuery.create().scrollSize(100));
+    assertThat(docIds).hasSize(numberOfDocuments);
+  }
+
   @Test(expected = StrictDynamicMappingException.class)
   public void should_forbid_dynamic_mapping() throws Exception {
     searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json");