Search in sources :

Example 51 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_security_cwe_return_vulnerabilities_and_hotspots_only.

@Test
public void search_by_security_cwe_return_vulnerabilities_and_hotspots_only() {
    RuleDefinitionDto rule1 = createRule(setSecurityStandards(of("cwe:543", "cwe:123", "owaspTop10:a1")), r -> r.setType(VULNERABILITY));
    RuleDefinitionDto rule2 = createRule(setSecurityStandards(of("cwe:543", "owaspTop10:a1")), r -> r.setType(SECURITY_HOTSPOT));
    createRule(setSecurityStandards(of("owaspTop10:a1")), r -> r.setType(CODE_SMELL));
    index();
    RuleQuery query = new RuleQuery().setCwe(of("543"));
    SearchIdResult<String> results = underTest.search(query, new SearchOptions().addFacets("cwe"));
    assertThat(results.getUuids()).containsOnly(rule1.getUuid(), rule2.getUuid());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 52 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_activation.

@Test
public void search_by_activation() {
    RuleDefinitionDto rule1 = createJavaRule();
    RuleDefinitionDto rule2 = createJavaRule();
    RuleDefinitionDto rule3 = createJavaRule();
    QProfileDto profile1 = createJavaProfile();
    QProfileDto profile2 = createJavaProfile();
    db.qualityProfiles().activateRule(profile1, rule1);
    db.qualityProfiles().activateRule(profile2, rule1);
    db.qualityProfiles().activateRule(profile1, rule2);
    index();
    // active rules
    verifySearch(newRuleQuery().setActivation(true).setQProfile(profile1), rule1, rule2);
    verifySearch(newRuleQuery().setActivation(true).setQProfile(profile2), rule1);
    // inactive rules
    verifySearch(newRuleQuery().setActivation(false).setQProfile(profile1), rule3);
    verifySearch(newRuleQuery().setActivation(false).setQProfile(profile2), rule2, rule3);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 53 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_activation_and_inheritance.

@Test
public void search_by_activation_and_inheritance() {
    RuleDefinitionDto rule1 = createJavaRule();
    RuleDefinitionDto rule2 = createJavaRule();
    RuleDefinitionDto rule3 = createJavaRule();
    RuleDefinitionDto rule4 = createJavaRule();
    QProfileDto parent = createJavaProfile();
    QProfileDto child = createJavaProfile();
    db.qualityProfiles().activateRule(parent, rule1);
    db.qualityProfiles().activateRule(parent, rule2);
    db.qualityProfiles().activateRule(parent, rule3);
    db.qualityProfiles().activateRule(child, rule1, ar -> ar.setInheritance(INHERITED.name()));
    db.qualityProfiles().activateRule(child, rule2, ar -> ar.setInheritance(OVERRIDES.name()));
    db.qualityProfiles().activateRule(child, rule3, ar -> ar.setInheritance(INHERITED.name()));
    index();
    // all rules
    verifySearch(newRuleQuery(), rule1, rule2, rule3, rule4);
    // inherited/overrides rules on parent
    verifyEmptySearch(newRuleQuery().setActivation(true).setQProfile(parent).setInheritance(of(INHERITED.name())));
    verifyEmptySearch(newRuleQuery().setActivation(true).setQProfile(parent).setInheritance(of(OVERRIDES.name())));
    // inherited/overrides rules on child
    verifySearch(newRuleQuery().setActivation(true).setQProfile(child).setInheritance(of(INHERITED.name())), rule1, rule3);
    verifySearch(newRuleQuery().setActivation(true).setQProfile(child).setInheritance(of(OVERRIDES.name())), rule2);
    // inherited AND overridden on parent
    verifyEmptySearch(newRuleQuery().setActivation(true).setQProfile(parent).setInheritance(of(INHERITED.name(), OVERRIDES.name())));
    // inherited AND overridden on child
    verifySearch(newRuleQuery().setActivation(true).setQProfile(child).setInheritance(of(INHERITED.name(), OVERRIDES.name())), rule1, rule2, rule3);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 54 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_is_external.

@Test
public void search_by_is_external() {
    RuleDefinitionDto ruleIsNotExternal = createRule(setIsExternal(false));
    RuleDefinitionDto ruleIsExternal = createRule(setIsExternal(true));
    index();
    // Only external
    RuleQuery query = new RuleQuery().setIncludeExternal(true);
    SearchIdResult<String> results = underTest.search(query, new SearchOptions());
    assertThat(results.getUuids()).containsOnly(ruleIsExternal.getUuid(), ruleIsNotExternal.getUuid());
    // Only not external
    query = new RuleQuery().setIncludeExternal(false);
    results = underTest.search(query, new SearchOptions());
    assertThat(results.getUuids()).containsOnly(ruleIsNotExternal.getUuid());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 55 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_any_of_languages.

@Test
public void search_by_any_of_languages() {
    createRule(setLanguage("java"));
    RuleDefinitionDto javascript = createRule(setLanguage("js"));
    index();
    RuleQuery query = new RuleQuery().setLanguages(asList("cobol", "js"));
    SearchIdResult<String> results = underTest.search(query, new SearchOptions());
    assertThat(results.getUuids()).containsOnly(javascript.getUuid());
    // no results
    query = new RuleQuery().setLanguages(singletonList("cpp"));
    assertThat(underTest.search(query, new SearchOptions()).getUuids()).isEmpty();
    // empty list => no filter
    query = new RuleQuery().setLanguages(emptyList());
    assertThat(underTest.search(query, new SearchOptions()).getUuids()).hasSize(2);
    // null list => no filter
    query = new RuleQuery().setLanguages(null);
    assertThat(underTest.search(query, new SearchOptions()).getUuids()).hasSize(2);
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) 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