]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9170 do not allow to create custom rules of removed templates
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Tue, 9 May 2017 11:46:44 +0000 (13:46 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Wed, 10 May 2017 21:06:42 +0000 (23:06 +0200)
server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java

index 7f127c5c92fe03505130e684b688196e8ccbf658..a1bba61a37600d974fa1a0bda32da07bcf54573c 100644 (file)
@@ -77,6 +77,7 @@ public class RuleCreator {
     RuleDto templateRule = dbClient.ruleDao().selectByKey(dbSession, defaultOrganization, templateKey)
       .orElseThrow(() -> new IllegalArgumentException(format("The template key doesn't exist: %s", templateKey)));
     checkArgument(templateRule.isTemplate(), "This rule is not a template rule: %s", templateKey.toString());
+    checkArgument(templateRule.getStatus() != RuleStatus.REMOVED, "The template key doesn't exist: %s", templateKey.toString());
     validateCustomRule(newRule, dbSession, templateKey);
 
     RuleKey customRuleKey = RuleKey.of(templateRule.getRepositoryKey(), newRule.ruleKey());
index 2e66cde28c795bc4b2b2349cd4c538417fbf72e1..360cb45b4c8e9987aedf218cfe40f45c3ee18e90 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.text.MacroInterpreter;
+import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
@@ -161,6 +162,44 @@ public class CreateActionTest {
       "}\n");
   }
 
+  @Test
+  public void create_custom_rule_of_non_existing_template_should_fail() {
+    logInAsQProfileAdministrator();
+
+    TestRequest request = ws.newRequest()
+      .setParam("custom_key", "MY_CUSTOM")
+      .setParam("template_key", "non:existing")
+      .setParam("name", "My custom rule")
+      .setParam("markdown_description", "Description")
+      .setParam("severity", "MAJOR")
+      .setParam("prevent_reactivation", "true");
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("The template key doesn't exist: non:existing");
+
+    request.execute();
+  }
+
+  @Test
+  public void create_custom_rule_of_removed_template_should_fail() {
+    logInAsQProfileAdministrator();
+
+    RuleDefinitionDto templateRule = db.rules().insert(r -> r.setIsTemplate(true).setStatus(RuleStatus.REMOVED));
+
+    TestRequest request = ws.newRequest()
+      .setParam("custom_key", "MY_CUSTOM")
+      .setParam("template_key", templateRule.getKey().toString())
+      .setParam("name", "My custom rule")
+      .setParam("markdown_description", "Description")
+      .setParam("severity", "MAJOR")
+      .setParam("prevent_reactivation", "true");
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("The template key doesn't exist: " + templateRule.getKey());
+
+    request.execute();
+  }
+
   @Test
   public void throw_ForbiddenException_if_not_profile_administrator() throws Exception {
     userSession.logIn();