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());
}
}
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()));
}
}
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>");
}
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>");
}
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>");
}
Aggregations