diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-09 17:51:05 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-09 17:51:05 +0100 |
commit | c4cb7f52bc27d97e9c9302133ce36aa635816e83 (patch) | |
tree | 5d69a2b8a1da5a719a376d77821595eaf22de086 | |
parent | ca09baff2193d15687ec7428365d49764b472bb1 (diff) | |
download | sonarqube-c4cb7f52bc27d97e9c9302133ce36aa635816e83.tar.gz sonarqube-c4cb7f52bc27d97e9c9302133ce36aa635816e83.zip |
SONAR-4923 Improve inheritance query management
4 files changed, 59 insertions, 23 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRuleQuery.java b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRuleQuery.java index 138221a77c5..6cc9387581c 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRuleQuery.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRuleQuery.java @@ -38,8 +38,8 @@ import static com.google.common.collect.Lists.newArrayList; */ public class ProfileRuleQuery { - public static final String INHERITANCE_ANY = "any"; - public static final String INHERITANCE_NOT = "NOT"; + private static final String INHERITANCE_ANY = "any"; + private static final String INHERITANCE_NOT = "NOT"; private static final Set<String> AUTHORIZED_INHERITANCE_PARAMS = ImmutableSet.of(QProfileRule.INHERITED, QProfileRule.OVERRIDES, INHERITANCE_ANY, INHERITANCE_NOT); private static final String PARAM_PROFILE_ID = "profileId"; @@ -57,6 +57,8 @@ public class ProfileRuleQuery { private List<String> severities; private List<String> statuses; private String inheritance; + private boolean anyInheritance; + private boolean noInheritance; private ProfileRuleQuery() { repositoryKeys = Lists.newArrayList(); @@ -90,7 +92,15 @@ public class ProfileRuleQuery { if (params.containsKey(PARAM_INHERITANCE)) { String inheritance = (String) params.get(PARAM_INHERITANCE); validateInheritance(inheritance, errors); - result.setInheritance(inheritance); + if (inheritance.equals(INHERITANCE_ANY)) { + result.setAnyInheritance(true); + } else if (inheritance.equals(INHERITANCE_NOT)) { + result.setNoInheritance(true); + } else { + result.setInheritance(inheritance); + } + } else { + result.setAnyInheritance(true); } if (!errors.isEmpty()) { @@ -151,6 +161,15 @@ public class ProfileRuleQuery { return this; } + public ProfileRuleQuery setAnyInheritance(boolean anyInheritance) { + this.anyInheritance = anyInheritance; + return this; + } + + public ProfileRuleQuery setNoInheritance(boolean noInheritance) { + this.noInheritance = noInheritance; + return this; + } public int profileId() { return profileId; @@ -183,6 +202,14 @@ public class ProfileRuleQuery { return inheritance; } + public boolean anyInheritance() { + return anyInheritance; + } + + public boolean noInheritance() { + return noInheritance; + } + private static String[] optionalVarargs(Object jRubyArray) { List<String> items = RubyUtils.toStrings(jRubyArray); String[] empty = new String[0]; diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java index 24a7546618d..467f1d83093 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java @@ -157,12 +157,10 @@ public class ProfileRules implements ServerExtension { ); addMustTermOrTerms(filter, ActiveRuleDocument.FIELD_SEVERITY, query.severities()); String inheritance = query.inheritance(); - if (inheritance != null && !inheritance.equals(ProfileRuleQuery.INHERITANCE_ANY)) { - if (!inheritance.equals(ProfileRuleQuery.INHERITANCE_NOT)) { - addMustTermOrTerms(filter, ActiveRuleDocument.FIELD_INHERITANCE, newArrayList(inheritance)); - } else { - filter.mustNot(getTermOrTerms(ActiveRuleDocument.FIELD_INHERITANCE, newArrayList(QProfileRule.INHERITED, QProfileRule.OVERRIDES))); - } + if (inheritance != null) { + addMustTermOrTerms(filter, ActiveRuleDocument.FIELD_INHERITANCE, newArrayList(inheritance)); + } else if (query.noInheritance()) { + filter.mustNot(getTermOrTerms(ActiveRuleDocument.FIELD_INHERITANCE, newArrayList(QProfileRule.INHERITED, QProfileRule.OVERRIDES))); } return filter; } diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ProfileRuleQueryTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ProfileRuleQueryTest.java index 41844b014a9..7358ccd6593 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ProfileRuleQueryTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ProfileRuleQueryTest.java @@ -44,6 +44,7 @@ public class ProfileRuleQueryTest { Map<String, Object> params = ImmutableMap.of("profileId", (Object) Integer.toString(profileId)); ProfileRuleQuery query = ProfileRuleQuery.parse(params); assertThat(query.profileId()).isEqualTo(profileId); + assertThat(query.anyInheritance()).isTrue(); } @Test diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java index fa6864d5507..60a178e866a 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java @@ -63,14 +63,14 @@ public class ProfileRulesTest { profileRules = new ProfileRules(index); esSetup.client().prepareBulk() - .add(Requests.indexRequest().index("rules").type("rule").source(testFileAsString("should_find_active_rules/rule25.json"))) - .add(Requests.indexRequest().index("rules").type("rule").source(testFileAsString("should_find_active_rules/rule759.json"))) - .add(Requests.indexRequest().index("rules").type("rule").source(testFileAsString("should_find_active_rules/rule1482.json"))) - .add(Requests.indexRequest().index("rules").type("active_rule").parent("25").source(testFileAsString("should_find_active_rules/active_rule25.json"))) - .add(Requests.indexRequest().index("rules").type("active_rule").parent("759").source(testFileAsString("should_find_active_rules/active_rule391.json"))) - .add(Requests.indexRequest().index("rules").type("active_rule").parent("759").source(testFileAsString("should_find_active_rules/active_rule523.json"))) - .add(Requests.indexRequest().index("rules").type("active_rule").parent("1482").source(testFileAsString("should_find_active_rules/active_rule2702.json"))) - .setRefresh(true).execute().actionGet(); + .add(Requests.indexRequest().index("rules").type("rule").source(testFileAsString("should_find_active_rules/rule25.json"))) + .add(Requests.indexRequest().index("rules").type("rule").source(testFileAsString("should_find_active_rules/rule759.json"))) + .add(Requests.indexRequest().index("rules").type("rule").source(testFileAsString("should_find_active_rules/rule1482.json"))) + .add(Requests.indexRequest().index("rules").type("active_rule").parent("25").source(testFileAsString("should_find_active_rules/active_rule25.json"))) + .add(Requests.indexRequest().index("rules").type("active_rule").parent("759").source(testFileAsString("should_find_active_rules/active_rule391.json"))) + .add(Requests.indexRequest().index("rules").type("active_rule").parent("759").source(testFileAsString("should_find_active_rules/active_rule523.json"))) + .add(Requests.indexRequest().index("rules").type("active_rule").parent("1482").source(testFileAsString("should_find_active_rules/active_rule2702.json"))) + .setRefresh(true).execute().actionGet(); } @After @@ -119,12 +119,21 @@ public class ProfileRulesTest { public void find_profile_rules_with_inheritance() { Paging paging = Paging.create(10, 1); - assertThat(profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(null), paging).rules()).hasSize(3); - assertThat(profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(QProfileRule.INHERITED), paging).rules()).hasSize(1); - assertThat(profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(QProfileRule.OVERRIDES), paging).rules()).hasSize(1); - assertThat(profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(ProfileRuleQuery.INHERITANCE_NOT), paging).rules()).hasSize(1); + List<QProfileRule> rules = profileRules.searchProfileRules(ProfileRuleQuery.create(1), paging).rules(); + assertThat(rules).hasSize(3); - List<QProfileRule> rules = profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(ProfileRuleQuery.INHERITANCE_NOT), paging).rules(); + rules = profileRules.searchProfileRules(ProfileRuleQuery.create(1).setAnyInheritance(true), paging).rules(); + assertThat(rules).hasSize(3); + + rules = profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(QProfileRule.INHERITED), paging).rules(); + assertThat(rules).hasSize(1); + assertThat(rules.get(0).activeRuleId()).isEqualTo(391); + + rules = profileRules.searchProfileRules(ProfileRuleQuery.create(1).setInheritance(QProfileRule.OVERRIDES), paging).rules(); + assertThat(rules).hasSize(1); + assertThat(rules.get(0).activeRuleId()).isEqualTo(25); + + rules = profileRules.searchProfileRules(ProfileRuleQuery.create(1).setNoInheritance(true), paging).rules(); assertThat(rules).hasSize(1); assertThat(rules.get(0).activeRuleId()).isEqualTo(2702); } @@ -135,7 +144,8 @@ public class ProfileRulesTest { // All rules for profile 1 List<Integer> result = profileRules.searchProfileRuleIds(ProfileRuleQuery.create(1)); assertThat(result).hasSize(3); - assertThat(result.get(0)).isEqualTo(1); } + assertThat(result.get(0)).isEqualTo(1); + } @Test public void get_from_active_rule() { |