Search in sources :

Example 61 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RegisterRulesMediumTest method do_not_update_custom_rule_params_from_template.

@Test
public void do_not_update_custom_rule_params_from_template() {
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("T1").setName("template1 name").setHtmlDescription("template1 desc").setSeverity(Severity.MAJOR).setTemplate(true).createParam("format").setDefaultValue("csv").setType(RuleParamType.STRING).setDescription("format parameter");
        }
    });
    RuleDto templateRule = ruleDao.selectOrFailByKey(dbSession, RuleKey.of("xoo", "T1"));
    // Create custom rule
    RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("format", "txt")));
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("T1").setName("template1 name").setHtmlDescription("template1 desc").setSeverity(Severity.MAJOR).setTemplate(true).createParam("format2").setDefaultValue("csv").setType(RuleParamType.STRING).setDescription("format parameter");
        }
    });
    // Verify custom rule param has not been changed!
    List<RuleParamDto> customRuleParams = ruleDao.selectRuleParamsByRuleKey(dbSession, customRuleKey);
    assertThat(customRuleParams.get(0).getName()).isEqualTo("format");
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Example 62 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RegisterRulesMediumTest method disable_custom_rules_if_template_disabled.

@Test
public void disable_custom_rules_if_template_disabled() {
    Rules rules = new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("T1").setName("template1 name").setHtmlDescription("template1 desc").setSeverity(Severity.MAJOR).setTemplate(true).createParam("format").setDefaultValue("csv").setType(RuleParamType.STRING).setDescription("format parameter");
        }
    };
    register(rules);
    RuleDto templateRule = ruleDao.selectOrFailByKey(dbSession, RuleKey.of("xoo", "T1"));
    // Create custom rule
    RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("format", "txt")));
    assertThat(ruleDao.selectOrFailByKey(dbSession, customRuleKey).getStatus()).isEqualTo(RuleStatus.READY);
    // Restart without template
    register(null);
    // Verify custom rule is removed
    assertThat(ruleDao.selectOrFailByKey(dbSession, templateRule.getKey()).getStatus()).isEqualTo(RuleStatus.REMOVED);
    assertThat(ruleDao.selectOrFailByKey(dbSession, customRuleKey).getStatus()).isEqualTo(RuleStatus.REMOVED);
    // Re-install template
    register(rules);
    assertThat(ruleDao.selectOrFailByKey(dbSession, templateRule.getKey()).getStatus()).isEqualTo(RuleStatus.READY);
    assertThat(ruleDao.selectOrFailByKey(dbSession, customRuleKey).getStatus()).isEqualTo(RuleStatus.READY);
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 63 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RegisterRulesMediumTest method remove_user_tags_that_are_newly_declared_as_system.

@Test
public void remove_user_tags_that_are_newly_declared_as_system() {
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setTags("tag1");
        }
    });
    RuleDto rule = ruleDao.selectOrFailByKey(dbSession, RuleTesting.XOO_X1);
    assertThat(rule.getSystemTags()).containsOnly("tag1");
    assertThat(rule.getTags()).isEmpty();
    // User adds tag
    TESTER.get(RuleUpdater.class).update(dbSession, RuleUpdate.createForPluginRule(RuleTesting.XOO_X1).setTags(newHashSet("tag2")), userSessionRule);
    dbSession.clearCache();
    rule = ruleDao.selectOrFailByKey(dbSession, RuleTesting.XOO_X1);
    assertThat(rule.getSystemTags()).containsOnly("tag1");
    assertThat(rule.getTags()).containsOnly("tag2");
    // Definition updated -> user tag "tag2" becomes a system tag
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setTags("tag1", "tag2");
        }
    });
    rule = ruleDao.selectOrFailByKey(dbSession, RuleTesting.XOO_X1);
    assertThat(rule.getSystemTags()).containsOnly("tag1", "tag2");
    assertThat(rule.getTags()).isEmpty();
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 64 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RegisterRulesTest method add_new_tag.

@Test
public void add_new_tag() {
    execute(new RulesDefinition() {

        @Override
        public void define(RulesDefinition.Context context) {
            RulesDefinition.NewRepository repo = context.createRepository("fake", "java");
            repo.createRule("rule1").setName("Rule One").setHtmlDescription("Description of Rule One").setTags("tag1");
            repo.done();
        }
    });
    RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
    assertThat(rule.getSystemTags()).containsOnly("tag1");
    execute(new RulesDefinition() {

        @Override
        public void define(RulesDefinition.Context context) {
            RulesDefinition.NewRepository repo = context.createRepository("fake", "java");
            repo.createRule("rule1").setName("Rule One").setHtmlDescription("Description of Rule One").setTags("tag1", "tag2");
            repo.done();
        }
    });
    rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
    assertThat(rule.getSystemTags()).containsOnly("tag1", "tag2");
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 65 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RegisterRulesTest method remove_system_tags_when_plugin_does_not_provide_any.

@Test
public void remove_system_tags_when_plugin_does_not_provide_any() {
    // Rule already exists in DB, with some system tags
    dbClient.ruleDao().insert(dbTester.getSession(), new RuleDto().setRuleKey("rule1").setRepositoryKey("findbugs").setName("Rule One").setDescription("Rule one description").setDescriptionFormat(RuleDto.Format.HTML).setSystemTags(newHashSet("tag1", "tag2")));
    dbTester.getSession().commit();
    // Synchronize rule without tag
    execute(new FindbugsRepository());
    List<RuleDto> rules = dbClient.ruleDao().selectAll(dbTester.getSession());
    assertThat(rules).hasSize(1);
    RuleDto result = rules.get(0);
    assertThat(result.getKey()).isEqualTo(RuleKey.of("findbugs", "rule1"));
    assertThat(result.getSystemTags()).isEmpty();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Aggregations

RuleDto (org.sonar.db.rule.RuleDto)197 Test (org.junit.Test)140 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)80 ComponentDto (org.sonar.db.component.ComponentDto)47 WsTester (org.sonar.server.ws.WsTester)38 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)29 RuleParamDto (org.sonar.db.rule.RuleParamDto)29 IssueDto (org.sonar.db.issue.IssueDto)28 RuleKey (org.sonar.api.rule.RuleKey)24 SearchOptions (org.sonar.server.es.SearchOptions)16 RuleQuery (org.sonar.server.rule.index.RuleQuery)16 BadRequestException (org.sonar.server.exceptions.BadRequestException)15 RuleTesting.newRuleDto (org.sonar.db.rule.RuleTesting.newRuleDto)14 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)12 IssueIndexer (org.sonar.server.issue.index.IssueIndexer)10 Date (java.util.Date)9 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)8 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)7 ArrayList (java.util.ArrayList)6