Search in sources :

Example 56 with ActiveRuleDto

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

the class BuiltInQProfileInsertImplTest method verifyActiveRuleInDb.

// TODO test lot of active_rules, params, orgas
private void verifyActiveRuleInDb(QProfileDto profile, RuleDefinitionDto rule, String expectedSeverity, RuleParamDto... paramDtos) {
    ActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByKey(dbSession, ActiveRuleKey.of(profile, rule.getKey())).get();
    assertThat(activeRule.getUuid()).isNotNull();
    assertThat(activeRule.getInheritance()).isNull();
    assertThat(activeRule.doesOverride()).isFalse();
    assertThat(activeRule.getRuleUuid()).isEqualTo(rule.getUuid());
    assertThat(activeRule.getProfileUuid()).isEqualTo(profile.getRulesProfileUuid());
    assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
    assertThat(activeRule.getCreatedAt()).isPositive();
    assertThat(activeRule.getUpdatedAt()).isPositive();
    List<ActiveRuleParamDto> params = db.getDbClient().activeRuleDao().selectParamsByActiveRuleUuid(dbSession, activeRule.getUuid());
    assertThat(params).extracting(ActiveRuleParamDto::getKey).containsOnly(Arrays.stream(paramDtos).map(RuleParamDto::getName).toArray(String[]::new));
    QProfileChangeQuery changeQuery = new QProfileChangeQuery(profile.getKee());
    QProfileChangeDto change = db.getDbClient().qProfileChangeDao().selectByQuery(dbSession, changeQuery).stream().filter(c -> c.getDataAsMap().get("ruleUuid").equals(rule.getUuid())).findFirst().get();
    assertThat(change.getChangeType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED.name());
    assertThat(change.getCreatedAt()).isPositive();
    assertThat(change.getUuid()).isNotEmpty();
    assertThat(change.getUserUuid()).isNull();
    assertThat(change.getRulesProfileUuid()).isEqualTo(profile.getRulesProfileUuid());
    assertThat(change.getDataAsMap()).containsEntry("severity", expectedSeverity);
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) QProfileChangeQuery(org.sonar.db.qualityprofile.QProfileChangeQuery) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QProfileChangeDto(org.sonar.db.qualityprofile.QProfileChangeDto)

Example 57 with ActiveRuleDto

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

the class RuleUpdaterTest method update_active_rule_parameters_when_updating_custom_rule.

@Test
public void update_active_rule_parameters_when_updating_custom_rule() {
    // Create template rule with 3 parameters
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
    RuleDefinitionDto templateRuleDefinition = templateRule.getDefinition();
    db.rules().insert(templateRuleDefinition);
    db.rules().insertRuleParam(templateRuleDefinition, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
    db.rules().insertRuleParam(templateRuleDefinition, param -> param.setName("format").setType("STRING").setDescription("format").setDefaultValue("csv"));
    db.rules().insertRuleParam(templateRuleDefinition, param -> param.setName("message").setType("STRING").setDescription("message"));
    // Create custom rule
    RuleDefinitionDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo").getDefinition();
    db.rules().insert(customRule);
    RuleParamDto ruleParam1 = db.rules().insertRuleParam(customRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue("a.*"));
    db.rules().insertRuleParam(customRule, param -> param.setName("format").setType("STRING").setDescription("format").setDefaultValue("txt"));
    db.rules().insertRuleParam(customRule, param -> param.setName("message").setType("STRING").setDescription("message"));
    // Create a quality profile
    QProfileDto profileDto = QualityProfileTesting.newQualityProfileDto();
    db.getDbClient().qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    // Activate the custom rule
    ActiveRuleDto activeRuleDto = new ActiveRuleDto().setProfileUuid(profileDto.getRulesProfileUuid()).setRuleUuid(customRule.getUuid()).setSeverity(Severity.BLOCKER);
    db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto);
    db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, new ActiveRuleParamDto().setActiveRuleUuid(activeRuleDto.getUuid()).setRulesParameterUuid(ruleParam1.getUuid()).setKey(ruleParam1.getName()).setValue(ruleParam1.getDefaultValue()));
    dbSession.commit();
    // Update custom rule parameter 'regex', add 'message' and remove 'format'
    RuleUpdate update = createForCustomRule(customRule.getKey()).setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
    underTest.update(dbSession, update, userSessionRule);
    // Verify custom rule parameters has been updated
    List<RuleParamDto> params = db.getDbClient().ruleDao().selectRuleParamsByRuleKey(dbSession, customRule.getKey());
    assertThat(params).hasSize(3);
    Map<String, RuleParamDto> paramsByKey = paramsByName(params);
    assertThat(paramsByKey.get("regex")).isNotNull();
    assertThat(paramsByKey.get("regex").getDefaultValue()).isEqualTo("b.*");
    assertThat(paramsByKey.get("message")).isNotNull();
    assertThat(paramsByKey.get("message").getDefaultValue()).isEqualTo("a message");
    assertThat(paramsByKey.get("format")).isNotNull();
    assertThat(paramsByKey.get("format").getDefaultValue()).isNull();
    // Verify that severity has not changed
    ActiveRuleDto activeRuleReloaded = db.getDbClient().activeRuleDao().selectByKey(dbSession, ActiveRuleKey.of(profileDto, customRule.getKey())).get();
    assertThat(activeRuleReloaded.getSeverityString()).isEqualTo(Severity.BLOCKER);
    // Verify active rule parameters has been updated
    List<ActiveRuleParamDto> activeRuleParams = db.getDbClient().activeRuleDao().selectParamsByActiveRuleUuid(dbSession, activeRuleReloaded.getUuid());
    assertThat(activeRuleParams).hasSize(2);
    Map<String, ActiveRuleParamDto> activeRuleParamsByKey = ActiveRuleParamDto.groupByKey(activeRuleParams);
    assertThat(activeRuleParamsByKey.get("regex").getValue()).isEqualTo("b.*");
    assertThat(activeRuleParamsByKey.get("message").getValue()).isEqualTo("a message");
    assertThat(activeRuleParamsByKey.get("format")).isNull();
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) 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 58 with ActiveRuleDto

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

the class ChangeParentActionTest method createActiveRule.

private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
    dbClient.activeRuleDao().insert(dbSession, activeRule);
    dbSession.commit();
    return activeRule;
}
Also used : OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 59 with ActiveRuleDto

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

the class CompareActionTest method createActiveRuleWithParam.

private ActiveRuleDto createActiveRuleWithParam(RuleDefinitionDto rule, QProfileDto profile, String value) {
    ActiveRuleDto activeRule = createActiveRule(rule, profile);
    RuleParamDto paramDto = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
    ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
    dbClient.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
    return activeRule;
}
Also used : 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 60 with ActiveRuleDto

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

the class CompareActionTest method createActiveRule.

private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
    dbClient.activeRuleDao().insert(session, activeRule);
    return activeRule;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

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