Search in sources :

Example 96 with ActiveRuleDto

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

the class ActiveRuleIndexerTest method verifyOnlyIndexed.

private void verifyOnlyIndexed(ActiveRuleDto... expected) {
    List<String> docs = es.getIds(TYPE_ACTIVE_RULE);
    assertThat(docs).hasSize(expected.length);
    for (ActiveRuleDto activeRuleDto : expected) {
        assertThat(docs).contains("ar_" + activeRuleDto.getUuid());
    }
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 97 with ActiveRuleDto

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

the class RegisterRules method mergeParams.

private void mergeParams(RegisterRulesContext context, RulesDefinition.Rule ruleDef, RuleDefinitionDto rule, DbSession session) {
    List<RuleParamDto> paramDtos = context.getRuleParametersFor(rule.getUuid());
    Map<String, RuleParamDto> existingParamsByName = new HashMap<>();
    Profiler profiler = Profiler.create(Loggers.get(getClass()));
    for (RuleParamDto paramDto : paramDtos) {
        RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
        if (paramDef == null) {
            profiler.start();
            dbClient.activeRuleDao().deleteParamsByRuleParam(session, paramDto);
            profiler.stopDebug(format("Propagate deleted param with name %s to active rules of rule %s", paramDto.getName(), rule.getKey()));
            dbClient.ruleDao().deleteRuleParam(session, paramDto.getUuid());
        } else {
            if (mergeParam(paramDto, paramDef)) {
                dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
            }
            existingParamsByName.put(paramDto.getName(), paramDto);
        }
    }
    // Create newly parameters
    for (RulesDefinition.Param param : ruleDef.params()) {
        RuleParamDto paramDto = existingParamsByName.get(param.key());
        if (paramDto != null) {
            continue;
        }
        paramDto = RuleParamDto.createFor(rule).setName(param.key()).setDescription(param.description()).setDefaultValue(param.defaultValue()).setType(param.type().toString());
        dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
        if (StringUtils.isEmpty(param.defaultValue())) {
            continue;
        }
        // Propagate the default value to existing active rule parameters
        profiler.start();
        for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleUuid(session, rule.getUuid())) {
            ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
            dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
        }
        profiler.stopDebug(format("Propagate new param with name %s to active rules of rule %s", paramDto.getName(), rule.getKey()));
    }
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Profiler(org.sonar.api.utils.log.Profiler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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 98 with ActiveRuleDto

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

the class QProfileBackuperImplTest method backup_rules_having_parameters.

@Test
public void backup_rules_having_parameters() {
    RuleDefinitionDto rule = createRule();
    RuleParamDto param = db.rules().insertRuleParam(rule);
    QProfileDto profile = createProfile(rule.getLanguage());
    ActiveRuleDto activeRule = activate(profile, rule, param);
    StringWriter writer = new StringWriter();
    underTest.backup(db.getSession(), profile, writer);
    assertThat(writer.toString()).contains("<rule>" + "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" + "<key>" + rule.getRuleKey() + "</key>" + "<type>" + RuleType.valueOf(rule.getType()).name() + "</type>" + "<priority>" + activeRule.getSeverityString() + "</priority>" + "<parameters><parameter>" + "<key>" + param.getName() + "</key>" + "<value>20</value>" + "</parameter></parameters>" + "</rule>");
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringWriter(java.io.StringWriter) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 99 with ActiveRuleDto

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

the class QProfileBackuperImplTest method backup_generates_xml_file.

@Test
public void backup_generates_xml_file() {
    RuleDefinitionDto rule = createRule();
    QProfileDto profile = createProfile(rule.getLanguage());
    ActiveRuleDto activeRule = activate(profile, rule);
    StringWriter writer = new StringWriter();
    underTest.backup(db.getSession(), profile, writer);
    assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<profile><name>" + profile.getName() + "</name>" + "<language>" + profile.getLanguage() + "</language>" + "<rules>" + "<rule>" + "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" + "<key>" + rule.getRuleKey() + "</key>" + "<type>" + RuleType.valueOf(rule.getType()).name() + "</type>" + "<priority>" + activeRule.getSeverityString() + "</priority>" + "<parameters></parameters>" + "</rule>" + "</rules>" + "</profile>");
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringWriter(java.io.StringWriter) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 100 with ActiveRuleDto

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

the class QProfileBackuperImplTest method backup_custom_rules_with_params.

@Test
public void backup_custom_rules_with_params() {
    RuleDefinitionDto templateRule = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setIsTemplate(true));
    RuleDefinitionDto rule = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setDescription("custom rule description").setName("custom rule name").setStatus(RuleStatus.READY).setTemplateUuid(templateRule.getUuid()));
    RuleParamDto param = db.rules().insertRuleParam(rule);
    QProfileDto profile = createProfile(rule.getLanguage());
    ActiveRuleDto activeRule = activate(profile, rule, param);
    StringWriter writer = new StringWriter();
    underTest.backup(db.getSession(), profile, writer);
    assertThat(writer).hasToString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<profile>" + "<name>" + profile.getName() + "</name>" + "<language>" + profile.getLanguage() + "</language>" + "<rules><rule>" + "<repositoryKey>" + rule.getRepositoryKey() + "</repositoryKey>" + "<key>" + rule.getKey().rule() + "</key>" + "<type>" + RuleType.valueOf(rule.getType()) + "</type>" + "<priority>" + activeRule.getSeverityString() + "</priority>" + "<name>" + rule.getName() + "</name>" + "<templateKey>" + templateRule.getKey().rule() + "</templateKey>" + "<description>" + rule.getDescription() + "</description>" + "<parameters><parameter>" + "<key>" + param.getName() + "</key>" + "<value>20</value>" + "</parameter></parameters>" + "</rule></rules></profile>");
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringWriter(java.io.StringWriter) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)112 Test (org.junit.Test)51 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)49 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)26 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)21 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)21 RuleDto (org.sonar.db.rule.RuleDto)21 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)19 RuleParamDto (org.sonar.db.rule.RuleParamDto)19 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)18 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)15 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)14 RuleKey (org.sonar.api.rule.RuleKey)12 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 ArrayList (java.util.ArrayList)9 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)9 NewBuiltInQualityProfile (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile)9 Map (java.util.Map)7 WsTester (org.sonar.server.ws.WsTester)6