Search in sources :

Example 1 with RuleMetadataDto

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();
}
Also used : RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto)

Example 2 with RuleMetadataDto

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());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Test(org.junit.Test)

Example 3 with RuleMetadataDto

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();
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 4 with RuleMetadataDto

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]));
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Rule(org.sonarqube.ws.Rules.Rule) UserSessionRule(org.sonar.server.tester.UserSessionRule) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Rules(org.sonarqube.ws.Rules) QProfileRules(org.sonar.server.qualityprofile.QProfileRules) SearchResponse(org.sonarqube.ws.Rules.SearchResponse) Test(org.junit.Test)

Example 5 with RuleMetadataDto

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());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Rule(org.sonarqube.ws.Rules.Rule) UserSessionRule(org.sonar.server.tester.UserSessionRule) SearchResponse(org.sonarqube.ws.Rules.SearchResponse) Test(org.junit.Test)

Aggregations

RuleMetadataDto (org.sonar.db.rule.RuleMetadataDto)17 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)15 Test (org.junit.Test)13 UserSessionRule (org.sonar.server.tester.UserSessionRule)7 Rule (org.sonarqube.ws.Rules.Rule)7 SearchResponse (org.sonarqube.ws.Rules.SearchResponse)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Rules (org.sonarqube.ws.Rules)3 ShowResponse (org.sonarqube.ws.Rules.ShowResponse)3 RuleType (org.sonar.api.rules.RuleType)2 RuleDto (org.sonar.db.rule.RuleDto)2 Common (org.sonarqube.ws.Common)2 Arrays.asList (java.util.Arrays.asList)1 Arrays.stream (java.util.Arrays.stream)1 Collections.singleton (java.util.Collections.singleton)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1