use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RuleQueryFactoryTest method create_query.
@Test
public void create_query() throws Exception {
RuleQuery result = execute(PARAM_RULE_KEY, "ruleKey", PARAM_ACTIVATION, "true", PARAM_ACTIVE_SEVERITIES, "MINOR,MAJOR", PARAM_AVAILABLE_SINCE, "2016-01-01", PARAM_INHERITANCE, "INHERITED,OVERRIDES", PARAM_IS_TEMPLATE, "true", PARAM_LANGUAGES, "java,js", TEXT_QUERY, "S001", PARAM_QPROFILE, "sonar-way", PARAM_REPOSITORIES, "pmd,checkstyle", PARAM_SEVERITIES, "MINOR,CRITICAL", PARAM_STATUSES, "DEPRECATED,READY", PARAM_TAGS, "tag1,tag2", PARAM_TEMPLATE_KEY, "architectural", PARAM_TYPES, "CODE_SMELL,BUG", SORT, "updatedAt", ASCENDING, "false");
assertThat(result.getKey()).isEqualTo("ruleKey");
assertThat(result.getActivation()).isTrue();
assertThat(result.getActiveSeverities()).containsOnly(MINOR, MAJOR);
assertThat(result.isAscendingSort()).isFalse();
assertThat(result.getAvailableSinceLong()).isNotNull();
assertThat(result.getInheritance()).containsOnly(INHERITED, OVERRIDES);
assertThat(result.isTemplate()).isTrue();
assertThat(result.getLanguages()).containsOnly("java", "js");
assertThat(result.getQueryText()).isEqualTo("S001");
assertThat(result.getQProfileKey()).isEqualTo("sonar-way");
assertThat(result.getRepositories()).containsOnly("pmd", "checkstyle");
assertThat(result.getRuleKey()).isNull();
assertThat(result.getSeverities()).containsOnly(MINOR, CRITICAL);
assertThat(result.getStatuses()).containsOnly(DEPRECATED, READY);
assertThat(result.getTags()).containsOnly("tag1", "tag2");
assertThat(result.templateKey()).isEqualTo("architectural");
assertThat(result.getTypes()).containsOnly(BUG, CODE_SMELL);
assertThat(result.getSortField()).isEqualTo("updatedAt");
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RuleQueryFactoryTest method create_query_add_language_from_profile.
@Test
public void create_query_add_language_from_profile() throws Exception {
String profileKey = "sonar-way";
dbClient.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor(profileKey).setOrganizationUuid(organization.getUuid()).setName("Sonar Way").setLanguage("xoo"));
dbSession.commit();
RuleQuery result = execute(PARAM_QPROFILE, profileKey, PARAM_LANGUAGES, "java,js");
assertThat(result.getQProfileKey()).isEqualTo(profileKey);
assertThat(result.getLanguages()).containsOnly("xoo");
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RegisterRulesTest method update_only_rule_description.
@Test
public void update_only_rule_description() 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("Name").setHtmlDescription("Desc1");
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("Name").setHtmlDescription("Desc2");
repo.done();
}
});
// rule1 has been updated
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RuleKey.of("fake", "rule"));
assertThat(rule1.getName()).isEqualTo("Name");
assertThat(rule1.getDescription()).isEqualTo("Desc2");
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc2"), new SearchOptions()).getTotal()).isEqualTo(1);
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc1"), new SearchOptions()).getTotal()).isEqualTo(0);
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RuleDeleterMediumTest method delete_custom_rule.
@Test
public void delete_custom_rule() {
// Create template rule
RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1")).setLanguage("xoo").setCreatedAt(PAST).setUpdatedAt(PAST);
dao.insert(dbSession, templateRule);
// Create custom rule
RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo").setCreatedAt(PAST).setUpdatedAt(PAST);
dao.insert(dbSession, customRule);
// Create a quality profile
QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
db.qualityProfileDao().insert(dbSession, profileDto);
dbSession.commit();
dbSession.clearCache();
ruleIndexer.index();
activeRuleIndexer.index();
// Activate the custom rule
activate(new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER), QProfileTesting.XOO_P1_KEY);
// Delete custom rule
deleter.delete(customRule.getKey());
// Verify custom rule have status REMOVED
RuleDto customRuleReloaded = dao.selectOrFailByKey(dbSession, customRule.getKey());
assertThat(customRuleReloaded).isNotNull();
assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.REMOVED);
assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
// Verify there's no more active rule from custom rule
assertThat(index.searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).isEmpty();
// Verify in index
assertThat(index.searchAll(new RuleQuery())).containsOnly(templateRule.getKey());
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RuleUpdaterMediumTest method update_custom_rule.
@Test
public void update_custom_rule() {
// Create template rule
RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
ruleDao.insert(dbSession, templateRule);
RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("Format");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
// Create custom rule
RuleDto customRule = RuleTesting.newCustomRule(templateRule).setName("Old name").setDescription("Old description").setSeverity(Severity.MINOR).setStatus(RuleStatus.BETA);
ruleDao.insert(dbSession, customRule);
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue(null));
dbSession.commit();
// Update custom rule
RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setName("New name").setMarkdownDescription("New description").setSeverity("MAJOR").setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", "b.*"));
underTest.update(update, userSessionRule);
dbSession.clearCache();
// Verify custom rule is updated
RuleDto customRuleReloaded = ruleDao.selectOrFailByKey(dbSession, customRule.getKey());
assertThat(customRuleReloaded).isNotNull();
assertThat(customRuleReloaded.getName()).isEqualTo("New name");
assertThat(customRuleReloaded.getDescription()).isEqualTo("New description");
assertThat(customRuleReloaded.getSeverityString()).isEqualTo("MAJOR");
assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.READY);
List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRuleReloaded.getKey());
assertThat(params).hasSize(2);
assertThat(params.get(0).getDefaultValue()).isEqualTo("b.*");
assertThat(params.get(1).getDefaultValue()).isNull();
// Verify in index
assertThat(ruleIndex.search(new RuleQuery().setQueryText("New name"), new SearchOptions()).getIds()).containsOnly(customRule.getKey());
assertThat(ruleIndex.search(new RuleQuery().setQueryText("New description"), new SearchOptions()).getIds()).containsOnly(customRule.getKey());
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Old name"), new SearchOptions()).getTotal()).isZero();
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Old description"), new SearchOptions()).getTotal()).isZero();
}
Aggregations