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.

UpdateKeyActionTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.project.ws;
  21. import java.util.Optional;
  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.server.ws.WebService;
  27. import org.sonar.api.server.ws.WebService.Param;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.api.web.UserRole;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.component.ComponentTesting;
  34. import org.sonar.server.component.ComponentService;
  35. import org.sonar.server.es.ProjectIndexers;
  36. import org.sonar.server.es.ProjectIndexersImpl;
  37. import org.sonar.server.exceptions.ForbiddenException;
  38. import org.sonar.server.exceptions.NotFoundException;
  39. import org.sonar.server.project.ProjectLifeCycleListenersImpl;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.WsActionTester;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_FROM;
  45. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_TO;
  46. public class UpdateKeyActionTest {
  47. private static final String ANOTHER_KEY = "another_key";
  48. @Rule
  49. public ExpectedException expectedException = ExpectedException.none();
  50. @Rule
  51. public DbTester db = DbTester.create(System2.INSTANCE);
  52. @Rule
  53. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  54. private DbClient dbClient = db.getDbClient();
  55. private ProjectIndexers projectIndexers = new ProjectIndexersImpl();
  56. private ComponentService componentService = new ComponentService(dbClient, userSessionRule, projectIndexers, new ProjectLifeCycleListenersImpl());
  57. private WsActionTester ws = new WsActionTester(new UpdateKeyAction(dbClient, componentService));
  58. @Test
  59. public void update_key_of_project_referenced_by_its_key() {
  60. ComponentDto project = insertProject();
  61. userSessionRule.addProjectPermission(UserRole.ADMIN, project);
  62. call(project.getKey(), ANOTHER_KEY);
  63. assertThat(selectByKey(project.getKey()).isPresent()).isFalse();
  64. assertThat(selectByKey(ANOTHER_KEY).get().uuid()).isEqualTo(project.uuid());
  65. }
  66. @Test
  67. public void update_key_of_disabled_module() {
  68. ComponentDto project = insertProject();
  69. ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project).setEnabled(false));
  70. userSessionRule.addProjectPermission(UserRole.ADMIN, project);
  71. call(module.getKey(), ANOTHER_KEY);
  72. assertThat(selectByKey(project.getKey()).isPresent()).isTrue();
  73. assertThat(selectByKey(module.getKey()).isPresent()).isFalse();
  74. ComponentDto loadedModule = selectByKey(ANOTHER_KEY).get();
  75. assertThat(loadedModule.uuid()).isEqualTo(module.uuid());
  76. assertThat(loadedModule.isEnabled()).isFalse();
  77. }
  78. @Test
  79. public void fail_if_not_authorized() {
  80. ComponentDto project = insertProject();
  81. userSessionRule.addProjectPermission(UserRole.USER, project);
  82. expectedException.expect(ForbiddenException.class);
  83. expectedException.expectMessage("Insufficient privileges");
  84. call(project.getKey(), ANOTHER_KEY);
  85. }
  86. @Test
  87. public void fail_if_new_key_is_not_provided() {
  88. ComponentDto project = insertProject();
  89. userSessionRule.addProjectPermission(UserRole.ADMIN, project);
  90. expectedException.expect(IllegalArgumentException.class);
  91. expectedException.expectMessage("The 'to' parameter is missing");
  92. call(project.getKey(), null);
  93. }
  94. @Test
  95. public void fail_if_key_not_provided() {
  96. expectedException.expect(IllegalArgumentException.class);
  97. expectedException.expectMessage("The 'from' parameter is missing");
  98. call(null, ANOTHER_KEY);
  99. }
  100. @Test
  101. public void fail_if_project_does_not_exist() {
  102. expectedException.expect(NotFoundException.class);
  103. call("UNKNOWN_UUID", ANOTHER_KEY);
  104. }
  105. @Test
  106. public void fail_when_using_branch_db_key() throws Exception {
  107. ComponentDto project = db.components().insertMainBranch();
  108. ComponentDto branch = db.components().insertProjectBranch(project);
  109. userSessionRule.addProjectPermission(UserRole.ADMIN, project);
  110. expectedException.expect(NotFoundException.class);
  111. expectedException.expectMessage("Component not found");
  112. call(branch.getDbKey(), ANOTHER_KEY);
  113. }
  114. @Test
  115. public void api_definition() {
  116. WebService.Action definition = ws.getDef();
  117. assertThat(definition.since()).isEqualTo("6.1");
  118. assertThat(definition.isPost()).isTrue();
  119. assertThat(definition.key()).isEqualTo("update_key");
  120. assertThat(definition.changelog()).hasSize(1);
  121. assertThat(definition.params())
  122. .hasSize(2)
  123. .extracting(Param::key)
  124. .containsOnlyOnce("from", "to");
  125. }
  126. private ComponentDto insertProject() {
  127. return db.components().insertComponent(ComponentTesting.newPrivateProjectDto(db.organizations().insert()));
  128. }
  129. private String call(@Nullable String key, @Nullable String newKey) {
  130. TestRequest request = ws.newRequest();
  131. if (key != null) {
  132. request.setParam(PARAM_FROM, key);
  133. }
  134. if (newKey != null) {
  135. request.setParam(PARAM_TO, newKey);
  136. }
  137. return request.execute().getInput();
  138. }
  139. private Optional<ComponentDto> selectByKey(String key) {
  140. return db.getDbClient().componentDao().selectByKey(db.getSession(), key);
  141. }
  142. }