Search in sources :

Example 6 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_sort_by_status.

@Test
public void should_sort_by_status() {
    IssueDto issue1 = new IssueDto().setKee("A").setStatus("CLOSED");
    IssueDto issue2 = new IssueDto().setKee("B").setStatus("REOPENED");
    IssueDto issue3 = new IssueDto().setKee("C").setStatus("OPEN");
    List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3);
    IssueQuery query = IssueQuery.builder().sort(IssueQuery.SORT_BY_STATUS).asc(false).build();
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
    List<IssueDto> result = newArrayList(issuesFinderSort.sort());
    assertThat(result).hasSize(3);
    assertThat(result.get(0).getStatus()).isEqualTo("REOPENED");
    assertThat(result.get(1).getStatus()).isEqualTo("OPEN");
    assertThat(result.get(2).getStatus()).isEqualTo("CLOSED");
}
Also used : IssueDto(org.sonar.db.issue.IssueDto) IssueQuery(org.sonar.server.issue.index.IssueQuery) Test(org.junit.Test)

Example 7 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class SearchAction method doIndexSearch.

private SearchResponse doIndexSearch(WsRequest wsRequest, DbSession dbSession, @Nullable ComponentDto project) {
    var builder = IssueQuery.builder().types(singleton(RuleType.SECURITY_HOTSPOT.name())).sort(IssueQuery.SORT_HOTSPOTS).asc(true).statuses(wsRequest.getStatus().map(Collections::singletonList).orElse(STATUSES));
    if (project != null) {
        String projectUuid = firstNonNull(project.getMainBranchProjectUuid(), project.uuid());
        if (Qualifiers.APP.equals(project.qualifier())) {
            builder.viewUuids(singletonList(projectUuid));
            if (wsRequest.isSinceLeakPeriod() && wsRequest.getPullRequest().isEmpty()) {
                addSinceLeakPeriodFilterByProjects(builder, dbSession, project);
            }
        } else {
            builder.projectUuids(singletonList(projectUuid));
            if (wsRequest.isSinceLeakPeriod() && wsRequest.getPullRequest().isEmpty()) {
                addSinceLeakPeriodFilter(dbSession, project, builder);
            }
        }
        addMainBranchFilter(project, builder);
    }
    if (!wsRequest.getHotspotKeys().isEmpty()) {
        builder.issueKeys(wsRequest.getHotspotKeys());
    }
    if (!wsRequest.getFiles().isEmpty()) {
        builder.files(wsRequest.getFiles());
    }
    if (wsRequest.isOnlyMine()) {
        userSession.checkLoggedIn();
        builder.assigneeUuids(Collections.singletonList(userSession.getUuid()));
    }
    wsRequest.getStatus().ifPresent(status -> builder.resolved(STATUS_REVIEWED.equals(status)));
    wsRequest.getResolution().ifPresent(resolution -> builder.resolutions(singleton(resolution)));
    addSecurityStandardFilters(wsRequest, builder);
    IssueQuery query = builder.build();
    SearchOptions searchOptions = new SearchOptions().setPage(wsRequest.page, wsRequest.index);
    return issueIndex.search(query, searchOptions);
}
Also used : IssueQuery(org.sonar.server.issue.index.IssueQuery) SearchOptions(org.sonar.server.es.SearchOptions) Collections(java.util.Collections)

Example 8 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_sort_by_close_date.

@Test
public void should_sort_by_close_date() {
    Date date = new Date();
    Date date1 = DateUtils.addDays(date, -3);
    Date date2 = DateUtils.addDays(date, -2);
    Date date3 = DateUtils.addDays(date, -1);
    IssueDto issue1 = new IssueDto().setKee("A").setIssueCloseDate(date1);
    IssueDto issue2 = new IssueDto().setKee("B").setIssueCloseDate(date3);
    IssueDto issue3 = new IssueDto().setKee("C").setIssueCloseDate(date2);
    List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3);
    IssueQuery query = IssueQuery.builder().sort(IssueQuery.SORT_BY_CLOSE_DATE).asc(false).build();
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
    List<IssueDto> result = newArrayList(issuesFinderSort.sort());
    assertThat(result).hasSize(3);
    assertThat(result.get(0).getIssueCloseDate()).isEqualTo(date3);
    assertThat(result.get(1).getIssueCloseDate()).isEqualTo(date2);
    assertThat(result.get(2).getIssueCloseDate()).isEqualTo(date1);
}
Also used : IssueDto(org.sonar.db.issue.IssueDto) IssueQuery(org.sonar.server.issue.index.IssueQuery) Date(java.util.Date) Test(org.junit.Test)

Example 9 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_sort_by_creation_date.

@Test
public void should_sort_by_creation_date() {
    Date date = new Date();
    Date date1 = DateUtils.addDays(date, -3);
    Date date2 = DateUtils.addDays(date, -2);
    Date date3 = DateUtils.addDays(date, -1);
    IssueDto issue1 = new IssueDto().setKee("A").setIssueCreationDate(date1);
    IssueDto issue2 = new IssueDto().setKee("B").setIssueCreationDate(date3);
    IssueDto issue3 = new IssueDto().setKee("C").setIssueCreationDate(date2);
    List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3);
    IssueQuery query = IssueQuery.builder().sort(IssueQuery.SORT_BY_CREATION_DATE).asc(false).build();
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
    List<IssueDto> result = newArrayList(issuesFinderSort.sort());
    assertThat(result).hasSize(3);
    assertThat(result.get(0).getIssueCreationDate()).isEqualTo(date3);
    assertThat(result.get(1).getIssueCreationDate()).isEqualTo(date2);
    assertThat(result.get(2).getIssueCreationDate()).isEqualTo(date1);
}
Also used : IssueDto(org.sonar.db.issue.IssueDto) IssueQuery(org.sonar.server.issue.index.IssueQuery) Date(java.util.Date) Test(org.junit.Test)

Example 10 with IssueQuery

use of org.sonar.server.issue.index.IssueQuery in project sonarqube by SonarSource.

the class IssuesFinderSortTest method should_fail_to_sort_with_unknown_sort.

@Test
public void should_fail_to_sort_with_unknown_sort() {
    IssueQuery query = mock(IssueQuery.class);
    when(query.sort()).thenReturn("unknown");
    IssuesFinderSort issuesFinderSort = new IssuesFinderSort(null, query);
    try {
        issuesFinderSort.sort();
    } catch (Exception e) {
        assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Cannot sort on field : unknown");
    }
}
Also used : IssueQuery(org.sonar.server.issue.index.IssueQuery) Test(org.junit.Test)

Aggregations

IssueQuery (org.sonar.server.issue.index.IssueQuery)11 Test (org.junit.Test)8 IssueDto (org.sonar.db.issue.IssueDto)7 Date (java.util.Date)3 SearchOptions (org.sonar.server.es.SearchOptions)2 Collections (java.util.Collections)1 Map (java.util.Map)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 Paging (org.sonar.api.utils.Paging)1 JsonWriter (org.sonar.api.utils.text.JsonWriter)1 Facets (org.sonar.server.es.Facets)1 SearchRequest (org.sonar.server.issue.SearchRequest)1