From 386ca2fafa3dcff07e85a08ef0970354661dadf4 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Fri, 20 Jun 2014 12:48:18 +0200 Subject: [PATCH] SONAR-5329 - Added QProfileKey filter --- .../qualityprofile/QProfileService.java | 5 ++-- .../org/sonar/server/search/BaseIndex.java | 1 + .../QProfileServiceMediumTest.java | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java index f511f5141b0..64dedfbc402 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java @@ -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); diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index f121b994a19..6460aca50f3 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -293,6 +293,7 @@ public abstract class BaseIndex, KEY extends Serial protected Map mapNestedField(IndexField field) { Map mapping = new HashMap(); mapping.put("type", "nested"); + mapping.put("dynamic", "true"); Map mappings = new HashMap(); for (IndexField nestedField : field.nestedFields()) { if (nestedField != null) { diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java index 2f5ec282877..d1a38a0ddf8 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java @@ -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 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); + } + } -- 2.39.5