Search in sources :

Example 6 with RuleQuery

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());
}
Also used : RuleQuery(org.sonar.server.rule.index.RuleQuery) Test(org.junit.Test)

Example 7 with RuleQuery

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);
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RuleIndex(org.sonar.server.rule.index.RuleIndex) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) StringReader(java.io.StringReader) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 8 with RuleQuery

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();
}
Also used : RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 9 with RuleQuery

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);
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleActivator(org.sonar.server.qualityprofile.RuleActivator) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 10 with RuleQuery

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");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) Platform(org.sonar.server.platform.Platform) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) ServerTester(org.sonar.server.tester.ServerTester) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Aggregations

RuleQuery (org.sonar.server.rule.index.RuleQuery)39 Test (org.junit.Test)34 SearchOptions (org.sonar.server.es.SearchOptions)22 RuleDto (org.sonar.db.rule.RuleDto)16 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)14 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)14 RuleIndex (org.sonar.server.rule.index.RuleIndex)7 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RuleKey (org.sonar.api.rule.RuleKey)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)4 RuleParamDto (org.sonar.db.rule.RuleParamDto)4 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)3 RuleActivation (org.sonar.server.qualityprofile.RuleActivation)3 RuleActivator (org.sonar.server.qualityprofile.RuleActivator)3 ActiveRuleIndex (org.sonar.server.qualityprofile.index.ActiveRuleIndex)3 StringReader (java.io.StringReader)2 QProfileService (org.sonar.server.qualityprofile.QProfileService)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1