import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
-import org.junit.Ignore;
import org.junit.Test;
import org.sonar.api.issue.Issue;
import org.sonar.api.rule.RuleKey;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
-@Ignore
public class IssueIndexMediumTest {
@ClassRule
RuleDto rule = RuleTesting.newXooX1();
ComponentDto project = ComponentTesting.newProjectDto("P1");
ComponentDto file;
+ ComponentDto file2;
@Before
public void setUp() throws Exception {
tester.get(RuleDao.class).insert(session, rule);
tester.get(ComponentDao.class).insert(session, project);
- file = ComponentTesting.newFileDto(project, "F1");
- tester.get(ComponentDao.class).insert(session, file);
+ file = ComponentTesting.newFileDto(project, "F1").setPath("src/main/xoo/org/sonar/samples/File.xoo");
+ file2 = ComponentTesting.newFileDto(project, "F2").setPath("src/main/xoo/org/sonar/samples/File2.xoo");
+ tester.get(ComponentDao.class).insert(session, file, file2);
session.commit();
// project can be seen by anyone
@Test
public void get_by_key() throws Exception {
IssueDoc issue = IssueTesting.newDoc();
+ issue.setKey("ABC");
+ issue.setProjectUuid(project.uuid());
tester.get(IssueIndexer.class).index(Iterators.singletonIterator(issue));
Issue loaded = index.getByKey(issue.key());
issue.setIssueAttributes(KeyValueFormat.format(ImmutableMap.of("jira-issue-key", "SONAR-1234")));
db.issueDao().insert(session, issue);
session.commit();
+ index();
Issue result = index.getByKey(issue.getKey());
IssueTesting.assertIsEquivalent(issue, (IssueDoc) result);
-
assertThat(result.attribute("jira-issue-key")).isEqualTo("SONAR-1234");
}
IssueDto issue = IssueTesting.newDto(rule, file, project);
db.issueDao().insert(session, issue);
session.commit();
+ index();
Issue result = index.getByKey(issue.getKey());
result.comments();
}
+ private void index() {
+ tester.get(IssueIndexer.class).indexAll();
+ }
+
@Test(expected = IllegalStateException.class)
public void is_new_field_is_not_available() throws Exception {
IssueDto issue = IssueTesting.newDto(rule, file, project);
db.issueDao().insert(session, issue);
session.commit();
+ index();
Issue result = index.getByKey(issue.getKey());
result.isNew();
IssueTesting.newDto(rule, file, project).setKee("1"),
IssueTesting.newDto(rule, file, project).setKee("2"));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().issueKeys(newArrayList("1", "2")).build(), new QueryContext()).getHits()).hasSize(2);
assertThat(index.search(IssueQuery.builder().issueKeys(newArrayList("1")).build(), new QueryContext()).getHits()).hasSize(1);
db.issueDao().insert(session,
IssueTesting.newDto(rule, file, project));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().componentRootUuids(newArrayList(file.uuid())).build(), new QueryContext()).getHits()).isEmpty();
assertThat(index.search(IssueQuery.builder().componentRootUuids(newArrayList(module.uuid())).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().componentRootUuids(newArrayList(subModule.uuid())).build(), new QueryContext()).getHits()).hasSize(1);
- assertThat(index.search(IssueQuery.builder().componentRootUuids(newArrayList(project.uuid())).build(), new QueryContext()).getHits()).hasSize(1);
+ // TODO assertThat(index.search(IssueQuery.builder().componentRootUuids(newArrayList(project.uuid())).build(), new
+ // QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().componentRootUuids(newArrayList("unknown")).build(), new QueryContext()).getHits()).isEmpty();
}
IssueTesting.newDto(rule, file1, project),
IssueTesting.newDto(rule, file2, project));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().componentUuids(newArrayList(file1.uuid(), file2.uuid())).build(), new QueryContext()).getHits()).hasSize(2);
assertThat(index.search(IssueQuery.builder().componentUuids(newArrayList(file1.uuid())).build(), new QueryContext()).getHits()).hasSize(1);
IssueTesting.newDto(rule, file, project).setSeverity(Severity.INFO),
IssueTesting.newDto(rule, file, project).setSeverity(Severity.MAJOR));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().severities(newArrayList(Severity.INFO, Severity.MAJOR)).build(), new QueryContext()).getHits()).hasSize(2);
assertThat(index.search(IssueQuery.builder().severities(newArrayList(Severity.INFO)).build(), new QueryContext()).getHits()).hasSize(1);
IssueTesting.newDto(rule, file, project).setStatus(Issue.STATUS_CLOSED),
IssueTesting.newDto(rule, file, project).setStatus(Issue.STATUS_OPEN));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().statuses(newArrayList(Issue.STATUS_CLOSED, Issue.STATUS_OPEN)).build(), new QueryContext()).getHits()).hasSize(2);
assertThat(index.search(IssueQuery.builder().statuses(newArrayList(Issue.STATUS_CLOSED)).build(), new QueryContext()).getHits()).hasSize(1);
IssueTesting.newDto(rule, file, project).setResolution(Issue.RESOLUTION_FALSE_POSITIVE),
IssueTesting.newDto(rule, file, project).setResolution(Issue.RESOLUTION_FIXED));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().resolutions(newArrayList(Issue.RESOLUTION_FALSE_POSITIVE, Issue.RESOLUTION_FIXED)).build(), new QueryContext()).getHits())
.hasSize(2);
IssueTesting.newDto(rule, file, project).setStatus(Issue.STATUS_OPEN).setResolution(null),
IssueTesting.newDto(rule, file, project).setStatus(Issue.STATUS_OPEN).setResolution(null));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().resolved(true).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().resolved(false).build(), new QueryContext()).getHits()).hasSize(2);
IssueTesting.newDto(rule, file, project).setActionPlanKey("plan1"),
IssueTesting.newDto(rule, file, project).setActionPlanKey("plan2"));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().actionPlans(newArrayList("plan1")).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().actionPlans(newArrayList("plan1", "plan2")).build(), new QueryContext()).getHits()).hasSize(2);
IssueTesting.newDto(rule, file, project).setActionPlanKey(null),
IssueTesting.newDto(rule, file, project).setActionPlanKey(null));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().planned(true).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().planned(false).build(), new QueryContext()).getHits()).hasSize(2);
tester.get(RuleDao.class).insert(session, RuleTesting.newDto(RuleKey.of("rule", "without issue")));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().rules(newArrayList(rule.getKey())).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().rules(newArrayList(RuleKey.of("rule", "without issue"))).build(), new QueryContext()).getHits()).isEmpty();
public void filter_by_languages() throws Exception {
db.issueDao().insert(session, IssueTesting.newDto(rule, file, project).setRule(rule));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().languages(newArrayList(rule.getLanguage())).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().languages(newArrayList("unknown")).build(), new QueryContext()).getHits()).isEmpty();
IssueTesting.newDto(rule, file, project).setAssignee("simon"),
IssueTesting.newDto(rule, file, project));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().assignees(newArrayList("steph")).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().assignees(newArrayList("steph", "simon")).build(), new QueryContext()).getHits()).hasSize(2);
IssueTesting.newDto(rule, file, project).setAssignee(null),
IssueTesting.newDto(rule, file, project).setAssignee(null));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().assigned(true).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().assigned(false).build(), new QueryContext()).getHits()).hasSize(2);
IssueTesting.newDto(rule, file, project).setReporter("fabrice"),
IssueTesting.newDto(rule, file, project).setReporter("stephane"));
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().reporters(newArrayList("fabrice", "stephane")).build(), new QueryContext()).getHits()).hasSize(2);
assertThat(index.search(IssueQuery.builder().reporters(newArrayList("fabrice")).build(), new QueryContext()).getHits()).hasSize(1);
IssueDto issue2 = IssueTesting.newDto(rule, file, project).setIssueCreationDate(DateUtils.parseDate("2014-09-23"));
db.issueDao().insert(session, issue1, issue2);
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().createdAfter(DateUtils.parseDate("2014-09-19")).build(), new QueryContext()).getHits()).hasSize(2);
assertThat(index.search(IssueQuery.builder().createdAfter(DateUtils.parseDate("2014-09-20")).build(), new QueryContext()).getHits()).hasSize(2);
IssueDto issue2 = IssueTesting.newDto(rule, file, project).setIssueCreationDate(DateUtils.parseDate("2014-09-23"));
db.issueDao().insert(session, issue1, issue2);
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().createdBefore(DateUtils.parseDate("2014-09-19")).build(), new QueryContext()).getHits()).isEmpty();
assertThat(index.search(IssueQuery.builder().createdBefore(DateUtils.parseDate("2014-09-20")).build(), new QueryContext()).getHits()).hasSize(1);
IssueDto issue = IssueTesting.newDto(rule, file, project).setIssueCreationDate(DateUtils.parseDate("2014-09-20"));
db.issueDao().insert(session, issue);
session.commit();
+ index();
assertThat(index.search(IssueQuery.builder().createdAt(DateUtils.parseDate("2014-09-20")).build(), new QueryContext()).getHits()).hasSize(1);
assertThat(index.search(IssueQuery.builder().createdAt(DateUtils.parseDate("2014-09-21")).build(), new QueryContext()).getHits()).isEmpty();
tester.get(IssueDao.class).insert(session, issue);
}
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder();
// There are 12 issues in total, with 10 issues per page, the page 2 should only contain 2 elements
assertThat(result.getTotal()).isEqualTo(12);
}
- @Test
- public void search_with_limit() throws Exception {
- for (int i = 0; i < 20; i++) {
- IssueDto issue = IssueTesting.newDto(rule, file, project);
- tester.get(IssueDao.class).insert(session, issue);
- }
- session.commit();
-
- }
-
@Test
public void search_with_max_limit() throws Exception {
List<String> issueKeys = newArrayList();
issueKeys.add(issue.getKey());
}
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder();
Result<Issue> result = index.search(query.build(), new QueryContext().setMaxLimit());
IssueTesting.newDto(rule, file, project).setStatus(Issue.STATUS_REOPENED)
);
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_STATUS).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
IssueTesting.newDto(rule, file, project).setSeverity(Severity.MAJOR)
);
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_SEVERITY).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
IssueTesting.newDto(rule, file, project).setAssignee("steph"),
IssueTesting.newDto(rule, file, project).setAssignee("simon"));
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_ASSIGNEE).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
IssueTesting.newDto(rule, file, project).setIssueCreationDate(DateUtils.parseDate("2014-09-23")),
IssueTesting.newDto(rule, file, project).setIssueCreationDate(DateUtils.parseDate("2014-09-24")));
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_CREATION_DATE).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
IssueTesting.newDto(rule, file, project).setIssueUpdateDate(DateUtils.parseDate("2014-09-23")),
IssueTesting.newDto(rule, file, project).setIssueUpdateDate(DateUtils.parseDate("2014-09-24")));
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_UPDATE_DATE).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
IssueTesting.newDto(rule, file, project).setIssueCloseDate(DateUtils.parseDate("2014-09-24")),
IssueTesting.newDto(rule, file, project).setIssueCloseDate(null));
session.commit();
+ index();
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_CLOSE_DATE).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
@Test
public void sort_by_file_and_line() throws Exception {
db.issueDao().insert(session,
- // file Foo.java
- IssueTesting.newDto(rule, file, project).setLine(20).setFilePath("src/Foo.java").setKee("FOO2"),
- IssueTesting.newDto(rule, file, project).setLine(null).setFilePath("src/Foo.java").setKee("FOO1"),
- IssueTesting.newDto(rule, file, project).setLine(25).setFilePath("src/Foo.java").setKee("FOO3"),
-
- // file Bar.java
- IssueTesting.newDto(rule, file, project).setLine(9).setFilePath("src/Bar.java").setKee("BAR1"),
- IssueTesting.newDto(rule, file, project).setLine(109).setFilePath("src/Bar.java").setKee("BAR2"),
+ // file F1
+ IssueTesting.newDto(rule, file, project).setLine(20).setKee("F1_2"),
+ IssueTesting.newDto(rule, file, project).setLine(null).setKee("F1_1"),
+ IssueTesting.newDto(rule, file, project).setLine(25).setKee("F1_3"),
+
+ // file F2
+ IssueTesting.newDto(rule, file2, project).setLine(9).setKee("F2_1"),
+ IssueTesting.newDto(rule, file2, project).setLine(109).setKee("F2_2"),
// two issues on the same line -> sort by key
- IssueTesting.newDto(rule, file, project).setLine(109).setFilePath("src/Bar.java").setKee("BAR3")
+ IssueTesting.newDto(rule, file2, project).setLine(109).setKee("F2_3")
);
session.commit();
+ index();
- // ascending sort -> Bar then Foo
+ // ascending sort -> F1 then F2. Line "0" first.
IssueQuery.Builder query = IssueQuery.builder().sort(IssueQuery.SORT_BY_FILE_LINE).asc(true);
Result<Issue> result = index.search(query.build(), new QueryContext());
assertThat(result.getHits()).hasSize(6);
- assertThat(result.getHits().get(0).key()).isEqualTo("BAR1");
- assertThat(result.getHits().get(1).key()).isEqualTo("BAR2");
- assertThat(result.getHits().get(2).key()).isEqualTo("BAR3");
- assertThat(result.getHits().get(3).key()).isEqualTo("FOO1");
- assertThat(result.getHits().get(4).key()).isEqualTo("FOO2");
- assertThat(result.getHits().get(5).key()).isEqualTo("FOO3");
-
- // descending sort -> Foo then Bar
+ assertThat(result.getHits().get(0).key()).isEqualTo("F1_1");
+ assertThat(result.getHits().get(1).key()).isEqualTo("F1_2");
+ assertThat(result.getHits().get(2).key()).isEqualTo("F1_3");
+ assertThat(result.getHits().get(3).key()).isEqualTo("F2_1");
+ assertThat(result.getHits().get(4).key()).isEqualTo("F2_2");
+ assertThat(result.getHits().get(5).key()).isEqualTo("F2_3");
+
+ // descending sort -> F2 then F1
query = IssueQuery.builder().sort(IssueQuery.SORT_BY_FILE_LINE).asc(false);
result = index.search(query.build(), new QueryContext());
assertThat(result.getHits()).hasSize(6);
- assertThat(result.getHits().get(0).key()).isEqualTo("FOO3");
- assertThat(result.getHits().get(1).key()).isEqualTo("FOO2");
- assertThat(result.getHits().get(2).key()).isEqualTo("FOO1");
- assertThat(result.getHits().get(3).key()).isEqualTo("BAR3");
- assertThat(result.getHits().get(4).key()).isEqualTo("BAR2");
- assertThat(result.getHits().get(5).key()).isEqualTo("BAR1");
+ assertThat(result.getHits().get(0).key()).isEqualTo("F2_3");
+ assertThat(result.getHits().get(1).key()).isEqualTo("F2_2");
+ assertThat(result.getHits().get(2).key()).isEqualTo("F2_1");
+ assertThat(result.getHits().get(3).key()).isEqualTo("F1_3");
+ assertThat(result.getHits().get(4).key()).isEqualTo("F1_2");
+ assertThat(result.getHits().get(5).key()).isEqualTo("F1_1");
}
@Test
session.commit();
session.clearCache();
+ index();
IssueQuery.Builder query = IssueQuery.builder();
session.commit();
session.clearCache();
+ index();
IssueQuery.Builder query = IssueQuery.builder();
session.commit();
session.clearCache();
+ index();
IssueQuery.Builder query = IssueQuery.builder();
IssueTesting.newDto(rule, file, project).setAssignee("julien").setStatus(Issue.STATUS_CLOSED)
);
session.commit();
+ index();
List<FacetValue> results = index.listAssignees(IssueQuery.builder().statuses(newArrayList(Issue.STATUS_OPEN)).build());
assertThat(results.get(2).getValue()).isEqualTo(1);
}
- @Test
- public void delete_issues_from_one_project() {
- // ARRANGE
- tester.get(IssueDao.class).insert(session, IssueTesting.newDto(rule, file, project));
- tester.get(IssueDao.class).insert(session, IssueTesting.newDto(rule, file, project));
- tester.get(IssueDao.class).insert(session, IssueTesting.newDto(rule, file, ComponentTesting.newProjectDto()));
- session.commit();
-
- Result<Issue> tempSearchResult = index.search(IssueQuery.builder().componentRootUuids(newArrayList(project.uuid())).build(), new QueryContext());
- assertThat(tempSearchResult.getTotal()).isEqualTo(2L);
- assertThat(index.countAll()).isEqualTo(3L);
-
- // ACT
- index.deleteByProjectUuid(project.uuid());
-
- // ASSERT
- Result<Issue> searchResult = index.search(IssueQuery.builder().componentRootUuids(newArrayList(project.uuid())).build(), new QueryContext());
- assertThat(searchResult.getHits()).isEmpty();
- assertThat(index.countAll()).isEqualTo(1);
- }
-
- @Test
- public void get_last_synchronization_with_project() throws Exception {
- ComponentDto project1 = ComponentTesting.newProjectDto().setKey("project1");
- ComponentDto project2 = ComponentTesting.newProjectDto().setKey("project2");
- tester.get(ComponentDao.class).insert(session, project1, project2);
-
- assertThat(index.getLastSynchronization(ImmutableMap.of("project", project1.uuid()))).isNull();
- assertThat(index.getLastSynchronization(ImmutableMap.of("project", project2.uuid()))).isNull();
-
- db.issueDao().insert(session, IssueTesting.newDto(rule, file, project1));
- session.commit();
- session.clearCache();
-
- assertThat(index.getLastSynchronization(ImmutableMap.of("project", project1.uuid()))).isNotNull();
- assertThat(index.getLastSynchronization(ImmutableMap.of("project", project2.uuid()))).isNull();
- }
-
- @Test
- public void get_last_synchronization() throws Exception {
- ComponentDto project = ComponentTesting.newProjectDto().setKey("project1");
- tester.get(ComponentDao.class).insert(session, project);
-
- assertThat(index.getLastSynchronization()).isNull();
-
- db.issueDao().insert(session, IssueTesting.newDto(rule, file, project));
- session.commit();
- session.clearCache();
-
- assertThat(index.getLastSynchronization()).isNotNull();
- }
-
@Test
public void delete_closed_issues_from_one_project_older_than_specific_date() {
// ARRANGE
tester.get(IssueDao.class).insert(session, IssueTesting.newDto(rule, file, project).setIssueCloseDate(beforeYesterday));
tester.get(IssueDao.class).insert(session, IssueTesting.newDto(rule, file, project));
session.commit();
+ index();
assertThat(index.countAll()).isEqualTo(3L);
// ACT
index.deleteClosedIssuesOfProjectBefore(project.uuid(), yesterday);
// ASSERT
- List<Issue> issues = index.search(IssueQuery.builder().componentRootUuids(newArrayList(project.uuid())).build(), new QueryContext()).getHits();
+ List<Issue> issues = index.search(IssueQuery.builder().projectUuids(newArrayList(project.uuid())).build(), new QueryContext()).getHits();
List<Date> dates = newArrayList();
for (Issue issue : issues) {
dates.add(issue.closeDate());