use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_profile_and_active_severity.
@Test
public void search_by_profile_and_active_severity() {
indexRules(newDoc(RULE_KEY_1).setSeverity(MAJOR), newDoc(RULE_KEY_2).setSeverity(MINOR), newDoc(RULE_KEY_3).setSeverity(INFO));
indexActiveRules(ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1)).setSeverity(BLOCKER), ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1)).setSeverity(BLOCKER), ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2)).setSeverity(CRITICAL));
// 1. get all active rules.
assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY1), new SearchOptions()).getIds()).hasSize(2);
// 2. get rules with active severity critical.
SearchIdResult<RuleKey> result = index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY1).setActiveSeverities(singletonList(CRITICAL)), new SearchOptions().addFacets(singletonList(RuleIndex.FACET_ACTIVE_SEVERITIES)));
assertThat(result.getIds()).containsOnly(RULE_KEY_2);
// check stickyness of active severity facet
assertThat(result.getFacets().get(RuleIndex.FACET_ACTIVE_SEVERITIES)).containsOnly(entry(BLOCKER, 1L), entry(CRITICAL, 1L));
// 3. count activation severities of all active rules
result = index.search(new RuleQuery(), new SearchOptions().addFacets(singletonList(RuleIndex.FACET_ACTIVE_SEVERITIES)));
assertThat(result.getIds()).hasSize(3);
assertThat(result.getFacets().get(RuleIndex.FACET_ACTIVE_SEVERITIES)).containsOnly(entry(BLOCKER, 2L), entry(CRITICAL, 1L));
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleCreatorMediumTest method create_custom_rule_with_empty_parameter_value.
@Test
public void create_custom_rule_with_empty_parameter_value() {
// insert template rule
RuleDto templateRule = createTemplateRule();
NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", ""));
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("regex");
assertThat(param.getDescription()).isEqualTo("Reg ex");
assertThat(param.getType()).isEqualTo("STRING");
assertThat(param.getDefaultValue()).isNull();
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleCreatorMediumTest method reactivate_custom_rule_if_already_exists_in_removed_status.
@Test
public void reactivate_custom_rule_if_already_exists_in_removed_status() {
String key = "CUSTOM_RULE";
// insert template rule
RuleDto templateRule = createTemplateRule();
// insert a removed rule
RuleDto rule = RuleTesting.newCustomRule(templateRule).setRuleKey(key).setStatus(RuleStatus.REMOVED).setName("Old name").setDescription("Old description").setDescriptionFormat(Format.MARKDOWN).setSeverity(Severity.INFO);
dao.insert(dbSession, rule);
dao.insertRuleParam(dbSession, rule, dao.selectRuleParamsByRuleKey(dbSession, templateRule.getKey()).get(0).setDefaultValue("a.*"));
dbSession.commit();
dbSession.clearCache();
// Create custom rule with same key, but with different values
NewCustomRule newRule = NewCustomRule.createForCustomRule(key, templateRule.getKey()).setName("New name").setMarkdownDescription("New description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", "c.*"));
RuleKey customRuleKey = creator.create(newRule);
dbSession.clearCache();
RuleDto result = db.ruleDao().selectOrFailByKey(dbSession, customRuleKey);
assertThat(result.getKey()).isEqualTo(RuleKey.of("java", key));
assertThat(result.getStatus()).isEqualTo(RuleStatus.READY);
// These values should be the same than before
assertThat(result.getName()).isEqualTo("Old name");
assertThat(result.getDescription()).isEqualTo("Old description");
assertThat(result.getSeverityString()).isEqualTo(Severity.INFO);
List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
assertThat(params).hasSize(1);
assertThat(params.get(0).getDefaultValue()).isEqualTo("a.*");
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class DefaultRulesTest method createRule.
private NewRule createRule(String key, String repo, String internalKey) {
RuleKey ruleKey = RuleKey.of(repo, key);
NewRule newRule = new NewRule(ruleKey);
newRule.setInternalKey(internalKey);
return newRule;
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class CheckFactoryTest method param_as_primitive_fields.
@Test
public void param_as_primitive_fields() {
RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties");
builder.create(ruleKey).setParam("max", "300").setParam("ignore", "true").activate();
CheckFactory checkFactory = new CheckFactory(builder.build());
Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class);
Object check = checks.of(ruleKey);
assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class);
assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300);
assertThat(((CheckWithPrimitiveProperties) check).isIgnore()).isTrue();
}
Aggregations