From a908b2efd262ad73e7ac88069832dfba6e1b96cb Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 3 Feb 2014 13:29:16 +0100 Subject: [PATCH] RuleShowWS : Return manual rules --- .../main/java/org/sonar/server/rule/Rule.java | 19 +++++++ .../server/rule/ws/RuleShowWsHandlerTest.java | 54 ++++++++++++++++++- .../show_manuel_rule.json | 9 ++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/ws/RuleShowWsHandlerTest/show_manuel_rule.json diff --git a/sonar-server/src/main/java/org/sonar/server/rule/Rule.java b/sonar-server/src/main/java/org/sonar/server/rule/Rule.java index d764e5b03e7..9e084358159 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/Rule.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/Rule.java @@ -32,6 +32,8 @@ import java.util.Date; import java.util.List; import java.util.Map; +import static com.google.common.collect.Lists.newArrayList; + public class Rule { private int id; @@ -51,6 +53,23 @@ public class Rule { private RuleNote ruleNote; private List params; + /** + * Used to create manual rule + */ + public Rule(String key, String name, String description, String repositoryKey, String severity, String status, Date createdAt, Date updatedAt) { + this.key = key; + this.name = name; + this.description = description; + this.repositoryKey = repositoryKey; + this.severity = severity; + this.status = status; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + this.systemTags = newArrayList(); + this.adminTags = newArrayList(); + + } + public Rule(Map ruleSource) { id = (Integer) ruleSource.get(RuleDocument.FIELD_ID); key = (String) ruleSource.get(RuleDocument.FIELD_KEY); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java index 09ff30706aa..3c4e934f850 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java @@ -28,8 +28,10 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.i18n.I18n; import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.RuleFinder; import org.sonar.api.server.ws.WsTester; import org.sonar.api.utils.DateUtils; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.rule.Rule; import org.sonar.server.rule.RuleNote; import org.sonar.server.rule.Rules; @@ -38,6 +40,8 @@ import org.sonar.server.user.MockUserSession; import java.util.Date; import java.util.Locale; +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; @@ -49,6 +53,9 @@ public class RuleShowWsHandlerTest { @Mock Rules rules; + @Mock + RuleFinder ruleFinder; + @Mock I18n i18n; @@ -56,7 +63,7 @@ public class RuleShowWsHandlerTest { @Before public void setUp() throws Exception { - tester = new WsTester(new RulesWs(new RuleShowWsHandler(rules, i18n), mock(AddTagsWsHandler.class), mock(RemoveTagsWsHandler.class))); + tester = new WsTester(new RulesWs(new RuleShowWsHandler(rules, ruleFinder, i18n), mock(AddTagsWsHandler.class), mock(RemoveTagsWsHandler.class))); } @Test @@ -71,6 +78,23 @@ public class RuleShowWsHandlerTest { request.execute().assertJson(getClass(), "show_rule.json"); } + @Test + public void return_not_found_on_unkwown_rule() throws Exception { + String ruleKey = "squid:AvoidCycle"; + + when(rules.findByKey(RuleKey.of("squid", "AvoidCycle"))).thenReturn(null); + + MockUserSession.set(); + WsTester.TestRequest request = tester.newRequest("show").setParam("key", ruleKey); + + try { + request.execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(NotFoundException.class); + } + } + @Test public void show_rule_with_dates() throws Exception { Date date1 = DateUtils.parseDateTime("2014-01-22T19:10:03+0100"); @@ -115,6 +139,34 @@ public class RuleShowWsHandlerTest { request.execute().assertJson(getClass(), "show_rule_with_tags.json"); } + @Test + public void show_manuel_rule() throws Exception { + String ruleKey = "manual:api"; + when(ruleFinder.findByKey(RuleKey.of("manual", "api"))).thenReturn(org.sonar.api.rules.Rule.create("manual", "api", "API").setDescription("API rule description")); + + MockUserSession.set(); + WsTester.TestRequest request = tester.newRequest("show").setParam("key", ruleKey); + request.execute(); + request.execute().assertJson(getClass(), "show_manuel_rule.json"); + } + + @Test + public void return_not_found_on_unkwown_manual_rule() throws Exception { + String ruleKey = "manual:api"; + + when(rules.findByKey(RuleKey.of("squid", "AvoidCycle"))).thenReturn(null); + + MockUserSession.set(); + WsTester.TestRequest request = tester.newRequest("show").setParam("key", ruleKey); + + try { + request.execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(NotFoundException.class); + } + } + private Rule create(String repoKey, String key, String name, String description) { Rule mockRule = mock(Rule.class); when(mockRule.repositoryKey()).thenReturn(repoKey); diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/RuleShowWsHandlerTest/show_manuel_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RuleShowWsHandlerTest/show_manuel_rule.json new file mode 100644 index 00000000000..d272b57329b --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RuleShowWsHandlerTest/show_manuel_rule.json @@ -0,0 +1,9 @@ +{ + "rule": { + "key": "manual:api", + "name": "API", + "description": "API rule description", + "tags": [], + "sysTags": [] + } +} -- 2.39.5