use of org.sonar.db.rule.RuleMetadataDto in project sonarqube by SonarSource.
the class ExportAdHocRulesStepTest method insertRuleMetadata.
private void insertRuleMetadata(String ruleUuid, String adHocName) {
dbTester.rules().insertOrUpdateMetadata(new RuleMetadataDto().setRuleUuid(ruleUuid).setAdHocName(adHocName));
dbTester.commit();
}
use of org.sonar.db.rule.RuleMetadataDto in project sonarqube by SonarSource.
the class QualityProfileExportDaoTest method selectRulesByProfile_verify_columns.
@Test
public void selectRulesByProfile_verify_columns() {
String language = "java";
RuleDefinitionDto ruleTemplate = createRule(language);
RuleDefinitionDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getUuid());
RuleMetadataDto customRuleMetadata = createRuleMetadata(new RuleMetadataDto().setRuleUuid(customRule.getUuid()).setNoteData("Extended description").setTags(Sets.newHashSet("tag1", "tag2", "tag3")));
RuleDefinitionDto rule = createRule(language, RuleStatus.READY, null);
RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto().setRuleUuid(rule.getUuid()));
QProfileDto profile = createProfile(language);
List<ActiveRuleDto> activeRules = activate(profile, customRule, rule);
List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
assertThat(results).isNotNull();
assertThat(results).isNotEmpty();
// verify custom rule
ExportRuleDto exportCustomRuleDto = results.stream().filter(ExportRuleDto::isCustomRule).findFirst().get();
assertThat(exportCustomRuleDto).isNotNull();
assertThat(exportCustomRuleDto.isCustomRule()).isTrue();
assertThat(exportCustomRuleDto.getParams()).isEmpty();
assertThat(exportCustomRuleDto.getDescription()).isEqualTo(customRule.getDescription());
assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRuleMetadata.getNoteData());
assertThat(exportCustomRuleDto.getName()).isEqualTo(customRule.getName());
assertThat(exportCustomRuleDto.getRuleKey()).isEqualTo(customRule.getKey());
assertThat(exportCustomRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(customRule.getType()));
assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRuleMetadata.getTags()));
assertThat(exportCustomRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey());
ActiveRuleDto activeCustomRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(customRule.getKey())).findFirst().get();
assertThat(exportCustomRuleDto.getSeverityString()).isEqualTo(activeCustomRule.getSeverityString());
// verify regular rule
ExportRuleDto exportRuleDto = results.stream().filter(regularRule -> !regularRule.isCustomRule()).findFirst().get();
assertThat(exportRuleDto).isNotNull();
assertThat(exportRuleDto.isCustomRule()).isFalse();
assertThat(exportRuleDto.getParams()).isEmpty();
assertThat(exportRuleDto.getDescription()).isEqualTo(rule.getDescription());
assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(ruleMetadata.getNoteData());
assertThat(exportRuleDto.getName()).isEqualTo(rule.getName());
assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey());
assertThat(exportRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(rule.getType()));
ActiveRuleDto activeRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(rule.getKey())).findFirst().get();
assertThat(exportRuleDto.getSeverityString()).isEqualTo(activeRule.getSeverityString());
}
use of org.sonar.db.rule.RuleMetadataDto in project sonarqube by SonarSource.
the class RuleCreator method createCustomRule.
private String createCustomRule(RuleKey ruleKey, NewCustomRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
RuleDefinitionDto ruleDefinition = new RuleDefinitionDto().setUuid(uuidFactory.create()).setRuleKey(ruleKey).setPluginKey(templateRuleDto.getPluginKey()).setTemplateUuid(templateRuleDto.getUuid()).setConfigKey(templateRuleDto.getConfigKey()).setName(newRule.name()).setDescription(newRule.markdownDescription()).setDescriptionFormat(Format.MARKDOWN).setSeverity(newRule.severity()).setStatus(newRule.status()).setType(newRule.type() == null ? templateRuleDto.getType() : newRule.type().getDbConstant()).setLanguage(templateRuleDto.getLanguage()).setDefRemediationFunction(templateRuleDto.getDefRemediationFunction()).setDefRemediationGapMultiplier(templateRuleDto.getDefRemediationGapMultiplier()).setDefRemediationBaseEffort(templateRuleDto.getDefRemediationBaseEffort()).setGapDescription(templateRuleDto.getGapDescription()).setScope(templateRuleDto.getScope()).setSystemTags(templateRuleDto.getSystemTags()).setSecurityStandards(templateRuleDto.getSecurityStandards()).setIsExternal(false).setIsAdHoc(false).setCreatedAt(system2.now()).setUpdatedAt(system2.now());
dbClient.ruleDao().insert(dbSession, ruleDefinition);
Set<String> tags = templateRuleDto.getTags();
if (!tags.isEmpty()) {
RuleMetadataDto ruleMetadata = new RuleMetadataDto().setRuleUuid(ruleDefinition.getUuid()).setTags(tags).setCreatedAt(system2.now()).setUpdatedAt(system2.now());
dbClient.ruleDao().insertOrUpdate(dbSession, ruleMetadata);
}
for (RuleParamDto templateRuleParamDto : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, templateRuleDto.getKey())) {
String customRuleParamValue = Strings.emptyToNull(newRule.parameter(templateRuleParamDto.getName()));
createCustomRuleParams(customRuleParamValue, ruleDefinition, templateRuleParamDto, dbSession);
}
return ruleDefinition.getUuid();
}
use of org.sonar.db.rule.RuleMetadataDto in project sonarqube by SonarSource.
the class SearchActionTest method should_return_specific_tags.
@Test
public void should_return_specific_tags() {
RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("java"));
RuleMetadataDto metadata = insertMetadata(rule, setTags("tag1", "tag2"));
indexRules();
SearchResponse result = ws.newRequest().setParam("f", "tags").executeProtobuf(SearchResponse.class);
assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString());
assertThat(result.getRulesList()).extracting(Rule::getTags).flatExtracting(Rules.Tags::getTagsList).containsExactly(metadata.getTags().toArray(new String[0]));
}
use of org.sonar.db.rule.RuleMetadataDto in project sonarqube by SonarSource.
the class SearchActionTest method search_debt_rules_with_default_and_overridden_debt_values.
@Test
public void search_debt_rules_with_default_and_overridden_debt_values() {
RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("java").setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setDefRemediationGapMultiplier("1h").setDefRemediationBaseEffort("15min"));
RuleMetadataDto metadata = insertMetadata(rule, r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setRemediationGapMultiplier("2h").setRemediationBaseEffort("25min"));
indexRules();
SearchResponse result = ws.newRequest().setParam("f", "debtRemFn,debtOverloaded,defaultDebtRemFn").executeProtobuf(SearchResponse.class);
assertThat(result.getTotal()).isOne();
assertThat(result.getRulesCount()).isOne();
Rule searchedRule = result.getRules(0);
assertThat(searchedRule).isNotNull();
assertThat(searchedRule.getDefaultDebtRemFnCoeff()).isEqualTo("1h");
assertThat(searchedRule.getDefaultDebtRemFnOffset()).isEqualTo("15min");
assertThat(searchedRule.getDefaultDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
assertThat(searchedRule.getDefaultRemFnBaseEffort()).isEqualTo("15min");
assertThat(searchedRule.getDefaultRemFnGapMultiplier()).isEqualTo("1h");
assertThat(searchedRule.getDefaultRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
assertThat(searchedRule.getDebtOverloaded()).isTrue();
assertThat(searchedRule.getDebtRemFnCoeff()).isEqualTo("2h");
assertThat(searchedRule.getDebtRemFnOffset()).isEqualTo("25min");
assertThat(searchedRule.getDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
}
Aggregations