diff options
Diffstat (limited to 'sonar-server/src/test')
6 files changed, 91 insertions, 6 deletions
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java index 4849a182ea2..8f02e61607b 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java @@ -71,6 +71,7 @@ public class RuleCreatorMediumTest { // Create custom rule NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setName("My custom") .setHtmlDescription("Some description") @@ -83,6 +84,7 @@ public class RuleCreatorMediumTest { RuleDto rule = db.ruleDao().getNullableByKey(dbSession, customRuleKey); assertThat(rule).isNotNull(); + assertThat(rule.getKey()).isEqualTo(RuleKey.of("java", "CUSTOM_RULE")); assertThat(rule.getParentId()).isEqualTo(templateRule.getId()); assertThat(rule.getName()).isEqualTo("My custom"); assertThat(rule.getDescription()).isEqualTo("Some description"); @@ -111,12 +113,88 @@ public class RuleCreatorMediumTest { } @Test + public void fail_to_create_custom_rule_when_missing_key() throws Exception { + // insert template rule + RuleDto templateRule = createTemplateRule(); + + NewRule newRule = new NewRule() + .setName("My custom") + .setTemplateKey(templateRule.getKey()) + .setHtmlDescription("Some description") + .setSeverity(Severity.MAJOR) + .setStatus(RuleStatus.READY) + .setParameters(ImmutableMap.of("regex", "a.*")); + + try { + creator.create(newRule); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The rule key is missing"); + } + } + + @Test + public void fail_to_create_custom_rule_when_invalid_key() throws Exception { + // insert template rule + RuleDto templateRule = createTemplateRule(); + + NewRule newRule = new NewRule() + .setRuleKey("*INVALID*") + .setName("My custom") + .setTemplateKey(templateRule.getKey()) + .setHtmlDescription("Some description") + .setSeverity(Severity.MAJOR) + .setStatus(RuleStatus.READY) + .setParameters(ImmutableMap.of("regex", "a.*")); + + try { + creator.create(newRule); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The rule key '*INVALID*' is invalid, it should only contains : a-z, 0-9, '_'"); + } + } + + @Test + public void fail_to_create_custom_rule_when_rule_key_already_exits() throws Exception { + // insert template rule + RuleDto templateRule = createTemplateRule(); + + // Create a custom rule + NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") + .setName("My custom") + .setTemplateKey(templateRule.getKey()) + .setHtmlDescription("Some description") + .setSeverity(Severity.MAJOR) + .setStatus(RuleStatus.READY) + .setParameters(ImmutableMap.of("regex", "a.*")); + creator.create(newRule); + + try { + // Create another custom rule having same key + newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") + .setName("My another custom") + .setTemplateKey(templateRule.getKey()) + .setHtmlDescription("Some description") + .setSeverity(Severity.MAJOR) + .setStatus(RuleStatus.READY) + .setParameters(ImmutableMap.of("regex", "a.*")); + creator.create(newRule); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("A rule with the key 'CUSTOM_RULE' already exits"); + } + } + + @Test public void fail_to_create_custom_rule_when_missing_name() throws Exception { // insert template rule RuleDto templateRule = createTemplateRule(); - // Create custom rule without name NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setHtmlDescription("Some description") .setSeverity(Severity.MAJOR) @@ -136,8 +214,8 @@ public class RuleCreatorMediumTest { // insert template rule RuleDto templateRule = createTemplateRule(); - // Create custom rule without description NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setName("My custom") .setSeverity(Severity.MAJOR) @@ -157,8 +235,8 @@ public class RuleCreatorMediumTest { // insert template rule RuleDto templateRule = createTemplateRule(); - // Create custom rule without description NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setName("My custom") .setHtmlDescription("Some description") @@ -178,8 +256,8 @@ public class RuleCreatorMediumTest { // insert template rule RuleDto templateRule = createTemplateRule(); - // Create custom rule without description NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setName("My custom") .setHtmlDescription("Some description") @@ -200,8 +278,8 @@ public class RuleCreatorMediumTest { // insert template rule RuleDto templateRule = createTemplateRule(); - // Create custom rule without description NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setName("My custom") .setHtmlDescription("Some description") @@ -226,6 +304,7 @@ public class RuleCreatorMediumTest { // Create custom rule with unknown template rule NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(rule.getKey()) .setName("My custom") .setHtmlDescription("Some description") @@ -246,8 +325,8 @@ public class RuleCreatorMediumTest { // insert template rule RuleDto templateRule = createTemplateRule(); - // Create custom rule NewRule newRule = new NewRule() + .setRuleKey("CUSTOM_RULE") .setTemplateKey(templateRule.getKey()) .setName("My custom") .setHtmlDescription("Some description") diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java index 0f5b80eb841..87c1d753bef 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java @@ -139,6 +139,7 @@ public class RuleServiceMediumTest { // Create custom rule NewRule newRule = new NewRule() + .setRuleKey("MY_CUSTOM") .setTemplateKey(templateRuleKey) .setName("My custom") .setHtmlDescription("Some description") @@ -173,6 +174,7 @@ public class RuleServiceMediumTest { // Create custom rule NewRule newRule = new NewRule() + .setRuleKey("MY_CUSTOM") .setTemplateKey(templateRuleKey) .setName("My custom") .setHtmlDescription("Some description") diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionMediumTest.java index 923f36e4ab4..4627de3acb6 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/CreateActionMediumTest.java @@ -79,6 +79,7 @@ public class CreateActionMediumTest { session.commit(); WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "create") + .setParam("key", "MY_CUSTOM") .setParam("template_key", templateRule.getKey().toString()) .setParam("name", "My custom rule") .setParam("html_description", "Description") diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java index e2f054094a0..caefdad1159 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java @@ -84,6 +84,7 @@ public class UpdateActionMediumTest { // Custom rule NewRule newRule = new NewRule() + .setRuleKey("MY_CUSTOM") .setTemplateKey(templateRule.getKey()) .setName("Old custom") .setHtmlDescription("Old description") diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/CreateActionMediumTest/create_custom_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/CreateActionMediumTest/create_custom_rule.json index 53d351c6790..648e6c9b2fe 100644 --- a/sonar-server/src/test/resources/org/sonar/server/rule/ws/CreateActionMediumTest/create_custom_rule.json +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/CreateActionMediumTest/create_custom_rule.json @@ -1,5 +1,6 @@ { "rule": { + "key": "java:MY_CUSTOM", "repo": "java", "name": "My custom rule", "htmlDesc": "Description", diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/UpdateActionMediumTest/update_custom_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/UpdateActionMediumTest/update_custom_rule.json index 53d351c6790..648e6c9b2fe 100644 --- a/sonar-server/src/test/resources/org/sonar/server/rule/ws/UpdateActionMediumTest/update_custom_rule.json +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/UpdateActionMediumTest/update_custom_rule.json @@ -1,5 +1,6 @@ { "rule": { + "key": "java:MY_CUSTOM", "repo": "java", "name": "My custom rule", "htmlDesc": "Description", |