]> source.dussan.org Git - sonarqube.git/blob
a85345e71cee50c9c1a76b3f9cadafb9daf6280b
[sonarqube.git] /
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.project.ws;
21
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;
47
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;
61
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_";
66
67   private System2 system2 = System2.INSTANCE;
68
69   @Rule
70   public ExpectedException expectedException = ExpectedException.none();
71   @Rule
72   public UserSessionRule userSession = UserSessionRule.standalone().logIn().setRoot();
73   @Rule
74   public EsTester es = EsTester.create();
75   @Rule
76   public DbTester db = DbTester.create(system2);
77
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));
84
85   @Test
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));
94
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();
101
102     assertJson(result).withStrictArrayOrder().isSimilarTo(getClass().getResource("bulk_update_key-example.json"));
103   }
104
105   @Test
106   public void dry_run_by_key() {
107     insertMyProject();
108
109     BulkUpdateKeyWsResponse result = callDryRunByKey(MY_PROJECT_KEY, FROM, TO);
110
111     assertThat(result.getKeysCount()).isEqualTo(1);
112     assertThat(result.getKeys(0).getNewKey()).isEqualTo("your_project");
113   }
114
115   @Test
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));
122
123     BulkUpdateKeyWsResponse result = callByKey(project.getDbKey(), FROM, TO);
124
125     assertThat(result.getKeysCount()).isEqualTo(2);
126     assertThat(result.getKeysList()).extracting(Key::getKey, Key::getNewKey, Key::getDuplicate)
127       .containsExactly(
128         tuple(project.getDbKey(), "your_project", false),
129         tuple(module.getDbKey(), "your_project:root:module", false));
130
131     verify(componentService).bulkUpdateKey(any(DbSession.class), eq(componentDb.getProjectDto(project)), eq(FROM), eq(TO));
132   }
133
134   @Test
135   public void bulk_update_provisioned_project_key() {
136     String newKey = "provisionedProject2";
137     ComponentDto provisionedProject = componentDb.insertPrivateProject();
138
139     callByKey(provisionedProject.getDbKey(), provisionedProject.getDbKey(), newKey);
140
141     verify(componentService).bulkUpdateKey(any(DbSession.class), eq(componentDb.getProjectDto(provisionedProject)), eq(provisionedProject.getDbKey()), eq(newKey));
142   }
143
144   @Test
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);
149
150     expectedException.expect(NotFoundException.class);
151     expectedException.expectMessage(String.format("Project '%s' not found", branch.getDbKey()));
152
153     callByKey(branch.getDbKey(), FROM, TO);
154   }
155
156   @Test
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"));
160
161     expectedException.expect(BadRequestException.class);
162     expectedException.expectMessage("Impossible to update key: a component with key \"your_project\" already exists.");
163
164     callByKey("my_project", "my_", "your_");
165   }
166
167   @Test
168   public void fail_to_bulk_update_with_invalid_new_key() {
169     insertMyProject();
170
171     expectedException.expect(IllegalArgumentException.class);
172     expectedException.expectMessage("Malformed key for 'my aproject'. Project key cannot be empty nor contain whitespaces.");
173
174     callByKey(MY_PROJECT_KEY, FROM, "my a");
175   }
176
177   @Test
178   public void fail_to_dry_bulk_update_with_invalid_new_key() {
179     insertMyProject();
180
181     expectedException.expect(IllegalArgumentException.class);
182     expectedException.expectMessage("Malformed key for 'my aproject'. Project key cannot be empty nor contain whitespaces.");
183
184     callDryRunByKey(MY_PROJECT_KEY, FROM, "my a");
185   }
186
187   @Test
188   public void fail_to_bulk_update_if_not_project_or_module() {
189     ComponentDto project = insertMyProject();
190     ComponentDto file = componentDb.insertComponent(newFileDto(project, null));
191
192     expectedException.expect(NotFoundException.class);
193     expectedException.expectMessage(String.format("Project '%s' not found", file.getDbKey()));
194
195     callByKey(file.getDbKey(), FROM, TO);
196   }
197
198   @Test
199   public void fail_if_from_string_is_not_provided() {
200     expectedException.expect(IllegalArgumentException.class);
201
202     ComponentDto project = insertMyProject();
203
204     callDryRunByKey(project.getDbKey(), null, TO);
205   }
206
207   @Test
208   public void fail_if_to_string_is_not_provided() {
209     expectedException.expect(IllegalArgumentException.class);
210
211     ComponentDto project = insertMyProject();
212
213     callDryRunByKey(project.getDbKey(), FROM, null);
214   }
215
216   @Test
217   public void fail_if_key_not_provided() {
218     expectedException.expect(IllegalArgumentException.class);
219
220     call(null, FROM, TO, false);
221   }
222
223   @Test
224   public void fail_if_project_does_not_exist() {
225     expectedException.expect(NotFoundException.class);
226
227     callDryRunByKey("UNKNOWN_KEY", FROM, TO);
228   }
229
230   @Test
231   public void throw_ForbiddenException_if_not_project_administrator() {
232     userSession.logIn();
233     ComponentDto project = insertMyProject();
234
235     expectedException.expect(ForbiddenException.class);
236
237     callDryRunByKey(project.getDbKey(), FROM, TO);
238   }
239
240   @Test
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);
245
246     expectedException.expect(NotFoundException.class);
247     expectedException.expectMessage(String.format("Project '%s' not found", branch.getDbKey()));
248
249     callByKey(branch.getDbKey(), FROM, TO);
250   }
251
252   @Test
253   public void api_definition() {
254     WebService.Action definition = ws.getDef();
255
256     assertThat(definition.isPost()).isTrue();
257     assertThat(definition.since()).isEqualTo("6.1");
258     assertThat(definition.key()).isEqualTo("bulk_update_key");
259     assertThat(definition.params())
260       .hasSize(4)
261       .extracting(WebService.Param::key)
262       .containsOnlyOnce("project", "from", "to", "dryRun");
263   }
264
265   private ComponentDto insertMyProject() {
266     return componentDb.insertPublicProject(db.organizations().insert(), c -> c.setDbKey(MY_PROJECT_KEY));
267   }
268
269   private BulkUpdateKeyWsResponse callDryRunByKey(@Nullable String key, @Nullable String from, @Nullable String to) {
270     return call(key, from, to, true);
271   }
272
273   private BulkUpdateKeyWsResponse callByKey(@Nullable String key, @Nullable String from, @Nullable String to) {
274     return call(key, from, to, false);
275   }
276
277   private BulkUpdateKeyWsResponse call(@Nullable String key, @Nullable String from, @Nullable String to, @Nullable Boolean dryRun) {
278     TestRequest request = ws.newRequest();
279
280     if (key != null) {
281       request.setParam(PARAM_PROJECT, key);
282     }
283     if (from != null) {
284       request.setParam(PARAM_FROM, from);
285     }
286     if (to != null) {
287       request.setParam(PARAM_TO, to);
288     }
289     if (dryRun != null) {
290       request.setParam(PARAM_DRY_RUN, String.valueOf(dryRun));
291     }
292
293     return request.executeProtobuf(BulkUpdateKeyWsResponse.class);
294   }
295 }