Search in sources :

Example 1 with RuleParam

use of org.sonar.api.rules.RuleParam in project sonarqube by SonarSource.

the class QProfileResetMediumTest method reset_language_profile.

@Test
public void reset_language_profile() {
    RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
    defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))), RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MINOR);
            x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
    // Change the severity and the value of the parameter in the active rule
    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(ruleKey).setSeverity(BLOCKER).setParameter("acceptWhitespace", "false"), profile.getKey());
    dbSession.commit();
    // Verify severity and param has changed
    ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
    List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
    reset.resetLanguage(ServerTester.Xoo.KEY);
    dbSession.commit();
    // Severity and parameter value come back to origin after reset
    activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
    activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleParam(org.sonar.api.rules.RuleParam) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 2 with RuleParam

use of org.sonar.api.rules.RuleParam in project sonarqube by SonarSource.

the class DeprecatedRulesDefinitionLoader method complete.

void complete(RulesDefinition.Context context) {
    // Load rule debt definitions from xml files provided by plugin
    List<RuleDebt> ruleDebts = loadRuleDebtList();
    for (RuleRepository repository : repositories) {
        // RuleRepository API does not handle difference between new and extended repositories,
        RulesDefinition.NewRepository newRepository;
        if (context.repository(repository.getKey()) == null) {
            newRepository = context.createRepository(repository.getKey(), repository.getLanguage());
            newRepository.setName(repository.getName());
        } else {
            newRepository = context.extendRepository(repository.getKey(), repository.getLanguage());
        }
        for (org.sonar.api.rules.Rule rule : repository.createRules()) {
            RulesDefinition.NewRule newRule = newRepository.createRule(rule.getKey());
            newRule.setName(ruleName(repository.getKey(), rule));
            newRule.setHtmlDescription(ruleDescription(repository.getKey(), rule));
            newRule.setInternalKey(rule.getConfigKey());
            newRule.setTemplate(rule.isTemplate());
            newRule.setSeverity(rule.getSeverity().toString());
            newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus()));
            newRule.setTags(rule.getTags());
            for (RuleParam param : rule.getParams()) {
                RulesDefinition.NewParam newParam = newRule.createParam(param.getKey());
                newParam.setDefaultValue(param.getDefaultValue());
                newParam.setDescription(paramDescription(repository.getKey(), rule.getKey(), param));
                newParam.setType(RuleParamType.parse(param.getType()));
            }
            updateRuleDebtDefinitions(newRule, repository.getKey(), rule.getKey(), ruleDebts);
        }
        newRepository.done();
    }
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleParam(org.sonar.api.rules.RuleParam) RuleRepository(org.sonar.api.rules.RuleRepository) RuleDebt(org.sonar.server.debt.DebtModelXMLExporter.RuleDebt)

Aggregations

RuleParam (org.sonar.api.rules.RuleParam)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 Test (org.junit.Test)1 RulesProfile (org.sonar.api.profiles.RulesProfile)1 RuleKey (org.sonar.api.rule.RuleKey)1 RuleRepository (org.sonar.api.rules.RuleRepository)1 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)1 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)1 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)1 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)1 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)1 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)1 RuleDebt (org.sonar.server.debt.DebtModelXMLExporter.RuleDebt)1