Search in sources :

Example 6 with OrgActiveRuleDto

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

the class BuiltInQProfileUpdateImplTest 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) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Example 7 with OrgActiveRuleDto

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

the class ChangeParentActionTest method as_qprofile_editor.

@Test
public void as_qprofile_editor() {
    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);
    UserDto user = db.users().insertUser();
    db.qualityProfiles().addUserPermission(child, user);
    userSession.logIn(user);
    ws.newRequest().setMethod("POST").setParam(PARAM_LANGUAGE, child.getLanguage()).setParam(PARAM_QUALITY_PROFILE, child.getName()).setParam(PARAM_PARENT_QUALITY_PROFILE, parent2.getName()).execute();
    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) UserDto(org.sonar.db.user.UserDto) 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 8 with OrgActiveRuleDto

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

the class QualityProfileChangeEventServiceImpl method createRuleChanges.

private List<RuleChange> createRuleChanges(@NotNull QProfileDto profileDto) {
    List<RuleChange> ruleChanges = new ArrayList<>();
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profileDto);
        List<String> activeRuleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getUuid).collect(Collectors.toList());
        Map<String, List<ActiveRuleParamDto>> paramsByActiveRuleUuid = dbClient.activeRuleDao().selectParamsByActiveRuleUuids(dbSession, activeRuleUuids).stream().collect(Collectors.groupingBy(ActiveRuleParamDto::getActiveRuleUuid));
        Map<String, String> activeRuleUuidByRuleUuid = activeRuleDtos.stream().collect(Collectors.toMap(ActiveRuleDto::getRuleUuid, ActiveRuleDto::getUuid));
        List<String> ruleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getRuleUuid).collect(Collectors.toList());
        List<RuleDto> ruleDtos = dbClient.ruleDao().selectByUuids(dbSession, ruleUuids);
        for (RuleDto ruleDto : ruleDtos) {
            String activeRuleUuid = activeRuleUuidByRuleUuid.get(ruleDto.getUuid());
            List<ActiveRuleParamDto> params = paramsByActiveRuleUuid.getOrDefault(activeRuleUuid, new ArrayList<>());
            RuleChange ruleChange = toRuleChange(ruleDto, params);
            ruleChanges.add(ruleChange);
        }
    }
    return ruleChanges;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ArrayList(java.util.ArrayList) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with OrgActiveRuleDto

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

the class QProfileTreeImplTest 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 10 with OrgActiveRuleDto

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

the class QProfileTreeImplTest 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)

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