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.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.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;
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;
45 public class ProjectsActionTest {
48 public ExpectedException expectedException = ExpectedException.none();
50 public DbTester db = DbTester.create(System2.INSTANCE);
52 public UserSessionRule userSession = UserSessionRule.standalone();
54 private WsActionTester ws = new WsActionTester(new ProjectsAction(db.getDbClient(), userSession));
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);
68 .setParam(PARAM_KEY, qualityProfile.getKee())
69 .setParam("selected", "selected")
71 .assertJson("{\"results\":\n" +
74 " \"key\": \"" + project1.getKey() + "\",\n" +
75 " \"name\": \"" + project1.name() + "\",\n" +
76 " \"selected\": true\n" +
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);
92 .setParam(PARAM_KEY, qualityProfile.getKee())
93 .setParam("selected", "selected")
94 .setParam(PAGE_SIZE, "2")
100 " \"key\": \"" + project4.getKey() + "\",\n" +
101 " \"selected\": true\n" +
104 " \"key\": \"" + project1.getKey() + "\",\n" +
105 " \"selected\": true\n" +
110 .setParam(PARAM_KEY, qualityProfile.getKee())
111 .setParam("selected", "selected")
112 .setParam(PAGE_SIZE, "2")
119 " \"key\": \"" + project3.getKey() + "\",\n" +
120 " \"selected\": true\n" +
123 " \"key\": \"" + project2.getKey() + "\",\n" +
124 " \"selected\": true\n" +
129 .setParam(PARAM_KEY, qualityProfile.getKee())
130 .setParam("selected", "selected")
131 .setParam(PAGE_SIZE, "2")
134 .assertJson("{\"results\":[]}");
136 .setParam(PARAM_KEY, qualityProfile.getKee())
137 .setParam("selected", "selected")
138 .setParam(PAGE_SIZE, "2")
141 .assertJson("{\"results\":[]}");
144 .setParam(PARAM_KEY, qualityProfile.getKee())
145 .setParam("selected", "selected")
146 .setParam(PAGE_SIZE, "3")
153 " \"key\": \"" + project4.getKey() + "\",\n" +
154 " \"selected\": true\n" +
157 " \"key\": \"" + project1.getKey() + "\",\n" +
158 " \"selected\": true\n" +
161 " \"key\": \"" + project3.getKey() + "\",\n" +
162 " \"selected\": true\n" +
167 .setParam(PARAM_KEY, qualityProfile.getKee())
168 .setParam("selected", "selected")
169 .setParam(PAGE_SIZE, "3")
176 " \"key\": \"" + project2.getKey() + "\",\n" +
177 " \"selected\": true\n" +
182 .setParam(PARAM_KEY, qualityProfile.getKee())
183 .setParam("selected", "selected")
184 .setParam(PAGE_SIZE, "3")
187 .assertJson("{\"results\":[]}");
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);
198 .setParam(PARAM_KEY, qualityProfile.getKee())
199 .setParam("selected", "deselected")
201 .assertJson("{ \"results\":\n" +
204 " \"key\": \"" + project2.getKey() + "\",\n" +
205 " \"selected\": false\n" +
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);
223 .setParam(PARAM_KEY, qualityProfile1.getKee())
224 .setParam("selected", "all")
227 " \"results\": [\n" +
229 " \"key\": \"" + project1.getKey() + "\",\n" +
230 " \"selected\": true\n" +
233 " \"key\": \"" + project2.getKey() + "\",\n" +
234 " \"selected\": true\n" +
237 " \"key\": \"" + project3.getKey() + "\",\n" +
238 " \"selected\": false\n" +
241 " \"key\": \"" + project4.getKey() + "\",\n" +
242 " \"selected\": false\n" +
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);
257 .setParam(PARAM_KEY, qualityProfile.getKee())
258 .setParam("selected", "all")
259 .setParam(TEXT_QUERY, "project t")
265 " \"key\": \"" + project3.getKey() + "\",\n" +
266 " \"name\": \"" + project3.getName() + "\",\n" +
267 " \"selected\": false\n" +
270 " \"key\": \"" + project2.getKey() + "\",\n" +
271 " \"name\": \"" + project2.getName() + "\",\n" +
272 " \"selected\": true\n" +
278 public void return_deprecated_uuid_field() {
279 ProjectDto project = db.components().insertPublicProjectDto();
280 QProfileDto qualityProfile = db.qualityProfiles().insert();
281 associateProjectsWithProfile(qualityProfile, project);
284 .setParam(PARAM_KEY, qualityProfile.getKee())
285 .setParam("selected", "all")
287 .assertJson("{\"results\":\n" +
290 " \"id\": \"" + project.getUuid() + "\",\n" +
291 " \"key\": \"" + project.getKey() + "\",\n" +
297 public void fail_on_nonexistent_profile() {
298 expectedException.expect(NotFoundException.class);
301 .setParam(PARAM_KEY, "unknown")
306 public void definition() {
307 WebService.Action definition = ws.getDef();
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");
319 private void associateProjectsWithProfile(QProfileDto profile, ProjectDto... projects) {
320 for (ProjectDto project : projects) {
321 db.getDbClient().qualityProfileDao().insertProjectProfileAssociation(db.getSession(), project, profile);