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.

ShowActionIT.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.component.ws;
  21. import java.util.Date;
  22. import java.util.Optional;
  23. import javax.annotation.Nullable;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.api.resources.Qualifiers;
  27. import org.sonar.api.server.ws.Change;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.api.web.UserRole;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.component.ProjectData;
  34. import org.sonar.server.component.TestComponentFinder;
  35. import org.sonar.server.exceptions.ForbiddenException;
  36. import org.sonar.server.exceptions.NotFoundException;
  37. import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
  38. import org.sonar.server.tester.UserSessionRule;
  39. import org.sonar.server.ws.TestRequest;
  40. import org.sonar.server.ws.WsActionTester;
  41. import org.sonarqube.ws.Components.Component;
  42. import org.sonarqube.ws.Components.ShowWsResponse;
  43. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  46. import static org.assertj.core.api.Assertions.tuple;
  47. import static org.sonar.api.utils.DateUtils.formatDateTime;
  48. import static org.sonar.api.utils.DateUtils.parseDateTime;
  49. import static org.sonar.api.web.UserRole.USER;
  50. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  51. import static org.sonar.db.component.BranchType.BRANCH;
  52. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  53. import static org.sonar.db.component.ComponentTesting.newDirectory;
  54. import static org.sonar.db.component.ComponentTesting.newDirectoryOnBranch;
  55. import static org.sonar.db.component.ComponentTesting.newFileDto;
  56. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  57. import static org.sonar.db.component.SnapshotTesting.newAnalysis;
  58. import static org.sonar.test.JsonAssert.assertJson;
  59. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BRANCH;
  60. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT;
  61. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_PULL_REQUEST;
  62. public class ShowActionIT {
  63. @Rule
  64. public final UserSessionRule userSession = UserSessionRule.standalone().logIn();
  65. @Rule
  66. public final DbTester db = DbTester.create(System2.INSTANCE);
  67. private final WsActionTester ws = new WsActionTester(new ShowAction(userSession, db.getDbClient(), TestComponentFinder.from(db),
  68. new IssueIndexSyncProgressChecker(db.getDbClient())));
  69. @Test
  70. public void verify_definition() {
  71. WebService.Action action = ws.getDef();
  72. assertThat(action.since()).isEqualTo("5.4");
  73. assertThat(action.description()).isNotNull();
  74. assertThat(action.responseExample()).isNotNull();
  75. assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder(
  76. tuple("7.6", "The use of module keys in parameter 'component' is deprecated"),
  77. tuple("10.1", "The use of module keys in parameter 'component' is removed"));
  78. assertThat(action.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("component", "branch", "pullRequest");
  79. WebService.Param component = action.param(PARAM_COMPONENT);
  80. assertThat(component.isRequired()).isTrue();
  81. assertThat(component.description()).isNotNull();
  82. assertThat(component.exampleValue()).isNotNull();
  83. WebService.Param branch = action.param(PARAM_BRANCH);
  84. assertThat(branch.isInternal()).isFalse();
  85. assertThat(branch.isRequired()).isFalse();
  86. assertThat(branch.since()).isEqualTo("6.6");
  87. WebService.Param pullRequest = action.param(PARAM_PULL_REQUEST);
  88. assertThat(pullRequest.isInternal()).isFalse();
  89. assertThat(pullRequest.isRequired()).isFalse();
  90. assertThat(pullRequest.since()).isEqualTo("7.1");
  91. }
  92. @Test
  93. public void json_example() {
  94. insertJsonExampleComponentsAndSnapshots();
  95. String response = ws.newRequest()
  96. .setParam("component", "com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/Rule.java")
  97. .execute()
  98. .getInput();
  99. assertJson(response).isSimilarTo(getClass().getResource("show-example.json"));
  100. }
  101. @Test
  102. public void tags_displayed_only_for_project() {
  103. insertJsonExampleComponentsAndSnapshots();
  104. String response = ws.newRequest()
  105. .setParam(PARAM_COMPONENT, "com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/Rule.java")
  106. .execute()
  107. .getInput();
  108. assertThat(response).containsOnlyOnce("\"tags\"");
  109. }
  110. @Test
  111. public void show_with_browse_permission() {
  112. ProjectData projectData = db.components().insertPrivateProject();
  113. ComponentDto mainBranch = projectData.getMainBranchComponent();
  114. db.components().insertSnapshot(mainBranch);
  115. userSession.addProjectPermission(USER, projectData.getProjectDto())
  116. .registerBranches(projectData.getMainBranchDto());
  117. ShowWsResponse response = newRequest(mainBranch.getKey());
  118. assertThat(response.getComponent().getKey()).isEqualTo(mainBranch.getKey());
  119. }
  120. @Test
  121. public void show_with_ancestors_when_not_project() {
  122. ProjectData projectData = db.components().insertPrivateProject();
  123. ComponentDto mainBranch = projectData.getMainBranchComponent();
  124. ComponentDto directory = db.components().insertComponent(newDirectory(mainBranch, "dir"));
  125. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch, directory));
  126. userSession.addProjectPermission(USER, projectData.getProjectDto())
  127. .registerBranches(projectData.getMainBranchDto());
  128. ShowWsResponse response = newRequest(file.getKey());
  129. assertThat(response.getComponent().getKey()).isEqualTo(file.getKey());
  130. assertThat(response.getAncestorsList()).extracting(Component::getKey).containsOnly(directory.getKey(), mainBranch.getKey());
  131. }
  132. @Test
  133. public void show_without_ancestors_when_project() {
  134. ProjectData projectData = db.components().insertPrivateProject();
  135. ComponentDto mainBranch = projectData.getMainBranchComponent();
  136. userSession.addProjectPermission(USER, projectData.getProjectDto())
  137. .registerBranches(projectData.getMainBranchDto());
  138. ShowWsResponse response = newRequest(mainBranch.getKey());
  139. assertThat(response.getComponent().getKey()).isEqualTo(mainBranch.getKey());
  140. assertThat(response.getAncestorsList()).isEmpty();
  141. }
  142. @Test
  143. public void show_with_last_analysis_date() {
  144. ProjectData projectData = db.components().insertPrivateProject();
  145. ComponentDto mainBranch = projectData.getMainBranchComponent();
  146. db.components().insertSnapshots(
  147. newAnalysis(mainBranch).setCreatedAt(1_000_000_000L).setLast(false),
  148. newAnalysis(mainBranch).setCreatedAt(2_000_000_000L).setLast(false),
  149. newAnalysis(mainBranch).setCreatedAt(3_000_000_000L).setLast(true));
  150. userSession.addProjectPermission(USER, projectData.getProjectDto())
  151. .registerBranches(projectData.getMainBranchDto());
  152. ShowWsResponse response = newRequest(mainBranch.getKey());
  153. assertThat(response.getComponent().getAnalysisDate()).isNotEmpty().isEqualTo(formatDateTime(new Date(3_000_000_000L)));
  154. }
  155. @Test
  156. public void show_with_new_code_period_date() {
  157. ProjectData projectData = db.components().insertPrivateProject();
  158. ComponentDto mainBranch = projectData.getMainBranchComponent();
  159. db.components().insertSnapshots(
  160. newAnalysis(mainBranch).setPeriodDate(1_000_000_000L).setLast(false),
  161. newAnalysis(mainBranch).setPeriodDate(2_000_000_000L).setLast(false),
  162. newAnalysis(mainBranch).setPeriodDate(3_000_000_000L).setLast(true));
  163. userSession.addProjectPermission(USER, projectData.getProjectDto())
  164. .registerBranches(projectData.getMainBranchDto());
  165. ShowWsResponse response = newRequest(mainBranch.getKey());
  166. assertThat(response.getComponent().getLeakPeriodDate()).isNotEmpty().isEqualTo(formatDateTime(new Date(3_000_000_000L)));
  167. }
  168. @Test
  169. public void show_with_ancestors_and_analysis_date() {
  170. ProjectData projectData = db.components().insertPrivateProject();
  171. ComponentDto mainBranch = projectData.getMainBranchComponent();
  172. db.components().insertSnapshot(newAnalysis(mainBranch).setCreatedAt(3_000_000_000L).setLast(true));
  173. ComponentDto directory = db.components().insertComponent(newDirectory(mainBranch, "dir"));
  174. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch, directory));
  175. userSession.addProjectPermission(USER, projectData.getProjectDto())
  176. .registerBranches(projectData.getMainBranchDto());
  177. ShowWsResponse response = newRequest(file.getKey());
  178. String expectedDate = formatDateTime(new Date(3_000_000_000L));
  179. assertThat(response.getAncestorsList()).extracting(Component::getAnalysisDate)
  180. .containsOnly(expectedDate, expectedDate, expectedDate);
  181. }
  182. @Test
  183. public void should_return_visibility_for_private_project() {
  184. ProjectData projectData = db.components().insertPrivateProject();
  185. ComponentDto mainBranch = projectData.getMainBranchComponent();
  186. userSession.addProjectPermission(USER, projectData.getProjectDto())
  187. .registerBranches(projectData.getMainBranchDto());
  188. ShowWsResponse result = newRequest(mainBranch.getKey());
  189. assertThat(result.getComponent().hasVisibility()).isTrue();
  190. assertThat(result.getComponent().getVisibility()).isEqualTo("private");
  191. }
  192. @Test
  193. public void should_return_visibility_for_public_project() {
  194. ProjectData projectData = db.components().insertPublicProject();
  195. ComponentDto publicProject = projectData.getMainBranchComponent();
  196. userSession.addProjectBranchMapping(projectData.projectUuid(), projectData.getMainBranchComponent());
  197. userSession.registerProjects(projectData.getProjectDto());
  198. ShowWsResponse result = newRequest(publicProject.getKey());
  199. assertThat(result.getComponent().hasVisibility()).isTrue();
  200. assertThat(result.getComponent().getVisibility()).isEqualTo("public");
  201. }
  202. @Test
  203. public void should_return_visibility_for_portfolio() {
  204. ComponentDto view = db.components().insertPrivatePortfolio();
  205. userSession.addPortfolioPermission(USER, view);
  206. ShowWsResponse result = newRequest(view.getKey());
  207. assertThat(result.getComponent().hasVisibility()).isTrue();
  208. }
  209. @Test
  210. public void display_version() {
  211. ProjectData projectData = db.components().insertPrivateProject();
  212. ComponentDto mainBranch = projectData.getMainBranchComponent();
  213. ComponentDto directory = db.components().insertComponent(newDirectory(mainBranch, "dir"));
  214. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch, directory));
  215. db.components().insertSnapshot(mainBranch, s -> s.setProjectVersion("1.1"));
  216. userSession.addProjectPermission(USER, projectData.getProjectDto())
  217. .registerBranches(projectData.getMainBranchDto());
  218. ShowWsResponse response = newRequest(file.getKey());
  219. assertThat(response.getComponent().getVersion()).isEqualTo("1.1");
  220. assertThat(response.getAncestorsList())
  221. .extracting(Component::getVersion)
  222. .containsOnly("1.1");
  223. }
  224. @Test
  225. public void branch() {
  226. ProjectData projectData = db.components().insertPrivateProject();
  227. ComponentDto mainBranch = projectData.getMainBranchComponent();
  228. userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
  229. String branchKey = "my_branch";
  230. ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchKey));
  231. userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
  232. ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch, "dir", mainBranch.uuid()));
  233. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch.uuid(), branch, directory));
  234. db.components().insertSnapshot(branch, s -> s.setProjectVersion("1.1"));
  235. ShowWsResponse response = ws.newRequest()
  236. .setParam(PARAM_COMPONENT, file.getKey())
  237. .setParam(PARAM_BRANCH, branchKey)
  238. .executeProtobuf(ShowWsResponse.class);
  239. assertThat(response.getComponent())
  240. .extracting(Component::getKey, Component::getBranch, Component::getVersion)
  241. .containsExactlyInAnyOrder(file.getKey(), branchKey, "1.1");
  242. assertThat(response.getAncestorsList()).extracting(Component::getKey, Component::getBranch, Component::getVersion)
  243. .containsExactlyInAnyOrder(
  244. tuple(directory.getKey(), branchKey, "1.1"),
  245. tuple(branch.getKey(), branchKey, "1.1"));
  246. }
  247. @Test
  248. public void dont_show_branch_if_main_branch() {
  249. ProjectData projectData = db.components().insertPrivateProject();
  250. ComponentDto mainBranch = projectData.getMainBranchComponent();
  251. userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto())
  252. .registerBranches(projectData.getMainBranchDto());
  253. ShowWsResponse response = ws.newRequest()
  254. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  255. .setParam(PARAM_BRANCH, DEFAULT_MAIN_BRANCH_NAME)
  256. .executeProtobuf(ShowWsResponse.class);
  257. assertThat(response.getComponent())
  258. .extracting(Component::getKey, Component::getBranch)
  259. .containsExactlyInAnyOrder(mainBranch.getKey(), "");
  260. }
  261. @Test
  262. public void pull_request() {
  263. ProjectData projectData = db.components().insertPrivateProject();
  264. ComponentDto mainBranch = projectData.getMainBranchComponent();
  265. userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
  266. String pullRequest = "pr-1234";
  267. ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(pullRequest).setBranchType(PULL_REQUEST));
  268. userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
  269. ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch, "dir", mainBranch.uuid()));
  270. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch.uuid(), branch, directory));
  271. db.components().insertSnapshot(branch, s -> s.setProjectVersion("1.1"));
  272. ShowWsResponse response = ws.newRequest()
  273. .setParam(PARAM_COMPONENT, file.getKey())
  274. .setParam(PARAM_PULL_REQUEST, pullRequest)
  275. .executeProtobuf(ShowWsResponse.class);
  276. assertThat(response.getComponent())
  277. .extracting(Component::getKey, Component::getPullRequest, Component::getVersion)
  278. .containsExactlyInAnyOrder(file.getKey(), pullRequest, "1.1");
  279. assertThat(response.getAncestorsList()).extracting(Component::getKey, Component::getPullRequest, Component::getVersion)
  280. .containsExactlyInAnyOrder(
  281. tuple(directory.getKey(), pullRequest, "1.1"),
  282. tuple(branch.getKey(), pullRequest, "1.1"));
  283. }
  284. @Test
  285. public void verify_need_issue_sync_pr() {
  286. ComponentDto portfolio1 = db.components().insertPublicPortfolio();
  287. ComponentDto portfolio2 = db.components().insertPublicPortfolio();
  288. ComponentDto subview = db.components().insertSubView(portfolio1);
  289. String pullRequestKey1 = randomAlphanumeric(100);
  290. ProjectData projectData1 = db.components().insertPrivateProject();
  291. ComponentDto project1 = projectData1.getMainBranchComponent();
  292. ComponentDto branch1 = db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey1)
  293. .setNeedIssueSync(true));
  294. ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch1, "dir", project1.uuid()));
  295. ComponentDto file = db.components().insertComponent(newFileDto(project1.uuid(), branch1, directory));
  296. userSession.addProjectBranchMapping(projectData1.projectUuid(), projectData1.getMainBranchComponent());
  297. userSession.addProjectBranchMapping(projectData1.projectUuid(), branch1);
  298. ProjectData projectData2 = db.components().insertPrivateProject();
  299. ComponentDto project2 = projectData2.getMainBranchComponent();
  300. String branchName2 = randomAlphanumeric(248);
  301. ComponentDto branch2 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(true).setKey(branchName2));
  302. String branchName3 = randomAlphanumeric(248);
  303. ComponentDto branch3 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(false).setKey(branchName3));
  304. userSession.addProjectBranchMapping(projectData2.projectUuid(), projectData2.getMainBranchComponent());
  305. userSession.addProjectBranchMapping(projectData2.projectUuid(), branch2);
  306. userSession.addProjectBranchMapping(projectData2.projectUuid(), branch3);
  307. ProjectData projectData3 = db.components().insertPrivateProject();
  308. ComponentDto project3 = projectData3.getMainBranchComponent();
  309. String pullRequestKey4 = randomAlphanumeric(100);
  310. ComponentDto branch4 = db.components().insertProjectBranch(project3, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey4).setNeedIssueSync(false));
  311. ComponentDto directoryOfBranch4 = db.components().insertComponent(newDirectoryOnBranch(branch4, "dir", project3.uuid()));
  312. ComponentDto fileOfBranch4 = db.components().insertComponent(newFileDto(project3.uuid(), branch4, directoryOfBranch4));
  313. String branchName5 = randomAlphanumeric(248);
  314. ComponentDto branch5 = db.components().insertProjectBranch(project3, b -> b.setBranchType(BRANCH).setNeedIssueSync(false).setKey(branchName5));
  315. userSession.addProjectBranchMapping(projectData3.projectUuid(), projectData3.getMainBranchComponent());
  316. userSession.addProjectBranchMapping(projectData3.projectUuid(), branch4);
  317. userSession.addProjectBranchMapping(projectData3.projectUuid(), branch5);
  318. userSession.addProjectPermission(UserRole.USER, projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto())
  319. .registerBranches(projectData1.getMainBranchDto(), projectData2.getMainBranchDto(), projectData3.getMainBranchDto());
  320. userSession.registerPortfolios(portfolio1, portfolio2, subview);
  321. userSession.registerProjects(projectData1.getProjectDto(), projectData2.getProjectDto(), projectData3.getProjectDto());
  322. // for portfolios, sub-views need issue sync flag is set to true if any project need sync
  323. assertNeedIssueSyncEqual(null, null, portfolio1, true);
  324. assertNeedIssueSyncEqual(null, null, subview, true);
  325. assertNeedIssueSyncEqual(null, null, portfolio2, true);
  326. // if branch need sync it is propagated to other components
  327. assertNeedIssueSyncEqual(null, null, project1, true);
  328. assertNeedIssueSyncEqual(pullRequestKey1, null, branch1, true);
  329. assertNeedIssueSyncEqual(pullRequestKey1, null, directory, true);
  330. assertNeedIssueSyncEqual(pullRequestKey1, null, file, true);
  331. assertNeedIssueSyncEqual(null, null, project2, true);
  332. assertNeedIssueSyncEqual(null, branchName2, branch2, true);
  333. assertNeedIssueSyncEqual(null, branchName3, branch3, true);
  334. // if all branches are synced, need issue sync on project is is set to false
  335. assertNeedIssueSyncEqual(null, null, project3, false);
  336. assertNeedIssueSyncEqual(pullRequestKey4, null, branch4, false);
  337. assertNeedIssueSyncEqual(pullRequestKey4, null, directoryOfBranch4, false);
  338. assertNeedIssueSyncEqual(pullRequestKey4, null, fileOfBranch4, false);
  339. assertNeedIssueSyncEqual(null, branchName5, branch5, false);
  340. }
  341. private void assertNeedIssueSyncEqual(@Nullable String pullRequest, @Nullable String branch, ComponentDto component, boolean needIssueSync) {
  342. TestRequest testRequest = ws.newRequest()
  343. .setParam(PARAM_COMPONENT, component.getKey());
  344. Optional.ofNullable(pullRequest).ifPresent(pr -> testRequest.setParam(PARAM_PULL_REQUEST, pr));
  345. Optional.ofNullable(branch).ifPresent(br -> testRequest.setParam(PARAM_BRANCH, br));
  346. ShowWsResponse response = testRequest.executeProtobuf(ShowWsResponse.class);
  347. assertThat(response.getComponent())
  348. .extracting(Component::getNeedIssueSync)
  349. .isEqualTo(needIssueSync);
  350. }
  351. @Test
  352. public void throw_ForbiddenException_if_user_doesnt_have_browse_permission_on_project() {
  353. ComponentDto componentDto = newPrivateProjectDto("project-uuid");
  354. db.components().insertProjectAndSnapshot(componentDto);
  355. String componentDtoDbKey = componentDto.getKey();
  356. assertThatThrownBy(() -> newRequest(componentDtoDbKey))
  357. .isInstanceOf(ForbiddenException.class);
  358. }
  359. @Test
  360. public void fail_if_component_does_not_exist() {
  361. assertThatThrownBy(() -> newRequest("unknown-key"))
  362. .isInstanceOf(NotFoundException.class)
  363. .hasMessage("Component key 'unknown-key' not found");
  364. }
  365. @Test
  366. public void fail_if_component_is_removed() {
  367. ProjectData projectData = db.components().insertPrivateProject();
  368. ComponentDto mainBranch = projectData.getMainBranchComponent();
  369. userSession.addProjectPermission(USER, projectData.getProjectDto())
  370. .registerBranches(projectData.getMainBranchDto());
  371. db.components().insertComponent(newFileDto(mainBranch).setKey("file-key").setEnabled(false));
  372. assertThatThrownBy(() -> newRequest("file-key"))
  373. .isInstanceOf(NotFoundException.class)
  374. .hasMessage("Component key 'file-key' not found");
  375. }
  376. @Test
  377. public void fail_if_branch_does_not_exist() {
  378. ProjectData projectData = db.components().insertPrivateProject();
  379. ComponentDto mainBranch = projectData.getMainBranchComponent();
  380. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
  381. userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto())
  382. .registerBranches(projectData.getMainBranchDto());
  383. db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
  384. TestRequest request = ws.newRequest()
  385. .setParam(PARAM_COMPONENT, file.getKey())
  386. .setParam(PARAM_BRANCH, "another_branch");
  387. assertThatThrownBy(request::execute)
  388. .isInstanceOf(NotFoundException.class)
  389. .hasMessage(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
  390. }
  391. private ShowWsResponse newRequest(@Nullable String key) {
  392. TestRequest request = ws.newRequest();
  393. if (key != null) {
  394. request.setParam(PARAM_COMPONENT, key);
  395. }
  396. return request.executeProtobuf(ShowWsResponse.class);
  397. }
  398. private void insertJsonExampleComponentsAndSnapshots() {
  399. ProjectData projectData = db.components().insertPrivateProject(c -> c.setUuid("AVIF98jgA3Ax6PH2efOW")
  400. .setBranchUuid("AVIF98jgA3Ax6PH2efOW")
  401. .setKey("com.sonarsource:java-markdown")
  402. .setName("Java Markdown")
  403. .setDescription("Java Markdown Project")
  404. .setQualifier(Qualifiers.PROJECT),
  405. p -> p.setTagsString("language, plugin"));
  406. ComponentDto mainBranch = projectData.getMainBranchComponent();
  407. userSession.addProjectPermission(USER, projectData.getProjectDto())
  408. .registerBranches(projectData.getMainBranchDto());
  409. db.components().insertSnapshot(mainBranch, snapshot -> snapshot
  410. .setProjectVersion("1.1")
  411. .setCreatedAt(parseDateTime("2017-03-01T11:39:03+0100").getTime())
  412. .setPeriodDate(parseDateTime("2017-01-01T11:39:03+0100").getTime()));
  413. ComponentDto directory = newDirectory(mainBranch, "AVIF-FfgA3Ax6PH2efPF", "src/main/java/com/sonarsource/markdown/impl")
  414. .setKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl")
  415. .setName("src/main/java/com/sonarsource/markdown/impl")
  416. .setQualifier(Qualifiers.DIRECTORY);
  417. db.components().insertComponent(directory);
  418. db.components().insertComponent(
  419. newFileDto(directory, directory, "AVIF-FffA3Ax6PH2efPD")
  420. .setKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/Rule.java")
  421. .setName("Rule.java")
  422. .setPath("src/main/java/com/sonarsource/markdown/impl/Rule.java")
  423. .setLanguage("java")
  424. .setQualifier(Qualifiers.FILE));
  425. }
  426. }