aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-05-06 16:45:53 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-05-06 16:46:00 +0200
commit99420201a6f44cfeec98e02396a56dcce9835149 (patch)
treeb59878e3f8e99c18bff991d7471f308500e1cba4 /sonar-ws-client
parentc1f7fc1990c69bf870c8b3d5797574a8b4e9e82f (diff)
downloadsonarqube-99420201a6f44cfeec98e02396a56dcce9835149.tar.gz
sonarqube-99420201a6f44cfeec98e02396a56dcce9835149.zip
SONAR-3755 add rule metadata to ws-client
Diffstat (limited to 'sonar-ws-client')
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java24
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java20
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/rule/Rule.java50
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java17
-rw-r--r--sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/empty.json9
-rw-r--r--sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/search.json27
6 files changed, 130 insertions, 17 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java
index 1595aa72c73..4fa2517c528 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java
@@ -20,6 +20,7 @@
package org.sonar.wsclient.issue;
import org.json.simple.JSONValue;
+import org.sonar.wsclient.rule.Rule;
import org.sonar.wsclient.unmarshallers.JsonUtils;
import java.util.ArrayList;
@@ -30,15 +31,24 @@ class IssueParser {
Issues parseIssues(String json) {
Issues result = new Issues();
- Map jRoot = (Map) JSONValue.parse(json);
- List<Map> jIssues = (List) jRoot.get("issues");
- for (Map jIssue : jIssues) {
- result.add(new Issue(jIssue));
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ List<Map> jsonIssues = (List) jsonRoot.get("issues");
+ if (jsonIssues != null) {
+ for (Map jsonIssue : jsonIssues) {
+ result.add(new Issue(jsonIssue));
+ }
}
- Map paging = (Map) jRoot.get("paging");
- result.setPaging(new Paging(paging));
- result.setSecurityExclusions(JsonUtils.getBoolean(jRoot, "securityExclusions"));
+ List<Map> jsonRules = (List) jsonRoot.get("rules");
+ if (jsonRules != null) {
+ for (Map jsonRule : jsonRules) {
+ result.add(new Rule(jsonRule));
+ }
+ }
+
+ Map paging = (Map) jsonRoot.get("paging");
+ result.setPaging(new Paging(paging));
+ result.setSecurityExclusions(JsonUtils.getBoolean(jsonRoot, "securityExclusions"));
return result;
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java
index 7f9c655aa6b..b5db438fa63 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java
@@ -19,8 +19,9 @@
*/
package org.sonar.wsclient.issue;
-import java.util.ArrayList;
-import java.util.List;
+import org.sonar.wsclient.rule.Rule;
+
+import java.util.*;
/**
* @since 3.6
@@ -28,6 +29,7 @@ import java.util.List;
public class Issues {
private final List<Issue> list = new ArrayList<Issue>();
+ private final Map<String,Rule> rulesByKey = new HashMap<String, Rule>();
private Paging paging;
private Boolean securityExclusions;
@@ -35,10 +37,24 @@ public class Issues {
list.add(issue);
return this;
}
+
public List<Issue> list() {
return list;
}
+ Issues add(Rule rule) {
+ rulesByKey.put(rule.key(), rule);
+ return this;
+ }
+
+ public Collection<Rule> rules() {
+ return rulesByKey.values();
+ }
+
+ public Rule rule(Issue issue) {
+ return rulesByKey.get(issue.ruleKey());
+ }
+
Issues setPaging(Paging paging) {
this.paging = paging;
return this;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/Rule.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/Rule.java
new file mode 100644
index 00000000000..1b86bc4970c
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/Rule.java
@@ -0,0 +1,50 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.wsclient.rule;
+
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import java.util.Map;
+
+/**
+ * @since 3.6
+ */
+public class Rule {
+ private final Map json;
+
+ public Rule(Map json) {
+ this.json = json;
+ }
+
+ /**
+ * Unique key
+ */
+ public String key() {
+ return JsonUtils.getString(json, "key");
+ }
+
+ public String name() {
+ return JsonUtils.getString(json, "name");
+ }
+
+ public String description() {
+ return JsonUtils.getString(json, "desc");
+ }
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java
index 007707fa42a..276266f5c1d 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java
@@ -37,7 +37,7 @@ public class IssueParserTest {
Issue first = list.get(0);
assertThat(first.key()).isEqualTo("ABCDE");
assertThat(first.componentKey()).isEqualTo("Action.java");
- assertThat(first.ruleKey()).isEqualTo("squid:AvoidCycle");
+ assertThat(first.ruleKey()).isEqualTo("squid:CycleBetweenPackages");
assertThat(first.severity()).isEqualTo("CRITICAL");
assertThat(first.line()).isEqualTo(10);
assertThat(first.resolution()).isEqualTo("FIXED");
@@ -61,6 +61,11 @@ public class IssueParserTest {
assertThat(second.attribute("JIRA")).isNull();
assertThat(second.attributes()).isEmpty();
+ assertThat(issues.rules()).hasSize(2);
+ assertThat(issues.rule(first).key()).isEqualTo("squid:CycleBetweenPackages");
+ assertThat(issues.rule(first).name()).isEqualTo("Avoid cycle between java packages");
+ assertThat(issues.rule(first).description()).contains("When several packages");
+
assertThat(issues.paging()).isNotNull();
Paging paging = issues.paging();
assertThat(paging.pageIndex()).isEqualTo(1);
@@ -72,6 +77,16 @@ public class IssueParserTest {
}
@Test
+ public void test_GET_empty_search() throws Exception {
+ String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/empty.json"));
+ Issues issues = new IssueParser().parseIssues(json);
+ assertThat(issues).isNotNull();
+ assertThat(issues.list()).isEmpty();
+ assertThat(issues.rules()).isEmpty();
+ }
+
+
+ @Test
public void test_GET_transitions() throws Exception {
String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/getTransitions.json"));
List<String> transitions = new IssueParser().parseTransitions(json);
diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/empty.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/empty.json
new file mode 100644
index 00000000000..8f05e1953b7
--- /dev/null
+++ b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/empty.json
@@ -0,0 +1,9 @@
+{
+ "paging": {
+ "pageIndex": 1,
+ "pageSize": 100,
+ "total": 0,
+ "pages": 0
+ },
+ "securityExclusions": true
+} \ No newline at end of file
diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/search.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/search.json
index 369223a48f3..c59e1bad4b6 100644
--- a/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/search.json
+++ b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/search.json
@@ -3,7 +3,7 @@
{
"key": "ABCDE",
"component": "Action.java",
- "rule": "squid:AvoidCycle",
+ "rule": "squid:CycleBetweenPackages",
"severity": "CRITICAL",
"line": 10,
"resolution": "FIXED",
@@ -23,18 +23,31 @@
{
"key": "FGHIJ",
"component": "Filter.java",
- "rule": "squid:NullRef",
+ "rule": "checkstyle:com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck",
"severity": "BLOCKER",
"resolution": "FIXED",
"status": "OPEN"
}
],
- "paging":
+ "rules": [
{
- "pageIndex": 1,
- "pageSize": 100,
- "total": 2,
- "pages": 1
+
+ "key": "squid:CycleBetweenPackages",
+ "name": "Avoid cycle between java packages",
+ "desc": "<p>\nWhen several packages are involved in a cycle (package A > package B > package C > package A where \">\" means \"depends upon\"),\nthat means that those packages are highly coupled and that there is no way to reuse/extract one of those packages without importing all the other packages.\nSuch cycle could quickly increase the effort required to maintain an application and to embrace business change.\nSonar not only detect cycles between packages but also determines what is the minimum effort to break those cycles.\nThis rule log a violation on each source file having an outgoing dependency to be but in order to break a cycle.\n</p>\n"
+
},
+ {
+ "key": "checkstyle:com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck",
+ "name": "Unused Imports",
+ "desc": "Checks for unused import statements."
+ }
+ ],
+ "paging": {
+ "pageIndex": 1,
+ "pageSize": 100,
+ "total": 2,
+ "pages": 1
+ },
"securityExclusions": true
} \ No newline at end of file