use of org.sonar.api.rule.RuleKey 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");
}
use of org.sonar.api.rule.RuleKey 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);
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleCreatorMediumTest method create_custom_rule_with_multiple_parameter_values.
@Test
public void create_custom_rule_with_multiple_parameter_values() {
// insert template rule
RuleDto templateRule = createTemplateRuleWithIntArrayParam();
NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("myIntegers", "1,3"));
RuleKey customRuleKey = creator.create(newRule);
dbSession.clearCache();
List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
assertThat(params).hasSize(1);
RuleParamDto param = params.get(0);
assertThat(param.getName()).isEqualTo("myIntegers");
assertThat(param.getDescription()).isEqualTo("My Integers");
assertThat(param.getType()).isEqualTo("INTEGER,multiple=true,values=1;2;3");
assertThat(param.getDefaultValue()).isEqualTo("1,3");
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleDeleterMediumTest method fail_to_delete_if_not_custom.
@Test
public void fail_to_delete_if_not_custom() {
// Create rule
RuleKey ruleKey = RuleKey.of("java", "S001");
dao.insert(dbSession, RuleTesting.newDto(ruleKey));
dbSession.commit();
try {
// Delete rule
deleter.delete(ruleKey);
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Only custom rules can be deleted");
}
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_key.
@Test
public void search_by_key() {
RuleKey js1 = RuleKey.of("javascript", "X001");
RuleKey cobol1 = RuleKey.of("cobol", "X001");
RuleKey php2 = RuleKey.of("php", "S002");
indexRules(newDoc(js1).setName("First rule").setHtmlDescription("The first rule"), newDoc(cobol1).setName("Second rule").setHtmlDescription("The second rule"), newDoc(php2).setName("Third rule").setHtmlDescription("The third rule"));
// key
RuleQuery query = new RuleQuery().setQueryText("X001");
assertThat(index.search(query, new SearchOptions()).getIds()).containsOnly(js1, cobol1);
// partial key does not match
query = new RuleQuery().setQueryText("X00");
assertThat(index.search(query, new SearchOptions()).getIds()).isEmpty();
// repo:key -> nice-to-have !
query = new RuleQuery().setQueryText("javascript:X001");
assertThat(index.search(query, new SearchOptions()).getIds()).containsOnly(js1);
}
Aggregations