Search in sources :

Example 6 with RuleMetadataDto

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

the class SearchActionTest method insertMetadata.

@SafeVarargs
private final RuleMetadataDto insertMetadata(RuleDefinitionDto rule, Consumer<RuleMetadataDto>... populaters) {
    RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, populaters);
    ruleIndexer.commitAndIndex(db.getSession(), rule.getUuid());
    return metadata;
}
Also used : RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto)

Example 7 with RuleMetadataDto

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

the class UpdateActionTest method update_rule_remediation_function.

@Test
public void update_rule_remediation_function() {
    logInAsQProfileAdministrator();
    RuleDefinitionDto rule = db.rules().insert(r -> r.setDefRemediationFunction(LINEAR.toString()), r -> r.setDefRemediationGapMultiplier("10d"), r -> r.setDefRemediationBaseEffort(null));
    String newOffset = LINEAR_OFFSET.toString();
    String newMultiplier = "15d";
    String newEffort = "5min";
    Rules.UpdateResponse result = ws.newRequest().setMethod("POST").setParam("key", rule.getKey().toString()).setParam(PARAM_REMEDIATION_FN_TYPE, newOffset).setParam(PARAM_REMEDIATION_FN_GAP_MULTIPLIER, newMultiplier).setParam(PARAM_REMEDIATION_FN_BASE_EFFORT, newEffort).executeProtobuf(Rules.UpdateResponse.class);
    Rules.Rule updatedRule = result.getRule();
    assertThat(updatedRule).isNotNull();
    assertThat(updatedRule.getKey()).isEqualTo(rule.getKey().toString());
    assertThat(updatedRule.getDefaultRemFnType()).isEqualTo(rule.getDefRemediationFunction());
    assertThat(updatedRule.getDefaultRemFnGapMultiplier()).isEqualTo(rule.getDefRemediationGapMultiplier());
    assertThat(updatedRule.getDefaultRemFnBaseEffort()).isEmpty();
    assertThat(updatedRule.getGapDescription()).isEqualTo(rule.getGapDescription());
    assertThat(updatedRule.getRemFnType()).isEqualTo(newOffset);
    assertThat(updatedRule.getRemFnGapMultiplier()).isEqualTo(newMultiplier);
    assertThat(updatedRule.getRemFnBaseEffort()).isEqualTo(newEffort);
    // check database
    RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey()).orElseThrow(() -> new IllegalStateException("Cannot load metadata"));
    assertThat(metadataOfSpecificOrg.getRemediationFunction()).isEqualTo(newOffset);
    assertThat(metadataOfSpecificOrg.getRemediationGapMultiplier()).isEqualTo(newMultiplier);
    assertThat(metadataOfSpecificOrg.getRemediationBaseEffort()).isEqualTo(newEffort);
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Rules(org.sonarqube.ws.Rules) Test(org.junit.Test)

Example 8 with RuleMetadataDto

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

the class UpdateActionTest method update_note.

@Test
public void update_note() {
    RuleDefinitionDto rule = db.rules().insert();
    UserDto userHavingUpdatingNote = db.users().insertUser();
    db.rules().insertOrUpdateMetadata(rule, userHavingUpdatingNote, m -> m.setNoteData("old data"));
    UserDto userAuthenticated = db.users().insertUser();
    userSession.logIn(userAuthenticated).addPermission(ADMINISTER_QUALITY_PROFILES);
    Rules.UpdateResponse result = ws.newRequest().setMethod("POST").setParam(PARAM_KEY, rule.getKey().toString()).setParam(PARAM_MARKDOWN_NOTE, "new data").executeProtobuf(Rules.UpdateResponse.class);
    Rules.Rule updatedRule = result.getRule();
    // check response
    assertThat(updatedRule.getMdNote()).isEqualTo("new data");
    assertThat(updatedRule.getNoteLogin()).isEqualTo(userAuthenticated.getLogin());
    // check database
    RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey()).get();
    assertThat(metadataOfSpecificOrg.getNoteData()).isEqualTo("new data");
    assertThat(metadataOfSpecificOrg.getNoteUserUuid()).isEqualTo(userAuthenticated.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) Rules(org.sonarqube.ws.Rules) Test(org.junit.Test)

Example 9 with RuleMetadataDto

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

the class ShowActionTest method show_adhoc_rule.

@Test
public void show_adhoc_rule() {
    RuleDefinitionDto externalRule = db.rules().insert(r -> r.setIsExternal(true).setIsAdHoc(true));
    RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(externalRule, m -> m.setAdHocName("adhoc name").setAdHocDescription("<div>desc</div>").setAdHocSeverity(Severity.BLOCKER).setAdHocType(RuleType.VULNERABILITY).setNoteData(null).setNoteUserUuid(null));
    doReturn("&lt;div&gt;desc2&lt;/div&gt;").when(macroInterpreter).interpret(metadata.getAdHocDescription());
    ShowResponse result = ws.newRequest().setParam(PARAM_KEY, externalRule.getKey().toString()).executeProtobuf(ShowResponse.class);
    Rule resultRule = result.getRule();
    assertThat(resultRule).extracting(Rule::getName, Rule::getHtmlDesc, Rule::getSeverity, Rule::getType).containsExactlyInAnyOrder("adhoc name", "&lt;div&gt;desc2&lt;/div&gt;", Severity.BLOCKER, VULNERABILITY);
}
Also used : ShowResponse(org.sonarqube.ws.Rules.ShowResponse) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleMetadataDto(org.sonar.db.rule.RuleMetadataDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) Rule(org.sonarqube.ws.Rules.Rule) RuleTesting.newTemplateRule(org.sonar.db.rule.RuleTesting.newTemplateRule) RuleTesting.newCustomRule(org.sonar.db.rule.RuleTesting.newCustomRule) Test(org.junit.Test)

Example 10 with RuleMetadataDto

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

the class SearchActionTest method search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.

@Test
public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() {
    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.CONSTANT_ISSUE.name()).setRemediationGapMultiplier(null).setRemediationBaseEffort("5min"));
    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()).isEmpty();
    assertThat(searchedRule.getDebtRemFnOffset()).isEqualTo("5min");
    assertThat(searchedRule.getDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.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