]> source.dussan.org Git - sonarqube.git/blob
11369776308b6b18d86e3eb229d1953b2f2fb8d4
[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.qualityprofile.ws;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.Param;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.db.project.ProjectDto;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.tester.UserSessionRule;
36 import org.sonar.server.ws.WsActionTester;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.sonar.api.server.ws.WebService.Param.PAGE;
40 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
41 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
42 import static org.sonar.api.web.UserRole.USER;
43 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
44
45 public class ProjectsActionTest {
46
47   @Rule
48   public ExpectedException expectedException = ExpectedException.none();
49   @Rule
50   public DbTester db = DbTester.create(System2.INSTANCE);
51   @Rule
52   public UserSessionRule userSession = UserSessionRule.standalone();
53
54   private WsActionTester ws = new WsActionTester(new ProjectsAction(db.getDbClient(), userSession));
55
56   @Test
57   public void list_authorized_projects_only() {
58     ComponentDto project1 = db.components().insertPrivateProject();
59     ComponentDto project2 = db.components().insertPrivateProject();
60     QProfileDto qualityProfile = db.qualityProfiles().insert();
61     associateProjectsWithProfile(qualityProfile, db.components().getProjectDto(project1), db.components().getProjectDto(project2));
62     // user only sees project1
63     UserDto user = db.users().insertUser();
64     db.users().insertProjectPermissionOnUser(user, USER, project1);
65     userSession.logIn(user);
66
67     ws.newRequest()
68       .setParam(PARAM_KEY, qualityProfile.getKee())
69       .setParam("selected", "selected")
70       .execute()
71       .assertJson("{\"results\":\n" +
72         "  [\n" +
73         "    {\n" +
74         "      \"key\": \"" + project1.getKey() + "\",\n" +
75         "      \"name\": \"" + project1.name() + "\",\n" +
76         "      \"selected\": true\n" +
77         "    }\n" +
78         "  ]}");
79   }
80
81   @Test
82   public void paginate() {
83     OrganizationDto organization = db.organizations().getDefaultOrganization();
84     ProjectDto project1 = db.components().insertPublicProjectDto(organization, p -> p.setName("Project One"));
85     ProjectDto project2 = db.components().insertPublicProjectDto(organization, p -> p.setName("Project Two"));
86     ProjectDto project3 = db.components().insertPublicProjectDto(organization, p -> p.setName("Project Three"));
87     ProjectDto project4 = db.components().insertPublicProjectDto(organization, p -> p.setName("Project Four"));
88     QProfileDto qualityProfile = db.qualityProfiles().insert();
89     associateProjectsWithProfile(qualityProfile, project1, project2, project3, project4);
90
91     ws.newRequest()
92       .setParam(PARAM_KEY, qualityProfile.getKee())
93       .setParam("selected", "selected")
94       .setParam(PAGE_SIZE, "2")
95       .execute()
96       .assertJson("{\n" +
97         "  \"results\":\n" +
98         "  [\n" +
99         "    {\n" +
100         "      \"key\": \"" + project4.getKey() + "\",\n" +
101         "      \"selected\": true\n" +
102         "    },\n" +
103         "    {\n" +
104         "      \"key\": \"" + project1.getKey() + "\",\n" +
105         "      \"selected\": true\n" +
106         "    }\n" +
107         "  ]\n" +
108         "}\n");
109     ws.newRequest()
110       .setParam(PARAM_KEY, qualityProfile.getKee())
111       .setParam("selected", "selected")
112       .setParam(PAGE_SIZE, "2")
113       .setParam(PAGE, "2")
114       .execute()
115       .assertJson("{\n" +
116         "  \"results\":\n" +
117         "  [\n" +
118         "    {\n" +
119         "      \"key\": \"" + project3.getKey() + "\",\n" +
120         "      \"selected\": true\n" +
121         "    },\n" +
122         "    {\n" +
123         "      \"key\": \"" + project2.getKey() + "\",\n" +
124         "      \"selected\": true\n" +
125         "    }\n" +
126         "  ]\n" +
127         "}\n");
128     ws.newRequest()
129       .setParam(PARAM_KEY, qualityProfile.getKee())
130       .setParam("selected", "selected")
131       .setParam(PAGE_SIZE, "2")
132       .setParam(PAGE, "3")
133       .execute()
134       .assertJson("{\"results\":[]}");
135     ws.newRequest()
136       .setParam(PARAM_KEY, qualityProfile.getKee())
137       .setParam("selected", "selected")
138       .setParam(PAGE_SIZE, "2")
139       .setParam(PAGE, "4")
140       .execute()
141       .assertJson("{\"results\":[]}");
142
143     ws.newRequest()
144       .setParam(PARAM_KEY, qualityProfile.getKee())
145       .setParam("selected", "selected")
146       .setParam(PAGE_SIZE, "3")
147       .setParam(PAGE, "1")
148       .execute()
149       .assertJson("{\n" +
150         "  \"results\":\n" +
151         "  [\n" +
152         "    {\n" +
153         "      \"key\": \"" + project4.getKey() + "\",\n" +
154         "      \"selected\": true\n" +
155         "    },\n" +
156         "    {\n" +
157         "      \"key\": \"" + project1.getKey() + "\",\n" +
158         "      \"selected\": true\n" +
159         "    },\n" +
160         "    {\n" +
161         "      \"key\": \"" + project3.getKey() + "\",\n" +
162         "      \"selected\": true\n" +
163         "    }\n" +
164         "  ]\n" +
165         "}\n");
166     ws.newRequest()
167       .setParam(PARAM_KEY, qualityProfile.getKee())
168       .setParam("selected", "selected")
169       .setParam(PAGE_SIZE, "3")
170       .setParam(PAGE, "2")
171       .execute()
172       .assertJson("{\n" +
173         "  \"results\":\n" +
174         "  [\n" +
175         "    {\n" +
176         "      \"key\": \"" + project2.getKey() + "\",\n" +
177         "      \"selected\": true\n" +
178         "    }\n" +
179         "  ]\n" +
180         "}\n");
181     ws.newRequest()
182       .setParam(PARAM_KEY, qualityProfile.getKee())
183       .setParam("selected", "selected")
184       .setParam(PAGE_SIZE, "3")
185       .setParam(PAGE, "3")
186       .execute()
187       .assertJson("{\"results\":[]}");
188   }
189
190   @Test
191   public void show_unselected() {
192     ProjectDto project1 = db.components().insertPublicProjectDto();
193     ProjectDto project2 = db.components().insertPublicProjectDto();
194     QProfileDto qualityProfile = db.qualityProfiles().insert();
195     associateProjectsWithProfile(qualityProfile, project1);
196
197     ws.newRequest()
198       .setParam(PARAM_KEY, qualityProfile.getKee())
199       .setParam("selected", "deselected")
200       .execute()
201       .assertJson("{ \"results\":\n" +
202         "  [\n" +
203         "    {\n" +
204         "      \"key\": \"" + project2.getKey() + "\",\n" +
205         "      \"selected\": false\n" +
206         "    }\n" +
207         "  ]}");
208   }
209
210   @Test
211   public void show_all() {
212     ProjectDto project1 = db.components().insertPublicProjectDto();
213     ProjectDto project2 = db.components().insertPublicProjectDto();
214     ProjectDto project3 = db.components().insertPublicProjectDto();
215     ProjectDto project4 = db.components().insertPublicProjectDto();
216     QProfileDto qualityProfile1 = db.qualityProfiles().insert();
217     associateProjectsWithProfile(qualityProfile1, project1, project2);
218     QProfileDto qualityProfile2 = db.qualityProfiles().insert();
219     // project3 is associated with P2, must appear as not associated with xooP1
220     associateProjectsWithProfile(qualityProfile2, project3);
221
222     ws.newRequest()
223       .setParam(PARAM_KEY, qualityProfile1.getKee())
224       .setParam("selected", "all")
225       .execute()
226       .assertJson("{\n" +
227         "  \"results\": [\n" +
228         "    {\n" +
229         "      \"key\": \"" + project1.getKey() + "\",\n" +
230         "      \"selected\": true\n" +
231         "    },\n" +
232         "    {\n" +
233         "      \"key\": \"" + project2.getKey() + "\",\n" +
234         "      \"selected\": true\n" +
235         "    },\n" +
236         "    {\n" +
237         "      \"key\": \"" + project3.getKey() + "\",\n" +
238         "      \"selected\": false\n" +
239         "    },\n" +
240         "    {\n" +
241         "      \"key\": \"" + project4.getKey() + "\",\n" +
242         "      \"selected\": false\n" +
243         "    }\n" +
244         "  ]}\n");
245   }
246
247   @Test
248   public void filter_on_name() {
249     ProjectDto project1 = db.components().insertPublicProjectDto(p -> p.setName("Project One"));
250     ProjectDto project2 = db.components().insertPublicProjectDto(p -> p.setName("Project Two"));
251     ProjectDto project3 = db.components().insertPublicProjectDto(p -> p.setName("Project Three"));
252     ProjectDto project4 = db.components().insertPublicProjectDto(p -> p.setName("Project Four"));
253     QProfileDto qualityProfile = db.qualityProfiles().insert();
254     associateProjectsWithProfile(qualityProfile, project1, project2);
255
256     ws.newRequest()
257       .setParam(PARAM_KEY, qualityProfile.getKee())
258       .setParam("selected", "all")
259       .setParam(TEXT_QUERY, "project t")
260       .execute()
261       .assertJson("{\n" +
262         "  \"results\":\n" +
263         "  [\n" +
264         "    {\n" +
265         "      \"key\": \"" + project3.getKey() + "\",\n" +
266         "      \"name\": \"" + project3.getName() + "\",\n" +
267         "      \"selected\": false\n" +
268         "    },\n" +
269         "    {\n" +
270         "      \"key\": \"" + project2.getKey() + "\",\n" +
271         "      \"name\": \"" + project2.getName() + "\",\n" +
272         "      \"selected\": true\n" +
273         "    }\n" +
274         "  ]}\n");
275   }
276
277   @Test
278   public void return_deprecated_uuid_field() {
279     ProjectDto project = db.components().insertPublicProjectDto();
280     QProfileDto qualityProfile = db.qualityProfiles().insert();
281     associateProjectsWithProfile(qualityProfile, project);
282
283     ws.newRequest()
284       .setParam(PARAM_KEY, qualityProfile.getKee())
285       .setParam("selected", "all")
286       .execute()
287       .assertJson("{\"results\":\n" +
288         "  [\n" +
289         "    {\n" +
290         "      \"id\": \"" + project.getUuid() + "\",\n" +
291         "      \"key\": \"" + project.getKey() + "\",\n" +
292         "    }\n" +
293         "  ]}");
294   }
295
296   @Test
297   public void fail_on_nonexistent_profile() {
298     expectedException.expect(NotFoundException.class);
299
300     ws.newRequest()
301       .setParam(PARAM_KEY, "unknown")
302       .execute();
303   }
304
305   @Test
306   public void definition() {
307     WebService.Action definition = ws.getDef();
308
309     assertThat(definition.key()).isEqualTo("projects");
310     assertThat(definition.responseExampleAsString()).isNotEmpty();
311     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("key", "p", "ps", "q", "selected");
312     Param profile = definition.param("key");
313     assertThat(profile.deprecatedKey()).isNullOrEmpty();
314     assertThat(definition.param("p")).isNotNull();
315     assertThat(definition.param("ps")).isNotNull();
316     Param query = definition.param("q");
317   }
318
319   private void associateProjectsWithProfile(QProfileDto profile, ProjectDto... projects) {
320     for (ProjectDto project : projects) {
321       db.getDbClient().qualityProfileDao().insertProjectProfileAssociation(db.getSession(), project, profile);
322     }
323     db.commit();
324   }
325 }