Search in sources :

Example 16 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class RuleCreatorMediumTest method create_custom_rule_with_no_parameter_value.

@Test
public void create_custom_rule_with_no_parameter_value() {
    // insert template rule
    RuleDto templateRule = createTemplateRuleWithIntArrayParam();
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY);
    RuleKey customRuleKey = creator.create(newRule);
    dbSession.clearCache();
    List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
    assertThat(params).hasSize(1);
    RuleParamDto param = params.get(0);
    assertThat(param.getName()).isEqualTo("myIntegers");
    assertThat(param.getDescription()).isEqualTo("My Integers");
    assertThat(param.getType()).isEqualTo("INTEGER,multiple=true,values=1;2;3");
    assertThat(param.getDefaultValue()).isNull();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Example 17 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class ShowActionMediumTest method show_rule.

@Test
public void show_rule() throws Exception {
    RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("java", "S001")).setName("Rule S001").setDescription("Rule S001 <b>description</b>").setDescriptionFormat(Format.HTML).setSeverity(MINOR).setStatus(RuleStatus.BETA).setConfigKey("InternalKeyS001").setLanguage("xoo").setTags(newHashSet("tag1", "tag2")).setSystemTags(newHashSet("systag1", "systag2")).setType(RuleType.BUG);
    ruleDao.insert(session, ruleDto);
    RuleParamDto param = RuleParamDto.createFor(ruleDto).setName("regex").setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*");
    ruleDao.insertRuleParam(session, ruleDto, param);
    session.commit();
    session.clearCache();
    WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show").setParam("key", ruleDto.getKey().toString());
    request.execute().assertJson(getClass(), "show_rule.json");
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Example 18 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class QProfileReset method resetLanguage.

/**
   * Reset built-in profiles for the given language. Missing profiles are created and
   * existing ones are updated.
   */
public void resetLanguage(String language) {
    DbSession dbSession = db.openSession(false);
    try {
        ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
        for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) {
            QProfileName profileName = entry.getKey();
            QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
            List<RuleActivation> activations = Lists.newArrayList();
            for (RulesProfile def : entry.getValue()) {
                for (ActiveRule activeRule : def.getActiveRules()) {
                    RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
                    activation.setSeverity(activeRule.getSeverity().name());
                    if (!activeRule.getActiveRuleParams().isEmpty()) {
                        for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
                            activation.setParameter(param.getParamKey(), param.getValue());
                        }
                    } else {
                        for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
                            activation.setParameter(param.getName(), param.getDefaultValue());
                        }
                    }
                    activations.add(activation);
                }
            }
            doReset(dbSession, profile, activations);
        }
    } finally {
        dbSession.close();
    }
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRule(org.sonar.api.rules.ActiveRule) DbSession(org.sonar.db.DbSession) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) Collection(java.util.Collection) RuleParamDto(org.sonar.db.rule.RuleParamDto) Map(java.util.Map) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 19 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class RuleActivator method applySeverityAndParamToChange.

/**
   * Severity and parameter values are :
   * 1. defined by end-user
   * 2. else inherited from parent profile
   * 3. else defined by rule defaults
   * <p/>
   * On custom rules, it's always rule parameters that are used
   */
private void applySeverityAndParamToChange(RuleActivation request, RuleActivatorContext context, ActiveRuleChange change) {
    if (request.isReset()) {
        // load severity and params from parent profile, else from default values
        change.setSeverity(firstNonNull(context.parentSeverity(), context.defaultSeverity()));
        for (RuleParamDto ruleParamDto : context.ruleParams()) {
            String paramKey = ruleParamDto.getName();
            change.setParameter(paramKey, validateParam(ruleParamDto, firstNonNull(context.parentParamValue(paramKey), context.defaultParamValue(paramKey))));
        }
    } else if (context.activeRule() != null) {
        // already activated -> load severity and parameters from request, else keep existing ones, else from parent,
        // else from default
        change.setSeverity(firstNonNull(request.getSeverity(), context.currentSeverity(), context.parentSeverity(), context.defaultSeverity()));
        for (RuleParamDto ruleParamDto : context.ruleParams()) {
            String paramKey = ruleParamDto.getName();
            String paramValue = context.hasRequestParamValue(request, paramKey) ? // If the request contains the parameter then we're using either value from request, or parent value, or default value
            firstNonNull(context.requestParamValue(request, paramKey), context.parentParamValue(paramKey), context.defaultParamValue(paramKey)) : // If the request doesn't contain the parameter, then we're using either value in DB, or parent value, or default value
            firstNonNull(context.currentParamValue(paramKey), context.parentParamValue(paramKey), context.defaultParamValue(paramKey));
            change.setParameter(paramKey, validateParam(ruleParamDto, paramValue));
        }
    } else if (context.activeRule() == null) {
        // not activated -> load severity and parameters from request, else from parent, else from defaults
        change.setSeverity(firstNonNull(request.getSeverity(), context.parentSeverity(), context.defaultSeverity()));
        for (RuleParamDto ruleParamDto : context.ruleParams()) {
            String paramKey = ruleParamDto.getName();
            change.setParameter(paramKey, validateParam(ruleParamDto, firstNonNull(context.requestParamValue(request, paramKey), context.parentParamValue(paramKey), context.defaultParamValue(paramKey))));
        }
    }
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 20 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto 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

RuleParamDto (org.sonar.db.rule.RuleParamDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 Test (org.junit.Test)17 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)14 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)12 RuleKey (org.sonar.api.rule.RuleKey)10 ArrayList (java.util.ArrayList)6 WsTester (org.sonar.server.ws.WsTester)6 Date (java.util.Date)5 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 SearchOptions (org.sonar.server.es.SearchOptions)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 DbSession (org.sonar.db.DbSession)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Before (org.junit.Before)1