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());
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;
"}\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();