Search in sources :

Example 11 with OrgActiveRuleDto

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

the class QProfileRuleImplTest method assertThatRuleIsActivated.

private void assertThatRuleIsActivated(QProfileDto profile, RuleDefinitionDto rule, @Nullable List<ActiveRuleChange> changes, String expectedSeverity, @Nullable ActiveRuleInheritance expectedInheritance, Map<String, String> expectedParams) {
    OrgActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile).stream().filter(ar -> ar.getRuleKey().equals(rule.getKey())).findFirst().orElseThrow(IllegalStateException::new);
    assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
    assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance != null ? expectedInheritance.name() : null);
    List<ActiveRuleParamDto> params = db.getDbClient().activeRuleDao().selectParamsByActiveRuleUuid(db.getSession(), activeRule.getUuid());
    assertThat(params).hasSize(expectedParams.size());
    if (changes != null) {
        ActiveRuleChange change = changes.stream().filter(c -> c.getActiveRule().getUuid().equals(activeRule.getUuid())).findFirst().orElseThrow(IllegalStateException::new);
        assertThat(change.getInheritance()).isEqualTo(expectedInheritance);
        assertThat(change.getSeverity()).isEqualTo(expectedSeverity);
        assertThat(change.getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
    }
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Example 12 with OrgActiveRuleDto

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

the class QProfileRuleImplTest method assertThatRuleIsUpdated.

private void assertThatRuleIsUpdated(QProfileDto profile, RuleDefinitionDto rule, String expectedSeverity, @Nullable ActiveRuleInheritance expectedInheritance, Map<String, String> expectedParams) {
    OrgActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile).stream().filter(ar -> ar.getRuleKey().equals(rule.getKey())).findFirst().orElseThrow(IllegalStateException::new);
    assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
    assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance != null ? expectedInheritance.name() : null);
    List<ActiveRuleParamDto> params = db.getDbClient().activeRuleDao().selectParamsByActiveRuleUuid(db.getSession(), activeRule.getUuid());
    assertThat(params).hasSize(expectedParams.size());
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Example 13 with OrgActiveRuleDto

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

the class ActiveRuleCompleter method loadParams.

private ListMultimap<ActiveRuleKey, ActiveRuleParamDto> loadParams(DbSession dbSession, List<OrgActiveRuleDto> activeRules) {
    Map<String, ActiveRuleKey> activeRuleUuidsByKey = new HashMap<>();
    for (OrgActiveRuleDto activeRule : activeRules) {
        activeRuleUuidsByKey.put(activeRule.getUuid(), activeRule.getKey());
    }
    List<ActiveRuleParamDto> activeRuleParams = dbClient.activeRuleDao().selectParamsByActiveRuleUuids(dbSession, Lists.transform(activeRules, ActiveRuleDto::getUuid));
    ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRules.size(), 10);
    for (ActiveRuleParamDto activeRuleParam : activeRuleParams) {
        ActiveRuleKey activeRuleKey = activeRuleUuidsByKey.get(activeRuleParam.getActiveRuleUuid());
        activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParam);
    }
    return activeRuleParamsByActiveRuleKey;
}
Also used : HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 14 with OrgActiveRuleDto

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

the class QProfileTreeImpl method removeParent.

private List<ActiveRuleChange> removeParent(DbSession dbSession, QProfileDto profile) {
    List<ActiveRuleChange> changes = new ArrayList<>();
    if (profile.getParentKee() == null) {
        return changes;
    }
    profile.setParentKee(null);
    db.qualityProfileDao().update(dbSession, profile);
    List<OrgActiveRuleDto> activeRules = db.activeRuleDao().selectByProfile(dbSession, profile);
    Collection<String> ruleUuids = activeRules.stream().map(ActiveRuleDto::getRuleUuid).collect(MoreCollectors.toArrayList());
    RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
    for (OrgActiveRuleDto activeRule : activeRules) {
        if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
            changes.addAll(ruleActivator.deactivate(dbSession, context, activeRule.getRuleUuid(), true));
        } else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
            context.reset(activeRule.getRuleUuid());
            activeRule.setInheritance(null);
            activeRule.setUpdatedAt(system2.now());
            db.activeRuleDao().update(dbSession, activeRule);
            changes.add(new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, activeRule, context.getRule().get()).setInheritance(null));
        }
    }
    qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
    return changes;
}
Also used : ArrayList(java.util.ArrayList) RuleActivationContext(org.sonar.server.qualityprofile.builtin.RuleActivationContext) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Example 15 with OrgActiveRuleDto

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

the class ChangeParentActionTest method change_parent_with_no_parent_before.

@Test
public void change_parent_with_no_parent_before() {
    QProfileDto parent1 = createProfile();
    QProfileDto child = createProfile();
    RuleDefinitionDto rule1 = createRule();
    createActiveRule(rule1, parent1);
    ruleIndexer.commitAndIndex(dbSession, rule1.getUuid());
    activeRuleIndexer.indexAll();
    assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, child.getKee())).isEmpty();
    // Set parent
    ws.newRequest().setMethod("POST").setParam(PARAM_LANGUAGE, child.getLanguage()).setParam(PARAM_QUALITY_PROFILE, child.getName()).setParam(PARAM_PARENT_QUALITY_PROFILE, parent1.getName()).execute();
    // 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);
}
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)

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