]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5987 Add qProfiles hash when f=actives
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 20 Jan 2015 15:52:09 +0000 (16:52 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 21 Jan 2015 10:09:44 +0000 (11:09 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_no_rules.json
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules.json
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_profile_active_rules_inheritance.json [new file with mode: 0644]

index bb8f2da4ee3db938192fa86a1c15a546b05a34c6..7180176a1c407fdffabeb3e781c383f516c80d93 100644 (file)
  */
 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<Rule> rules, JsonWriter json) {
     json.name("actives").beginObject();
 
+    Collection<String> 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<String, QualityProfileDto> 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<ActiveRule> activeRules, JsonWriter json) {
+  private Collection<String> writeActiveRules(RuleKey ruleKey, Collection<ActiveRule> activeRules, JsonWriter json) {
+    Collection<String> 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();
   }
 }
index e209139529570d665e728d7e235b304ca8da109e..eff12f08eec375d21d1b901249b85309a01e636b 100644 (file)
@@ -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();
index 27982443da41a126aaf53919541ef7e498fdfb33..1b7d587e7b05ced6fb9f10e263a33f89097b007f 100644 (file)
@@ -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 (file)
index 0000000..80d049d
--- /dev/null
@@ -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"
+    }
+  }
+}