You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssueQueryFactoryTest.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.issue.index;
  21. import java.time.Clock;
  22. import java.time.ZoneOffset;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.Date;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.sonar.api.resources.Qualifiers;
  30. import org.sonar.api.rule.RuleKey;
  31. import org.sonar.api.utils.DateUtils;
  32. import org.sonar.db.DbTester;
  33. import org.sonar.db.component.ComponentDto;
  34. import org.sonar.db.component.SnapshotDto;
  35. import org.sonar.db.organization.OrganizationDto;
  36. import org.sonar.db.rule.RuleDbTester;
  37. import org.sonar.db.rule.RuleDefinitionDto;
  38. import org.sonar.db.user.UserDto;
  39. import org.sonar.server.issue.SearchRequest;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import static java.util.Arrays.asList;
  42. import static java.util.Collections.singletonList;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.junit.Assert.fail;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. import static org.sonar.api.resources.Qualifiers.APP;
  48. import static org.sonar.api.utils.DateUtils.addDays;
  49. import static org.sonar.api.web.UserRole.USER;
  50. import static org.sonar.db.component.ComponentTesting.newDirectory;
  51. import static org.sonar.db.component.ComponentTesting.newFileDto;
  52. import static org.sonar.db.component.ComponentTesting.newModuleDto;
  53. import static org.sonar.db.component.ComponentTesting.newProjectCopy;
  54. import static org.sonar.db.component.ComponentTesting.newSubView;
  55. import static org.sonar.db.rule.RuleTesting.newRule;
  56. public class IssueQueryFactoryTest {
  57. @Rule
  58. public UserSessionRule userSession = UserSessionRule.standalone();
  59. @Rule
  60. public ExpectedException expectedException = ExpectedException.none();
  61. @Rule
  62. public DbTester db = DbTester.create();
  63. private RuleDbTester ruleDbTester = new RuleDbTester(db);
  64. private Clock clock = mock(Clock.class);
  65. private IssueQueryFactory underTest = new IssueQueryFactory(db.getDbClient(), clock, userSession);
  66. @Test
  67. public void create_from_parameters() {
  68. UserDto user = db.users().insertUser(u -> u.setLogin("joanna"));
  69. OrganizationDto organization = db.organizations().insert();
  70. ComponentDto project = db.components().insertPrivateProject(organization);
  71. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  72. ComponentDto file = db.components().insertComponent(newFileDto(project));
  73. RuleDefinitionDto rule1 = ruleDbTester.insert();
  74. RuleDefinitionDto rule2 = ruleDbTester.insert();
  75. newRule(RuleKey.of("findbugs", "NullReference"));
  76. SearchRequest request = new SearchRequest()
  77. .setIssues(asList("anIssueKey"))
  78. .setSeverities(asList("MAJOR", "MINOR"))
  79. .setStatuses(asList("CLOSED"))
  80. .setResolutions(asList("FALSE-POSITIVE"))
  81. .setResolved(true)
  82. .setProjectKeys(asList(project.getDbKey()))
  83. .setModuleUuids(asList(module.uuid()))
  84. .setDirectories(asList("aDirPath"))
  85. .setFileUuids(asList(file.uuid()))
  86. .setAssigneesUuid(asList(user.getUuid()))
  87. .setLanguages(asList("xoo"))
  88. .setTags(asList("tag1", "tag2"))
  89. .setOrganization(organization.getKey())
  90. .setAssigned(true)
  91. .setCreatedAfter("2013-04-16T09:08:24+0200")
  92. .setCreatedBefore("2013-04-17T09:08:24+0200")
  93. .setRules(asList(rule1.getKey().toString(), rule2.getKey().toString()))
  94. .setSort("CREATION_DATE")
  95. .setAsc(true);
  96. IssueQuery query = underTest.create(request);
  97. assertThat(query.issueKeys()).containsOnly("anIssueKey");
  98. assertThat(query.severities()).containsOnly("MAJOR", "MINOR");
  99. assertThat(query.statuses()).containsOnly("CLOSED");
  100. assertThat(query.resolutions()).containsOnly("FALSE-POSITIVE");
  101. assertThat(query.resolved()).isTrue();
  102. assertThat(query.projectUuids()).containsOnly(project.uuid());
  103. assertThat(query.moduleUuids()).containsOnly(module.uuid());
  104. assertThat(query.fileUuids()).containsOnly(file.uuid());
  105. assertThat(query.assignees()).containsOnly(user.getUuid());
  106. assertThat(query.languages()).containsOnly("xoo");
  107. assertThat(query.tags()).containsOnly("tag1", "tag2");
  108. assertThat(query.organizationUuid()).isEqualTo(organization.getUuid());
  109. assertThat(query.onComponentOnly()).isFalse();
  110. assertThat(query.assigned()).isTrue();
  111. assertThat(query.rules()).hasSize(2);
  112. assertThat(query.directories()).containsOnly("aDirPath");
  113. assertThat(query.createdAfter().date()).isEqualTo(DateUtils.parseDateTime("2013-04-16T09:08:24+0200"));
  114. assertThat(query.createdAfter().inclusive()).isTrue();
  115. assertThat(query.createdBefore()).isEqualTo(DateUtils.parseDateTime("2013-04-17T09:08:24+0200"));
  116. assertThat(query.sort()).isEqualTo(IssueQuery.SORT_BY_CREATION_DATE);
  117. assertThat(query.asc()).isTrue();
  118. }
  119. @Test
  120. public void leak_period_start_date_is_exclusive() {
  121. long leakPeriodStart = addDays(new Date(), -14).getTime();
  122. ComponentDto project = db.components().insertPublicProject();
  123. ComponentDto file = db.components().insertComponent(newFileDto(project));
  124. SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setPeriodDate(leakPeriodStart));
  125. SearchRequest request = new SearchRequest()
  126. .setComponentUuids(Collections.singletonList(file.uuid()))
  127. .setOnComponentOnly(true)
  128. .setSinceLeakPeriod(true);
  129. IssueQuery query = underTest.create(request);
  130. assertThat(query.componentUuids()).containsOnly(file.uuid());
  131. assertThat(query.createdAfter().date()).isEqualTo(new Date(leakPeriodStart));
  132. assertThat(query.createdAfter().inclusive()).isFalse();
  133. }
  134. @Test
  135. public void dates_are_inclusive() {
  136. SearchRequest request = new SearchRequest()
  137. .setCreatedAfter("2013-04-16")
  138. .setCreatedBefore("2013-04-17");
  139. IssueQuery query = underTest.create(request);
  140. assertThat(query.createdAfter().date()).isEqualTo(DateUtils.parseDate("2013-04-16"));
  141. assertThat(query.createdAfter().inclusive()).isTrue();
  142. assertThat(query.createdBefore()).isEqualTo(DateUtils.parseDate("2013-04-18"));
  143. }
  144. @Test
  145. public void creation_date_support_localdate() {
  146. SearchRequest request = new SearchRequest()
  147. .setCreatedAt("2013-04-16");
  148. IssueQuery query = underTest.create(request);
  149. assertThat(query.createdAt()).isEqualTo(DateUtils.parseDate("2013-04-16"));
  150. }
  151. @Test
  152. public void creation_date_support_zoneddatetime() {
  153. SearchRequest request = new SearchRequest()
  154. .setCreatedAt("2013-04-16T09:08:24+0200");
  155. IssueQuery query = underTest.create(request);
  156. assertThat(query.createdAt()).isEqualTo(DateUtils.parseDateTime("2013-04-16T09:08:24+0200"));
  157. }
  158. @Test
  159. public void add_unknown_when_no_component_found() {
  160. SearchRequest request = new SearchRequest()
  161. .setComponentKeys(asList("does_not_exist"));
  162. IssueQuery query = underTest.create(request);
  163. assertThat(query.componentUuids()).containsOnly("<UNKNOWN>");
  164. }
  165. @Test
  166. public void query_without_any_parameter() {
  167. SearchRequest request = new SearchRequest();
  168. IssueQuery query = underTest.create(request);
  169. assertThat(query.componentUuids()).isEmpty();
  170. assertThat(query.projectUuids()).isEmpty();
  171. assertThat(query.moduleUuids()).isEmpty();
  172. assertThat(query.moduleRootUuids()).isEmpty();
  173. assertThat(query.directories()).isEmpty();
  174. assertThat(query.fileUuids()).isEmpty();
  175. assertThat(query.viewUuids()).isEmpty();
  176. assertThat(query.organizationUuid()).isNull();
  177. assertThat(query.branchUuid()).isNull();
  178. }
  179. @Test
  180. public void fail_if_components_and_components_uuid_params_are_set_at_the_same_time() {
  181. SearchRequest request = new SearchRequest()
  182. .setComponentKeys(singletonList("foo"))
  183. .setComponentUuids(singletonList("bar"));
  184. expectedException.expect(IllegalArgumentException.class);
  185. expectedException.expectMessage("At most one of the following parameters can be provided: componentKeys and componentUuids");
  186. underTest.create(request);
  187. }
  188. @Test
  189. public void fail_if_both_componentRoots_and_componentRootUuids_params_are_set() {
  190. SearchRequest request = new SearchRequest()
  191. .setComponentRoots(singletonList("foo"))
  192. .setComponentRootUuids(singletonList("bar"));
  193. expectedException.expect(IllegalArgumentException.class);
  194. expectedException.expectMessage("At most one of the following parameters can be provided: componentKeys and componentUuids");
  195. underTest.create(request);
  196. }
  197. @Test
  198. public void fail_if_componentRoots_references_components_with_different_qualifier() {
  199. ComponentDto project = db.components().insertPrivateProject();
  200. ComponentDto file = db.components().insertComponent(newFileDto(project));
  201. SearchRequest request = new SearchRequest()
  202. .setComponentRoots(asList(project.getDbKey(), file.getDbKey()));
  203. expectedException.expect(IllegalArgumentException.class);
  204. expectedException.expectMessage("All components must have the same qualifier, found FIL,TRK");
  205. underTest.create(request);
  206. }
  207. @Test
  208. public void param_componentRootUuids_enables_search_in_view_tree_if_user_has_permission_on_view() {
  209. ComponentDto view = db.components().insertView();
  210. SearchRequest request = new SearchRequest()
  211. .setComponentRootUuids(asList(view.uuid()));
  212. userSession.registerComponents(view);
  213. IssueQuery query = underTest.create(request);
  214. assertThat(query.viewUuids()).containsOnly(view.uuid());
  215. assertThat(query.onComponentOnly()).isFalse();
  216. }
  217. @Test
  218. public void application_search_project_issues() {
  219. ComponentDto project1 = db.components().insertPublicProject();
  220. ComponentDto project2 = db.components().insertPublicProject();
  221. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  222. db.components().insertComponents(newProjectCopy("PC1", project1, application));
  223. db.components().insertComponents(newProjectCopy("PC2", project2, application));
  224. userSession.registerComponents(application, project1, project2);
  225. IssueQuery result = underTest.create(new SearchRequest().setComponentUuids(singletonList(application.uuid())));
  226. assertThat(result.viewUuids()).containsExactlyInAnyOrder(application.uuid());
  227. }
  228. @Test
  229. public void application_search_project_issues_on_leak() {
  230. Date now = new Date();
  231. ComponentDto project1 = db.components().insertPublicProject();
  232. SnapshotDto analysis1 = db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
  233. ComponentDto project2 = db.components().insertPublicProject();
  234. SnapshotDto analysis2 = db.components().insertSnapshot(project2, s -> s.setPeriodDate(null));
  235. ComponentDto project3 = db.components().insertPublicProject();
  236. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  237. db.components().insertComponents(newProjectCopy("PC1", project1, application));
  238. db.components().insertComponents(newProjectCopy("PC2", project2, application));
  239. db.components().insertComponents(newProjectCopy("PC3", project3, application));
  240. userSession.registerComponents(application, project1, project2, project3);
  241. IssueQuery result = underTest.create(new SearchRequest()
  242. .setComponentUuids(singletonList(application.uuid()))
  243. .setSinceLeakPeriod(true));
  244. assertThat(result.createdAfterByProjectUuids()).hasSize(1);
  245. assertThat(result.createdAfterByProjectUuids().get(project1.uuid()).date().getTime()).isEqualTo(analysis1.getPeriodDate());
  246. assertThat(result.createdAfterByProjectUuids().get(project1.uuid()).inclusive()).isFalse();
  247. assertThat(result.viewUuids()).containsExactlyInAnyOrder(application.uuid());
  248. }
  249. @Test
  250. public void return_empty_results_if_not_allowed_to_search_for_subview() {
  251. ComponentDto view = db.components().insertView();
  252. ComponentDto subView = db.components().insertComponent(newSubView(view));
  253. SearchRequest request = new SearchRequest()
  254. .setComponentRootUuids(asList(subView.uuid()));
  255. IssueQuery query = underTest.create(request);
  256. assertThat(query.viewUuids()).containsOnly("<UNKNOWN>");
  257. }
  258. @Test
  259. public void param_componentUuids_enables_search_on_project_tree_by_default() {
  260. ComponentDto project = db.components().insertPrivateProject();
  261. SearchRequest request = new SearchRequest()
  262. .setComponentUuids(asList(project.uuid()));
  263. IssueQuery query = underTest.create(request);
  264. assertThat(query.projectUuids()).containsExactly(project.uuid());
  265. assertThat(query.onComponentOnly()).isFalse();
  266. }
  267. @Test
  268. public void onComponentOnly_restricts_search_to_specified_componentKeys() {
  269. ComponentDto project = db.components().insertPrivateProject();
  270. SearchRequest request = new SearchRequest()
  271. .setComponentKeys(asList(project.getDbKey()))
  272. .setOnComponentOnly(true);
  273. IssueQuery query = underTest.create(request);
  274. assertThat(query.projectUuids()).isEmpty();
  275. assertThat(query.componentUuids()).containsExactly(project.uuid());
  276. assertThat(query.onComponentOnly()).isTrue();
  277. }
  278. @Test
  279. public void should_search_in_tree_with_module_uuid() {
  280. ComponentDto project = db.components().insertPrivateProject();
  281. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  282. SearchRequest request = new SearchRequest()
  283. .setComponentUuids(asList(module.uuid()));
  284. IssueQuery query = underTest.create(request);
  285. assertThat(query.moduleRootUuids()).containsExactly(module.uuid());
  286. assertThat(query.onComponentOnly()).isFalse();
  287. }
  288. @Test
  289. public void param_componentUuids_enables_search_in_directory_tree() {
  290. ComponentDto project = db.components().insertPrivateProject();
  291. ComponentDto dir = db.components().insertComponent(newDirectory(project, "src/main/java/foo"));
  292. SearchRequest request = new SearchRequest()
  293. .setComponentUuids(asList(dir.uuid()));
  294. IssueQuery query = underTest.create(request);
  295. assertThat(query.moduleUuids()).containsOnly(dir.moduleUuid());
  296. assertThat(query.directories()).containsOnly(dir.path());
  297. assertThat(query.onComponentOnly()).isFalse();
  298. }
  299. @Test
  300. public void param_componentUuids_enables_search_by_file() {
  301. ComponentDto project = db.components().insertPrivateProject();
  302. ComponentDto file = db.components().insertComponent(newFileDto(project));
  303. SearchRequest request = new SearchRequest()
  304. .setComponentUuids(asList(file.uuid()));
  305. IssueQuery query = underTest.create(request);
  306. assertThat(query.fileUuids()).containsExactly(file.uuid());
  307. }
  308. @Test
  309. public void param_componentUuids_enables_search_by_test_file() {
  310. ComponentDto project = db.components().insertPrivateProject();
  311. ComponentDto file = db.components().insertComponent(newFileDto(project).setQualifier(Qualifiers.UNIT_TEST_FILE));
  312. SearchRequest request = new SearchRequest()
  313. .setComponentUuids(asList(file.uuid()));
  314. IssueQuery query = underTest.create(request);
  315. assertThat(query.fileUuids()).containsExactly(file.uuid());
  316. }
  317. @Test
  318. public void search_issue_from_branch() {
  319. ComponentDto project = db.components().insertPrivateProject();
  320. ComponentDto branch = db.components().insertProjectBranch(project);
  321. assertThat(underTest.create(new SearchRequest()
  322. .setProjectKeys(singletonList(branch.getKey()))
  323. .setBranch(branch.getBranch())))
  324. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
  325. .containsOnly(branch.uuid(), singletonList(project.uuid()), false);
  326. assertThat(underTest.create(new SearchRequest()
  327. .setComponentKeys(singletonList(branch.getKey()))
  328. .setBranch(branch.getBranch())))
  329. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
  330. .containsOnly(branch.uuid(), singletonList(project.uuid()), false);
  331. }
  332. @Test
  333. public void search_file_issue_from_branch() {
  334. ComponentDto project = db.components().insertPrivateProject();
  335. ComponentDto branch = db.components().insertProjectBranch(project);
  336. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  337. assertThat(underTest.create(new SearchRequest()
  338. .setComponentKeys(singletonList(file.getKey()))
  339. .setBranch(branch.getBranch())))
  340. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.fileUuids()), IssueQuery::isMainBranch)
  341. .containsOnly(branch.uuid(), singletonList(file.uuid()), false);
  342. assertThat(underTest.create(new SearchRequest()
  343. .setComponentKeys(singletonList(branch.getKey()))
  344. .setFileUuids(singletonList(file.uuid()))
  345. .setBranch(branch.getBranch())))
  346. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.fileUuids()), IssueQuery::isMainBranch)
  347. .containsOnly(branch.uuid(), singletonList(file.uuid()), false);
  348. assertThat(underTest.create(new SearchRequest()
  349. .setProjectKeys(singletonList(branch.getKey()))
  350. .setFileUuids(singletonList(file.uuid()))
  351. .setBranch(branch.getBranch())))
  352. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.fileUuids()), IssueQuery::isMainBranch)
  353. .containsOnly(branch.uuid(), singletonList(file.uuid()), false);
  354. }
  355. @Test
  356. public void search_issue_on_component_only_from_branch() {
  357. ComponentDto project = db.components().insertPrivateProject();
  358. ComponentDto branch = db.components().insertProjectBranch(project);
  359. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  360. assertThat(underTest.create(new SearchRequest()
  361. .setComponentKeys(singletonList(file.getKey()))
  362. .setBranch(branch.getBranch())
  363. .setOnComponentOnly(true)))
  364. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.componentUuids()), IssueQuery::isMainBranch)
  365. .containsOnly(branch.uuid(), singletonList(file.uuid()), false);
  366. }
  367. @Test
  368. public void search_issues_from_main_branch() {
  369. ComponentDto project = db.components().insertMainBranch();
  370. ComponentDto branch = db.components().insertProjectBranch(project);
  371. assertThat(underTest.create(new SearchRequest()
  372. .setProjectKeys(singletonList(project.getKey()))
  373. .setBranch("master")))
  374. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
  375. .containsOnly(project.uuid(), singletonList(project.uuid()), true);
  376. assertThat(underTest.create(new SearchRequest()
  377. .setComponentKeys(singletonList(project.getKey()))
  378. .setBranch("master")))
  379. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
  380. .containsOnly(project.uuid(), singletonList(project.uuid()), true);
  381. }
  382. @Test
  383. public void search_by_application_key() {
  384. ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization());
  385. ComponentDto project1 = db.components().insertPrivateProject();
  386. ComponentDto project2 = db.components().insertPrivateProject();
  387. db.components().insertComponents(newProjectCopy(project1, application));
  388. db.components().insertComponents(newProjectCopy(project2, application));
  389. userSession.addProjectPermission(USER, application);
  390. assertThat(underTest.create(new SearchRequest()
  391. .setComponentKeys(singletonList(application.getKey())))
  392. .viewUuids()).containsExactly(application.uuid());
  393. }
  394. @Test
  395. public void search_by_application_key_and_branch() {
  396. ComponentDto application = db.components().insertMainBranch(c -> c.setQualifier(APP).setDbKey("app"));
  397. ComponentDto applicationBranch1 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch1"));
  398. ComponentDto applicationBranch2 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch2"));
  399. ComponentDto project1 = db.components().insertPrivateProject(p -> p.setDbKey("prj1"));
  400. ComponentDto project1Branch1 = db.components().insertProjectBranch(project1);
  401. ComponentDto fileOnProject1Branch1 = db.components().insertComponent(newFileDto(project1Branch1));
  402. ComponentDto project1Branch2 = db.components().insertProjectBranch(project1);
  403. ComponentDto project2 = db.components().insertPrivateProject(p -> p.setDbKey("prj2"));
  404. db.components().insertComponents(newProjectCopy(project1Branch1, applicationBranch1));
  405. db.components().insertComponents(newProjectCopy(project2, applicationBranch1));
  406. db.components().insertComponents(newProjectCopy(project1Branch2, applicationBranch2));
  407. // Search on applicationBranch1
  408. assertThat(underTest.create(new SearchRequest()
  409. .setComponentKeys(singletonList(applicationBranch1.getKey()))
  410. .setBranch(applicationBranch1.getBranch())))
  411. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
  412. .containsOnly(applicationBranch1.uuid(), Collections.emptyList(), false);
  413. // Search on project1Branch1
  414. assertThat(underTest.create(new SearchRequest()
  415. .setComponentKeys(singletonList(applicationBranch1.getKey()))
  416. .setProjectKeys(singletonList(project1.getKey()))
  417. .setBranch(applicationBranch1.getBranch())))
  418. .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
  419. .containsOnly(applicationBranch1.uuid(), singletonList(project1.uuid()), false);
  420. }
  421. @Test
  422. public void fail_if_created_after_and_created_since_are_both_set() {
  423. SearchRequest request = new SearchRequest()
  424. .setCreatedAfter("2013-07-25T07:35:00+0100")
  425. .setCreatedInLast("palap");
  426. try {
  427. underTest.create(request);
  428. fail();
  429. } catch (Exception e) {
  430. assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Parameters createdAfter and createdInLast cannot be set simultaneously");
  431. }
  432. }
  433. @Test
  434. public void set_created_after_from_created_since() {
  435. Date now = DateUtils.parseDateTime("2013-07-25T07:35:00+0100");
  436. when(clock.instant()).thenReturn(now.toInstant());
  437. when(clock.getZone()).thenReturn(ZoneOffset.UTC);
  438. SearchRequest request = new SearchRequest()
  439. .setCreatedInLast("1y2m3w4d");
  440. assertThat(underTest.create(request).createdAfter().date()).isEqualTo(DateUtils.parseDateTime("2012-04-30T07:35:00+0100"));
  441. assertThat(underTest.create(request).createdAfter().inclusive()).isTrue();
  442. }
  443. @Test
  444. public void fail_if_since_leak_period_and_created_after_set_at_the_same_time() {
  445. expectedException.expect(IllegalArgumentException.class);
  446. expectedException.expectMessage("Parameters 'createdAfter' and 'sinceLeakPeriod' cannot be set simultaneously");
  447. underTest.create(new SearchRequest()
  448. .setSinceLeakPeriod(true)
  449. .setCreatedAfter("2013-07-25T07:35:00+0100"));
  450. }
  451. @Test
  452. public void fail_if_no_component_provided_with_since_leak_period() {
  453. expectedException.expect(IllegalArgumentException.class);
  454. expectedException.expectMessage("One and only one component must be provided when searching since leak period");
  455. underTest.create(new SearchRequest().setSinceLeakPeriod(true));
  456. }
  457. @Test
  458. public void fail_if_several_components_provided_with_since_leak_period() {
  459. ComponentDto project1 = db.components().insertPrivateProject();
  460. ComponentDto project2 = db.components().insertPrivateProject();
  461. expectedException.expect(IllegalArgumentException.class);
  462. expectedException.expectMessage("One and only one component must be provided when searching since leak period");
  463. underTest.create(new SearchRequest()
  464. .setSinceLeakPeriod(true)
  465. .setComponentKeys(asList(project1.getKey(), project2.getKey())));
  466. }
  467. @Test
  468. public void fail_if_date_is_not_formatted_correctly() {
  469. expectedException.expect(IllegalArgumentException.class);
  470. expectedException.expectMessage("'unknown-date' cannot be parsed as either a date or date+time");
  471. underTest.create(new SearchRequest()
  472. .setCreatedAfter("unknown-date"));
  473. }
  474. @Test
  475. public void return_empty_results_if_organization_with_specified_key_does_not_exist() {
  476. SearchRequest request = new SearchRequest()
  477. .setOrganization("does_not_exist");
  478. IssueQuery query = underTest.create(request);
  479. assertThat(query.organizationUuid()).isEqualTo("<UNKNOWN>");
  480. }
  481. }