use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RegisterRulesMediumTest method do_not_update_custom_rule_if_no_template_change.
@Test
public void do_not_update_custom_rule_if_no_template_change() {
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 template = ruleDao.selectOrFailByKey(dbSession, RuleKey.of("xoo", "T1"));
// Create custom rule
RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewCustomRule.createForCustomRule("CUSTOM_RULE", template.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("format", "txt")));
Long updatedAt = ruleDao.selectOrFailByKey(dbSession, customRuleKey).getUpdatedAt();
register(rules);
// Verify custom rule has been restore from the template
RuleDto customRuleReloaded = ruleDao.selectOrFailByKey(dbSession, customRuleKey);
assertThat(customRuleReloaded.getUpdatedAt()).isEqualTo(updatedAt);
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RegisterRulesMediumTest method update_custom_rule_on_template_change.
@Test
public void update_custom_rule_on_template_change() {
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 template = ruleDao.selectOrFailByKey(dbSession, RuleKey.of("xoo", "T1"));
// Create custom rule
RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewCustomRule.createForCustomRule("CUSTOM_RULE", template.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("format", "txt")));
// Update template and restart
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule rule = repository.createRule("T1").setName("template1 name").setHtmlDescription("template1 desc").setSeverity(Severity.BLOCKER).setStatus(RuleStatus.BETA).setTemplate(true).setInternalKey("new_internal");
rule.setDebtRemediationFunction(rule.debtRemediationFunctions().linearWithOffset("1h", "30min")).setGapDescription("Effort");
}
});
// Verify custom rule has been restore from the template
RuleDto customRule = ruleDao.selectOrFailByKey(dbSession, customRuleKey);
assertThat(customRule.getLanguage()).isEqualTo("xoo");
assertThat(customRule.getConfigKey()).isEqualTo("new_internal");
assertThat(customRule.getSeverityString()).isEqualTo(Severity.BLOCKER);
assertThat(customRule.getStatus()).isEqualTo(RuleStatus.BETA);
assertThat(customRule.getDefaultRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
assertThat(customRule.getGapDescription()).isEqualTo("Effort");
assertThat(ruleIndex.search(new RuleQuery().setKey(customRuleKey.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleCreatorMediumTest method create_custom_rule.
@Test
public void create_custom_rule() {
// insert template rule
RuleDto templateRule = createTemplateRule();
// Create custom rule
NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setMarkdownDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", "a.*"));
RuleKey customRuleKey = creator.create(newRule);
dbSession.clearCache();
RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, customRuleKey);
assertThat(rule).isNotNull();
assertThat(rule.getKey()).isEqualTo(RuleKey.of("java", "CUSTOM_RULE"));
assertThat(rule.getTemplateId()).isEqualTo(templateRule.getId());
assertThat(rule.getName()).isEqualTo("My custom");
assertThat(rule.getDescription()).isEqualTo("Some description");
assertThat(rule.getSeverityString()).isEqualTo("MAJOR");
assertThat(rule.getStatus()).isEqualTo(RuleStatus.READY);
assertThat(rule.getLanguage()).isEqualTo("java");
assertThat(rule.getConfigKey()).isEqualTo("S001");
assertThat(rule.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET");
assertThat(rule.getDefaultRemediationGapMultiplier()).isEqualTo("1h");
assertThat(rule.getDefaultRemediationBaseEffort()).isEqualTo("5min");
assertThat(rule.getGapDescription()).isEqualTo("desc");
assertThat(rule.getTags()).containsOnly("usertag1", "usertag2");
assertThat(rule.getSystemTags()).containsOnly("tag1", "tag4");
List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
assertThat(params).hasSize(1);
RuleParamDto param = params.get(0);
// From template rule
assertThat(param.getName()).isEqualTo("regex");
assertThat(param.getDescription()).isEqualTo("Reg ex");
assertThat(param.getType()).isEqualTo("STRING");
// From user
assertThat(param.getDefaultValue()).isEqualTo("a.*");
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(customRuleKey, templateRule.getKey());
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_any_of_statuses.
@Test
public void search_by_any_of_statuses() {
indexRules(newDoc(RuleKey.of("java", "S001")).setStatus(RuleStatus.BETA.name()), newDoc(RuleKey.of("java", "S002")).setStatus(RuleStatus.READY.name()));
RuleQuery query = new RuleQuery().setStatuses(asList(RuleStatus.DEPRECATED, RuleStatus.READY));
SearchIdResult<RuleKey> results = index.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(RuleKey.of("java", "S002"));
// no results
query = new RuleQuery().setStatuses(singletonList(RuleStatus.DEPRECATED));
assertThat(index.search(query, new SearchOptions()).getIds()).isEmpty();
// empty list => no filter
query = new RuleQuery().setStatuses(Collections.emptyList());
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
// null list => no filter
query = new RuleQuery().setStatuses(null);
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_case_insensitive_key.
@Test
public void search_by_case_insensitive_key() {
RuleKey ruleKey = RuleKey.of("javascript", "X001");
indexRules(newDoc(ruleKey).setName("Name without key").setHtmlDescription("Description without key"));
RuleQuery query = new RuleQuery().setQueryText("x001");
assertThat(index.search(query, new SearchOptions()).getIds()).containsOnly(ruleKey);
}
Aggregations