use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class QProfileBackuperMediumTest method backup.
@Test
public void backup() throws Exception {
RuleKey blahRuleKey = RuleKey.of("blah", "my-rule");
RuleDto blahRule = newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
db.ruleDao().insert(dbSession, blahRule);
dbSession.commit();
dbSession.clearCache();
ruleIndexer.index();
// create profile P1 with rules x2 and x1 activated
db.qualityProfileDao().insert(dbSession, newXooP1("org-123"));
RuleActivation activation1 = new RuleActivation(XOO_X2).setSeverity("MINOR");
RuleActivation activation2 = new RuleActivation(XOO_X1);
RuleActivation activation3 = new RuleActivation(blahRuleKey);
activation2.setSeverity(Severity.BLOCKER);
activation2.setParameter("max", "7");
tester.get(RuleActivator.class).activate(dbSession, activation1, XOO_P1_NAME);
tester.get(RuleActivator.class).activate(dbSession, activation2, XOO_P1_NAME);
tester.get(RuleActivator.class).activate(dbSession, activation3, XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
StringWriter output = new StringWriter();
tester.get(QProfileBackuper.class).backup(XOO_P1_KEY, output);
String expectedXml = Resources.toString(getClass().getResource("QProfileBackuperMediumTest/expected-backup.xml"), StandardCharsets.UTF_8);
assertThat(output.toString()).isXmlEqualTo(expectedXml);
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class QProfileResetMediumTest method reset_language_profile.
@Test
public void reset_language_profile() {
RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))), RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MINOR);
x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
}
}, defProfile);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
// Change the severity and the value of the parameter in the active rule
tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(ruleKey).setSeverity(BLOCKER).setParameter("acceptWhitespace", "false"), profile.getKey());
dbSession.commit();
// Verify severity and param has changed
ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
reset.resetLanguage(ServerTester.Xoo.KEY);
dbSession.commit();
// Severity and parameter value come back to origin after reset
activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RegisterQualityProfilesMediumTest method register_profile_definitions.
@Test
public void register_profile_definitions() {
tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
// Check Profile in DB
QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
assertThat(profile).isNotNull();
// Check Default Profile
verifyDefaultProfile("xoo", "Basic");
// Check ActiveRules in DB
ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
// Check ActiveRuleParameters in DB
Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
assertThat(params).hasSize(2);
// set by profile
assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
// default value
assertThat(params.get("max").getValue()).isEqualTo("10");
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RegisterQualityProfilesMediumTest method register_existing_profile_definitions.
@Test
public void register_existing_profile_definitions() {
tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
// Check Profile in DB
QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
assertThat(profile).isNotNull();
// Check ActiveRules in DB
ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
// Check in ES
assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
tester.get(Platform.class).restart();
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
// Check ActiveRules
ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
// Check in ES
assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
// TODO
// Check ActiveRuleParameters in DB
Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
assertThat(params).hasSize(2);
// set by profile
assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
// default value
assertThat(params.get("max").getValue()).isEqualTo("10");
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleIndexTest method search_by_tags_in_query_text.
@Test
public void search_by_tags_in_query_text() {
RuleKey key1 = RuleKey.of("java", "S001");
RuleKey key2 = RuleKey.of("java", "S002");
indexRules(newDoc(key1).setAllTags(singleton("tag1")), newDoc(key2).setAllTags(singleton("tag2")));
// find all
RuleQuery query = new RuleQuery();
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
// tag1 in query
query = new RuleQuery().setQueryText("tag1");
assertThat(index.search(query, new SearchOptions()).getIds()).containsOnly(key1);
// tag1 OR tag2
// note: should it be AND instead of OR ?
query = new RuleQuery().setQueryText("tag1 tag2");
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
// tag2 OR tag1
query = new RuleQuery().setQueryText("tag2 tag1");
assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
}
Aggregations