Search in sources :

Example 16 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class QProfileBackuperMediumTest method restore_and_create_profile.

@Test
public void restore_and_create_profile() throws Exception {
    // Backup file declares profile P1 on xoo
    tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
    // Check in db
    QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
    assertThat(profile).isNotNull();
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
    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(profile.getKey()).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) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 17 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class QProfileFactoryMediumTest method delete.

@Test
public void delete() {
    initRules();
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1("org-123"));
    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();
    List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
    dbSession.commit();
    activeRuleIndexer.index(changes);
    dbSession.clearCache();
    assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).isEmpty();
}
Also used : ActiveRuleIndex(org.sonar.server.qualityprofile.index.ActiveRuleIndex) RuleIndex(org.sonar.server.rule.index.RuleIndex) RuleQuery(org.sonar.server.rule.index.RuleQuery) Test(org.junit.Test)

Example 18 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class CreateActionTest method create_profile_from_backup_xml.

@Test
public void create_profile_from_backup_xml() {
    logInAsQProfileAdministrator();
    insertRule(RULE);
    executeRequest("New Profile", XOO_LANGUAGE, ImmutableMap.of("xoo_lint", "<xml/>"));
    QualityProfileDto dto = dbClient.qualityProfileDao().selectByNameAndLanguage("New Profile", XOO_LANGUAGE, dbSession);
    assertThat(dto.getKey()).isNotNull();
    assertThat(dbClient.activeRuleDao().selectByProfileKey(dbSession, dto.getKey())).hasSize(1);
    assertThat(ruleIndex.searchAll(new RuleQuery().setQProfileKey(dto.getKey()).setActivation(true))).hasSize(1);
}
Also used : RuleQuery(org.sonar.server.rule.index.RuleQuery) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 19 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class ChangeParentActionMediumTest method change_parent_with_no_parent_before.

@Test
public void change_parent_with_no_parent_before() throws Exception {
    QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
    QualityProfileDto child = createProfile("xoo", "Child");
    RuleDto rule1 = createRule("xoo", "rule1");
    createActiveRule(rule1, parent1);
    session.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
    // Set parent
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKey()).setParam("parentKey", parent1.getKey()).execute();
    session.clearCache();
    // Check rule 1 enabled
    List<ActiveRuleDto> activeRules1 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
    assertThat(activeRules1).hasSize(1);
    assertThat(activeRules1.get(0).getKey().ruleKey().rule()).isEqualTo("rule1");
    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) 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 20 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class ChangeParentActionMediumTest method remove_parent_with_empty_key.

@Test
public void remove_parent_with_empty_key() throws Exception {
    QualityProfileDto parent = createProfile("xoo", "Parent 1");
    QualityProfileDto child = createProfile("xoo", "Child");
    RuleDto rule1 = createRule("xoo", "rule1");
    createActiveRule(rule1, parent);
    session.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    assertThat(db.activeRuleDao().selectByProfileKey(session, child.getKey())).isEmpty();
    // Set parent
    tester.get(RuleActivator.class).setParent(session, child.getKey(), parent.getKey());
    session.clearCache();
    // Remove parent
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKey()).setParam("parentKey", "").execute();
    session.clearCache();
    // Check no rule enabled
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(session, child.getKey());
    assertThat(activeRules).isEmpty();
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).isEmpty();
}
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)

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