use of org.sonar.server.rule.index.RuleQuery 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.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesMediumTest method deactivate_removed_rules_only_if_repository_still_exists.
@Test
public void deactivate_removed_rules_only_if_repository_still_exists() {
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
}
});
// Create a profile and activate rule
logInAsQProfileAdministrator();
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1("org-123"));
dbSession.commit();
dbSession.clearCache();
RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
TESTER.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
// Restart, repo xoo still exists -> deactivate x1
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
repository.createRule("x2").setName("x2 name").setHtmlDescription("x2 desc");
}
});
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X2.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
assertThat(ruleIndex.search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).isEmpty();
assertThat(activeRuleIndex.countAllByQualityProfileKey()).isEmpty();
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).isEmpty();
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesMediumTest method do_not_deactivate_removed_rules_if_repository_accidentally_uninstalled.
@Test
public void do_not_deactivate_removed_rules_if_repository_accidentally_uninstalled() {
Rules rules = new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
}
};
register(rules);
// create a profile and activate rule
logInAsQProfileAdministrator();
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1("org-123"));
dbSession.commit();
dbSession.clearCache();
RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
TESTER.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
// Restart without xoo
register(null);
dbSession.commit();
dbSession.clearCache();
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).isEmpty();
// Re-install
register(rules);
assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesTest method update_and_remove_rules_on_changes.
@Test
public void update_and_remove_rules_on_changes() {
execute(new FakeRepositoryV1());
assertThat(dbClient.ruleDao().selectAll(dbTester.getSession())).hasSize(2);
assertThat(esTester.getIds(RuleIndexDefinition.INDEX_TYPE_RULE)).containsOnly(RULE_KEY1.toString(), RULE_KEY2.toString());
// user adds tags and sets markdown note
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
rule1.setTags(newHashSet("usertag1", "usertag2"));
rule1.setNoteData("user *note*");
rule1.setNoteUserLogin("marius");
dbClient.ruleDao().update(dbTester.getSession(), rule1);
dbTester.getSession().commit();
when(system.now()).thenReturn(DATE2.getTime());
execute(new FakeRepositoryV2());
// rule1 has been updated
rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
assertThat(rule1.getName()).isEqualTo("One v2");
assertThat(rule1.getDescription()).isEqualTo("Description of One v2");
assertThat(rule1.getSeverityString()).isEqualTo(INFO);
assertThat(rule1.getTags()).containsOnly("usertag1", "usertag2");
assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag4");
assertThat(rule1.getConfigKey()).isEqualTo("config1 v2");
assertThat(rule1.getNoteData()).isEqualTo("user *note*");
assertThat(rule1.getNoteUserLogin()).isEqualTo("marius");
assertThat(rule1.getStatus()).isEqualTo(RuleStatus.READY);
assertThat(rule1.getType()).isEqualTo(RuleType.BUG.getDbConstant());
assertThat(rule1.getCreatedAt()).isEqualTo(DATE1.getTime());
assertThat(rule1.getUpdatedAt()).isEqualTo(DATE2.getTime());
// TODO check remediation function
List<RuleParamDto> params = dbClient.ruleDao().selectRuleParamsByRuleKey(dbTester.getSession(), RULE_KEY1);
assertThat(params).hasSize(2);
RuleParamDto param = getParam(params, "param1");
assertThat(param.getDescription()).isEqualTo("parameter one v2");
assertThat(param.getDefaultValue()).isEqualTo("default1 v2");
// rule2 has been removed -> status set to REMOVED but db row is not deleted
RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY2);
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(rule2.getUpdatedAt()).isEqualTo(DATE2.getTime());
// rule3 has been created
RuleDto rule3 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY3);
assertThat(rule3).isNotNull();
assertThat(rule3.getStatus()).isEqualTo(RuleStatus.READY);
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
// verify repositories
assertThat(dbClient.ruleRepositoryDao().selectAll(dbTester.getSession())).extracting(RuleRepositoryDto::getKey).containsOnly("fake");
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesTest method do_not_update_already_removed_rules.
@Test
public void do_not_update_already_removed_rules() {
execute(new FakeRepositoryV1());
assertThat(dbClient.ruleDao().selectAll(dbTester.getSession())).hasSize(2);
assertThat(esTester.getIds(RuleIndexDefinition.INDEX_TYPE_RULE)).containsOnly(RULE_KEY1.toString(), RULE_KEY2.toString());
RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY2);
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.READY);
when(system.now()).thenReturn(DATE2.getTime());
execute(new FakeRepositoryV2());
// On MySQL, need to update a rule otherwise rule2 will be seen as READY, but why ???
dbClient.ruleDao().update(dbTester.getSession(), dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1));
dbTester.getSession().commit();
// rule2 is removed
rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY2);
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
when(system.now()).thenReturn(DATE3.getTime());
execute(new FakeRepositoryV2());
dbTester.getSession().commit();
// -> rule2 is still removed, but not update at DATE3
rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY2);
assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(rule2.getUpdatedAt()).isEqualTo(DATE2.getTime());
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
}
Aggregations