Search in sources :

Example 1 with RuleActivation

use of org.sonar.server.qualityprofile.RuleActivation in project sonarqube by SonarSource.

the class RuleActivationActions method activate.

private void activate(Request request, Response response) {
    RuleKey ruleKey = readRuleKey(request);
    RuleActivation activation = new RuleActivation(ruleKey);
    activation.setSeverity(request.param(SEVERITY));
    String params = request.param(PARAMS);
    if (params != null) {
        activation.setParameters(KeyValueFormat.parse(params));
    }
    activation.setReset(Boolean.TRUE.equals(request.paramAsBoolean(RESET)));
    service.activate(request.mandatoryParam(PROFILE_KEY), activation);
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) RuleActivation(org.sonar.server.qualityprofile.RuleActivation)

Example 2 with RuleActivation

use of org.sonar.server.qualityprofile.RuleActivation in project sonarqube by SonarSource.

the class RuleDeleterMediumTest method delete_custom_rule.

@Test
public void delete_custom_rule() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1")).setLanguage("xoo").setCreatedAt(PAST).setUpdatedAt(PAST);
    dao.insert(dbSession, templateRule);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo").setCreatedAt(PAST).setUpdatedAt(PAST);
    dao.insert(dbSession, customRule);
    // Create a quality profile
    QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    dbSession.clearCache();
    ruleIndexer.index();
    activeRuleIndexer.index();
    // Activate the custom rule
    activate(new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER), QProfileTesting.XOO_P1_KEY);
    // Delete custom rule
    deleter.delete(customRule.getKey());
    // Verify custom rule have status REMOVED
    RuleDto customRuleReloaded = dao.selectOrFailByKey(dbSession, customRule.getKey());
    assertThat(customRuleReloaded).isNotNull();
    assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.REMOVED);
    assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
    // Verify there's no more active rule from custom rule
    assertThat(index.searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).isEmpty();
    // Verify in index
    assertThat(index.searchAll(new RuleQuery())).containsOnly(templateRule.getKey());
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) RuleQuery(org.sonar.server.rule.index.RuleQuery) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 3 with RuleActivation

use of org.sonar.server.qualityprofile.RuleActivation in project sonarqube by SonarSource.

the class RuleUpdaterMediumTest method update_active_rule_parameters_when_updating_custom_rule.

@Test
public void update_active_rule_parameters_when_updating_custom_rule() {
    // Create template rule with 3 parameters
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("format").setDefaultValue("csv");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
    RuleParamDto templateRuleParam3 = RuleParamDto.createFor(templateRule).setName("message").setType("STRING").setDescription("message");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo");
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3);
    // Create a quality profile
    QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    // Activate the custom rule
    RuleActivation activation = new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER);
    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
    dbSession.commit();
    dbSession.clearCache();
    // Update custom rule parameter 'regex', add 'message' and remove 'format'
    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    // Verify custom rule parameters has been updated
    List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRule.getKey());
    assertThat(params).hasSize(3);
    Map<String, RuleParamDto> paramsByKey = paramsByKey(params);
    assertThat(paramsByKey.get("regex")).isNotNull();
    assertThat(paramsByKey.get("regex").getDefaultValue()).isEqualTo("b.*");
    assertThat(paramsByKey.get("message")).isNotNull();
    assertThat(paramsByKey.get("message").getDefaultValue()).isEqualTo("a message");
    assertThat(paramsByKey.get("format")).isNotNull();
    assertThat(paramsByKey.get("format").getDefaultValue()).isNull();
    // Verify that severity has not changed
    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, ActiveRuleKey.of(profileDto.getKey(), customRule.getKey()));
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(Severity.BLOCKER);
    // Verify active rule parameters has been updated
    List<ActiveRuleParamDto> activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParams).hasSize(2);
    Map<String, ActiveRuleParamDto> activeRuleParamsByKey = ActiveRuleParamDto.groupByKey(activeRuleParams);
    assertThat(activeRuleParamsByKey.get("regex").getValue()).isEqualTo("b.*");
    assertThat(activeRuleParamsByKey.get("message").getValue()).isEqualTo("a message");
    assertThat(activeRuleParamsByKey.get("format")).isNull();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) RuleActivator(org.sonar.server.qualityprofile.RuleActivator) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 4 with RuleActivation

use of org.sonar.server.qualityprofile.RuleActivation in project sonarqube by SonarSource.

the class RegisterRulesMediumTest method update_active_rules_on_param_changes.

@Test
public void update_active_rules_on_param_changes() {
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1Rule = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
            // has default value
            x1Rule.createParam("min").setType(RuleParamType.INTEGER).setDefaultValue("5");
            // no default value
            x1Rule.createParam("format").setType(RuleParamType.STRING);
        }
    });
    // Create profile and activate rule
    logInAsQProfileAdministrator();
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1("org-123"));
    dbSession.commit();
    dbSession.clearCache();
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    activation.setParameter("format", "txt");
    TESTER.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
    // Default value of "min" is changed, "format" is removed, "format2" is added, "max" is added with a default value
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1Rule = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
            x1Rule.createParam("min").setType(RuleParamType.INTEGER).setDefaultValue("6");
            x1Rule.createParam("format2").setType(RuleParamType.STRING);
            x1Rule.createParam("max").setType(RuleParamType.INTEGER).setDefaultValue("10");
        }
    });
    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
    List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(params).hasSize(2);
    Map<String, ActiveRuleParamDto> parmsByKey = FluentIterable.from(params).uniqueIndex(ActiveRuleParamToKey.INSTANCE);
    // do not change default value on existing active rules -> keep min=5
    assertThat(parmsByKey.get("min").getValue()).isEqualTo("5");
    // new param with default value
    assertThat(parmsByKey.get("max").getValue()).isEqualTo("10");
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) QProfileService(org.sonar.server.qualityprofile.QProfileService) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 5 with RuleActivation

use of org.sonar.server.qualityprofile.RuleActivation in project sonarqube by SonarSource.

the class RegisterRulesMediumTest method deactivate_removed_rules_only_if_repository_still_exists.

@Test
public void deactivate_removed_rules_only_if_repository_still_exists() {
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
        }
    });
    // Create a profile and activate rule
    logInAsQProfileAdministrator();
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1("org-123"));
    dbSession.commit();
    dbSession.clearCache();
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    TESTER.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
    // Restart, repo xoo still exists -> deactivate x1
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            repository.createRule("x2").setName("x2 name").setHtmlDescription("x2 desc");
        }
    });
    assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
    assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X2.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).isEmpty();
    assertThat(activeRuleIndex.countAllByQualityProfileKey()).isEmpty();
    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).isEmpty();
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) QProfileService(org.sonar.server.qualityprofile.QProfileService) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Aggregations

RuleActivation (org.sonar.server.qualityprofile.RuleActivation)6 Test (org.junit.Test)5 QProfileService (org.sonar.server.qualityprofile.QProfileService)3 RuleQuery (org.sonar.server.rule.index.RuleQuery)3 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)2 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)2 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)2 RuleDto (org.sonar.db.rule.RuleDto)2 SearchOptions (org.sonar.server.es.SearchOptions)2 RuleKey (org.sonar.api.rule.RuleKey)1 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1 RuleActivator (org.sonar.server.qualityprofile.RuleActivator)1