]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 - Added QProfileKey filter
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 20 Jun 2014 10:48:18 +0000 (12:48 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 20 Jun 2014 10:48:31 +0000 (12:48 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java

index f511f5141b01837a1a5618d515969417775c342d..64dedfbc402552ba5ce4e42fa3cb8327dc1e934c 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.collect.Multimap;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.index.query.FilterBuilders;
 import org.elasticsearch.index.query.OrFilterBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.SearchHit;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.rule.RuleKey;
@@ -46,7 +47,6 @@ import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -284,7 +284,8 @@ public class QProfileService implements ServerComponent {
     try {
       OrFilterBuilder activityFilter = FilterBuilders.orFilter();
       for (String profileKey : query.getQprofileKeys()) {
-        activityFilter.add(FilterBuilders.termFilter("details.profileKey", profileKey));
+        activityFilter.add(FilterBuilders.nestedFilter("details",
+          QueryBuilders.matchQuery("details.profileKey", profileKey)));
       }
 
       SearchResponse response = index.get(ActivityIndex.class).search(query, options, activityFilter);
index f121b994a198761408db1313af05913852e3de7b..6460aca50f3c464dfa4cc978ad0c2df3f854c0a0 100644 (file)
@@ -293,6 +293,7 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
   protected Map mapNestedField(IndexField field) {
     Map<String, Object> mapping = new HashMap<String, Object>();
     mapping.put("type", "nested");
+    mapping.put("dynamic", "true");
     Map<String, Object> mappings = new HashMap<String, Object>();
     for (IndexField nestedField : field.nestedFields()) {
       if (nestedField != null) {
index 2f5ec2828771eb7694ca33b360b47fe11bd35a26..d1a38a0ddf8ca4ffe5530f9cd77518b70055b9b4 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Multimap;
 import org.junit.After;
 import org.junit.Before;
@@ -181,4 +182,28 @@ public class QProfileServiceMediumTest {
     assertThat(activity.ruleName()).isNull();
   }
 
+  @Test
+  public void search_activity_by_qprofile() throws InterruptedException {
+
+    tester.get(ActivityService.class).write(dbSession, Activity.Type.QPROFILE,
+      ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)));
+    tester.get(ActivityService.class).write(dbSession, Activity.Type.QPROFILE,
+      ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P2_KEY, RuleTesting.XOO_X1)));
+    dbSession.commit();
+
+    // 0. Base case verify 2 activities in index
+    assertThat(service.findActivities(new QProfileActivityQuery(), new QueryOptions()))
+      .hasSize(2);
+
+    // 1. filter by QProfile
+    List<QProfileActivity> result = service.findActivities(new QProfileActivityQuery()
+      .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY)), new QueryOptions());
+    assertThat(result).hasSize(1);
+
+    // 1. filter by QProfiles
+    assertThat(service.findActivities(new QProfileActivityQuery()
+      .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY, XOO_P2_KEY))
+      , new QueryOptions())).hasSize(2);
+  }
+
 }