Search in sources :

Example 6 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey 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 7 with ActiveRuleKey

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

the class RuleActivatorMediumTest method do_not_change_severity_and_params_if_unset_and_already_activated.

@Test
public void do_not_change_severity_and_params_if_unset_and_already_activated() {
    // initial activation
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
    RuleActivation activation = new RuleActivation(XOO_X1);
    activation.setSeverity(BLOCKER);
    activation.setParameter("max", "7");
    activate(activation, XOO_P1_KEY);
    // update without any severity or params => keep
    RuleActivation update = new RuleActivation(XOO_X1);
    activate(update, XOO_P1_KEY);
    assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
    verifyHasActiveRuleInDb(activeRuleKey, BLOCKER, null, ImmutableMap.of("max", "7"));
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Test(org.junit.Test)

Example 8 with ActiveRuleKey

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

the class RuleActivatorMediumTest method revert_activation_to_default_severity_and_parameters.

@Test
public void revert_activation_to_default_severity_and_parameters() {
    // initial activation
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
    RuleActivation activation = new RuleActivation(XOO_X1);
    activation.setSeverity(BLOCKER);
    activation.setParameter("max", "7");
    activation.setParameter("min", "3");
    activate(activation, XOO_P1_KEY);
    // update without any severity or params = reset
    RuleActivation update = new RuleActivation(XOO_X1).setReset(true);
    activate(update, XOO_P1_KEY);
    assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
    verifyHasActiveRuleInDb(activeRuleKey, MINOR, null, // only default values
    ImmutableMap.of("max", "10"));
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Test(org.junit.Test)

Example 9 with ActiveRuleKey

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

the class RuleActivatorMediumTest method ignore_deactivation_if_rule_not_activated.

@Test
public void ignore_deactivation_if_rule_not_activated() {
    // deactivation
    ActiveRuleKey key = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
    ruleActivator.deactivate(key);
    verifyZeroActiveRules(XOO_P1_KEY);
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Test(org.junit.Test)

Example 10 with ActiveRuleKey

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

the class RuleActivatorMediumTest method update_activation_but_new_parameter.

@Test
public void update_activation_but_new_parameter() {
    // initial activation
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
    RuleActivation activation = new RuleActivation(XOO_X1);
    activation.setSeverity(BLOCKER);
    activate(activation, XOO_P1_KEY);
    assertThat(db.activeRuleDao().selectParamByKeyAndName(activeRuleKey, "max", dbSession)).isNotNull();
    db.activeRuleDao().deleteParamByKeyAndName(dbSession, activeRuleKey, "max");
    dbSession.commit();
    assertThat(db.activeRuleDao().selectParamByKeyAndName(activeRuleKey, "max", dbSession)).isNull();
    dbSession.clearCache();
    // update
    RuleActivation update = new RuleActivation(XOO_X1);
    update.setSeverity(CRITICAL);
    update.setParameter("max", "42");
    // contrary to activerule, the param 'max' is supposed to be inserted but not updated
    activate(update, XOO_P1_KEY);
    assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
    verifyHasActiveRuleInDb(activeRuleKey, CRITICAL, null, ImmutableMap.of("max", "42"));
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Test(org.junit.Test)

Aggregations

ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)26 Test (org.junit.Test)18 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)9 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)7 RuleKey (org.sonar.api.rule.RuleKey)6 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 BadRequestException (org.sonar.server.exceptions.BadRequestException)3 HashMap (java.util.HashMap)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 RuleDto (org.sonar.db.rule.RuleDto)2 SearchOptions (org.sonar.server.es.SearchOptions)2 RuleQuery (org.sonar.server.rule.index.RuleQuery)2 Multimap (com.google.common.collect.Multimap)1 RuleParam (org.sonar.api.rules.RuleParam)1 DbSession (org.sonar.db.DbSession)1 Platform (org.sonar.server.platform.Platform)1 ServerTester (org.sonar.server.tester.ServerTester)1