]> source.dussan.org Git - sonarqube.git/commitdiff
Fix NPE when severity is null on manual rules
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Feb 2014 12:10:06 +0000 (13:10 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Feb 2014 12:10:15 +0000 (13:10 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
sonar-server/src/main/java/org/sonar/server/rule/ws/RuleShowWsHandler.java
sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java

index 070ba5b450174be7b3fddebee73de5d982c6816a..c3596856c74dfd118874b8672802855503eefa45 100644 (file)
@@ -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
index f3537f60e53942505aab928022f712354e581514..6efedab37c1a1ac6a1b6f51d2bf111fe2f055169 100644 (file)
@@ -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();
index c380f753f9e657689a8a4cee95f659907cf35fa4..5a94be3938799de9315bd807643dbd30e3331775 100644 (file)
@@ -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);