]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4514 Add the possibility to delete a manual rule
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 16 Jun 2014 13:21:06 +0000 (15:21 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 16 Jun 2014 13:21:16 +0000 (15:21 +0200)
sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java
sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java

index 60860b3e114254ff4e36cd51f4f87eee9214127c..29ff1f22a28ade810ba3c008913d551d0cf527d5 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.core.persistence.DbSession;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.qualityprofile.RuleActivator;
+import org.sonar.server.rule.index.RuleDoc;
 
 public class RuleDeleter implements ServerComponent {
 
@@ -42,10 +43,14 @@ public class RuleDeleter implements ServerComponent {
     DbSession dbSession = dbClient.openSession(false);
     try {
       RuleDto rule = dbClient.ruleDao().getByKey(dbSession, ruleKey);
-      if (rule.getTemplateId() == null) {
-        throw new IllegalStateException("Only custom rules can be deleted");
+      if (rule.getTemplateId() == null && !rule.getRepositoryKey().equals(RuleDoc.MANUAL_REPOSITORY)) {
+        throw new IllegalStateException("Only custom rules and manual rules can be deleted");
+      }
+
+      // For custom rule, first deactivate the rule on all profiles
+      if (rule.getTemplateId() != null) {
+        ruleActivator.deactivate(dbSession, rule);
       }
-      ruleActivator.deactivate(dbSession, rule);
 
       rule.setStatus(RuleStatus.REMOVED);
       dbClient.ruleDao().update(dbSession, rule);
index 5d26d525adb5010d70d6868095a4c0f64effe0ae..52e633f4e83411a41ee63c5428117878613e71f8 100644 (file)
@@ -87,7 +87,7 @@ public class RuleDeleterMediumTest {
     // Activate the custom rule
     tester.get(RuleActivator.class).activate(
       new RuleActivation(ActiveRuleKey.of(profileDto.getKey(), customRule.getKey())).setSeverity(Severity.BLOCKER)
-    );
+      );
 
     // Delete custom rule
     deleter.delete(customRule.getKey());
@@ -103,7 +103,24 @@ public class RuleDeleterMediumTest {
   }
 
   @Test
-  public void fail_to_delete_if_not_custom() throws Exception {
+  public void delete_manual_rule() throws Exception {
+    // Create manual rule
+    RuleDto manualRule = RuleTesting.newManualRule("Manual_Rule");
+    dao.insert(dbSession, manualRule);
+
+    dbSession.commit();
+
+    // Delete manual rule
+    deleter.delete(manualRule.getKey());
+
+    // Verify custom rule have status REMOVED
+    Rule result = index.getByKey(manualRule.getKey());
+    assertThat(result).isNotNull();
+    assertThat(result.status()).isEqualTo(RuleStatus.REMOVED);
+  }
+
+  @Test
+  public void fail_to_delete_if_not_custom_or_not_manual() throws Exception {
     // Create rule
     RuleKey ruleKey = RuleKey.of("java", "S001");
     dao.insert(dbSession, RuleTesting.newDto(ruleKey));
@@ -113,7 +130,7 @@ public class RuleDeleterMediumTest {
       // Delete rule
       deleter.delete(ruleKey);
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Only custom rules can be deleted");
+      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Only custom rules and manual rules can be deleted");
     }
   }