use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesTest method disable_then_enable_rule.
@Test
public void disable_then_enable_rule() throws Exception {
// Install rule
when(system.now()).thenReturn(DATE1.getTime());
execute(new FakeRepositoryV1());
// Uninstall rule
when(system.now()).thenReturn(DATE2.getTime());
execute();
RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(ruleIndex.search(new RuleQuery().setKey(RULE_KEY1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
// Re-install rule
when(system.now()).thenReturn(DATE3.getTime());
execute(new FakeRepositoryV1());
rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
assertThat(rule.getStatus()).isEqualTo(RuleStatus.BETA);
assertThat(ruleIndex.search(new RuleQuery().setKey(RULE_KEY1.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesTest method update_only_rule_name.
@Test
public void update_only_rule_name() throws Exception {
when(system.now()).thenReturn(DATE1.getTime());
execute(new RulesDefinition() {
@Override
public void define(Context context) {
NewRepository repo = context.createRepository("fake", "java");
repo.createRule("rule").setName("Name1").setHtmlDescription("Description");
repo.done();
}
});
when(system.now()).thenReturn(DATE2.getTime());
execute(new RulesDefinition() {
@Override
public void define(Context context) {
NewRepository repo = context.createRepository("fake", "java");
repo.createRule("rule").setName("Name2").setHtmlDescription("Description");
repo.done();
}
});
// rule1 has been updated
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RuleKey.of("fake", "rule"));
assertThat(rule1.getName()).isEqualTo("Name2");
assertThat(rule1.getDescription()).isEqualTo("Description");
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()).getTotal()).isEqualTo(1);
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getTotal()).isEqualTo(0);
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesTest method insert_new_rules.
@Test
public void insert_new_rules() {
execute(new FakeRepositoryV1());
// verify db
assertThat(dbClient.ruleDao().selectAll(dbTester.getSession())).hasSize(2);
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
assertThat(rule1.getName()).isEqualTo("One");
assertThat(rule1.getDescription()).isEqualTo("Description of One");
assertThat(rule1.getSeverityString()).isEqualTo(BLOCKER);
assertThat(rule1.getTags()).isEmpty();
assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag2", "tag3");
assertThat(rule1.getConfigKey()).isEqualTo("config1");
assertThat(rule1.getStatus()).isEqualTo(RuleStatus.BETA);
assertThat(rule1.getCreatedAt()).isEqualTo(DATE1.getTime());
assertThat(rule1.getUpdatedAt()).isEqualTo(DATE1.getTime());
assertThat(rule1.getDefaultRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
assertThat(rule1.getDefaultRemediationGapMultiplier()).isEqualTo("5d");
assertThat(rule1.getDefaultRemediationBaseEffort()).isEqualTo("10h");
assertThat(rule1.getType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
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");
assertThat(param.getDefaultValue()).isEqualTo("default1");
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY2);
// 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 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());
}
Aggregations