use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class RuleActivatorMediumTest method bulk_change_severity.
@Test
public void bulk_change_severity() {
createChildProfiles();
// activate two rules on root profile P1 (propagated to P2 and P3)
RuleActivation activation = new RuleActivation(XOO_X1).setSeverity(INFO).setParameter("max", "7");
activate(activation, XOO_P1_KEY);
activation = new RuleActivation(XOO_X2).setSeverity(INFO);
activate(activation, XOO_P1_KEY);
// bulk change severity to BLOCKER. Parameters are not set.
RuleQuery query = new RuleQuery().setActivation(true).setQProfileKey(XOO_P1_KEY);
BulkChangeResult result = ruleActivator.bulkActivate(query, XOO_P1_KEY, "BLOCKER");
assertThat(result.countSucceeded()).isEqualTo(2);
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null, ImmutableMap.of("max", "7"));
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P1_KEY, XOO_X2), BLOCKER, null, Collections.<String, String>emptyMap());
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X1), BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), BLOCKER, INHERITED, Collections.<String, String>emptyMap());
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P3_KEY, XOO_X1), BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P3_KEY, XOO_X2), BLOCKER, INHERITED, Collections.<String, String>emptyMap());
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class QProfileBackuperMediumTest method restore_and_update_profile.
@Test
public void restore_and_update_profile() throws Exception {
// create profile P1 with rules x1 and x2 activated
db.qualityProfileDao().insert(dbSession, newXooP1("org-123"));
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
activation = new RuleActivation(XOO_X2);
activation.setSeverity(Severity.INFO);
tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
// restore backup, which activates only x1
// -> update x1 and deactivate x2
tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
// Check in db
List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(1);
ActiveRuleDto activeRuleDoc = activeRules.get(0);
assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
assertThat(activeRuleDoc.getInheritance()).isNull();
ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
// Check in es
assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).containsOnly(XOO_X1);
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class ChangeParentActionMediumTest method fail_if_parent_key_and_name_both_set.
@Test(expected = IllegalArgumentException.class)
public void fail_if_parent_key_and_name_both_set() throws Exception {
QualityProfileDto child = createProfile("xoo", "Child");
session.commit();
assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKee()).setParam("parentName", "polop").setParam("parentKey", "palap").execute();
}
use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.
the class ChangeParentActionMediumTest method replace_existing_parent.
@Test
public void replace_existing_parent() throws Exception {
QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
QualityProfileDto child = createProfile("xoo", "Child");
RuleDto rule1 = createRule("xoo", "rule1");
RuleDto rule2 = createRule("xoo", "rule2");
createActiveRule(rule1, parent1);
createActiveRule(rule2, parent2);
session.commit();
ruleIndexer.index();
activeRuleIndexer.index();
// Set parent 1
tester.get(RuleActivator.class).setParent(session, child.getKey(), parent1.getKey());
session.clearCache();
// Set parent 2 through WS
wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKey()).setParam("parentKey", parent2.getKey()).execute();
session.clearCache();
// Check rule 2 enabled
List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
assertThat(activeRules2).hasSize(1);
assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
}
use of org.sonar.server.rule.index.RuleQuery 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");
}
Aggregations