Search in sources :

Example 6 with ActiveRuleParamDto

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

the class RuleUpdater method updateParameters.

private void updateParameters(DbSession dbSession, RuleUpdate update, Context context) {
    if (update.isChangeParameters() && update.isCustomRule()) {
        RuleDto customRule = context.rule;
        Integer templateId = customRule.getTemplateId();
        checkNotNull(templateId, "Rule '%s' has no persisted template!", customRule);
        Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(templateId, dbSession);
        if (!templateRule.isPresent()) {
            throw new IllegalStateException(String.format("Template %s of rule %s does not exist", customRule.getTemplateId(), customRule.getKey()));
        }
        List<String> paramKeys = newArrayList();
        // Load active rules and its parameters in cache
        Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParamsByActiveRule = getActiveRuleParamsByActiveRule(dbSession, customRule);
        // Browse custom rule parameters to create, update or delete them
        deleteOrUpdateParameters(dbSession, update, customRule, paramKeys, activeRuleParamsByActiveRule);
    }
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 7 with ActiveRuleParamDto

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

the class RuleActivatorContextFactory method initActiveRules.

private void initActiveRules(String profileKey, RuleKey ruleKey, RuleActivatorContext context, DbSession session, boolean parent) {
    ActiveRuleKey key = ActiveRuleKey.of(profileKey, ruleKey);
    Optional<ActiveRuleDto> activeRule = db.activeRuleDao().selectByKey(session, key);
    Collection<ActiveRuleParamDto> activeRuleParams = null;
    if (activeRule.isPresent()) {
        activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(session, activeRule.get().getId());
    }
    if (parent) {
        context.setParentActiveRule(activeRule.orNull());
        context.setParentActiveRuleParams(activeRuleParams);
    } else {
        context.setActiveRule(activeRule.orNull());
        context.setActiveRuleParams(activeRuleParams);
    }
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 8 with ActiveRuleParamDto

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

the class CompareActionMediumTest method createActiveRuleWithParam.

private ActiveRuleDto createActiveRuleWithParam(RuleDto rule, QualityProfileDto profile, String value) {
    ActiveRuleDto activeRule = createActiveRule(rule, profile);
    RuleParamDto paramDto = db.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
    ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
    db.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
    return activeRule;
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 9 with ActiveRuleParamDto

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

the class QProfileBackuperMediumTest method restore_parent_profile.

@Test
public void restore_parent_profile() throws Exception {
    // define two parent/child profiles
    db.qualityProfileDao().insert(dbSession, newXooP1("org-123"), newXooP2("org-123").setParentKee(XOO_P1_KEY));
    dbSession.commit();
    // rule x1 is activated on parent profile (so inherited by child profile)
    RuleActivation activation = new RuleActivation(XOO_X1);
    activation.setSeverity(Severity.INFO);
    activation.setParameter("max", "10");
    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
    dbSession.commit();
    dbSession.clearCache();
    activeRuleIndexer.index();
    // restore backup of parent profile -> update x1 and propagates to child
    tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null);
    // parent profile is updated
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
    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");
    // child profile is inherited
    activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY);
    assertThat(activeRules).hasSize(1);
    activeRuleDoc = activeRules.get(0);
    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
    assertThat(activeRuleDoc.getInheritance()).isEqualTo(INHERITED);
    activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
    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");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) StringReader(java.io.StringReader) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 10 with ActiveRuleParamDto

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

the class QProfileBackuperMediumTest method restore_and_update_profile.

@Test
public void restore_and_update_profile() throws Exception {
    // create profile P1 with rules x1 and x2 activated
    db.qualityProfileDao().insert(dbSession, newXooP1("org-123"));
    RuleActivation activation = new RuleActivation(XOO_X1);
    activation.setSeverity(Severity.INFO);
    activation.setParameter("max", "10");
    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
    activation = new RuleActivation(XOO_X2);
    activation.setSeverity(Severity.INFO);
    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
    dbSession.commit();
    dbSession.clearCache();
    activeRuleIndexer.index();
    // restore backup, which activates only x1
    // -> update x1 and deactivate x2
    tester.get(QProfileBackuper.class).restore(new StringReader(Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
    // Check in db
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
    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(XOO_P1_KEY).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) Test(org.junit.Test)

Aggregations

ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)29 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)26 Test (org.junit.Test)13 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)13 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)9 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)7 RuleParamDto (org.sonar.db.rule.RuleParamDto)7 RuleDto (org.sonar.db.rule.RuleDto)6 Map (java.util.Map)5 StringReader (java.io.StringReader)4 RuleKey (org.sonar.api.rule.RuleKey)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 RulesProfile (org.sonar.api.profiles.RulesProfile)3 RuleIndex (org.sonar.server.rule.index.RuleIndex)3 WsTester (org.sonar.server.ws.WsTester)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 RuleActivation (org.sonar.server.qualityprofile.RuleActivation)2