]> source.dussan.org Git - sonarqube.git/commitdiff
Parse rule and active rule results, with parameters
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 13 Dec 2013 18:02:14 +0000 (19:02 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 13 Dec 2013 18:02:53 +0000 (19:02 +0100)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRule.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRuleResult.java
sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java
sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java
sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/active_rule25.json
sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/active_rule2702.json [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/rule1482.json [new file with mode: 0644]

index 5cfca0afd5107259c48801fd0c3120d38aceb200..72c430e0433643e9eb6852732543505de998f316 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
+import org.elasticsearch.common.collect.Lists;
+import org.elasticsearch.common.collect.Maps;
+import org.sonar.api.rules.RulePriority;
+
+import java.util.List;
+import java.util.Map;
+
 public class QProfileRule {
 
-  private final String ruleSource;
-  private final String activeRuleSource;
+  private final Map<String, Object> ruleSource;
+  private final Map<String, Object> activeRuleSource;
+
+  private final String key;
+  private final String name;
+  private final String description;
+  private final String status;
 
-  public QProfileRule(String ruleSource, String activeRuleSource) {
+  private final RulePriority severity;
+  private final List<Param> params;
+
+  public QProfileRule(Map<String, Object> ruleSource, Map<String, Object> activeRuleSource) {
     this.ruleSource = ruleSource;
     this.activeRuleSource = activeRuleSource;
+
+    key = (String) ruleSource.get("key");
+    name = (String) ruleSource.get("name");
+    description = (String) ruleSource.get("description");
+    status = (String) ruleSource.get("status");
+
+    severity = RulePriority.valueOf((String) activeRuleSource.get("severity"));
+    params = Lists.newArrayList();
+    if (ruleSource.containsKey("params")) {
+      Map<String, Map<String, Object>> ruleParams = Maps.newHashMap();
+      for (Map<String, Object> ruleParam: (List<Map<String, Object>>) ruleSource.get("params")) {
+        ruleParams.put((String) ruleParam.get("key"), ruleParam);
+      }
+      Map<String, Map<String, Object>> activeRuleParams = Maps.newHashMap();
+      for (Map<String, Object> activeRuleParam: (List<Map<String, Object>>) activeRuleSource.get("params")) {
+        activeRuleParams.put((String) activeRuleParam.get("key"), activeRuleParam);
+      }
+      for(Map.Entry<String, Map<String, Object>> ruleParam: ruleParams.entrySet()) {
+        params.add(new Param(
+          (String) ruleParam.getValue().get("key"),
+          activeRuleParams.containsKey(ruleParam.getKey()) ? (String) activeRuleParams.get(ruleParam.getKey())
+            .get("value") : null,
+          (String) ruleParam.getValue().get("description"),
+          (String) ruleParam.getValue().get("defaultValue"),
+          (String) ruleParam.getValue().get("type")
+        ));
+      }
+    }
+  }
+
+  public Map<String, Object> ruleSource() {
+    return ruleSource;
+  }
+
+  public Map<String, Object> activeRuleSource() {
+    return activeRuleSource;
+  }
+
+  public String key() {
+    return key;
+  }
+
+  public String name() {
+    return name;
+  }
+
+  public String description() {
+    return description;
+  }
+
+  public String status() {
+    return status;
+  }
+
+  public RulePriority severity() {
+    return severity;
+  }
+
+  public List<Param> params() {
+    return params;
+  }
+
+  class Param {
+    private final String key;
+    private final String value;
+    private final String description;
+    private final String defaultValue;
+    private final String type;
+    public Param(String key, String value, String description, String defaultValue, String type) {
+      super();
+      this.key = key;
+      this.value = value;
+      this.description = description;
+      this.defaultValue = defaultValue;
+      this.type = type;
+    }
+    public String key() {
+      return key;
+    }
+    public String value() {
+      return value;
+    }
+    public String description() {
+      return description;
+    }
+    public String defaultValue() {
+      return defaultValue;
+    }
+    public String type() {
+      return type;
+    }
   }
 
   @Override
index f964944868e7404a7dd136b335958e99dae16e25..e3796c6bb4ec7d7493c2e2b7cfefeaaf45f9c6cf 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.qualityprofile;
 
-import java.util.Collection;
 import java.util.List;
 
 public class QProfileRuleResult {
@@ -32,7 +31,7 @@ public class QProfileRuleResult {
     this.paging = paging;
   }
 
-  public Collection<QProfileRule> rules() {
+  public List<QProfileRule> rules() {
     return rules;
   }
 
index e5215173be31d1a9bad98107e87893a558cb1574..e412ddb0ce5aa5d5231b7457742c13a02e09a8ca 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.server.search.SearchIndex;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import static org.elasticsearch.index.query.FilterBuilders.*;
 import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
@@ -63,13 +64,13 @@ public class ProfileRules {
       .setFrom(paging.offset());
     SearchHits hits = index.executeRequest(builder);
 
-    String[] activeRuleSources = new String[hits.getHits().length];
+    List<Map<String, Object>> activeRuleSources = Lists.newArrayList();
     String[] parentIds = new String[hits.getHits().length];
     int hitCounter = 0;
 
     List<QProfileRule> result = Lists.newArrayList();
     for (SearchHit hit: hits.getHits()) {
-      activeRuleSources[hitCounter] = hit.sourceAsString();
+      activeRuleSources.add(hit.sourceAsMap());
       parentIds[hitCounter] = hit.field("_parent").value();
       hitCounter ++;
     }
@@ -79,7 +80,7 @@ public class ProfileRules {
         .execute().actionGet().getResponses();
 
       for (int i = 0; i < hitCounter; i ++) {
-        result.add(new QProfileRule(responses[i].getResponse().getSourceAsString(), activeRuleSources[i]));
+        result.add(new QProfileRule(responses[i].getResponse().getSourceAsMap(), activeRuleSources.get(i)));
       }
     }
 
index 56d16e980ac16869c16666fddf14c5d87cb844a0..8a13f0169fbf05d2b58cb15cbea2b393f35752fb 100644 (file)
@@ -26,12 +26,16 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
+import org.sonar.api.rules.RulePriority;
 import org.sonar.core.profiling.Profiling;
 import org.sonar.server.qualityprofile.Paging;
+import org.sonar.server.qualityprofile.QProfileRule;
 import org.sonar.server.search.SearchIndex;
 import org.sonar.server.search.SearchNode;
 import org.sonar.test.TestUtils;
 
+import java.util.List;
+
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -68,18 +72,26 @@ public class ProfileRulesTest {
     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();
 
     Paging paging = Paging.create(10, 1);
 
     // All rules for profile 1
-    assertThat(profileRules.searchActiveRules(ProfileRuleQuery.create(1), paging).rules()).hasSize(2);
+    List<QProfileRule> rules1 = profileRules.searchActiveRules(ProfileRuleQuery.create(1), paging).rules();
+    assertThat(rules1).hasSize(3);
+    assertThat(rules1.get(0).key()).isEqualTo("DM_CONVERT_CASE");
+    assertThat(rules1.get(0).severity()).isEqualTo(RulePriority.MINOR);
 
     // All rules for profile 2
-    assertThat(profileRules.searchActiveRules(ProfileRuleQuery.create(2), paging).rules()).hasSize(1);
+    List<QProfileRule> rules2 = profileRules.searchActiveRules(ProfileRuleQuery.create(2), paging).rules();
+    assertThat(rules2).hasSize(1);
+    assertThat(rules2.get(0).ruleSource().get("id")).isEqualTo(759);
+    assertThat(rules2.get(0).activeRuleSource().get("id")).isEqualTo(523);
 
     // Inexistent profile
     assertThat(profileRules.searchActiveRules(ProfileRuleQuery.create(3), paging).rules()).hasSize(0);
@@ -95,6 +107,11 @@ public class ProfileRulesTest {
 
     // Match on repositoryKey
     assertThat(profileRules.searchActiveRules(ProfileRuleQuery.create(1).addRepositoryKeys("findbugs"), paging).rules()).hasSize(1);
+
+    // Match on key, rule has parameters
+    List<QProfileRule> rulesWParam = profileRules.searchActiveRules(ProfileRuleQuery.create(1).setNameOrKey("ArchitecturalConstraint"), paging).rules();
+    assertThat(rulesWParam).hasSize(1);
+    assertThat(rulesWParam.get(0).params()).hasSize(2);
   }
 
   private String testFileAsString(String testFile) throws Exception {
index 7f929d2884459995bfefd6e87b178069fd5bbcee..717f25ab7dff519bd3925f2dc7f6cab986c6672c 100644 (file)
@@ -1 +1 @@
-{"id": 25, "severity": "INFO", "profileId": 1, "inheritance": null}
\ No newline at end of file
+{"id": 25, "severity": "MINOR", "profileId": 1, "inheritance": null}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/active_rule2702.json b/sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/active_rule2702.json
new file mode 100644 (file)
index 0000000..2c5c79d
--- /dev/null
@@ -0,0 +1 @@
+{"id":2702,"severity":"CRITICAL","profileId":1,"inheritance":null,"params":[{"key":"fromClasses","value":"**.core.**"},{"key":"toClasses","value":"**.server.**"}]}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/rule1482.json b/sonar-server/src/test/resources/org/sonar/server/rule/ProfileRulesTest/should_find_active_rules/rule1482.json
new file mode 100644 (file)
index 0000000..4d3cc9b
--- /dev/null
@@ -0,0 +1 @@
+{"id":1482,"key":"ArchitecturalConstraint","language":"java","name":"Architectural constraint","description":"<p>A source code comply to an architectural model when it fully\n\tadheres to a set of architectural constraints. A constraint allows to\n\tdeny references between classes by pattern.</p>\n<p>You can for instance use this rule to :</p>\n<ul>\n\t<li>forbid access to **.web.** from **.dao.** classes</li>\n\t<li>forbid access to java.util.Vector, java.util.Hashtable and\n\t\tjava.util.Enumeration from any classes</li>\n\t<li>forbid access to java.sql.** from **.ui.** and **.web.**\n\t\tclasses</li>\n</ul>","parentKey":null,"repositoryKey":"squid","severity":"MAJOR","status":"READY","createdAt":"2013-12-11T13:48:00.799Z","updatedAt":"2013-12-13T17:26:35.767Z","params":[{"key":"toClasses","type":"STRING","defaultValue":"","description":"Mandatory. Ex : java.util.Vector, java.util.Hashtable, java.util.Enumeration"},{"key":"fromClasses","type":"STRING","defaultValue":"","description":"Optional. If this property is not defined, all classes should adhere to this constraint. Ex : **.web.**"}]}