From: Jean-Baptiste Lievremont Date: Tue, 20 Jan 2015 15:52:09 +0000 (+0100) Subject: SONAR-5987 Add qProfiles hash when f=actives X-Git-Tag: latest-silver-master-#65~159 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=42688d94dfe659f91cb074afb2f50681196350f7;p=sonarqube.git SONAR-5987 Add qProfiles hash when f=actives --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java index bb8f2da4ee3..7180176a1c4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java @@ -19,10 +19,15 @@ */ package org.sonar.server.rule.ws; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.sonar.api.ServerComponent; +import org.sonar.api.resources.Language; +import org.sonar.api.resources.Languages; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.qualityprofile.db.ActiveRuleKey; +import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.qualityprofile.QProfileLoader; import org.sonar.server.rule.Rule; @@ -38,30 +43,58 @@ import java.util.Map; */ public class ActiveRuleCompleter implements ServerComponent { private final QProfileLoader loader; + private final Languages languages; - public ActiveRuleCompleter(QProfileLoader loader) { + public ActiveRuleCompleter(QProfileLoader loader, Languages languages) { this.loader = loader; + this.languages = languages; } void completeSearch(RuleQuery query, Collection rules, JsonWriter json) { json.name("actives").beginObject(); + Collection qProfileKeys = Sets.newHashSet(); + String profileKey = query.getQProfileKey(); if (profileKey != null) { // Load details of active rules on the selected profile for (Rule rule : rules) { ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.key())); if (activeRule != null) { - writeActiveRules(rule.key(), Arrays.asList(activeRule), json); + qProfileKeys = writeActiveRules(rule.key(), Arrays.asList(activeRule), json); } } } else { // Load details of all active rules for (Rule rule : rules) { - writeActiveRules(rule.key(), loader.findActiveRulesByRule(rule.key()), json); + qProfileKeys = writeActiveRules(rule.key(), loader.findActiveRulesByRule(rule.key()), json); } } json.endObject(); + + + Map qProfilesByKey = Maps.newHashMap(); + for (String qProfileKey: qProfileKeys) { + if (!qProfilesByKey.containsKey(qProfileKey)) { + QualityProfileDto profile = loader.getByKey(qProfileKey); + qProfilesByKey.put(qProfileKey, profile); + if (profile.getParentKee() != null && !qProfilesByKey.containsKey(profile.getParentKee())) { + qProfilesByKey.put(profile.getParentKee(), loader.getByKey(profile.getParentKee())); + } + } + } + json.name("qProfiles").beginObject(); + for (QualityProfileDto profile: qProfilesByKey.values()) { + Language language = languages.get(profile.getLanguage()); + String langName = language == null ? profile.getLanguage() : language.getName(); + json.name(profile.getKey()).beginObject() + .prop("name", profile.getName()) + .prop("lang", profile.getLanguage()) + .prop("langName", langName) + .prop("parent", profile.getParentKee()) + .endObject(); + } + json.endObject(); } void completeShow(Rule rule, JsonWriter json) { @@ -72,18 +105,20 @@ public class ActiveRuleCompleter implements ServerComponent { json.endArray(); } - private void writeActiveRules(RuleKey ruleKey, Collection activeRules, JsonWriter json) { + private Collection writeActiveRules(RuleKey ruleKey, Collection activeRules, JsonWriter json) { + Collection qProfileKeys = Sets.newHashSet(); if (!activeRules.isEmpty()) { json.name(ruleKey.toString()); json.beginArray(); for (ActiveRule activeRule : activeRules) { - writeActiveRule(activeRule, json); + qProfileKeys.add(writeActiveRule(activeRule, json)); } json.endArray(); } + return qProfileKeys; } - private void writeActiveRule(ActiveRule activeRule, JsonWriter json) { + private String writeActiveRule(ActiveRule activeRule, JsonWriter json) { json .beginObject() .prop("qProfile", activeRule.key().qProfile().toString()) @@ -102,5 +137,6 @@ public class ActiveRuleCompleter implements ServerComponent { .endObject(); } json.endArray().endObject(); + return activeRule.key().qProfile(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java index e2091395295..eff12f08eec 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.Severity; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.DateUtils; @@ -387,11 +388,41 @@ public class RulesWebServiceMediumTest { request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); request.setParam(SearchAction.PARAM_ACTIVATION, "true"); request.setParam(SearchAction.PARAM_QPROFILE, profile2.getKey()); - request.setParam(SearchOptions.PARAM_FIELDS, ""); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); WsTester.Result result = request.execute(); result.assertJson(this.getClass(), "search_profile_active_rules.json", false); } + @Test + public void search_profile_active_rules_with_inheritance() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + + QualityProfileDto profile2 = QProfileTesting.newXooP2().setParentKee(profile.getKee()); + tester.get(QualityProfileDao.class).insert(session, profile2); + + session.commit(); + + RuleDto rule = RuleTesting.newXooX1(); + ruleDao.insert(session, rule); + + ActiveRuleDto activeRule = newActiveRule(profile, rule); + tester.get(ActiveRuleDao.class).insert(session, activeRule); + ActiveRuleDto activeRule2 = newActiveRule(profile2, rule).setInheritance(ActiveRuleDto.OVERRIDES).setSeverity(Severity.CRITICAL); + tester.get(ActiveRuleDao.class).insert(session, activeRule2); + + session.commit(); + + MockUserSession.set(); + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); + request.setParam(SearchAction.PARAM_ACTIVATION, "true"); + request.setParam(SearchAction.PARAM_QPROFILE, profile2.getKey()); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_profile_active_rules_inheritance.json", false); + } + @Test public void search_all_active_rules_params() throws Exception { QualityProfileDto profile = QProfileTesting.newXooP1(); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_no_rules.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_no_rules.json index fd3fdfa66d5..0e7286cf282 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_no_rules.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_no_rules.json @@ -3,5 +3,6 @@ "p": 1, "ps": 100, "rules": [], - "actives": {} + "actives": {}, + "qProfiles": {} } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules.json index 27982443da4..1b7d587e7b0 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules.json @@ -1,10 +1,27 @@ { - "total": 1, - "p": 1, - "ps": 100, + "total":1, + "p":1, + "ps":100, "rules": [ { "key": "xoo:x1" } - ] + ], + "actives": { + "xoo:x1": [ + { + "qProfile": "XOO_P2", + "inherit": "NONE", + "severity": "BLOCKER", + "params": [] + } + ] + }, + "qProfiles": { + "XOO_P2": { + "name": "P2", + "lang": "xoo", + "langName": "xoo" + } + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules_inheritance.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules_inheritance.json new file mode 100644 index 00000000000..80d049d4442 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules_inheritance.json @@ -0,0 +1,33 @@ +{ + "total":1, + "p":1, + "ps":100, + "rules": [ + { + "key": "xoo:x1" + } + ], + "actives": { + "xoo:x1": [ + { + "qProfile": "XOO_P2", + "inherit": "OVERRIDES", + "severity": "CRITICAL", + "params": [] + } + ] + }, + "qProfiles": { + "XOO_P2": { + "name": "P2", + "lang": "xoo", + "langName": "xoo", + "parent": "XOO_P1" + }, + "XOO_P1": { + "name": "P1", + "lang": "xoo", + "langName": "xoo" + } + } +}