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.

SearchActionComponentsTest.java 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.ws;
  21. import java.time.Clock;
  22. import java.util.Arrays;
  23. import java.util.Date;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.resources.Languages;
  28. import org.sonar.api.resources.Qualifiers;
  29. import org.sonar.api.rule.RuleKey;
  30. import org.sonar.api.utils.Durations;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbTester;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.issue.IssueDto;
  36. import org.sonar.db.organization.OrganizationDto;
  37. import org.sonar.db.rule.RuleDefinitionDto;
  38. import org.sonar.server.es.EsTester;
  39. import org.sonar.server.issue.AvatarResolverImpl;
  40. import org.sonar.server.issue.IssueFieldsSetter;
  41. import org.sonar.server.issue.TextRangeResponseFormatter;
  42. import org.sonar.server.issue.TransitionService;
  43. import org.sonar.server.issue.index.IssueIndex;
  44. import org.sonar.server.issue.index.IssueIndexer;
  45. import org.sonar.server.issue.index.IssueIteratorFactory;
  46. import org.sonar.server.issue.index.IssueQueryFactory;
  47. import org.sonar.server.issue.workflow.FunctionExecutor;
  48. import org.sonar.server.issue.workflow.IssueWorkflow;
  49. import org.sonar.server.permission.index.PermissionIndexerTester;
  50. import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
  51. import org.sonar.server.tester.UserSessionRule;
  52. import org.sonar.server.view.index.ViewIndexer;
  53. import org.sonar.server.ws.WsActionTester;
  54. import org.sonarqube.ws.Issues;
  55. import org.sonarqube.ws.Issues.Component;
  56. import org.sonarqube.ws.Issues.Issue;
  57. import org.sonarqube.ws.Issues.SearchWsResponse;
  58. import static org.assertj.core.api.Assertions.assertThat;
  59. import static org.assertj.core.api.Assertions.tuple;
  60. import static org.sonar.api.resources.Qualifiers.APP;
  61. import static org.sonar.api.utils.DateUtils.addDays;
  62. import static org.sonar.api.utils.DateUtils.parseDateTime;
  63. import static org.sonar.api.web.UserRole.USER;
  64. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  65. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
  66. import static org.sonar.db.component.BranchType.BRANCH;
  67. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  68. import static org.sonar.db.component.ComponentTesting.newDirectory;
  69. import static org.sonar.db.component.ComponentTesting.newFileDto;
  70. import static org.sonar.db.component.ComponentTesting.newModuleDto;
  71. import static org.sonar.db.component.ComponentTesting.newProjectCopy;
  72. import static org.sonar.db.component.ComponentTesting.newSubView;
  73. import static org.sonar.db.component.ComponentTesting.newView;
  74. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BRANCH;
  75. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_KEYS;
  76. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_UUIDS;
  77. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_DIRECTORIES;
  78. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_FILE_UUIDS;
  79. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_MODULE_UUIDS;
  80. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PROJECTS;
  81. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PROJECT_KEYS;
  82. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PULL_REQUEST;
  83. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SINCE_LEAK_PERIOD;
  84. public class SearchActionComponentsTest {
  85. @Rule
  86. public ExpectedException expectedException = ExpectedException.none();
  87. @Rule
  88. public UserSessionRule userSession = UserSessionRule.standalone();
  89. @Rule
  90. public DbTester db = DbTester.create();
  91. @Rule
  92. public EsTester es = EsTester.create();
  93. private DbClient dbClient = db.getDbClient();
  94. private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSession, new WebAuthorizationTypeSupport(userSession));
  95. private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
  96. private ViewIndexer viewIndexer = new ViewIndexer(dbClient, es.client());
  97. private IssueQueryFactory issueQueryFactory = new IssueQueryFactory(dbClient, Clock.systemUTC(), userSession);
  98. private IssueFieldsSetter issueFieldsSetter = new IssueFieldsSetter();
  99. private IssueWorkflow issueWorkflow = new IssueWorkflow(new FunctionExecutor(issueFieldsSetter), issueFieldsSetter);
  100. private SearchResponseLoader searchResponseLoader = new SearchResponseLoader(userSession, dbClient, new TransitionService(userSession, issueWorkflow));
  101. private Languages languages = new Languages();
  102. private UserResponseFormatter userFormatter = new UserResponseFormatter(new AvatarResolverImpl());
  103. private SearchResponseFormat searchResponseFormat = new SearchResponseFormat(new Durations(), languages, new TextRangeResponseFormatter(), userFormatter);
  104. private PermissionIndexerTester permissionIndexer = new PermissionIndexerTester(es, issueIndexer);
  105. private WsActionTester ws = new WsActionTester(new SearchAction(userSession, issueIndex, issueQueryFactory, searchResponseLoader, searchResponseFormat,
  106. System2.INSTANCE, dbClient));
  107. @Test
  108. public void search_all_issues_when_no_parameter() {
  109. RuleDefinitionDto rule = db.rules().insertIssueRule();
  110. ComponentDto project = db.components().insertPublicProject();
  111. ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
  112. IssueDto issue = db.issues().insertIssue(rule, project, projectFile);
  113. allowAnyoneOnProjects(project);
  114. indexIssues();
  115. SearchWsResponse result = ws.newRequest().executeProtobuf(SearchWsResponse.class);
  116. assertThat(result.getIssuesList()).extracting(Issues.Issue::getKey)
  117. .containsExactlyInAnyOrder(issue.getKey());
  118. }
  119. @Test
  120. public void issues_on_different_projects() {
  121. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  122. OrganizationDto organization1 = db.organizations().insert();
  123. ComponentDto project = db.components().insertPublicProject(organization1);
  124. ComponentDto file = db.components().insertComponent(newFileDto(project));
  125. IssueDto issue1 = db.issues().insertIssue(rule, project, file);
  126. OrganizationDto organization2 = db.organizations().insert();
  127. ComponentDto project2 = db.components().insertPublicProject(organization2);
  128. ComponentDto file2 = db.components().insertComponent(newFileDto(project2));
  129. IssueDto issue2 = db.issues().insertIssue(rule, project2, file2);
  130. allowAnyoneOnProjects(project, project2);
  131. indexIssues();
  132. SearchWsResponse response = ws.newRequest().executeProtobuf(SearchWsResponse.class);
  133. assertThat(response.getIssuesList())
  134. .extracting(Issue::getKey, Issue::getComponent, Issue::getProject)
  135. .containsExactlyInAnyOrder(
  136. tuple(issue1.getKey(), file.getKey(), project.getKey()),
  137. tuple(issue2.getKey(), file2.getKey(), project2.getKey()));
  138. assertThat(response.getComponentsList())
  139. .extracting(Component::getKey, Component::getEnabled)
  140. .containsExactlyInAnyOrder(tuple(project.getKey(), true), tuple(file.getKey(), true), tuple(project2.getKey(), true), tuple(file2.getKey(), true));
  141. }
  142. @Test
  143. public void search_by_module() {
  144. ComponentDto project = db.components().insertPublicProject();
  145. ComponentDto module1 = db.components().insertComponent(newModuleDto(project));
  146. ComponentDto file1 = db.components().insertComponent(newFileDto(module1));
  147. ComponentDto module2 = db.components().insertComponent(newModuleDto(project));
  148. ComponentDto file2 = db.components().insertComponent(newFileDto(module2));
  149. RuleDefinitionDto rule = db.rules().insertIssueRule();
  150. IssueDto issue1 = db.issues().insertIssue(rule, project, file1);
  151. IssueDto issue2 = db.issues().insertIssue(rule, project, file2);
  152. allowAnyoneOnProjects(project);
  153. indexIssues();
  154. assertThat(ws.newRequest()
  155. .setParam(PARAM_COMPONENT_KEYS, module1.getKey())
  156. .executeProtobuf(SearchWsResponse.class).getIssuesList()).extracting(Issue::getKey)
  157. .containsExactlyInAnyOrder(issue1.getKey());
  158. assertThat(ws.newRequest()
  159. .setParam(PARAM_MODULE_UUIDS, module1.uuid())
  160. .executeProtobuf(SearchWsResponse.class).getIssuesList()).extracting(Issue::getKey)
  161. .containsExactlyInAnyOrder(issue1.getKey());
  162. }
  163. @Test
  164. public void do_not_return_module_key_on_single_module_projects() {
  165. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  166. ComponentDto module = db.components().insertComponent(newModuleDto("M1", project).setDbKey("MK1"));
  167. ComponentDto file = db.components().insertComponent(newFileDto(module, null, "F1").setDbKey("FK1"));
  168. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  169. db.issues().insertIssue(rule, project, file, i -> i.setKee("ISSUE_IN_MODULE"));
  170. db.issues().insertIssue(rule, project, project, i -> i.setKee("ISSUE_IN_ROOT_MODULE"));
  171. allowAnyoneOnProjects(project);
  172. indexIssues();
  173. SearchWsResponse searchResponse = ws.newRequest().executeProtobuf(SearchWsResponse.class);
  174. assertThat(searchResponse.getIssuesCount()).isEqualTo(2);
  175. for (Issue issue : searchResponse.getIssuesList()) {
  176. assertThat(issue.getProject()).isEqualTo("PK1");
  177. if (issue.getKey().equals("ISSUE_IN_MODULE")) {
  178. assertThat(issue.getSubProject()).isEqualTo("MK1");
  179. } else if (issue.getKey().equals("ISSUE_IN_ROOT_MODULE")) {
  180. assertThat(issue.hasSubProject()).isFalse();
  181. }
  182. }
  183. }
  184. @Test
  185. public void search_since_leak_period_on_project() {
  186. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  187. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  188. db.components().insertSnapshot(project, a -> a.setPeriodDate(parseDateTime("2015-09-03T00:00:00+0100").getTime()));
  189. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  190. IssueDto issueAfterLeak = db.issues().insertIssue(rule, project, file, i -> i.setKee(UUID_EXAMPLE_01)
  191. .setIssueCreationDate(parseDateTime("2015-09-04T00:00:00+0100"))
  192. .setIssueUpdateDate(parseDateTime("2015-10-04T00:00:00+0100")));
  193. IssueDto issueBeforeLeak = db.issues().insertIssue(rule, project, file, i -> i.setKee(UUID_EXAMPLE_02)
  194. .setIssueCreationDate(parseDateTime("2014-09-04T00:00:00+0100"))
  195. .setIssueUpdateDate(parseDateTime("2015-10-04T00:00:00+0100")));
  196. allowAnyoneOnProjects(project);
  197. indexIssues();
  198. ws.newRequest()
  199. .setParam(PARAM_COMPONENT_UUIDS, project.uuid())
  200. .setParam(PARAM_SINCE_LEAK_PERIOD, "true")
  201. .execute()
  202. .assertJson(this.getClass(), "search_since_leak_period.json");
  203. }
  204. @Test
  205. public void search_since_leak_period_on_file_in_module_project() {
  206. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  207. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  208. ComponentDto file = db.components().insertComponent(newFileDto(module, null, "F1").setDbKey("FK1"));
  209. db.components().insertSnapshot(project, a -> a.setPeriodDate(parseDateTime("2015-09-03T00:00:00+0100").getTime()));
  210. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  211. IssueDto issueAfterLeak = db.issues().insertIssue(rule, project, file, i -> i.setKee(UUID_EXAMPLE_01)
  212. .setIssueCreationDate(parseDateTime("2015-09-04T00:00:00+0100"))
  213. .setIssueUpdateDate(parseDateTime("2015-10-04T00:00:00+0100")));
  214. IssueDto issueBeforeLeak = db.issues().insertIssue(rule, project, file, i -> i.setKee(UUID_EXAMPLE_02)
  215. .setIssueCreationDate(parseDateTime("2014-09-04T00:00:00+0100"))
  216. .setIssueUpdateDate(parseDateTime("2015-10-04T00:00:00+0100")));
  217. allowAnyoneOnProjects(project);
  218. indexIssues();
  219. ws.newRequest()
  220. .setParam(PARAM_COMPONENT_UUIDS, project.uuid())
  221. .setParam(PARAM_FILE_UUIDS, file.uuid())
  222. .setParam(PARAM_SINCE_LEAK_PERIOD, "true")
  223. .execute()
  224. .assertJson(this.getClass(), "search_since_leak_period.json");
  225. }
  226. @Test
  227. public void search_by_file_uuid() {
  228. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  229. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  230. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  231. IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  232. allowAnyoneOnProjects(project);
  233. indexIssues();
  234. ws.newRequest()
  235. .setParam(PARAM_FILE_UUIDS, file.uuid())
  236. .execute()
  237. .assertJson(this.getClass(), "search_by_file_uuid.json");
  238. ws.newRequest()
  239. .setParam(PARAM_FILE_UUIDS, "unknown")
  240. .execute()
  241. .assertJson(this.getClass(), "no_issue.json");
  242. ws.newRequest()
  243. .setParam(PARAM_COMPONENT_UUIDS, file.uuid())
  244. .execute()
  245. .assertJson(this.getClass(), "search_by_file_uuid.json");
  246. ws.newRequest()
  247. .setParam(PARAM_COMPONENT_UUIDS, "unknown")
  248. .execute()
  249. .assertJson(this.getClass(), "no_issue.json");
  250. }
  251. @Test
  252. public void search_by_file_key() {
  253. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  254. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  255. ComponentDto unitTest = db.components().insertComponent(newFileDto(project, null, "F2").setQualifier(Qualifiers.UNIT_TEST_FILE).setDbKey("FK2"));
  256. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  257. IssueDto issueOnFile = db.issues().insertIssue(rule, project, file, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  258. IssueDto issueOnTest = db.issues().insertIssue(rule, project, unitTest, i -> i.setKee("2bd4eac2-b650-4037-80bc-7b1182fd47d4"));
  259. allowAnyoneOnProjects(project);
  260. indexIssues();
  261. ws.newRequest()
  262. .setParam(PARAM_COMPONENT_KEYS, file.getKey())
  263. .execute()
  264. .assertJson(this.getClass(), "search_by_file_key.json");
  265. ws.newRequest()
  266. .setParam(PARAM_COMPONENT_KEYS, unitTest.getKey())
  267. .execute()
  268. .assertJson(this.getClass(), "search_by_test_key.json");
  269. }
  270. @Test
  271. public void search_by_directory_path() {
  272. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  273. ComponentDto directory = db.components().insertComponent(newDirectory(project, "D1", "src/main/java/dir"));
  274. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1").setPath(directory.path() + "/MyComponent.java"));
  275. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  276. db.issues().insertIssue(rule, project, file, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  277. allowAnyoneOnProjects(project);
  278. indexIssues();
  279. ws.newRequest()
  280. .setParam(PARAM_COMPONENT_KEYS, directory.getKey())
  281. .execute()
  282. .assertJson(this.getClass(), "search_by_file_uuid.json");
  283. ws.newRequest()
  284. .setParam(PARAM_DIRECTORIES, "unknown")
  285. .execute()
  286. .assertJson(this.getClass(), "no_issue.json");
  287. ws.newRequest()
  288. .setParam(PARAM_DIRECTORIES, "src/main/java/dir")
  289. .execute()
  290. .assertJson(this.getClass(), "search_by_file_uuid.json");
  291. ws.newRequest()
  292. .setParam(PARAM_DIRECTORIES, "src/main/java")
  293. .execute()
  294. .assertJson(this.getClass(), "no_issue.json");
  295. }
  296. @Test
  297. public void search_by_directory_path_in_different_modules() {
  298. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  299. ComponentDto module1 = db.components().insertComponent(newModuleDto("M1", project).setDbKey("MK1"));
  300. ComponentDto module2 = db.components().insertComponent(newModuleDto("M2", project).setDbKey("MK2"));
  301. ComponentDto directory1 = db.components().insertComponent(newDirectory(module1, "D1", "src/main/java/dir"));
  302. ComponentDto directory2 = db.components().insertComponent(newDirectory(module2, "D2", "src/main/java/dir"));
  303. ComponentDto file1 = db.components().insertComponent(newFileDto(module1, directory1, "F1").setDbKey("FK1").setPath(directory1.path() + "/MyComponent.java"));
  304. db.components().insertComponent(newFileDto(module2, directory2, "F2").setDbKey("FK2").setPath(directory2.path() + "/MyComponent.java"));
  305. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  306. db.issues().insertIssue(rule, project, file1, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  307. allowAnyoneOnProjects(project);
  308. indexIssues();
  309. ws.newRequest()
  310. .setParam(PARAM_COMPONENT_KEYS, directory1.getKey())
  311. .execute()
  312. .assertJson(this.getClass(), "search_by_directory_uuid.json");
  313. ws.newRequest()
  314. .setParam(PARAM_COMPONENT_KEYS, directory2.getKey())
  315. .execute()
  316. .assertJson(this.getClass(), "no_issue.json");
  317. ws.newRequest()
  318. .setParam(PARAM_MODULE_UUIDS, module1.uuid())
  319. .setParam(PARAM_DIRECTORIES, "src/main/java/dir")
  320. .execute()
  321. .assertJson(this.getClass(), "search_by_directory_uuid.json");
  322. ws.newRequest()
  323. .setParam(PARAM_MODULE_UUIDS, module2.uuid())
  324. .setParam(PARAM_DIRECTORIES, "src/main/java/dir")
  325. .execute()
  326. .assertJson(this.getClass(), "no_issue.json");
  327. ws.newRequest()
  328. .setParam(PARAM_DIRECTORIES, "src/main/java/dir")
  329. .execute()
  330. .assertJson(this.getClass(), "search_by_directory_uuid.json");
  331. ws.newRequest()
  332. .setParam(PARAM_DIRECTORIES, "src/main/java")
  333. .execute()
  334. .assertJson(this.getClass(), "no_issue.json");
  335. }
  336. @Test
  337. public void search_by_view_uuid() {
  338. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  339. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  340. ComponentDto view = db.components().insertComponent(newView(db.getDefaultOrganization(), "V1").setDbKey("MyView"));
  341. db.components().insertComponent(newProjectCopy(project, view));
  342. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  343. db.issues().insertIssue(rule, project, file, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  344. allowAnyoneOnProjects(project, view);
  345. indexIssuesAndViews();
  346. ws.newRequest()
  347. .setParam(PARAM_COMPONENT_KEYS, view.getKey())
  348. .execute()
  349. .assertJson(this.getClass(), "search_by_view_uuid.json");
  350. }
  351. @Test
  352. public void search_by_sub_view_uuid() {
  353. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  354. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  355. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  356. db.issues().insertIssue(rule, project, file, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  357. ComponentDto view = db.components().insertComponent(newView(db.getDefaultOrganization(), "V1").setDbKey("MyView"));
  358. ComponentDto subView = db.components().insertComponent(newSubView(view, "SV1", "MySubView"));
  359. db.components().insertComponent(newProjectCopy(project, subView));
  360. allowAnyoneOnProjects(project, view, subView);
  361. indexIssuesAndViews();
  362. ws.newRequest()
  363. .setParam(PARAM_COMPONENT_KEYS, subView.getKey())
  364. .execute()
  365. .assertJson(this.getClass(), "search_by_view_uuid.json");
  366. }
  367. @Test
  368. public void search_by_sub_view_uuid_return_only_authorized_view() {
  369. ComponentDto project = db.components().insertPublicProject(p -> p.setDbKey("PK1"));
  370. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  371. RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  372. db.issues().insertIssue(rule, project, file, i -> i.setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2"));
  373. ComponentDto view = db.components().insertComponent(newView(db.getDefaultOrganization(), "V1").setDbKey("MyView"));
  374. ComponentDto subView = db.components().insertComponent(newSubView(view, "SV1", "MySubView"));
  375. db.components().insertComponent(newProjectCopy(project, subView));
  376. // User has no permission on the view, no issue will be returned
  377. allowAnyoneOnProjects(project);
  378. indexIssuesAndViews();
  379. ws.newRequest()
  380. .setParam(PARAM_COMPONENT_KEYS, subView.getKey())
  381. .execute()
  382. .assertJson(this.getClass(), "no_issue.json");
  383. }
  384. @Test
  385. public void search_by_application_key() {
  386. ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization());
  387. ComponentDto project1 = db.components().insertPrivateProject();
  388. ComponentDto project2 = db.components().insertPrivateProject();
  389. db.components().insertComponents(newProjectCopy(project1, application));
  390. db.components().insertComponents(newProjectCopy(project2, application));
  391. RuleDefinitionDto rule = db.rules().insertIssueRule();
  392. IssueDto issue1 = db.issues().insertIssue(rule, project1, project1);
  393. IssueDto issue2 = db.issues().insertIssue(rule, project2, project2);
  394. allowAnyoneOnProjects(project1, project2, application);
  395. userSession.addProjectPermission(USER, application);
  396. indexIssuesAndViews();
  397. SearchWsResponse result = ws.newRequest()
  398. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  399. .executeProtobuf(SearchWsResponse.class);
  400. assertThat(result.getIssuesList()).extracting(Issue::getKey)
  401. .containsExactlyInAnyOrder(issue1.getKey(), issue2.getKey());
  402. }
  403. @Test
  404. public void search_by_application_key_and_branch() {
  405. ComponentDto application = db.components().insertMainBranch(c -> c.setQualifier(APP).setDbKey("app"));
  406. ComponentDto applicationBranch1 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch1"));
  407. ComponentDto applicationBranch2 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch2"));
  408. ComponentDto project1 = db.components().insertPrivateProject(p -> p.setDbKey("prj1"));
  409. ComponentDto project1Branch1 = db.components().insertProjectBranch(project1);
  410. ComponentDto fileOnProject1Branch1 = db.components().insertComponent(newFileDto(project1Branch1));
  411. ComponentDto project1Branch2 = db.components().insertProjectBranch(project1);
  412. ComponentDto project2 = db.components().insertPrivateProject(p -> p.setDbKey("prj2"));
  413. db.components().insertComponents(newProjectCopy(project1Branch1, applicationBranch1));
  414. db.components().insertComponents(newProjectCopy(project2, applicationBranch1));
  415. db.components().insertComponents(newProjectCopy(project1Branch2, applicationBranch2));
  416. RuleDefinitionDto rule = db.rules().insertIssueRule();
  417. IssueDto issueOnProject1 = db.issues().insertIssue(rule, project1, project1);
  418. IssueDto issueOnProject1Branch1 = db.issues().insertIssue(rule, project1Branch1, project1Branch1);
  419. db.issues().insertHotspot(rule, project1Branch1, project1Branch1);
  420. IssueDto issueOnFileOnProject1Branch1 = db.issues().insertIssue(rule, project1Branch1, fileOnProject1Branch1);
  421. IssueDto issueOnProject1Branch2 = db.issues().insertIssue(rule, project1Branch2, project1Branch2);
  422. IssueDto issueOnProject2 = db.issues().insertIssue(rule, project2, project2);
  423. db.issues().insertHotspot(rule, project2, project2);
  424. allowAnyoneOnProjects(project1, project2, application);
  425. userSession.addProjectPermission(USER, application);
  426. indexIssuesAndViews();
  427. // All issues on applicationBranch1
  428. assertThat(ws.newRequest()
  429. .setParam(PARAM_COMPONENT_KEYS, applicationBranch1.getKey())
  430. .setParam(PARAM_BRANCH, applicationBranch1.getBranch())
  431. .executeProtobuf(SearchWsResponse.class).getIssuesList())
  432. .extracting(Issue::getKey, Issue::getComponent, Issue::getProject, Issue::getBranch, Issue::hasBranch)
  433. .containsExactlyInAnyOrder(
  434. tuple(issueOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getBranch(), true),
  435. tuple(issueOnFileOnProject1Branch1.getKey(), fileOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getBranch(), true),
  436. tuple(issueOnProject2.getKey(), project2.getKey(), project2.getKey(), "", false));
  437. // Issues on project1Branch1
  438. assertThat(ws.newRequest()
  439. .setParam(PARAM_COMPONENT_KEYS, applicationBranch1.getKey())
  440. .setParam(PARAM_PROJECTS, project1.getKey())
  441. .setParam(PARAM_BRANCH, applicationBranch1.getBranch())
  442. .executeProtobuf(SearchWsResponse.class).getIssuesList())
  443. .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch)
  444. .containsExactlyInAnyOrder(
  445. tuple(issueOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getBranch()),
  446. tuple(issueOnFileOnProject1Branch1.getKey(), fileOnProject1Branch1.getKey(), project1Branch1.getBranch()));
  447. }
  448. @Test
  449. public void ignore_application_without_browse_permission() {
  450. ComponentDto project = db.components().insertPublicProject();
  451. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  452. db.components().insertComponents(newProjectCopy("PC1", project, application));
  453. RuleDefinitionDto rule = db.rules().insertIssueRule();
  454. db.issues().insertIssue(rule, project, project);
  455. allowAnyoneOnProjects(project);
  456. indexIssuesAndViews();
  457. SearchWsResponse result = ws.newRequest()
  458. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  459. .executeProtobuf(SearchWsResponse.class);
  460. assertThat(result.getIssuesList()).isEmpty();
  461. }
  462. @Test
  463. public void search_application_without_projects() {
  464. ComponentDto project = db.components().insertPublicProject();
  465. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  466. RuleDefinitionDto rule = db.rules().insertIssueRule();
  467. db.issues().insertIssue(rule, project, project);
  468. allowAnyoneOnProjects(project, application);
  469. indexIssuesAndViews();
  470. SearchWsResponse result = ws.newRequest()
  471. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  472. .executeProtobuf(SearchWsResponse.class);
  473. assertThat(result.getIssuesList()).isEmpty();
  474. }
  475. @Test
  476. public void search_by_application_and_by_leak() {
  477. Date now = new Date();
  478. RuleDefinitionDto rule = db.rules().insertIssueRule();
  479. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  480. // Project 1
  481. ComponentDto project1 = db.components().insertPublicProject();
  482. db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
  483. db.components().insertComponents(newProjectCopy("PC1", project1, application));
  484. IssueDto project1Issue1 = db.issues().insertIssue(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -10)));
  485. IssueDto project1Issue2 = db.issues().insertIssue(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -20)));
  486. // Project 2
  487. ComponentDto project2 = db.components().insertPublicProject();
  488. db.components().insertSnapshot(project2, s -> s.setPeriodDate(addDays(now, -25).getTime()));
  489. db.components().insertComponents(newProjectCopy("PC2", project2, application));
  490. IssueDto project2Issue1 = db.issues().insertIssue(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -15)));
  491. IssueDto project2Issue2 = db.issues().insertIssue(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -30)));
  492. // Permissions and index
  493. allowAnyoneOnProjects(project1, project2, application);
  494. indexIssuesAndViews();
  495. SearchWsResponse result = ws.newRequest()
  496. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  497. .setParam(PARAM_SINCE_LEAK_PERIOD, "true")
  498. .executeProtobuf(SearchWsResponse.class);
  499. assertThat(result.getIssuesList()).extracting(Issue::getKey)
  500. .containsExactlyInAnyOrder(project1Issue1.getKey(), project2Issue1.getKey())
  501. .doesNotContain(project1Issue2.getKey(), project2Issue2.getKey());
  502. }
  503. @Test
  504. public void search_by_application_and_project() {
  505. ComponentDto project1 = db.components().insertPublicProject();
  506. ComponentDto project2 = db.components().insertPublicProject();
  507. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  508. db.components().insertComponents(newProjectCopy("PC1", project1, application));
  509. db.components().insertComponents(newProjectCopy("PC2", project2, application));
  510. RuleDefinitionDto rule = db.rules().insertIssueRule();
  511. IssueDto issue1 = db.issues().insertIssue(rule, project1, project1);
  512. IssueDto issue2 = db.issues().insertIssue(rule, project2, project2);
  513. allowAnyoneOnProjects(project1, project2, application);
  514. indexIssuesAndViews();
  515. SearchWsResponse result = ws.newRequest()
  516. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  517. .setParam(PARAM_PROJECT_KEYS, project1.getDbKey())
  518. .executeProtobuf(SearchWsResponse.class);
  519. assertThat(result.getIssuesList()).extracting(Issue::getKey)
  520. .containsExactlyInAnyOrder(issue1.getKey())
  521. .doesNotContain(issue2.getKey());
  522. }
  523. @Test
  524. public void search_by_application_and_project_and_leak() {
  525. Date now = new Date();
  526. RuleDefinitionDto rule = db.rules().insertIssueRule();
  527. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  528. // Project 1
  529. ComponentDto project1 = db.components().insertPublicProject();
  530. db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
  531. db.components().insertComponents(newProjectCopy("PC1", project1, application));
  532. IssueDto project1Issue1 = db.issues().insertIssue(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -10)));
  533. IssueDto project1Issue2 = db.issues().insertIssue(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -20)));
  534. // Project 2
  535. ComponentDto project2 = db.components().insertPublicProject();
  536. db.components().insertSnapshot(project2, s -> s.setPeriodDate(addDays(now, -25).getTime()));
  537. db.components().insertComponents(newProjectCopy("PC2", project2, application));
  538. IssueDto project2Issue1 = db.issues().insertIssue(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -15)));
  539. IssueDto project2Issue2 = db.issues().insertIssue(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -30)));
  540. // Permissions and index
  541. allowAnyoneOnProjects(project1, project2, application);
  542. indexIssuesAndViews();
  543. SearchWsResponse result = ws.newRequest()
  544. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  545. .setParam(PARAM_PROJECT_KEYS, project1.getDbKey())
  546. .setParam(PARAM_SINCE_LEAK_PERIOD, "true")
  547. .executeProtobuf(SearchWsResponse.class);
  548. assertThat(result.getIssuesList()).extracting(Issue::getKey)
  549. .containsExactlyInAnyOrder(project1Issue1.getKey())
  550. .doesNotContain(project1Issue2.getKey(), project2Issue1.getKey(), project2Issue2.getKey());
  551. }
  552. @Test
  553. public void search_by_application_and_by_leak_when_one_project_has_no_leak() {
  554. Date now = new Date();
  555. RuleDefinitionDto rule = db.rules().insertIssueRule();
  556. ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
  557. // Project 1
  558. ComponentDto project1 = db.components().insertPublicProject();
  559. db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
  560. db.components().insertComponents(newProjectCopy("PC1", project1, application));
  561. IssueDto project1Issue1 = db.issues().insertIssue(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -10)));
  562. IssueDto project1Issue2 = db.issues().insertIssue(rule, project1, project1, i -> i.setIssueCreationDate(addDays(now, -20)));
  563. // Project 2, without leak => no issue form it should be returned
  564. ComponentDto project2 = db.components().insertPublicProject();
  565. db.components().insertSnapshot(project2, s -> s.setPeriodDate(null));
  566. db.components().insertComponents(newProjectCopy("PC2", project2, application));
  567. IssueDto project2Issue1 = db.issues().insertIssue(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -15)));
  568. IssueDto project2Issue2 = db.issues().insertIssue(rule, project2, project2, i -> i.setIssueCreationDate(addDays(now, -30)));
  569. // Permissions and index
  570. allowAnyoneOnProjects(project1, project2, application);
  571. indexIssuesAndViews();
  572. SearchWsResponse result = ws.newRequest()
  573. .setParam(PARAM_COMPONENT_KEYS, application.getDbKey())
  574. .setParam(PARAM_SINCE_LEAK_PERIOD, "true")
  575. .executeProtobuf(SearchWsResponse.class);
  576. assertThat(result.getIssuesList()).extracting(Issue::getKey)
  577. .containsExactlyInAnyOrder(project1Issue1.getKey())
  578. .doesNotContain(project1Issue2.getKey(), project2Issue1.getKey(), project2Issue2.getKey());
  579. }
  580. @Test
  581. public void search_by_branch() {
  582. RuleDefinitionDto rule = db.rules().insertIssueRule();
  583. ComponentDto project = db.components().insertMainBranch();
  584. ComponentDto file = db.components().insertComponent(newFileDto(project));
  585. IssueDto issue = db.issues().insertIssue(rule, project, file);
  586. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
  587. ComponentDto branchFile = db.components().insertComponent(newFileDto(branch));
  588. IssueDto branchIssue = db.issues().insertIssue(rule, branch, branchFile);
  589. allowAnyoneOnProjects(project);
  590. indexIssuesAndViews();
  591. // On component key + branch
  592. assertThat(ws.newRequest()
  593. .setParam(PARAM_COMPONENT_KEYS, project.getKey())
  594. .setParam(PARAM_BRANCH, branch.getBranch())
  595. .executeProtobuf(SearchWsResponse.class).getIssuesList())
  596. .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch)
  597. .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch()));
  598. // On project key + branch
  599. assertThat(ws.newRequest()
  600. .setParam(PARAM_PROJECT_KEYS, project.getKey())
  601. .setParam(PARAM_BRANCH, branch.getBranch())
  602. .executeProtobuf(SearchWsResponse.class).getIssuesList())
  603. .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch)
  604. .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch()));
  605. // On file key + branch
  606. assertThat(ws.newRequest()
  607. .setParam(PARAM_COMPONENT_KEYS, branchFile.getKey())
  608. .setParam(PARAM_BRANCH, branch.getBranch())
  609. .executeProtobuf(SearchWsResponse.class).getIssuesList())
  610. .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch)
  611. .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch()));
  612. }
  613. @Test
  614. public void return_branch_in_component_list() {
  615. RuleDefinitionDto rule = db.rules().insertIssueRule();
  616. ComponentDto project = db.components().insertPrivateProject();
  617. ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
  618. IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile);
  619. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
  620. ComponentDto branchFile = db.components().insertComponent(newFileDto(branch));
  621. IssueDto branchIssue = db.issues().insertIssue(rule, branch, branchFile);
  622. allowAnyoneOnProjects(project);
  623. indexIssuesAndViews();
  624. SearchWsResponse result = ws.newRequest()
  625. .setParam(PARAM_COMPONENT_KEYS, branch.getKey())
  626. .setParam(PARAM_BRANCH, branch.getBranch())
  627. .executeProtobuf(SearchWsResponse.class);
  628. assertThat(result.getComponentsList())
  629. .extracting(Issues.Component::getKey, Issues.Component::getBranch)
  630. .containsExactlyInAnyOrder(
  631. tuple(branchFile.getKey(), branchFile.getBranch()),
  632. tuple(branch.getKey(), branch.getBranch()));
  633. }
  634. @Test
  635. public void search_by_pull_request() {
  636. RuleDefinitionDto rule = db.rules().insertIssueRule();
  637. ComponentDto project = db.components().insertPrivateProject();
  638. ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
  639. IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile);
  640. ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST));
  641. ComponentDto pullRequestFile = db.components().insertComponent(newFileDto(pullRequest));
  642. IssueDto pullRequestIssue = db.issues().insertIssue(rule, pullRequest, pullRequestFile);
  643. allowAnyoneOnProjects(project);
  644. indexIssuesAndViews();
  645. SearchWsResponse result = ws.newRequest()
  646. .setParam(PARAM_COMPONENT_KEYS, pullRequest.getKey())
  647. .setParam(PARAM_PULL_REQUEST, pullRequest.getPullRequest())
  648. .executeProtobuf(SearchWsResponse.class);
  649. assertThat(result.getIssuesList())
  650. .extracting(Issue::getKey, Issue::getComponent, Issue::getPullRequest)
  651. .containsExactlyInAnyOrder(tuple(pullRequestIssue.getKey(), pullRequestFile.getKey(), pullRequestFile.getPullRequest()));
  652. assertThat(result.getComponentsList())
  653. .extracting(Issues.Component::getKey, Issues.Component::getPullRequest)
  654. .containsExactlyInAnyOrder(
  655. tuple(pullRequestFile.getKey(), pullRequestFile.getPullRequest()),
  656. tuple(pullRequest.getKey(), pullRequest.getPullRequest()));
  657. }
  658. @Test
  659. public void search_using_main_branch_name() {
  660. RuleDefinitionDto rule = db.rules().insertIssueRule();
  661. ComponentDto project = db.components().insertMainBranch();
  662. ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
  663. IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile);
  664. allowAnyoneOnProjects(project);
  665. indexIssuesAndViews();
  666. SearchWsResponse result = ws.newRequest()
  667. .setParam(PARAM_COMPONENT_KEYS, project.getKey())
  668. .setParam(PARAM_BRANCH, "master")
  669. .executeProtobuf(SearchWsResponse.class);
  670. assertThat(result.getIssuesList())
  671. .extracting(Issue::getKey, Issue::getComponent, Issue::hasBranch)
  672. .containsExactlyInAnyOrder(tuple(projectIssue.getKey(), projectFile.getKey(), false));
  673. assertThat(result.getComponentsList())
  674. .extracting(Issues.Component::getKey, Issues.Component::hasBranch)
  675. .containsExactlyInAnyOrder(
  676. tuple(projectFile.getKey(), false),
  677. tuple(project.getKey(), false));
  678. }
  679. @Test
  680. public void does_not_return_branch_issues_on_not_contextualized_search() {
  681. RuleDefinitionDto rule = db.rules().insertIssueRule();
  682. ComponentDto project = db.components().insertPrivateProject();
  683. ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
  684. IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile);
  685. ComponentDto branch = db.components().insertProjectBranch(project);
  686. ComponentDto branchFile = db.components().insertComponent(newFileDto(branch));
  687. IssueDto branchIssue = db.issues().insertIssue(rule, branch, branchFile);
  688. allowAnyoneOnProjects(project);
  689. indexIssuesAndViews();
  690. SearchWsResponse result = ws.newRequest().executeProtobuf(SearchWsResponse.class);
  691. assertThat(result.getIssuesList()).extracting(Issue::getKey)
  692. .containsExactlyInAnyOrder(projectIssue.getKey())
  693. .doesNotContain(branchIssue.getKey());
  694. }
  695. @Test
  696. public void does_not_return_branch_issues_when_using_db_key() {
  697. RuleDefinitionDto rule = db.rules().insertIssueRule();
  698. ComponentDto project = db.components().insertPrivateProject();
  699. ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
  700. IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile);
  701. ComponentDto branch = db.components().insertProjectBranch(project);
  702. ComponentDto branchFile = db.components().insertComponent(newFileDto(branch));
  703. IssueDto branchIssue = db.issues().insertIssue(rule, branch, branchFile);
  704. allowAnyoneOnProjects(project);
  705. indexIssues();
  706. SearchWsResponse result = ws.newRequest()
  707. .setParam(PARAM_COMPONENT_KEYS, branch.getDbKey())
  708. .executeProtobuf(SearchWsResponse.class);
  709. assertThat(result.getIssuesList()).isEmpty();
  710. }
  711. private void allowAnyoneOnProjects(ComponentDto... projects) {
  712. userSession.registerComponents(projects);
  713. Arrays.stream(projects).forEach(p -> permissionIndexer.allowOnlyAnyone(p));
  714. }
  715. private void indexIssues() {
  716. issueIndexer.indexOnStartup(null);
  717. }
  718. private void indexIssuesAndViews() {
  719. indexIssues();
  720. viewIndexer.indexOnStartup(null);
  721. }
  722. }