aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java9
-rw-r--r--sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java35
-rw-r--r--sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java143
-rw-r--r--sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java3
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_custom_rule.json7
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_manual_rule.json6
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json24
7 files changed, 222 insertions, 5 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java b/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java
index 172dca21ff4..265730b321c 100644
--- a/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java
+++ b/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.rule.ws;
+import org.apache.commons.lang.StringEscapeUtils;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.utils.text.JsonWriter;
@@ -124,7 +125,13 @@ public class RuleMapping extends BaseMapping {
public void write(JsonWriter json, Rule rule) {
String html = rule.htmlDescription();
if (html != null) {
- json.prop("htmlDesc", macroInterpreter.interpret(html));
+ if (rule.isManual() || rule.templateKey() != null) {
+ String desc = StringEscapeUtils.escapeHtml(html);
+ desc = desc.replaceAll("\\n", "<br/>");
+ json.prop("htmlDesc", desc);
+ } else {
+ json.prop("htmlDesc", macroInterpreter.interpret(html));
+ }
}
}
}
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java
index 709a297e4cb..0147e02f3e4 100644
--- a/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java
@@ -66,7 +66,40 @@ public class RuleServiceMediumTest {
}
@Test
- public void test_list_tags() throws InterruptedException {
+ public void get_rule_by_key() throws Exception {
+ MockUserSession.set()
+ .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+ .setLogin("me");
+
+ RuleKey key = RuleKey.of("java", "S001");
+
+ dao.insert(dbSession, RuleTesting.newDto(key));
+ dbSession.commit();
+ dbSession.clearCache();
+
+ Rule rule = service.getByKey(key);
+ assertThat(rule).isNotNull();
+ }
+
+ @Test
+ public void get_rule_by_key_escape_description_on_manual_rule() throws Exception {
+ MockUserSession.set()
+ .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+ .setLogin("me");
+
+ RuleDto manualRule = RuleTesting.newManualRule("My manual")
+ .setDescription("<div>Manual rule desc</div>");
+ dao.insert(dbSession, manualRule);
+ dbSession.commit();
+ dbSession.clearCache();
+
+ Rule rule = service.getByKey(manualRule.getKey());
+ assertThat(rule).isNotNull();
+ assertThat(rule.htmlDescription()).isEqualTo("<div>Manual rule desc</div>");
+ }
+
+ @Test
+ public void list_tags() throws InterruptedException {
// insert db
RuleKey key1 = RuleKey.of("javascript", "S001");
RuleKey key2 = RuleKey.of("java", "S001");
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java
new file mode 100644
index 00000000000..63f6c909507
--- /dev/null
+++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/ShowActionMediumTest.java
@@ -0,0 +1,143 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.server.rule.ws;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rule.Severity;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.core.rule.RuleParamDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.rule.NewRule;
+import org.sonar.server.rule.RuleService;
+import org.sonar.server.rule.RuleTesting;
+import org.sonar.server.rule.db.RuleDao;
+import org.sonar.server.tester.ServerTester;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import static com.google.common.collect.Sets.newHashSet;
+
+public class ShowActionMediumTest {
+
+ @ClassRule
+ public static ServerTester tester = new ServerTester();
+
+ WsTester wsTester;
+
+ RuleService ruleService;
+ RuleDao ruleDao;
+ DbSession session;
+
+ @Before
+ public void setUp() throws Exception {
+ tester.clearDbAndIndexes();
+ wsTester = tester.get(WsTester.class);
+ ruleService = tester.get(RuleService.class);
+ ruleDao = tester.get(RuleDao.class);
+ session = tester.get(DbClient.class).openSession(false);
+ }
+
+ @After
+ public void after() {
+ session.close();
+ }
+
+ @Test
+ public void show_rule() throws Exception {
+ MockUserSession.set()
+ .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+ .setLogin("me");
+
+ RuleDto ruleDto = ruleDao.insert(session,
+ RuleTesting.newDto(RuleKey.of("java", "S001"))
+ .setName("Rule S001")
+ .setDescription("Rule S001 <b>description</b>")
+ .setSeverity(Severity.MINOR)
+ .setStatus(RuleStatus.BETA)
+ .setConfigKey("InternalKeyS001")
+ .setLanguage("xoo")
+ .setRemediationFunction("LINEAR_OFFSET")
+ .setRemediationCoefficient("5d")
+ .setRemediationOffset("10h")
+ .setTags(newHashSet("tag1", "tag2"))
+ .setSystemTags(newHashSet("systag1", "systag2"))
+ );
+ RuleParamDto param = RuleParamDto.createFor(ruleDto).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
+ ruleDao.addRuleParam(session, ruleDto, param);
+ session.commit();
+ session.clearCache();
+
+ WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show")
+ .setParam("key", ruleDto.getKey().toString());
+ request.execute().assertJson(getClass(), "show_rule.json", false);
+ }
+
+ @Test
+ public void encode_html_description_of_custom_rule() throws Exception {
+ MockUserSession.set()
+ .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+ .setLogin("me");
+
+ // Template rule
+ RuleDto templateRule = ruleDao.insert(session, RuleTesting.newTemplateRule(RuleKey.of("java", "S001")));
+ session.commit();
+
+ // Custom rule
+ NewRule customRule = NewRule.createForCustomRule("MY_CUSTOM", templateRule.getKey())
+ .setName("My custom")
+ .setSeverity(Severity.MINOR)
+ .setStatus(RuleStatus.READY)
+ .setHtmlDescription("<div>line1\nline2</div>");
+ RuleKey customRuleKey = ruleService.create(customRule);
+ session.clearCache();
+
+ WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show")
+ .setParam("key", customRuleKey.toString());
+ request.execute().assertJson(getClass(), "encode_html_description_of_custom_rule.json", false);
+ }
+
+ @Test
+ public void encode_html_description_of_manual_rule() throws Exception {
+ MockUserSession.set()
+ .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+ .setLogin("me");
+
+ // Manual rule
+ NewRule manualRule = NewRule.createForManualRule("MY_MANUAL")
+ .setName("My manual")
+ .setSeverity(Severity.MINOR)
+ .setHtmlDescription("<div>line1\nline2</div>");
+ RuleKey customRuleKey = ruleService.create(manualRule);
+ session.clearCache();
+
+ WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show")
+ .setParam("key", customRuleKey.toString());
+ request.execute().assertJson(getClass(), "encode_html_description_of_manual_rule.json", false);
+ }
+
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java
index dfc5483a7f2..8a82074bf6b 100644
--- a/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java
@@ -25,8 +25,6 @@ import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
@@ -46,7 +44,6 @@ import org.sonar.server.ws.WsTester;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
-@RunWith(MockitoJUnitRunner.class)
public class UpdateActionMediumTest {
@ClassRule
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_custom_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_custom_rule.json
new file mode 100644
index 00000000000..4e595e14eeb
--- /dev/null
+++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_custom_rule.json
@@ -0,0 +1,7 @@
+{
+ "rule": {
+ "key": "java:MY_CUSTOM",
+ "htmlDesc": "&lt;div&gt;line1<br/>line2&lt;/div&gt;",
+ "templateKey": "java:S001"
+ }
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_manual_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_manual_rule.json
new file mode 100644
index 00000000000..8708432c1b6
--- /dev/null
+++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/encode_html_description_of_manual_rule.json
@@ -0,0 +1,6 @@
+{
+ "rule": {
+ "key": "manual:MY_MANUAL",
+ "htmlDesc": "&lt;div&gt;line1<br/>line2&lt;/div&gt;"
+ }
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json
new file mode 100644
index 00000000000..418d0835186
--- /dev/null
+++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule.json
@@ -0,0 +1,24 @@
+{
+ "rule": {
+ "key": "java:S001",
+ "repo": "java",
+ "name": "Rule S001",
+ "htmlDesc": "Rule S001 <b>description</b>",
+ "severity": "MINOR",
+ "status": "BETA",
+ "isTemplate": false,
+ "tags": ["tag1", "tag2"],
+ "sysTags": ["systag1", "systag2"],
+ "debtRemFnType": "LINEAR_OFFSET",
+ "debtRemFnCoeff": "5d",
+ "debtRemFnOffset": "10h",
+ "lang": "xoo",
+ "params": [
+ {
+ "key": "regex",
+ "desc": "Reg ex",
+ "defaultValue": ".*"
+ }
+ ]
+ }
+}