3 * Copyright (C) 2009-2019 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.qualityprofile.ws;
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.qualityprofile.QProfileDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.organization.TestDefaultOrganizationProvider;
36 import org.sonar.server.tester.UserSessionRule;
37 import org.sonar.server.ws.WsActionTester;
39 import static java.lang.String.format;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.sonar.api.server.ws.WebService.Param.PAGE;
42 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
43 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
44 import static org.sonar.api.web.UserRole.USER;
45 import static org.sonar.db.organization.OrganizationDto.Subscription.PAID;
46 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
48 public class ProjectsActionTest {
51 public ExpectedException expectedException = ExpectedException.none();
53 public DbTester db = DbTester.create(System2.INSTANCE);
55 public UserSessionRule userSession = UserSessionRule.standalone();
57 private WsActionTester ws = new WsActionTester(
58 new ProjectsAction(db.getDbClient(), userSession, new QProfileWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db))));
61 public void list_authorized_projects_only() {
62 OrganizationDto organization = db.organizations().insert();
63 ComponentDto project1 = db.components().insertPrivateProject(organization);
64 ComponentDto project2 = db.components().insertPrivateProject(organization);
65 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
66 associateProjectsWithProfile(qualityProfile, project1, project2);
67 // user only sees project1
68 UserDto user = db.users().insertUser();
69 db.users().insertProjectPermissionOnUser(user, USER, project1);
70 userSession.logIn(user);
73 .setParam(PARAM_KEY, qualityProfile.getKee())
74 .setParam("selected", "selected")
76 .assertJson("{\"results\":\n" +
79 " \"key\": \"" + project1.getKey() + "\",\n" +
80 " \"name\": \"" + project1.name() + "\",\n" +
81 " \"selected\": true\n" +
87 public void paginate() {
88 OrganizationDto organization = db.organizations().insert();
89 ComponentDto project1 = db.components().insertPublicProject(organization, p -> p.setName("Project One"));
90 ComponentDto project2 = db.components().insertPublicProject(organization, p -> p.setName("Project Two"));
91 ComponentDto project3 = db.components().insertPublicProject(organization, p -> p.setName("Project Three"));
92 ComponentDto project4 = db.components().insertPublicProject(organization, p -> p.setName("Project Four"));
93 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
94 associateProjectsWithProfile(qualityProfile, project1, project2, project3, project4);
97 .setParam(PARAM_KEY, qualityProfile.getKee())
98 .setParam("selected", "selected")
99 .setParam(PAGE_SIZE, "2")
105 " \"key\": \"" + project4.getKey() + "\",\n" +
106 " \"selected\": true\n" +
109 " \"key\": \"" + project1.getKey() + "\",\n" +
110 " \"selected\": true\n" +
115 .setParam(PARAM_KEY, qualityProfile.getKee())
116 .setParam("selected", "selected")
117 .setParam(PAGE_SIZE, "2")
124 " \"key\": \"" + project3.getKey() + "\",\n" +
125 " \"selected\": true\n" +
128 " \"key\": \"" + project2.getKey() + "\",\n" +
129 " \"selected\": true\n" +
134 .setParam(PARAM_KEY, qualityProfile.getKee())
135 .setParam("selected", "selected")
136 .setParam(PAGE_SIZE, "2")
139 .assertJson("{\"results\":[]}");
141 .setParam(PARAM_KEY, qualityProfile.getKee())
142 .setParam("selected", "selected")
143 .setParam(PAGE_SIZE, "2")
146 .assertJson("{\"results\":[]}");
149 .setParam(PARAM_KEY, qualityProfile.getKee())
150 .setParam("selected", "selected")
151 .setParam(PAGE_SIZE, "3")
158 " \"key\": \"" + project4.getKey() + "\",\n" +
159 " \"selected\": true\n" +
162 " \"key\": \"" + project1.getKey() + "\",\n" +
163 " \"selected\": true\n" +
166 " \"key\": \"" + project3.getKey() + "\",\n" +
167 " \"selected\": true\n" +
172 .setParam(PARAM_KEY, qualityProfile.getKee())
173 .setParam("selected", "selected")
174 .setParam(PAGE_SIZE, "3")
181 " \"key\": \"" + project2.getKey() + "\",\n" +
182 " \"selected\": true\n" +
187 .setParam(PARAM_KEY, qualityProfile.getKee())
188 .setParam("selected", "selected")
189 .setParam(PAGE_SIZE, "3")
192 .assertJson("{\"results\":[]}");
196 public void show_unselected() {
197 OrganizationDto organization = db.organizations().insert();
198 ComponentDto project1 = db.components().insertPublicProject(organization);
199 ComponentDto project2 = db.components().insertPublicProject(organization);
200 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
201 associateProjectsWithProfile(qualityProfile, project1);
204 .setParam(PARAM_KEY, qualityProfile.getKee())
205 .setParam("selected", "deselected")
207 .assertJson("{ \"results\":\n" +
210 " \"key\": \"" + project2.getKey() + "\",\n" +
211 " \"selected\": false\n" +
217 public void show_all() {
218 OrganizationDto organization = db.organizations().insert();
219 ComponentDto project1 = db.components().insertPublicProject(organization, p -> p.setName("Project 1"));
220 ComponentDto project2 = db.components().insertPublicProject(organization, p -> p.setName("Project 2"));
221 ComponentDto project3 = db.components().insertPublicProject(organization, p -> p.setName("Project 3"));
222 ComponentDto project4 = db.components().insertPublicProject(organization, p -> p.setName("Project 4"));
223 QProfileDto qualityProfile1 = db.qualityProfiles().insert(organization);
224 associateProjectsWithProfile(qualityProfile1, project1, project2);
225 QProfileDto qualityProfile2 = db.qualityProfiles().insert(organization);
226 // project3 is associated with P2, must appear as not associated with xooP1
227 associateProjectsWithProfile(qualityProfile2, project3);
230 .setParam(PARAM_KEY, qualityProfile1.getKee())
231 .setParam("selected", "all")
234 " \"results\": [\n" +
236 " \"key\": \"" + project1.getKey() + "\",\n" +
237 " \"selected\": true\n" +
240 " \"key\": \"" + project2.getKey() + "\",\n" +
241 " \"selected\": true\n" +
244 " \"key\": \"" + project3.getKey() + "\",\n" +
245 " \"selected\": false\n" +
248 " \"key\": \"" + project4.getKey() + "\",\n" +
249 " \"selected\": false\n" +
255 public void filter_on_name() {
256 OrganizationDto organization = db.organizations().insert();
257 ComponentDto project1 = db.components().insertPublicProject(organization, p -> p.setName("Project One"));
258 ComponentDto project2 = db.components().insertPublicProject(organization, p -> p.setName("Project Two"));
259 ComponentDto project3 = db.components().insertPublicProject(organization, p -> p.setName("Project Three"));
260 ComponentDto project4 = db.components().insertPublicProject(organization, p -> p.setName("Project Four"));
261 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
262 associateProjectsWithProfile(qualityProfile, project1, project2);
265 .setParam(PARAM_KEY, qualityProfile.getKee())
266 .setParam("selected", "all")
267 .setParam(TEXT_QUERY, "project t")
273 " \"key\": \"" + project3.getKey() + "\",\n" +
274 " \"name\": \"Project Three\",\n" +
275 " \"selected\": false\n" +
278 " \"key\": \"" + project2.getKey() + "\",\n" +
279 " \"name\": \"Project Two\",\n" +
280 " \"selected\": true\n" +
286 public void return_deprecated_uuid_field() {
287 OrganizationDto organization = db.organizations().insert();
288 ComponentDto project = db.components().insertPublicProject(organization);
289 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
290 associateProjectsWithProfile(qualityProfile, project);
293 .setParam(PARAM_KEY, qualityProfile.getKee())
294 .setParam("selected", "all")
296 .assertJson("{\"results\":\n" +
299 " \"id\": \"" + project.uuid() + "\",\n" +
300 " \"key\": \"" + project.getKey() + "\",\n" +
306 public void projects_on_paid_organization() {
307 OrganizationDto organization = db.organizations().insert();
308 ComponentDto project = db.components().insertPublicProject(organization);
309 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
310 associateProjectsWithProfile(qualityProfile, project);
311 UserDto user = db.users().insertUser();
312 db.organizations().addMember(organization, user);
313 userSession.logIn(user);
316 .setParam(PARAM_KEY, qualityProfile.getKee())
317 .setParam("selected", "all")
319 .assertJson("{\"results\":\n" +
322 " \"key\": \"" + project.getKey() + "\",\n" +
328 public void fail_on_nonexistent_profile() {
329 expectedException.expect(NotFoundException.class);
332 .setParam(PARAM_KEY, "unknown")
337 public void fail_on_paid_organization_when_not_member() {
338 OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID));
339 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
341 expectedException.expect(ForbiddenException.class);
342 expectedException.expectMessage(format("You're not member of organization '%s'", organization.getKey()));
345 .setParam(PARAM_KEY, qualityProfile.getKee())
350 public void definition() {
351 WebService.Action definition = ws.getDef();
353 assertThat(definition.key()).isEqualTo("projects");
354 assertThat(definition.responseExampleAsString()).isNotEmpty();
355 assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("key", "p", "ps", "q", "selected");
356 Param profile = definition.param("key");
357 assertThat(profile.deprecatedKey()).isNullOrEmpty();
358 assertThat(definition.param("p")).isNotNull();
359 assertThat(definition.param("ps")).isNotNull();
360 Param query = definition.param("q");
361 assertThat(query.deprecatedKey()).isEqualTo("query");
364 private void associateProjectsWithProfile(QProfileDto profile, ComponentDto... projects) {
365 for (ComponentDto project : projects) {
366 db.getDbClient().qualityProfileDao().insertProjectProfileAssociation(db.getSession(), project, profile);