From cce7142d67b15a9621bd279eb3eca229b1a5db65 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 6 Feb 2014 13:10:06 +0100 Subject: [PATCH] Fix NPE when severity is null on manual rules --- .../main/java/org/sonar/api/rules/Rule.java | 3 ++- .../server/rule/ws/RuleShowWsHandler.java | 4 +++- .../server/rule/ws/RuleShowWsHandlerTest.java | 24 ++++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index 070ba5b4501..c3596856c74 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -35,6 +35,7 @@ import org.sonar.check.Cardinality; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.persistence.*; + import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -42,7 +43,7 @@ import java.util.Set; @Entity @Table(name = "rules") -public final class Rule { +public class Rule { /** * @since 3.6 diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleShowWsHandler.java b/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleShowWsHandler.java index f3537f60e53..6efedab37c1 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleShowWsHandler.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleShowWsHandler.java @@ -24,6 +24,7 @@ import com.google.common.base.Strings; import org.sonar.api.i18n.I18n; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RulePriority; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; @@ -76,12 +77,13 @@ public class RuleShowWsHandler implements RequestHandler { if (ruleKey.repository().equals(Rule.MANUAL_REPOSITORY_KEY)) { org.sonar.api.rules.Rule rule = ruleFinder.findByKey(ruleKey); if (rule != null) { + RulePriority severity = rule.getSeverity(); return new Rule.Builder() .setKey(rule.getKey()) .setRepositoryKey(rule.getRepositoryKey()) .setName(rule.getName()) .setDescription(rule.getDescription()) - .setSeverity(rule.getSeverity().name()) + .setSeverity(severity != null ? severity.name() : null) .setStatus(rule.getStatus()) .setCreatedAt(rule.getCreatedAt()) .setUpdatedAt(rule.getUpdatedAt()).build(); 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 c380f753f9e..5a94be39387 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 @@ -149,9 +149,10 @@ public class RuleShowWsHandlerTest { } @Test - public void show_manuel_rule() throws Exception { + public void show_manual_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")); + 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); @@ -160,7 +161,24 @@ public class RuleShowWsHandlerTest { } @Test - public void return_not_found_on_unkwown_manual_rule() throws Exception { + public void show_manual_rule_without_severity() throws Exception { + String ruleKey = "manual:api"; + org.sonar.api.rules.Rule rule = mock(org.sonar.api.rules.Rule.class); + when(rule.getKey()).thenReturn("api"); + when(rule.getRepositoryKey()).thenReturn("manual"); + when(rule.getName()).thenReturn("API"); + when(rule.getDescription()).thenReturn("API rule description"); + when(rule.getSeverity()).thenReturn(null); + when(ruleFinder.findByKey(RuleKey.of("manual", "api"))).thenReturn(rule); + + 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_unknown_manual_rule() throws Exception { String ruleKey = "manual:api"; when(rules.findByKey(RuleKey.of("squid", "AvoidCycle"))).thenReturn(null); -- 2.39.5