*/
package org.sonar.server.issue;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
import java.util.Date;
import java.util.HashMap;
import org.sonar.api.resources.Scopes;
.setProjectUuid(mainBranchProjectUuid == null ? componentDto.projectUuid() : mainBranchProjectUuid)
// File path make no sens on modules and projects
.setFilePath(!componentDto.scope().equals(Scopes.PROJECT) ? componentDto.path() : null)
- .setIsMainBranch(mainBranchProjectUuid == null);
+ .setIsMainBranch(mainBranchProjectUuid == null)
+ .setFuncCreationDate(Date.from(LocalDateTime.of(1970, 1, 1, 1, 1).toInstant(ZoneOffset.UTC)));
}
public static IssueDoc newDoc() {
this.sorting = new Sorting();
this.sorting.add(IssueQuery.SORT_BY_STATUS, FIELD_ISSUE_STATUS);
+ this.sorting.add(IssueQuery.SORT_BY_STATUS, FIELD_ISSUE_KEY);
this.sorting.add(IssueQuery.SORT_BY_SEVERITY, FIELD_ISSUE_SEVERITY_VALUE);
+ this.sorting.add(IssueQuery.SORT_BY_SEVERITY, FIELD_ISSUE_KEY);
this.sorting.add(IssueQuery.SORT_BY_CREATION_DATE, FIELD_ISSUE_FUNC_CREATED_AT);
+ this.sorting.add(IssueQuery.SORT_BY_CREATION_DATE, FIELD_ISSUE_KEY);
this.sorting.add(IssueQuery.SORT_BY_UPDATE_DATE, FIELD_ISSUE_FUNC_UPDATED_AT);
+ this.sorting.add(IssueQuery.SORT_BY_UPDATE_DATE, FIELD_ISSUE_KEY);
this.sorting.add(IssueQuery.SORT_BY_CLOSE_DATE, FIELD_ISSUE_FUNC_CLOSED_AT);
+ this.sorting.add(IssueQuery.SORT_BY_CLOSE_DATE, FIELD_ISSUE_KEY);
this.sorting.add(IssueQuery.SORT_BY_FILE_LINE, FIELD_ISSUE_PROJECT_UUID);
this.sorting.add(IssueQuery.SORT_BY_FILE_LINE, FIELD_ISSUE_FILE_PATH);
this.sorting.add(IssueQuery.SORT_BY_FILE_LINE, FIELD_ISSUE_LINE);
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.TotalHits.Relation;
import org.assertj.core.groups.Tuple;
import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.search.SearchHit;
import org.junit.Rule;
import org.junit.Test;
String key = "I" + i;
issues.add(newDoc(key, file));
}
- indexIssues(issues.toArray(new IssueDoc[] {}));
+ indexIssues(issues.toArray(new IssueDoc[]{}));
IssueQuery.Builder query = IssueQuery.builder();
SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(500));
String key = "I" + i;
issues.add(newDoc(key, file));
}
- indexIssues(issues.toArray(new IssueDoc[] {}));
+ indexIssues(issues.toArray(new IssueDoc[]{}));
IssueQuery.Builder query = IssueQuery.builder();
SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(500));
assertThat(result.getHits().getTotalHits().relation).isEqualTo(Relation.EQUAL_TO);
}
+ @Test
+ public void search_nine_issues_with_same_creation_date_sorted_by_creation_date_order_is_sorted_also_by_key() {
+ ComponentDto project = newPrivateProjectDto();
+ ComponentDto file = newFileDto(project, null);
+ List<IssueDoc> issues = new ArrayList<>();
+ //we are adding issues in reverse order to see if the sort is actually doing anything
+ for (int i = 9; i >= 1; i--) {
+ String key = "I" + i;
+ issues.add(newDoc(key, file));
+ }
+ indexIssues(issues.toArray(new IssueDoc[]{}));
+ IssueQuery.Builder query = IssueQuery.builder().asc(true);
+
+ SearchResponse result = underTest.search(query.sort(IssueQuery.SORT_BY_CREATION_DATE).build(), new SearchOptions());
+
+ SearchHit[] hits = result.getHits().getHits();
+ for (int i = 1; i <= 9; i++) {
+ assertThat(hits[i - 1].getId()).isEqualTo("I" + i);
+ }
+ }
+
+ @Test
+ public void search_nine_issues_5_times_with_same_creation_date_sorted_by_creation_date_returned_issues_same_order() {
+ ComponentDto project = newPrivateProjectDto();
+ ComponentDto file = newFileDto(project, null);
+ List<IssueDoc> issues = new ArrayList<>();
+ //we are adding issues in reverse order to see if the sort is actually doing anything
+ for (int i = 9; i >= 1; i--) {
+ String key = "I" + i;
+ issues.add(newDoc(key, file));
+ }
+ indexIssues(issues.toArray(new IssueDoc[]{}));
+ IssueQuery.Builder query = IssueQuery.builder().asc(true);
+
+ SearchResponse result = underTest.search(query.sort(IssueQuery.SORT_BY_CREATION_DATE).build(), new SearchOptions());
+
+ SearchHit[] originalHits = result.getHits().getHits();
+ for (int i = 0; i < 4; i++) {
+ result = underTest.search(query.sort(IssueQuery.SORT_BY_CREATION_DATE).build(), new SearchOptions());
+ for (int j = 0; j < originalHits.length; j++) {
+ SearchHit[] hits = result.getHits().getHits();
+ assertThat(originalHits[j].getId()).isEqualTo(hits[j].getId());
+ }
+ }
+
+ }
+
@Test
public void authorized_issues_on_groups() {
ComponentDto project1 = newPrivateProjectDto();