use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class QualityProfileExportDaoTest method selectRulesByProfile_ready_rules_only.
@Test
public void selectRulesByProfile_ready_rules_only() {
String language = "java";
RuleDefinitionDto rule1 = createRule(language);
RuleDefinitionDto rule2 = createRule(language);
RuleDefinitionDto rule3 = createRule(language);
createRule(language, RuleStatus.REMOVED);
QProfileDto profile = createProfile(language);
activate(profile, rule1, rule2, rule3);
List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
assertThat(results).isNotNull();
assertThat(results).extracting("ruleKey").containsOnly(rule1.getKey(), rule2.getKey(), rule3.getKey());
}
use of org.sonar.db.rule.RuleDefinitionDto 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.RuleDefinitionDto in project sonarqube by SonarSource.
the class ActiveRuleDaoTest method countActiveRulesByQuery_filter_by_rule_status.
@Test
public void countActiveRulesByQuery_filter_by_rule_status() {
RuleDefinitionDto betaRule = db.rules().insert(r -> r.setStatus(BETA));
db.qualityProfiles().activateRule(profile1, rule1);
db.qualityProfiles().activateRule(profile1, rule2);
db.qualityProfiles().activateRule(profile1, betaRule);
db.qualityProfiles().activateRule(profile1, removedRule);
db.qualityProfiles().activateRule(profile2, rule1);
db.qualityProfiles().activateRule(profile2, betaRule);
ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder();
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setRuleStatus(BETA).build())).containsOnly(entry(profile1.getKee(), 1L), entry(profile2.getKee(), 1L));
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(singletonList(profile1)).setRuleStatus(READY).build())).containsOnly(entry(profile1.getKee(), 2L));
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(singletonList(profile1)).setRuleStatus(REMOVED).build())).containsOnly(entry(profile1.getKee(), 1L));
}
use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class QualityProfileDaoTest method selectBuiltInRuleProfilesWithActiveRules.
@Test
public void selectBuiltInRuleProfilesWithActiveRules() {
// a quality profile without active rules but not builtin
db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(false));
// a built-in quality profile without active rules
db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true));
// a built-in quality profile with active rules
QProfileDto builtInQPWithActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true));
RuleDefinitionDto ruleDefinitionDto = db.rules().insert();
db.qualityProfiles().activateRule(builtInQPWithActiveRules, ruleDefinitionDto);
dbSession.commit();
List<RulesProfileDto> rulesProfileDtos = underTest.selectBuiltInRuleProfilesWithActiveRules(dbSession);
assertThat(rulesProfileDtos).extracting(RulesProfileDto::getName).containsOnly(builtInQPWithActiveRules.getName());
}
use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.
the class QualityProfileDaoTest method selectDefaultBuiltInProfilesWithoutActiveRules.
@Test
public void selectDefaultBuiltInProfilesWithoutActiveRules() {
// a quality profile without active rules but not builtin
db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(false).setLanguage("java"));
// a built-in quality profile without active rules
QProfileDto javaQPWithoutActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("java"));
db.qualityProfiles().setAsDefault(javaQPWithoutActiveRules);
// a built-in quality profile without active rules
QProfileDto cppQPWithoutActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("cpp"));
db.qualityProfiles().setAsDefault(cppQPWithoutActiveRules);
// a built-in quality profile with active rules
QProfileDto builtInQPWithActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("java"));
RuleDefinitionDto ruleDefinitionDto = db.rules().insert();
db.qualityProfiles().activateRule(builtInQPWithActiveRules, ruleDefinitionDto);
dbSession.commit();
assertThat(underTest.selectDefaultBuiltInProfilesWithoutActiveRules(dbSession, Sets.newHashSet("java", "cpp"))).extracting(QProfileDto::getName).containsOnly(javaQPWithoutActiveRules.getName(), cppQPWithoutActiveRules.getName());
assertThat(underTest.selectDefaultBuiltInProfilesWithoutActiveRules(dbSession, Sets.newHashSet("java"))).extracting(QProfileDto::getName).containsOnly(javaQPWithoutActiveRules.getName());
assertThat(underTest.selectDefaultBuiltInProfilesWithoutActiveRules(dbSession, Sets.newHashSet("cobol"))).isEmpty();
assertThat(underTest.selectDefaultBuiltInProfilesWithoutActiveRules(dbSession, Sets.newHashSet())).isEmpty();
}
Aggregations