Search in sources :

Example 46 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ChangeParentActionMediumTest method remove_parent.

@Test
public void remove_parent() 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();
    // Set parent
    tester.get(RuleActivator.class).setParent(session, child.getKey(), parent.getKey());
    session.clearCache();
    // Remove parent through WS
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKey()).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)

Example 47 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QProfileCopierMediumTest method verifyOneActiveRule.

private void verifyOneActiveRule(String profileKey, String expectedSeverity, @Nullable String expectedInheritance, Map<String, String> expectedParams) {
    // check in db
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileKey);
    assertThat(activeRules).hasSize(1);
    ActiveRuleDto activeRule = activeRules.get(0);
    assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
    assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance);
    // verify parameters
    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRule.getKey());
    List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(params).hasSize(expectedParams.size());
    Map<String, ActiveRuleParamDto> paramsByKey = ActiveRuleParamDto.groupByKey(params);
    for (Map.Entry<String, String> entry : expectedParams.entrySet()) {
        String value = paramsByKey.get(entry.getKey()).getValue();
        assertThat(value).isEqualTo(entry.getValue());
    }
    // check in es
    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileKey).setActivation(true))).hasSize(1);
}
Also used : ActiveRuleIndex(org.sonar.server.qualityprofile.index.ActiveRuleIndex) RuleIndex(org.sonar.server.rule.index.RuleIndex) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 48 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QProfileExportersTest method import_xml.

@Test
public void import_xml() {
    QualityProfileDto profileDto = QProfileTesting.newQProfileDto("org-123", QProfileName.createFor("xoo", "import_xml"), "import_xml");
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey())).isEmpty();
    QProfileResult result = exporters.importXml(profileDto, "XooProfileImporter", toInputStream("<xml/>", UTF_8), dbSession);
    dbSession.commit();
    activeRuleIndexer.index(result.getChanges());
    // Check in db
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey());
    assertThat(activeRules).hasSize(1);
    ActiveRuleDto activeRule = activeRules.get(0);
    assertThat(activeRule.getKey().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1"));
    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
    // Check in es
    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).containsOnly(RuleKey.of("xoo", "R1"));
}
Also used : RuleIndex(org.sonar.server.rule.index.RuleIndex) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 49 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ActiveRuleIndexerTest method index_from_changes_index_new_active_rule.

@Test
public void index_from_changes_index_new_active_rule() throws Exception {
    long yesterday = 1000000L;
    long now = 2000000L;
    // Index one active rule
    RuleDto rule = RuleTesting.newDto(RULE_KEY_1);
    dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), rule);
    QualityProfileDto profile = QualityProfileDto.createFor("qp").setOrganizationUuid(organization.getUuid()).setLanguage("xoo").setName("profile");
    dbTester.getDbClient().qualityProfileDao().insert(dbTester.getSession(), profile);
    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(Severity.BLOCKER).setCreatedAt(yesterday).setUpdatedAt(yesterday);
    dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule);
    dbTester.getSession().commit();
    indexer.index();
    assertThat(esTester.getIds(INDEX_TYPE_ACTIVE_RULE)).containsOnly(activeRule.getKey().toString());
    // Index another active rule
    RuleDto rule2 = RuleTesting.newDto(RULE_KEY_2);
    dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), rule2);
    ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profile, rule2).setSeverity(Severity.CRITICAL).setCreatedAt(now).setUpdatedAt(now);
    dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule2);
    dbTester.getSession().commit();
    indexer.index(singletonList(ActiveRuleChange.createFor(ACTIVATED, activeRule2.getKey())));
    assertThat(esTester.getIds(INDEX_TYPE_ACTIVE_RULE)).containsOnly(activeRule.getKey().toString(), activeRule2.getKey().toString());
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 50 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QProfilesWsMediumTest method createActiveRule.

private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
    db.activeRuleDao().insert(session, activeRule);
    return activeRule;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)55 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)26 Test (org.junit.Test)25 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)23 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)17 RuleDto (org.sonar.db.rule.RuleDto)17 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)10 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 RuleKey (org.sonar.api.rule.RuleKey)7 WsTester (org.sonar.server.ws.WsTester)7 RuleParamDto (org.sonar.db.rule.RuleParamDto)6 SearchOptions (org.sonar.server.es.SearchOptions)6 StringReader (java.io.StringReader)5 RuleIndex (org.sonar.server.rule.index.RuleIndex)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 RuleActivator (org.sonar.server.qualityprofile.RuleActivator)4 RulesProfile (org.sonar.api.profiles.RulesProfile)3