Search in sources :

Example 1 with RuleRepository

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

the class DeprecatedRulesDefinitionLoaderTest method fail_on_invalid_rule_debt.

@Test
public void fail_on_invalid_rule_debt() {
    RulesDefinition.Context context = new RulesDefinition.Context();
    List<DebtModelXMLExporter.RuleDebt> ruleDebts = newArrayList(new DebtModelXMLExporter.RuleDebt().setRuleKey(RuleKey.of("checkstyle", "ConstantName")).setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setCoefficient("1d"));
    Reader javaModelReader = mock(Reader.class);
    when(debtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
    when(debtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
    when(importer.importXML(eq(javaModelReader), any(ValidationMessages.class))).thenReturn(ruleDebts);
    try {
        new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, new RuleRepository[] { new CheckstyleRules() }).complete(context);
        fail();
    } catch (Exception e) {
        assertThat(e).isInstanceOf(IllegalArgumentException.class);
    }
    assertThat(context.repositories()).isEmpty();
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleRepository(org.sonar.api.rules.RuleRepository) Reader(java.io.Reader) ValidationMessages(org.sonar.api.utils.ValidationMessages) Test(org.junit.Test)

Example 2 with RuleRepository

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

the class DeprecatedRulesDefinitionLoaderTest method define_rule_debt.

@Test
public void define_rule_debt() {
    RulesDefinition.Context context = new RulesDefinition.Context();
    List<DebtModelXMLExporter.RuleDebt> ruleDebts = newArrayList(new DebtModelXMLExporter.RuleDebt().setRuleKey(RuleKey.of("checkstyle", "ConstantName")).setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setCoefficient("1d").setOffset("10min"));
    Reader javaModelReader = mock(Reader.class);
    when(debtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
    when(debtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
    when(importer.importXML(eq(javaModelReader), any(ValidationMessages.class))).thenReturn(ruleDebts);
    new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, new RuleRepository[] { new CheckstyleRules() }).complete(context);
    assertThat(context.repositories()).hasSize(1);
    RulesDefinition.Repository checkstyle = context.repository("checkstyle");
    assertThat(checkstyle.rules()).hasSize(1);
    RulesDefinition.Rule rule = checkstyle.rule("ConstantName");
    assertThat(rule).isNotNull();
    assertThat(rule.key()).isEqualTo("ConstantName");
    assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
    assertThat(rule.debtRemediationFunction().gapMultiplier()).isEqualTo("1d");
    assertThat(rule.debtRemediationFunction().baseEffort()).isEqualTo("10min");
}
Also used : DebtModelXMLExporter(org.sonar.server.debt.DebtModelXMLExporter) Reader(java.io.Reader) ValidationMessages(org.sonar.api.utils.ValidationMessages) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleRepository(org.sonar.api.rules.RuleRepository) Test(org.junit.Test)

Example 3 with RuleRepository

use of org.sonar.api.rules.RuleRepository 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

RuleRepository (org.sonar.api.rules.RuleRepository)3 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)3 Reader (java.io.Reader)2 Test (org.junit.Test)2 ValidationMessages (org.sonar.api.utils.ValidationMessages)2 RuleParam (org.sonar.api.rules.RuleParam)1 DebtModelXMLExporter (org.sonar.server.debt.DebtModelXMLExporter)1 RuleDebt (org.sonar.server.debt.DebtModelXMLExporter.RuleDebt)1