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.

ShowActionTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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.component.ws;
  21. import java.util.Date;
  22. import javax.annotation.Nullable;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  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.organization.OrganizationDto;
  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.tester.UserSessionRule;
  38. import org.sonar.server.ws.TestRequest;
  39. import org.sonar.server.ws.WsActionTester;
  40. import org.sonarqube.ws.Components.Component;
  41. import org.sonarqube.ws.Components.ShowWsResponse;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.assertj.core.api.Assertions.tuple;
  44. import static org.sonar.api.utils.DateUtils.formatDateTime;
  45. import static org.sonar.api.utils.DateUtils.parseDateTime;
  46. import static org.sonar.api.web.UserRole.USER;
  47. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  48. import static org.sonar.db.component.ComponentTesting.newDirectory;
  49. import static org.sonar.db.component.ComponentTesting.newFileDto;
  50. import static org.sonar.db.component.ComponentTesting.newModuleDto;
  51. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  52. import static org.sonar.db.component.SnapshotTesting.newAnalysis;
  53. import static org.sonar.test.JsonAssert.assertJson;
  54. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BRANCH;
  55. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT;
  56. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT_ID;
  57. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_PULL_REQUEST;
  58. public class ShowActionTest {
  59. @Rule
  60. public ExpectedException expectedException = ExpectedException.none();
  61. @Rule
  62. public UserSessionRule userSession = UserSessionRule.standalone();
  63. @Rule
  64. public DbTester db = DbTester.create(System2.INSTANCE);
  65. private WsActionTester ws = new WsActionTester(new ShowAction(userSession, db.getDbClient(), TestComponentFinder.from(db)));
  66. @Test
  67. public void verify_definition() {
  68. WebService.Action action = ws.getDef();
  69. assertThat(action.since()).isEqualTo("5.4");
  70. assertThat(action.description()).isNotNull();
  71. assertThat(action.responseExample()).isNotNull();
  72. assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder(
  73. tuple("6.4", "Analysis date has been added to the response"),
  74. tuple("6.4", "The field 'id' is deprecated in the response"),
  75. tuple("6.4", "The 'visibility' field is added to the response"),
  76. tuple("6.5", "Leak period date is added to the response"),
  77. tuple("6.6", "'branch' is added to the response"),
  78. tuple("6.6", "'version' is added to the response"),
  79. tuple("7.6", "The use of module keys in parameter 'component' is deprecated"));
  80. assertThat(action.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("component", "componentId", "branch", "pullRequest");
  81. WebService.Param componentId = action.param(PARAM_COMPONENT_ID);
  82. assertThat(componentId.isRequired()).isFalse();
  83. assertThat(componentId.description()).isNotNull();
  84. assertThat(componentId.exampleValue()).isNotNull();
  85. assertThat(componentId.deprecatedSince()).isEqualTo("6.4");
  86. assertThat(componentId.deprecatedKey()).isEqualTo("id");
  87. assertThat(componentId.deprecatedKeySince()).isEqualTo("6.4");
  88. WebService.Param component = action.param(PARAM_COMPONENT);
  89. assertThat(component.isRequired()).isFalse();
  90. assertThat(component.description()).isNotNull();
  91. assertThat(component.exampleValue()).isNotNull();
  92. assertThat(component.deprecatedKey()).isEqualTo("key");
  93. assertThat(component.deprecatedKeySince()).isEqualTo("6.4");
  94. WebService.Param branch = action.param(PARAM_BRANCH);
  95. assertThat(branch.isInternal()).isTrue();
  96. assertThat(branch.isRequired()).isFalse();
  97. assertThat(branch.since()).isEqualTo("6.6");
  98. WebService.Param pullRequest = action.param(PARAM_PULL_REQUEST);
  99. assertThat(pullRequest.isInternal()).isTrue();
  100. assertThat(pullRequest.isRequired()).isFalse();
  101. assertThat(pullRequest.since()).isEqualTo("7.1");
  102. }
  103. @Test
  104. public void json_example() {
  105. userSession.logIn().setRoot();
  106. insertJsonExampleComponentsAndSnapshots();
  107. String response = ws.newRequest()
  108. .setParam("id", "AVIF-FffA3Ax6PH2efPD")
  109. .execute()
  110. .getInput();
  111. assertJson(response).isSimilarTo(getClass().getResource("show-example.json"));
  112. }
  113. @Test
  114. public void tags_displayed_only_for_project() {
  115. userSession.logIn().setRoot();
  116. insertJsonExampleComponentsAndSnapshots();
  117. String response = ws.newRequest()
  118. .setParam("id", "AVIF-FffA3Ax6PH2efPD")
  119. .execute()
  120. .getInput();
  121. assertThat(response).containsOnlyOnce("\"tags\"");
  122. }
  123. @Test
  124. public void show_with_browse_permission() {
  125. ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid");
  126. db.components().insertProjectAndSnapshot(project);
  127. userSession.logIn().addProjectPermission(USER, project);
  128. ShowWsResponse response = newRequest("project-uuid", null);
  129. assertThat(response.getComponent().getId()).isEqualTo("project-uuid");
  130. }
  131. @Test
  132. public void show_provided_project() {
  133. userSession.logIn().setRoot();
  134. db.components().insertComponent(newPrivateProjectDto(db.organizations().insert(), "project-uuid"));
  135. ShowWsResponse response = newRequest("project-uuid", null);
  136. assertThat(response.getComponent().getId()).isEqualTo("project-uuid");
  137. assertThat(response.getComponent().hasAnalysisDate()).isFalse();
  138. assertThat(response.getComponent().hasLeakPeriodDate()).isFalse();
  139. }
  140. @Test
  141. public void show_with_ancestors_when_not_project() {
  142. ComponentDto project = db.components().insertPrivateProject();
  143. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  144. ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir"));
  145. ComponentDto file = db.components().insertComponent(newFileDto(directory));
  146. userSession.addProjectPermission(USER, project);
  147. ShowWsResponse response = newRequest(null, file.getDbKey());
  148. assertThat(response.getComponent().getKey()).isEqualTo(file.getDbKey());
  149. assertThat(response.getAncestorsList()).extracting(Component::getKey).containsOnly(directory.getDbKey(), module.getDbKey(), project.getDbKey());
  150. }
  151. @Test
  152. public void show_without_ancestors_when_project() {
  153. ComponentDto project = db.components().insertPrivateProject();
  154. db.components().insertComponent(newModuleDto(project));
  155. userSession.addProjectPermission(USER, project);
  156. ShowWsResponse response = newRequest(null, project.getDbKey());
  157. assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey());
  158. assertThat(response.getAncestorsList()).isEmpty();
  159. }
  160. @Test
  161. public void show_with_last_analysis_date() {
  162. ComponentDto project = db.components().insertPrivateProject();
  163. db.components().insertSnapshots(
  164. newAnalysis(project).setCreatedAt(1_000_000_000L).setLast(false),
  165. newAnalysis(project).setCreatedAt(2_000_000_000L).setLast(false),
  166. newAnalysis(project).setCreatedAt(3_000_000_000L).setLast(true));
  167. userSession.addProjectPermission(USER, project);
  168. ShowWsResponse response = newRequest(null, project.getDbKey());
  169. assertThat(response.getComponent().getAnalysisDate()).isNotEmpty().isEqualTo(formatDateTime(new Date(3_000_000_000L)));
  170. }
  171. @Test
  172. public void show_with_leak_period_date() {
  173. ComponentDto project = db.components().insertPrivateProject();
  174. db.components().insertSnapshots(
  175. newAnalysis(project).setPeriodDate(1_000_000_000L).setLast(false),
  176. newAnalysis(project).setPeriodDate(2_000_000_000L).setLast(false),
  177. newAnalysis(project).setPeriodDate(3_000_000_000L).setLast(true));
  178. userSession.addProjectPermission(USER, project);
  179. ShowWsResponse response = newRequest(null, project.getDbKey());
  180. assertThat(response.getComponent().getLeakPeriodDate()).isNotEmpty().isEqualTo(formatDateTime(new Date(3_000_000_000L)));
  181. }
  182. @Test
  183. public void show_with_ancestors_and_analysis_date() {
  184. ComponentDto project = db.components().insertPrivateProject();
  185. db.components().insertSnapshot(newAnalysis(project).setCreatedAt(3_000_000_000L).setLast(true));
  186. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  187. ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir"));
  188. ComponentDto file = db.components().insertComponent(newFileDto(directory));
  189. userSession.addProjectPermission(USER, project);
  190. ShowWsResponse response = newRequest(null, file.getDbKey());
  191. String expectedDate = formatDateTime(new Date(3_000_000_000L));
  192. assertThat(response.getAncestorsList()).extracting(Component::getAnalysisDate)
  193. .containsOnly(expectedDate, expectedDate, expectedDate);
  194. }
  195. @Test
  196. public void should_return_visibility_for_private_project() {
  197. userSession.logIn().setRoot();
  198. ComponentDto privateProject = db.components().insertPrivateProject();
  199. ShowWsResponse result = newRequest(null, privateProject.getDbKey());
  200. assertThat(result.getComponent().hasVisibility()).isTrue();
  201. assertThat(result.getComponent().getVisibility()).isEqualTo("private");
  202. }
  203. @Test
  204. public void should_return_visibility_for_public_project() {
  205. userSession.logIn().setRoot();
  206. ComponentDto publicProject = db.components().insertPublicProject();
  207. ShowWsResponse result = newRequest(null, publicProject.getDbKey());
  208. assertThat(result.getComponent().hasVisibility()).isTrue();
  209. assertThat(result.getComponent().getVisibility()).isEqualTo("public");
  210. }
  211. @Test
  212. public void should_return_visibility_for_view() {
  213. userSession.logIn().setRoot();
  214. ComponentDto view = db.components().insertView();
  215. ShowWsResponse result = newRequest(null, view.getDbKey());
  216. assertThat(result.getComponent().hasVisibility()).isTrue();
  217. }
  218. @Test
  219. public void should_not_return_visibility_for_module() {
  220. userSession.logIn().setRoot();
  221. ComponentDto privateProject = db.components().insertPrivateProject();
  222. ComponentDto module = db.components().insertComponent(newModuleDto(privateProject));
  223. ShowWsResponse result = newRequest(null, module.getDbKey());
  224. assertThat(result.getComponent().hasVisibility()).isFalse();
  225. }
  226. @Test
  227. public void display_version() {
  228. ComponentDto project = db.components().insertPrivateProject();
  229. ComponentDto module = db.components().insertComponent(newModuleDto(project));
  230. ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir"));
  231. ComponentDto file = db.components().insertComponent(newFileDto(directory));
  232. db.components().insertSnapshot(project, s -> s.setProjectVersion("1.1"));
  233. userSession.addProjectPermission(USER, project);
  234. ShowWsResponse response = newRequest(null, file.getDbKey());
  235. assertThat(response.getComponent().getVersion()).isEqualTo("1.1");
  236. assertThat(response.getAncestorsList())
  237. .extracting(Component::getVersion)
  238. .containsOnly("1.1");
  239. }
  240. @Test
  241. public void branch() {
  242. ComponentDto project = db.components().insertMainBranch();
  243. userSession.addProjectPermission(UserRole.USER, project);
  244. String branchKey = "my_branch";
  245. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchKey));
  246. ComponentDto module = db.components().insertComponent(newModuleDto(branch));
  247. ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir"));
  248. ComponentDto file = db.components().insertComponent(newFileDto(directory));
  249. db.components().insertSnapshot(branch, s -> s.setProjectVersion("1.1"));
  250. ShowWsResponse response = ws.newRequest()
  251. .setParam(PARAM_COMPONENT, file.getKey())
  252. .setParam(PARAM_BRANCH, branchKey)
  253. .executeProtobuf(ShowWsResponse.class);
  254. assertThat(response.getComponent())
  255. .extracting(Component::getKey, Component::getBranch, Component::getVersion)
  256. .containsExactlyInAnyOrder(file.getKey(), branchKey, "1.1");
  257. assertThat(response.getAncestorsList()).extracting(Component::getKey, Component::getBranch, Component::getVersion)
  258. .containsExactlyInAnyOrder(
  259. tuple(directory.getKey(), branchKey, "1.1"),
  260. tuple(module.getKey(), branchKey, "1.1"),
  261. tuple(branch.getKey(), branchKey, "1.1"));
  262. }
  263. @Test
  264. public void pull_request() {
  265. ComponentDto project = db.components().insertMainBranch();
  266. userSession.addProjectPermission(UserRole.USER, project);
  267. String pullRequest = "pr-1234";
  268. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(pullRequest).setBranchType(PULL_REQUEST));
  269. ComponentDto module = db.components().insertComponent(newModuleDto(branch));
  270. ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir"));
  271. ComponentDto file = db.components().insertComponent(newFileDto(directory));
  272. db.components().insertSnapshot(branch, s -> s.setProjectVersion("1.1"));
  273. ShowWsResponse response = ws.newRequest()
  274. .setParam(PARAM_COMPONENT, file.getKey())
  275. .setParam(PARAM_PULL_REQUEST, pullRequest)
  276. .executeProtobuf(ShowWsResponse.class);
  277. assertThat(response.getComponent())
  278. .extracting(Component::getKey, Component::getPullRequest, Component::getVersion)
  279. .containsExactlyInAnyOrder(file.getKey(), pullRequest, "1.1");
  280. assertThat(response.getAncestorsList()).extracting(Component::getKey, Component::getPullRequest, Component::getVersion)
  281. .containsExactlyInAnyOrder(
  282. tuple(directory.getKey(), pullRequest, "1.1"),
  283. tuple(module.getKey(), pullRequest, "1.1"),
  284. tuple(branch.getKey(), pullRequest, "1.1"));
  285. }
  286. @Test
  287. public void throw_ForbiddenException_if_user_doesnt_have_browse_permission_on_project() {
  288. userSession.logIn();
  289. expectedException.expect(ForbiddenException.class);
  290. db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert(), "project-uuid"));
  291. newRequest("project-uuid", null);
  292. }
  293. @Test
  294. public void fail_if_component_does_not_exist() {
  295. expectedException.expect(NotFoundException.class);
  296. expectedException.expectMessage("Component id 'unknown-uuid' not found");
  297. newRequest("unknown-uuid", null);
  298. }
  299. @Test
  300. public void fail_if_component_is_removed() {
  301. userSession.logIn().setRoot();
  302. ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization()));
  303. db.components().insertComponent(newFileDto(project).setDbKey("file-key").setEnabled(false));
  304. expectedException.expect(NotFoundException.class);
  305. expectedException.expectMessage("Component key 'file-key' not found");
  306. newRequest(null, "file-key");
  307. }
  308. @Test
  309. public void fail_when_componentId_and_branch_params_are_used_together() {
  310. ComponentDto project = db.components().insertPrivateProject();
  311. userSession.addProjectPermission(UserRole.USER, project);
  312. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  313. expectedException.expect(IllegalArgumentException.class);
  314. expectedException.expectMessage("Parameter 'componentId' cannot be used at the same time as 'branch' or 'pullRequest'");
  315. ws.newRequest()
  316. .setParam(PARAM_COMPONENT_ID, branch.uuid())
  317. .setParam(PARAM_BRANCH, "my_branch")
  318. .execute();
  319. }
  320. @Test
  321. public void fail_if_branch_does_not_exist() {
  322. ComponentDto project = db.components().insertPrivateProject();
  323. ComponentDto file = db.components().insertComponent(newFileDto(project));
  324. userSession.addProjectPermission(UserRole.USER, project);
  325. db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  326. expectedException.expect(NotFoundException.class);
  327. expectedException.expectMessage(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
  328. ws.newRequest()
  329. .setParam(PARAM_COMPONENT, file.getKey())
  330. .setParam(PARAM_BRANCH, "another_branch")
  331. .execute();
  332. }
  333. @Test
  334. public void fail_when_using_branch_db_key() {
  335. ComponentDto project = db.components().insertMainBranch();
  336. userSession.addProjectPermission(UserRole.USER, project);
  337. ComponentDto branch = db.components().insertProjectBranch(project);
  338. expectedException.expect(NotFoundException.class);
  339. expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
  340. ws.newRequest()
  341. .setParam(PARAM_COMPONENT, branch.getDbKey())
  342. .executeProtobuf(ShowWsResponse.class);
  343. }
  344. @Test
  345. public void fail_when_using_branch_uuid() {
  346. ComponentDto project = db.components().insertMainBranch();
  347. userSession.addProjectPermission(UserRole.USER, project);
  348. ComponentDto branch = db.components().insertProjectBranch(project);
  349. expectedException.expect(NotFoundException.class);
  350. expectedException.expectMessage(String.format("Component id '%s' not found", branch.uuid()));
  351. ws.newRequest()
  352. .setParam(PARAM_COMPONENT_ID, branch.uuid())
  353. .executeProtobuf(ShowWsResponse.class);
  354. }
  355. private ShowWsResponse newRequest(@Nullable String uuid, @Nullable String key) {
  356. TestRequest request = ws.newRequest();
  357. if (uuid != null) {
  358. request.setParam(PARAM_COMPONENT_ID, uuid);
  359. }
  360. if (key != null) {
  361. request.setParam(PARAM_COMPONENT, key);
  362. }
  363. return request.executeProtobuf(ShowWsResponse.class);
  364. }
  365. private void insertJsonExampleComponentsAndSnapshots() {
  366. OrganizationDto organizationDto = db.organizations().insertForKey("my-org-1");
  367. ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organizationDto, "AVIF98jgA3Ax6PH2efOW")
  368. .setDbKey("com.sonarsource:java-markdown")
  369. .setName("Java Markdown")
  370. .setDescription("Java Markdown Project")
  371. .setQualifier(Qualifiers.PROJECT)
  372. .setTagsString("language, plugin"));
  373. db.components().insertSnapshot(project, snapshot -> snapshot
  374. .setProjectVersion("1.1")
  375. .setCreatedAt(parseDateTime("2017-03-01T11:39:03+0100").getTime())
  376. .setPeriodDate(parseDateTime("2017-01-01T11:39:03+0100").getTime()));
  377. ComponentDto directory = newDirectory(project, "AVIF-FfgA3Ax6PH2efPF", "src/main/java/com/sonarsource/markdown/impl")
  378. .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl")
  379. .setName("src/main/java/com/sonarsource/markdown/impl")
  380. .setQualifier(Qualifiers.DIRECTORY);
  381. db.components().insertComponent(directory);
  382. db.components().insertComponent(
  383. newFileDto(directory, directory, "AVIF-FffA3Ax6PH2efPD")
  384. .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/Rule.java")
  385. .setName("Rule.java")
  386. .setPath("src/main/java/com/sonarsource/markdown/impl/Rule.java")
  387. .setLanguage("java")
  388. .setQualifier(Qualifiers.FILE));
  389. }
  390. }