aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-07-08 14:28:04 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-07-08 14:32:42 +0200
commitbb8edcd2ce9d932b56e2bd7b6e8f9b9f866f12eb (patch)
treebdea818b4e7d093494b86b638492815980af9994 /sonar-server
parent7c15d25b58f612dfa2224f5320d116a0a5c803cb (diff)
downloadsonarqube-bb8edcd2ce9d932b56e2bd7b6e8f9b9f866f12eb.tar.gz
sonarqube-bb8edcd2ce9d932b56e2bd7b6e8f9b9f866f12eb.zip
SONAR-5390 Add parameter to search rules by active severity
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java26
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rule/index/RuleQuery.java17
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java8
-rw-r--r--sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java35
4 files changed, 58 insertions, 28 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
index 3906b7f27fd..1dd442e1ec9 100644
--- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
+++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
@@ -27,14 +27,7 @@ import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.index.query.BoolFilterBuilder;
-import org.elasticsearch.index.query.BoolQueryBuilder;
-import org.elasticsearch.index.query.FilterBuilder;
-import org.elasticsearch.index.query.FilterBuilders;
-import org.elasticsearch.index.query.MatchQueryBuilder;
-import org.elasticsearch.index.query.QueryBuilder;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.index.query.QueryStringQueryBuilder;
+import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
@@ -50,22 +43,12 @@ import org.sonar.core.profiling.StopWatch;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
import org.sonar.server.rule.Rule;
-import org.sonar.server.search.BaseIndex;
-import org.sonar.server.search.ESNode;
-import org.sonar.server.search.IndexDefinition;
-import org.sonar.server.search.IndexField;
-import org.sonar.server.search.QueryOptions;
-import org.sonar.server.search.Result;
+import org.sonar.server.search.*;
import javax.annotation.CheckForNull;
+
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
import static com.google.common.collect.Lists.newArrayList;
@@ -292,6 +275,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
BoolFilterBuilder childrenFilter = FilterBuilders.boolFilter();
this.addTermFilter(childrenFilter, ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), query.getQProfileKey());
this.addTermFilter(childrenFilter, ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field(), query.getInheritance());
+ this.addTermFilter(childrenFilter, ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field(), query.getActiveSeverities());
// ChildQuery
QueryBuilder childQuery;
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleQuery.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleQuery.java
index 57f186a1d89..ca8a14e6fe3 100644
--- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleQuery.java
+++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleQuery.java
@@ -26,6 +26,7 @@ import org.sonar.server.search.IndexField;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.Collection;
import java.util.Date;
@@ -43,6 +44,7 @@ public class RuleQuery {
private Boolean activation;
private String qProfileKey;
private Collection<String> inheritance;
+ private Collection<String> activeSeverities;
private String templateKey;
private Boolean isTemplate;
private Date availableSince;
@@ -189,6 +191,21 @@ public class RuleQuery {
}
@CheckForNull
+ public Collection<String> getActiveSeverities() {
+ return activeSeverities;
+ }
+
+ public RuleQuery setActiveSeverities(@Nullable Collection<String> severities) {
+ if (severities != null) {
+ for (String severity : severities) {
+ Preconditions.checkArgument(Severity.ALL.contains(severity), "Unknown severity: " + severity);
+ }
+ }
+ this.activeSeverities = severities;
+ return this;
+ }
+
+ @CheckForNull
public Boolean isTemplate() {
return isTemplate;
}
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
index 06e3334574e..b4789c99836 100644
--- a/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
+++ b/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
@@ -59,6 +59,7 @@ public class SearchAction implements RequestHandler {
public static final String PARAM_HAS_DEBT_CHARACTERISTIC = "has_debt_characteristic";
public static final String PARAM_TAGS = "tags";
public static final String PARAM_INHERITANCE = "inheritance";
+ public static final String PARAM_ACTIVE_SEVERITIES = "active_severities";
public static final String PARAM_IS_TEMPLATE = "is_template";
public static final String PARAM_TEMPLATE_KEY = "template_key";
public static final String PARAM_FACETS = "facets";
@@ -177,6 +178,12 @@ public class SearchAction implements RequestHandler {
ActiveRule.Inheritance.OVERRIDES.name());
action
+ .createParam(PARAM_ACTIVE_SEVERITIES)
+ .setDescription("Comma-separated list of activation severities, i.e the severity of rules in Quality profiles.")
+ .setPossibleValues(Severity.ALL)
+ .setExampleValue("CRITICAL,BLOCKER");
+
+ action
.createParam(PARAM_IS_TEMPLATE)
.setDescription("Filter template rules")
.setBooleanPossibleValues();
@@ -236,6 +243,7 @@ public class SearchAction implements RequestHandler {
query.setQProfileKey(request.param(PARAM_QPROFILE));
query.setTags(request.paramAsStrings(PARAM_TAGS));
query.setInheritance(request.paramAsStrings(PARAM_INHERITANCE));
+ query.setActiveSeverities(request.paramAsStrings(PARAM_ACTIVE_SEVERITIES));
query.setIsTemplate(request.paramAsBoolean(PARAM_IS_TEMPLATE));
query.setTemplateKey(request.param(PARAM_TEMPLATE_KEY));
query.setKey(request.param(PARAM_KEY));
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java
index 3b3950d6993..8d763d52e3e 100644
--- a/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java
@@ -50,12 +50,8 @@ import org.sonar.server.search.Result;
import org.sonar.server.tester.ServerTester;
import javax.annotation.Nullable;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
+
+import java.util.*;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
@@ -621,7 +617,7 @@ public class RuleIndexMediumTest {
}
@Test
- public void search_by_profile_and_inheritance() throws InterruptedException {
+ public void search_by_profile_inheritance_and_active_severities() throws InterruptedException {
QualityProfileDto qualityProfileDto1 = QProfileTesting.newXooP1();
QualityProfileDto qualityProfileDto2 = QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY);
db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);
@@ -719,6 +715,31 @@ public class RuleIndexMediumTest {
new QueryOptions()
);
assertThat(result.getHits()).hasSize(3);
+
+ // 9. get rules active on profile1 with active severity BLOCKER
+ result = index.search(new RuleQuery().setActivation(true)
+ .setQProfileKey(qualityProfileDto1.getKey())
+ .setActiveSeverities(ImmutableSet.of(
+ Severity.BLOCKER.toString())),
+ new QueryOptions()
+ );
+ assertThat(result.getHits()).hasSize(3);
+
+ // 10. get rules active on profile2 with active severity MINOR, then BLOCKER + MINOR
+ result = index.search(new RuleQuery().setActivation(true)
+ .setQProfileKey(qualityProfileDto2.getKey())
+ .setActiveSeverities(ImmutableSet.of(
+ Severity.MINOR.toString())),
+ new QueryOptions()
+ );
+ assertThat(result.getHits()).hasSize(1);
+ result = index.search(new RuleQuery().setActivation(true)
+ .setQProfileKey(qualityProfileDto2.getKey())
+ .setActiveSeverities(ImmutableSet.of(
+ Severity.BLOCKER.toString(), Severity.MINOR.toString())),
+ new QueryOptions()
+ );
+ assertThat(result.getHits()).hasSize(3);
}
@Test