Search in sources :

Example 16 with ActiveRuleParamDto

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

the class SearchActionMediumTest method search_all_active_rules_params.

@Test
public void search_all_active_rules_params() throws Exception {
    QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
    tester.get(QualityProfileDao.class).insert(dbSession, profile);
    RuleDto rule = RuleTesting.newXooX1();
    ruleDao.insert(dbSession, rule);
    dbSession.commit();
    RuleParamDto param = RuleParamDto.createFor(rule).setDefaultValue("some value").setType("string").setDescription("My small description").setName("my_var");
    ruleDao.insertRuleParam(dbSession, rule, param);
    RuleParamDto param2 = RuleParamDto.createFor(rule).setDefaultValue("other value").setType("integer").setDescription("My small description").setName("the_var");
    ruleDao.insertRuleParam(dbSession, rule, param2);
    ActiveRuleDto activeRule = newActiveRule(profile, rule);
    tester.get(ActiveRuleDao.class).insert(dbSession, activeRule);
    ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param).setValue("The VALUE");
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam);
    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2).setValue("The Other Value");
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam2);
    dbSession.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
    request.setParam(WebService.Param.TEXT_QUERY, "x1");
    request.setParam(PARAM_ACTIVATION, "true");
    request.setParam(WebService.Param.FIELDS, "params");
    WsTester.Result result = request.execute();
    result.assertJson(this.getClass(), "search_active_rules_params.json");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) 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 17 with ActiveRuleParamDto

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

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

the class RuleActivator method doUpdate.

private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
    ActiveRuleDao dao = db.activeRuleDao();
    ActiveRuleDto activeRule = context.activeRule();
    if (activeRule != null) {
        String severity = change.getSeverity();
        if (severity != null) {
            activeRule.setSeverity(severity);
        }
        ActiveRule.Inheritance inheritance = change.getInheritance();
        if (inheritance != null) {
            activeRule.setInheritance(inheritance.name());
        }
        activeRule.setUpdatedAt(system2.now());
        dao.update(dbSession, activeRule);
        for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
            ActiveRuleParamDto activeRuleParamDto = context.activeRuleParamsAsMap().get(param.getKey());
            if (activeRuleParamDto == null) {
                // did not exist
                if (param.getValue() != null) {
                    activeRuleParamDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
                    activeRuleParamDto.setValue(param.getValue());
                    dao.insertParam(dbSession, activeRule, activeRuleParamDto);
                }
            } else {
                if (param.getValue() != null) {
                    activeRuleParamDto.setValue(param.getValue());
                    dao.updateParam(dbSession, activeRule, activeRuleParamDto);
                } else {
                    dao.deleteParam(dbSession, activeRule, activeRuleParamDto);
                }
            }
        }
    }
    return activeRule;
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map)

Example 19 with ActiveRuleParamDto

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

the class RuleActivatorContext method isSame.

boolean isSame(ActiveRuleChange change) {
    ActiveRule.Inheritance inheritance = change.getInheritance();
    if (inheritance != null && !inheritance.name().equals(activeRule.getInheritance())) {
        return false;
    }
    String severity = change.getSeverity();
    if (severity != null && !severity.equals(activeRule.getSeverityString())) {
        return false;
    }
    for (Map.Entry<String, String> changeParam : change.getParameters().entrySet()) {
        ActiveRuleParamDto param = activeRuleParams.get(changeParam.getKey());
        if (changeParam.getValue() == null && param != null && param.getValue() != null) {
            return false;
        }
        if (changeParam.getValue() != null && (param == null || !StringUtils.equals(changeParam.getValue(), param.getValue()))) {
            return false;
        }
    }
    return true;
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Map(java.util.Map)

Example 20 with ActiveRuleParamDto

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

the class RuleUpdater method deleteOrUpdateParameters.

private void deleteOrUpdateParameters(DbSession dbSession, RuleUpdate update, RuleDto customRule, List<String> paramKeys, Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParamsByActiveRule) {
    for (RuleParamDto ruleParamDto : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, update.getRuleKey())) {
        String key = ruleParamDto.getName();
        String value = Strings.emptyToNull(update.parameter(key));
        // Update rule param
        ruleParamDto.setDefaultValue(value);
        dbClient.ruleDao().updateRuleParam(dbSession, customRule, ruleParamDto);
        if (value != null) {
            // Update linked active rule params or create new one
            updateOrInsertActiveRuleParams(dbSession, ruleParamDto, activeRuleParamsByActiveRule);
        } else {
            // Delete linked active rule params
            deleteActiveRuleParams(dbSession, key, activeRuleParamsByActiveRule.values());
        }
        paramKeys.add(key);
    }
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto)

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