3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.project.ws;
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.utils.System2;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.ComponentDbTester;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.organization.OrganizationDto;
35 import org.sonar.server.component.ComponentFinder;
36 import org.sonar.server.component.ComponentService;
37 import org.sonar.server.component.TestComponentFinder;
38 import org.sonar.server.es.EsTester;
39 import org.sonar.server.exceptions.BadRequestException;
40 import org.sonar.server.exceptions.ForbiddenException;
41 import org.sonar.server.exceptions.NotFoundException;
42 import org.sonar.server.tester.UserSessionRule;
43 import org.sonar.server.ws.TestRequest;
44 import org.sonar.server.ws.WsActionTester;
45 import org.sonarqube.ws.Projects.BulkUpdateKeyWsResponse;
46 import org.sonarqube.ws.Projects.BulkUpdateKeyWsResponse.Key;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.assertj.core.api.Assertions.tuple;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.verify;
54 import static org.sonar.db.component.ComponentTesting.newFileDto;
55 import static org.sonar.db.component.ComponentTesting.newModuleDto;
56 import static org.sonar.test.JsonAssert.assertJson;
57 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_DRY_RUN;
58 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_FROM;
59 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT;
60 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_TO;
62 public class BulkUpdateKeyActionTest {
63 private static final String MY_PROJECT_KEY = "my_project";
64 private static final String FROM = "my_";
65 private static final String TO = "your_";
67 private System2 system2 = System2.INSTANCE;
70 public ExpectedException expectedException = ExpectedException.none();
72 public UserSessionRule userSession = UserSessionRule.standalone().logIn().setRoot();
74 public EsTester es = EsTester.create();
76 public DbTester db = DbTester.create(system2);
78 private ComponentDbTester componentDb = new ComponentDbTester(db);
79 private DbClient dbClient = db.getDbClient();
80 private ComponentFinder componentFinder = TestComponentFinder.from(db);
81 private ComponentService componentService = mock(ComponentService.class);
82 private WsActionTester ws = new WsActionTester(
83 new BulkUpdateKeyAction(dbClient, componentFinder, componentService, userSession));
86 public void json_example() {
87 OrganizationDto organizationDto = db.organizations().insert();
88 ComponentDto project = componentDb.insertPrivateProject(organizationDto, c -> c.setDbKey("my_project"));
89 componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:module_1"));
90 ComponentDto anotherProject = componentDb.insertPrivateProject(organizationDto, c -> c.setDbKey("another_project"));
91 componentDb.insertComponent(newModuleDto(anotherProject).setDbKey("my_new_project:module_1"));
92 ComponentDto module2 = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:module_2"));
93 componentDb.insertComponent(newFileDto(module2, null));
95 String result = ws.newRequest()
96 .setParam(PARAM_PROJECT, "my_project")
97 .setParam(PARAM_FROM, "my_")
98 .setParam(PARAM_TO, "my_new_")
99 .setParam(PARAM_DRY_RUN, String.valueOf(true))
100 .execute().getInput();
102 assertJson(result).withStrictArrayOrder().isSimilarTo(getClass().getResource("bulk_update_key-example.json"));
106 public void dry_run_by_key() {
109 BulkUpdateKeyWsResponse result = callDryRunByKey(MY_PROJECT_KEY, FROM, TO);
111 assertThat(result.getKeysCount()).isEqualTo(1);
112 assertThat(result.getKeys(0).getNewKey()).isEqualTo("your_project");
116 public void bulk_update_project_key() {
117 ComponentDto project = insertMyProject();
118 ComponentDto module = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:root:module"));
119 ComponentDto inactiveModule = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:root:inactive_module").setEnabled(false));
120 ComponentDto file = componentDb.insertComponent(newFileDto(module, null).setDbKey("my_project:root:module:src/File.xoo"));
121 ComponentDto inactiveFile = componentDb.insertComponent(newFileDto(module, null).setDbKey("my_project:root:module:src/InactiveFile.xoo").setEnabled(false));
123 BulkUpdateKeyWsResponse result = callByKey(project.getDbKey(), FROM, TO);
125 assertThat(result.getKeysCount()).isEqualTo(2);
126 assertThat(result.getKeysList()).extracting(Key::getKey, Key::getNewKey, Key::getDuplicate)
128 tuple(project.getDbKey(), "your_project", false),
129 tuple(module.getDbKey(), "your_project:root:module", false));
131 verify(componentService).bulkUpdateKey(any(DbSession.class), eq(componentDb.getProjectDto(project)), eq(FROM), eq(TO));
135 public void bulk_update_provisioned_project_key() {
136 String newKey = "provisionedProject2";
137 ComponentDto provisionedProject = componentDb.insertPrivateProject();
139 callByKey(provisionedProject.getDbKey(), provisionedProject.getDbKey(), newKey);
141 verify(componentService).bulkUpdateKey(any(DbSession.class), eq(componentDb.getProjectDto(provisionedProject)), eq(provisionedProject.getDbKey()), eq(newKey));
145 public void fail_to_bulk_update_key_using_branch_db_key() {
146 ComponentDto project = db.components().insertPrivateProject();
147 ComponentDto branch = db.components().insertProjectBranch(project);
148 userSession.addProjectPermission(UserRole.USER, project);
150 expectedException.expect(NotFoundException.class);
151 expectedException.expectMessage(String.format("Project '%s' not found", branch.getDbKey()));
153 callByKey(branch.getDbKey(), FROM, TO);
157 public void fail_to_bulk_if_a_component_already_exists_with_the_same_key() {
158 componentDb.insertPrivateProject(db.getDefaultOrganization(), c -> c.setDbKey("my_project"));
159 componentDb.insertPrivateProject(db.getDefaultOrganization(), c -> c.setDbKey("your_project"));
161 expectedException.expect(BadRequestException.class);
162 expectedException.expectMessage("Impossible to update key: a component with key \"your_project\" already exists.");
164 callByKey("my_project", "my_", "your_");
168 public void fail_to_bulk_update_with_invalid_new_key() {
171 expectedException.expect(IllegalArgumentException.class);
172 expectedException.expectMessage("Malformed key for 'my aproject'. Project key cannot be empty nor contain whitespaces.");
174 callByKey(MY_PROJECT_KEY, FROM, "my a");
178 public void fail_to_dry_bulk_update_with_invalid_new_key() {
181 expectedException.expect(IllegalArgumentException.class);
182 expectedException.expectMessage("Malformed key for 'my aproject'. Project key cannot be empty nor contain whitespaces.");
184 callDryRunByKey(MY_PROJECT_KEY, FROM, "my a");
188 public void fail_to_bulk_update_if_not_project_or_module() {
189 ComponentDto project = insertMyProject();
190 ComponentDto file = componentDb.insertComponent(newFileDto(project, null));
192 expectedException.expect(NotFoundException.class);
193 expectedException.expectMessage(String.format("Project '%s' not found", file.getDbKey()));
195 callByKey(file.getDbKey(), FROM, TO);
199 public void fail_if_from_string_is_not_provided() {
200 expectedException.expect(IllegalArgumentException.class);
202 ComponentDto project = insertMyProject();
204 callDryRunByKey(project.getDbKey(), null, TO);
208 public void fail_if_to_string_is_not_provided() {
209 expectedException.expect(IllegalArgumentException.class);
211 ComponentDto project = insertMyProject();
213 callDryRunByKey(project.getDbKey(), FROM, null);
217 public void fail_if_key_not_provided() {
218 expectedException.expect(IllegalArgumentException.class);
220 call(null, FROM, TO, false);
224 public void fail_if_project_does_not_exist() {
225 expectedException.expect(NotFoundException.class);
227 callDryRunByKey("UNKNOWN_KEY", FROM, TO);
231 public void throw_ForbiddenException_if_not_project_administrator() {
233 ComponentDto project = insertMyProject();
235 expectedException.expect(ForbiddenException.class);
237 callDryRunByKey(project.getDbKey(), FROM, TO);
241 public void fail_when_using_branch_db_key() {
242 ComponentDto project = db.components().insertPrivateProject();
243 userSession.logIn().addProjectPermission(UserRole.USER, project);
244 ComponentDto branch = db.components().insertProjectBranch(project);
246 expectedException.expect(NotFoundException.class);
247 expectedException.expectMessage(String.format("Project '%s' not found", branch.getDbKey()));
249 callByKey(branch.getDbKey(), FROM, TO);
253 public void api_definition() {
254 WebService.Action definition = ws.getDef();
256 assertThat(definition.isPost()).isTrue();
257 assertThat(definition.since()).isEqualTo("6.1");
258 assertThat(definition.key()).isEqualTo("bulk_update_key");
259 assertThat(definition.params())
261 .extracting(WebService.Param::key)
262 .containsOnlyOnce("project", "from", "to", "dryRun");
265 private ComponentDto insertMyProject() {
266 return componentDb.insertPublicProject(db.organizations().insert(), c -> c.setDbKey(MY_PROJECT_KEY));
269 private BulkUpdateKeyWsResponse callDryRunByKey(@Nullable String key, @Nullable String from, @Nullable String to) {
270 return call(key, from, to, true);
273 private BulkUpdateKeyWsResponse callByKey(@Nullable String key, @Nullable String from, @Nullable String to) {
274 return call(key, from, to, false);
277 private BulkUpdateKeyWsResponse call(@Nullable String key, @Nullable String from, @Nullable String to, @Nullable Boolean dryRun) {
278 TestRequest request = ws.newRequest();
281 request.setParam(PARAM_PROJECT, key);
284 request.setParam(PARAM_FROM, from);
287 request.setParam(PARAM_TO, to);
289 if (dryRun != null) {
290 request.setParam(PARAM_DRY_RUN, String.valueOf(dryRun));
293 return request.executeProtobuf(BulkUpdateKeyWsResponse.class);