aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-04-14 17:06:14 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-04-18 13:44:43 +0200
commit0c21171661726dcb481dd69a5732a94376f795c3 (patch)
treea37334aa3277cd97037f7bc13af09257e72e9e62 /server
parent853203cb0af367d9d3dea11388b3f1351fe83343 (diff)
downloadsonarqube-0c21171661726dcb481dd69a5732a94376f795c3.tar.gz
sonarqube-0c21171661726dcb481dd69a5732a94376f795c3.zip
SONAR-9013 Do not fail with error 500 when creating rule on unknown template key
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java5
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorTest.java14
2 files changed, 17 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
index caa3af9a65f..7f127c5c92f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
@@ -73,8 +73,9 @@ public class RuleCreator {
checkArgument(templateKey != null, "Rule template key should not be null");
String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid();
OrganizationDto defaultOrganization = dbClient.organizationDao().selectByUuid(dbSession, defaultOrganizationUuid)
- .orElseThrow(() -> new IllegalStateException(String.format("Could not find default organization for uuid '%s'", defaultOrganizationUuid)));
- RuleDto templateRule = dbClient.ruleDao().selectOrFailByKey(dbSession, defaultOrganization, templateKey);
+ .orElseThrow(() -> new IllegalStateException(format("Could not find default organization for uuid '%s'", defaultOrganizationUuid)));
+ 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());
validateCustomRule(newRule, dbSession, templateKey);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorTest.java
index 44c7c89a277..19c751e6de8 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorTest.java
@@ -443,6 +443,20 @@ public class RuleCreatorTest {
underTest.create(dbSession, newRule);
}
+ @Test
+ public void fail_to_create_custom_rule_when_unknown_template() throws Exception {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The template key doesn't exist: java:S001");
+
+ // Create custom rule
+ NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", RuleKey.of("java", "S001"))
+ .setName("My custom")
+ .setMarkdownDescription("Some description")
+ .setSeverity(Severity.MAJOR)
+ .setStatus(RuleStatus.READY);
+ underTest.create(dbSession, newRule);
+ }
+
private RuleDto createTemplateRule() {
RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001"), db.getDefaultOrganization())
.setIsTemplate(true)