Search in sources :

Example 81 with RuleDefinitionDto

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

Example 82 with RuleDefinitionDto

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

Example 83 with RuleDefinitionDto

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

Example 84 with RuleDefinitionDto

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

Example 85 with RuleDefinitionDto

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

Aggregations

RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)654 Test (org.junit.Test)583 ComponentDto (org.sonar.db.component.ComponentDto)305 IssueDto (org.sonar.db.issue.IssueDto)219 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)144 UserDto (org.sonar.db.user.UserDto)96 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)93 DbTester (org.sonar.db.DbTester)93 System2 (org.sonar.api.utils.System2)91 UserSessionRule (org.sonar.server.tester.UserSessionRule)84 List (java.util.List)80 Rule (org.junit.Rule)77 DbClient (org.sonar.db.DbClient)68 RuleParamDto (org.sonar.db.rule.RuleParamDto)67 Mockito.mock (org.mockito.Mockito.mock)65 TestRequest (org.sonar.server.ws.TestRequest)64 Consumer (java.util.function.Consumer)63 EsTester (org.sonar.server.es.EsTester)61 Random (java.util.Random)60 RuleType (org.sonar.api.rules.RuleType)60