Search in sources :

Example 46 with RuleDefinitionDto

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

the class RuleIndexTest method sort_by_name.

@Test
public void sort_by_name() {
    RuleDefinitionDto abcd = createRule(setName("abcd"));
    RuleDefinitionDto abc = createRule(setName("ABC"));
    RuleDefinitionDto fgh = createRule(setName("FGH"));
    index();
    // ascending
    RuleQuery query = new RuleQuery().setSortField(RuleIndexDefinition.FIELD_RULE_NAME);
    SearchIdResult<String> results = underTest.search(query, new SearchOptions());
    assertThat(results.getUuids()).containsExactly(abc.getUuid(), abcd.getUuid(), fgh.getUuid());
    // descending
    query = new RuleQuery().setSortField(RuleIndexDefinition.FIELD_RULE_NAME).setAscendingSort(false);
    results = underTest.search(query, new SearchOptions());
    assertThat(results.getUuids()).containsExactly(fgh.getUuid(), abcd.getUuid(), abc.getUuid());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 47 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_security_sansTop25_return_vulnerabilities_and_hotspots_only.

@Test
public void search_by_security_sansTop25_return_vulnerabilities_and_hotspots_only() {
    RuleDefinitionDto rule1 = createRule(setSecurityStandards(of("owaspTop10:a1", "owaspTop10:a10", "cwe:89")), r -> r.setType(VULNERABILITY));
    RuleDefinitionDto rule2 = createRule(setSecurityStandards(of("owaspTop10:a10", "cwe:829")), r -> r.setType(SECURITY_HOTSPOT));
    createRule(setSecurityStandards(of("cwe:306")), r -> r.setType(CODE_SMELL));
    index();
    RuleQuery query = new RuleQuery().setSansTop25(of(SANS_TOP_25_INSECURE_INTERACTION, SANS_TOP_25_RISKY_RESOURCE));
    SearchIdResult<String> results = underTest.search(query, new SearchOptions().addFacets("sansTop25"));
    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 48 with RuleDefinitionDto

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

the class RuleIndexTest method default_sort_is_by_updated_at_desc.

@Test
public void default_sort_is_by_updated_at_desc() {
    RuleDefinitionDto old = createRule(setCreatedAt(1000L), setUpdatedAt(1000L));
    RuleDefinitionDto oldest = createRule(setCreatedAt(1000L), setUpdatedAt(3000L));
    RuleDefinitionDto older = createRule(setCreatedAt(1000L), setUpdatedAt(2000L));
    index();
    SearchIdResult<String> results = underTest.search(new RuleQuery(), new SearchOptions());
    assertThat(results.getUuids()).containsExactly(oldest.getUuid(), older.getUuid(), old.getUuid());
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 49 with RuleDefinitionDto

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

the class RuleIndexTest method tags_facet_supports_selected_value_with_regexp_special_characters.

@Test
public void tags_facet_supports_selected_value_with_regexp_special_characters() {
    RuleDefinitionDto rule = createRule();
    createRuleMetadata(rule, setTags("misra++"));
    index();
    RuleQuery query = new RuleQuery().setTags(singletonList("misra["));
    SearchOptions options = new SearchOptions().addFacets(FACET_TAGS);
    // do not fail
    assertThat(underTest.search(query, options).getTotal()).isZero();
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 50 with RuleDefinitionDto

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

the class RuleIndexTest method search_by_key.

@Test
public void search_by_key() {
    RuleDefinitionDto js1 = createRule(setRepositoryKey("javascript"), setRuleKey("X001"));
    RuleDefinitionDto cobol1 = createRule(setRepositoryKey("cobol"), setRuleKey("X001"));
    createRule(setRepositoryKey("php"), setRuleKey("S002"));
    index();
    // key
    RuleQuery query = new RuleQuery().setQueryText("X001");
    assertThat(underTest.search(query, new SearchOptions()).getUuids()).containsOnly(js1.getUuid(), cobol1.getUuid());
    // partial key does not match
    query = new RuleQuery().setQueryText("X00");
    assertThat(underTest.search(query, new SearchOptions()).getUuids()).isEmpty();
    // repo:key -> nice-to-have !
    query = new RuleQuery().setQueryText("javascript:X001");
    assertThat(underTest.search(query, new SearchOptions()).getUuids()).containsOnly(js1.getUuid());
}
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