From 1f47755b387c6b9df30dfa9ef9fb9aee8b2fb900 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 1 Jul 2014 12:50:03 +0200 Subject: [PATCH] SONAR-5007 - Severity is null for manual rules --- .../sonar/server/rule/DefaultRuleFinder.java | 3 +- .../rule/DefaultRuleFinderMediumTest.java | 41 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java index be224ea5b0f..a7374945361 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java @@ -33,6 +33,7 @@ import org.sonar.server.search.QueryOptions; import org.sonar.server.search.Result; import javax.annotation.CheckForNull; + import java.util.Collection; import java.util.List; @@ -118,7 +119,7 @@ public class DefaultRuleFinder implements RuleFinder { .setUpdatedAt(rule.updatedAt()) .setDescription(rule.htmlDescription()) .setRepositoryKey(rule.key().repository()) - .setSeverity(RulePriority.valueOf(rule.severity())) + .setSeverity(rule.severity() != null ? RulePriority.valueOf(rule.severity()) : null) .setStatus(rule.status().name()) .setDefaultCharacteristicKey(rule.defaultDebtCharacteristicKey()) .setDefaultSubCharacteristicKey(rule.defaultDebtSubCharacteristicKey()) diff --git a/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java index c683d6234f0..92875e0c3c5 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java @@ -20,17 +20,13 @@ package org.sonar.server.rule; import org.fest.assertions.Assertions; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.*; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleQuery; import org.sonar.core.persistence.DbSession; import org.sonar.core.rule.RuleDto; import org.sonar.server.db.DbClient; -import org.sonar.server.rule.db.RuleDao; import org.sonar.server.tester.ServerTester; import java.util.Collections; @@ -44,16 +40,25 @@ public class DefaultRuleFinderMediumTest { @ClassRule public static ServerTester tester = new ServerTester(); + private DbClient dbClient; private DefaultRuleFinder finder; private DbSession session; @Before - public void setup() { + public void setUp() throws Exception { finder = tester.get(DefaultRuleFinder.class); + dbClient = tester.get(DbClient.class); + session = dbClient.openSession(false); + } - session = tester.get(DbClient.class).openSession(false); - - tester.get(RuleDao.class).insert(session, + /** + * Testing with ids required data to be identical to all tests + */ + @BeforeClass + public static void setupClass() { + tester.clearDbAndIndexes(); + DbSession session = tester.get(DbClient.class).openSession(false); + tester.get(DbClient.class).ruleDao().insert(session, new RuleDto() .setName("Check Header") .setConfigKey("Checker/Treewalker/HeaderCheck") @@ -81,9 +86,11 @@ public class DefaultRuleFinderMediumTest { .setRuleKey("CallSuperFirst") .setRepositoryKey("pmd") .setSeverity(2) - .setStatus(RuleStatus.READY) + .setStatus(RuleStatus.READY), + RuleTesting.newManualRule("Manual_Rule").setName("Manual Rule") ); session.commit(); + session.close(); } @After @@ -91,7 +98,6 @@ public class DefaultRuleFinderMediumTest { session.close(); } - @Test public void should_success_finder_wrap() { @@ -123,7 +129,7 @@ public class DefaultRuleFinderMediumTest { // find_all_enabled //Assertions.assertThat(finder.findAll(RuleQuery.create())).onProperty("id").containsOnly(1, 3, 4); - Assertions.assertThat(finder.findAll(RuleQuery.create())).hasSize(3); + Assertions.assertThat(finder.findAll(RuleQuery.create())).hasSize(4); // do_not_find_disabled_rules Assertions.assertThat(finder.findByKey("checkstyle", "DisabledCheck")).isNull(); @@ -135,4 +141,15 @@ public class DefaultRuleFinderMediumTest { tester.clearDbAndIndexes(); assertThat(finder.findByIds(Collections.emptyList())).isEmpty(); } + + @Test + public void find_manual_rule() { + // find by id + Assertions.assertThat(finder.findById(5)).isNotNull(); + + // find by key + Rule rule = finder.findByKey("manual", "Manual_Rule"); + Assertions.assertThat(rule).isNotNull(); + Assertions.assertThat(rule.isEnabled()).isTrue(); + } } -- 2.39.5