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