Search in sources :

Example 16 with OrgActiveRuleDto

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

the class ChangeParentActionTest method change_parent_with_names.

@Test
public void change_parent_with_names() {
    QProfileDto parent1 = createProfile();
    QProfileDto parent2 = createProfile();
    QProfileDto child = createProfile();
    RuleDefinitionDto rule1 = createRule();
    RuleDefinitionDto rule2 = createRule();
    createActiveRule(rule1, parent1);
    createActiveRule(rule2, parent2);
    ruleIndexer.commitAndIndex(dbSession, rule1.getUuid());
    activeRuleIndexer.indexAll();
    assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, child.getKee())).isEmpty();
    // 1. Set parent 1
    ws.newRequest().setMethod("POST").setParam(PARAM_LANGUAGE, child.getLanguage()).setParam(PARAM_QUALITY_PROFILE, child.getName()).setParam(PARAM_PARENT_QUALITY_PROFILE, parent1.getName()).execute();
    // 1. check rule 1 enabled
    List<OrgActiveRuleDto> activeRules1 = dbClient.activeRuleDao().selectByProfile(dbSession, child);
    assertThat(activeRules1).hasSize(1);
    assertThat(activeRules1.get(0).getKey().getRuleKey().rule()).isEqualTo(rule1.getRuleKey());
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfile(child), new SearchOptions()).getUuids()).hasSize(1);
    // 2. Set parent 2
    ws.newRequest().setMethod("POST").setParam(PARAM_LANGUAGE, child.getLanguage()).setParam(PARAM_QUALITY_PROFILE, child.getName()).setParam(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE, parent2.getName()).execute();
    // 2. check rule 2 enabled
    List<OrgActiveRuleDto> activeRules2 = dbClient.activeRuleDao().selectByProfile(dbSession, child);
    assertThat(activeRules2).hasSize(1);
    assertThat(activeRules2.get(0).getKey().getRuleKey().rule()).isEqualTo(rule2.getRuleKey());
    // 3. Remove parent
    ws.newRequest().setMethod("POST").setParam(PARAM_LANGUAGE, child.getLanguage()).setParam(PARAM_QUALITY_PROFILE, child.getName()).setParam(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE, "").execute();
    // 3. check no rule enabled
    List<OrgActiveRuleDto> activeRules = dbClient.activeRuleDao().selectByProfile(dbSession, child);
    assertThat(activeRules).isEmpty();
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfile(child), new SearchOptions()).getUuids()).isEmpty();
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) Test(org.junit.Test)

Example 17 with OrgActiveRuleDto

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

the class ChangeParentActionTest method replace_existing_parent.

@Test
public void replace_existing_parent() {
    QProfileDto parent1 = createProfile();
    QProfileDto parent2 = createProfile();
    QProfileDto child = createProfile();
    RuleDefinitionDto rule1 = createRule();
    RuleDefinitionDto rule2 = createRule();
    createActiveRule(rule1, parent1);
    createActiveRule(rule2, parent2);
    ruleIndexer.commitAndIndex(dbSession, asList(rule1.getUuid(), rule2.getUuid()));
    activeRuleIndexer.indexAll();
    // Set parent 1
    qProfileTree.setParentAndCommit(dbSession, child, parent1);
    // Set parent 2 through WS
    ws.newRequest().setMethod("POST").setParam(PARAM_LANGUAGE, child.getLanguage()).setParam(PARAM_QUALITY_PROFILE, child.getName()).setParam(PARAM_PARENT_QUALITY_PROFILE, parent2.getName()).execute();
    // Check rule 2 enabled
    List<OrgActiveRuleDto> activeRules2 = dbClient.activeRuleDao().selectByProfile(dbSession, child);
    assertThat(activeRules2).hasSize(1);
    assertThat(activeRules2.get(0).getKey().getRuleKey().rule()).isEqualTo(rule2.getRuleKey());
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfile(child), new SearchOptions()).getUuids()).hasSize(1);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) Test(org.junit.Test)

Example 18 with OrgActiveRuleDto

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

the class QProfileExporters method wrap.

private RulesProfile wrap(DbSession dbSession, QProfileDto profile) {
    RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
    List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile);
    List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleUuids(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getUuid));
    ListMultimap<String, ActiveRuleParamDto> activeRuleParamsByActiveRuleUuid = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleUuid);
    for (ActiveRuleDto activeRule : activeRuleDtos) {
        // TODO all rules should be loaded by using one query with all active rule keys as parameter
        Rule rule = ruleFinder.findByKey(activeRule.getRuleKey());
        org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString()));
        List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleUuid.get(activeRule.getUuid());
        for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
            wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
        }
    }
    return target;
}
Also used : ActiveRule(org.sonar.api.rules.ActiveRule) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Aggregations

OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)18 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)10 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)6 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)5 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)5 RuleQuery (org.sonar.server.rule.index.RuleQuery)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 SearchOptions (org.sonar.server.es.SearchOptions)4 RuleKey (org.sonar.api.rule.RuleKey)3 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)3 RuleDto (org.sonar.db.rule.RuleDto)3 HashMap (java.util.HashMap)2 List (java.util.List)2 DbSession (org.sonar.db.DbSession)2 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)2 Strings.nullToEmpty (com.google.common.base.Strings.nullToEmpty)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1