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 java.net.HttpURLConnection;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.resources.Languages;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.api.server.ws.WebService.Param;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.util.UuidFactoryFast;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.project.ProjectDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.exceptions.UnauthorizedException;
41 import org.sonar.server.language.LanguageTesting;
42 import org.sonar.server.qualityprofile.QProfileFactoryImpl;
43 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.TestResponse;
46 import org.sonar.server.ws.WsActionTester;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.mockito.Mockito.mock;
50 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
51 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
52 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
53 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
55 public class DeleteActionTest {
57 private static final String A_LANGUAGE = "xoo";
60 public DbTester db = DbTester.create(System2.INSTANCE);
62 public ExpectedException expectedException = ExpectedException.none();
64 public UserSessionRule userSession = UserSessionRule.standalone();
66 private DbClient dbClient = db.getDbClient();
67 private DbSession dbSession = db.getSession();
68 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
70 private DeleteAction underTest = new DeleteAction(
71 new Languages(LanguageTesting.newLanguage(A_LANGUAGE)),
72 new QProfileFactoryImpl(dbClient, UuidFactoryFast.getInstance(), System2.INSTANCE, activeRuleIndexer), dbClient, userSession,
73 new QProfileWsSupport(dbClient, userSession));
74 private WsActionTester ws = new WsActionTester(underTest);
77 public void delete_profile_by_language_and_name_in_default_organization() {
78 ProjectDto project = db.components().insertPrivateProjectDto();
79 QProfileDto profile1 = createProfile();
80 QProfileDto profile2 = createProfile();
81 db.qualityProfiles().associateWithProject(project, profile1);
83 logInAsQProfileAdministrator();
85 TestResponse response = ws.newRequest()
87 .setParam(PARAM_LANGUAGE, profile1.getLanguage())
88 .setParam(PARAM_QUALITY_PROFILE, profile1.getName())
91 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
93 verifyProfileDoesNotExist(profile1);
94 verifyProfileExists(profile2);
98 public void delete_profile_by_language_and_name_in_specified_organization() {
99 ProjectDto project = db.components().insertPrivateProjectDto();
100 QProfileDto profile1 = createProfile();
101 QProfileDto profile2 = createProfile();
102 db.qualityProfiles().associateWithProject(project, profile1);
103 logInAsQProfileAdministrator();
105 TestResponse response = ws.newRequest()
107 .setParam(PARAM_LANGUAGE, profile1.getLanguage())
108 .setParam(PARAM_QUALITY_PROFILE, profile1.getName())
110 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
112 verifyProfileDoesNotExist(profile1);
113 verifyProfileExists(profile2);
117 public void as_qprofile_editor() {
118 QProfileDto profile = createProfile();
119 UserDto user = db.users().insertUser();
120 db.qualityProfiles().addUserPermission(profile, user);
121 userSession.logIn(user);
123 TestResponse response = ws.newRequest()
125 .setParam(PARAM_LANGUAGE, profile.getLanguage())
126 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
128 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
130 verifyProfileDoesNotExist(profile);
134 public void fail_if_built_in_profile() {
135 QProfileDto profile1 = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setLanguage(A_LANGUAGE));
136 logInAsQProfileAdministrator();
138 expectedException.expect(BadRequestException.class);
142 .setParam(PARAM_LANGUAGE, profile1.getLanguage())
143 .setParam(PARAM_QUALITY_PROFILE, profile1.getName())
148 public void fail_if_not_profile_administrator() {
149 QProfileDto qprofile = createProfile();
150 userSession.logIn(db.users().insertUser());
152 expectedException.expect(ForbiddenException.class);
153 expectedException.expectMessage("Insufficient privileges");
157 .setParam(PARAM_LANGUAGE, qprofile.getLanguage())
158 .setParam(PARAM_QUALITY_PROFILE, qprofile.getName())
163 public void fail_if_not_logged_in() {
164 QProfileDto profile = createProfile();
166 expectedException.expect(UnauthorizedException.class);
170 .setParam(PARAM_KEY, profile.getKee())
175 public void fail_if_missing_parameters() {
178 expectedException.expect(IllegalArgumentException.class);
179 expectedException.expectMessage("The 'language' parameter is missing");
187 public void fail_if_missing_language_parameter() {
188 QProfileDto profile = createProfile();
189 logInAsQProfileAdministrator();
191 expectedException.expect(IllegalArgumentException.class);
192 expectedException.expectMessage("The 'language' parameter is missing");
196 .setParam("profileName", profile.getName())
201 public void fail_if_missing_name_parameter() {
202 QProfileDto profile = createProfile();
203 logInAsQProfileAdministrator();
205 expectedException.expect(IllegalArgumentException.class);
206 expectedException.expectMessage("The 'qualityProfile' parameter is missing");
210 .setParam(PARAM_LANGUAGE, profile.getLanguage())
215 public void fail_if_profile_does_not_exist() {
218 expectedException.expect(NotFoundException.class);
219 expectedException.expectMessage("Quality Profile for language 'xoo' and name 'does_not_exist' does not exist");
223 .setParam(PARAM_QUALITY_PROFILE, "does_not_exist")
224 .setParam(PARAM_LANGUAGE, "xoo")
229 public void fail_if_deleting_default_profile() {
230 QProfileDto profile = createProfile();
231 db.qualityProfiles().setAsDefault(profile);
232 logInAsQProfileAdministrator();
234 expectedException.expect(IllegalArgumentException.class);
235 expectedException.expectMessage("Profile '" + profile.getName() + "' cannot be deleted because it is marked as default");
239 .setParam(PARAM_LANGUAGE, profile.getLanguage())
240 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
245 public void fail_if_a_descendant_is_marked_as_default() {
246 QProfileDto parentProfile = createProfile();
247 QProfileDto childProfile = db.qualityProfiles().insert(p -> p.setLanguage(A_LANGUAGE).setParentKee(parentProfile.getKee()));
248 db.qualityProfiles().setAsDefault(childProfile);
249 logInAsQProfileAdministrator();
251 expectedException.expect(IllegalArgumentException.class);
252 expectedException.expectMessage("Profile '" + parentProfile.getName() + "' cannot be deleted because its descendant named '" + childProfile.getName() +
253 "' is marked as default");
257 .setParam(PARAM_LANGUAGE, parentProfile.getLanguage())
258 .setParam(PARAM_QUALITY_PROFILE, parentProfile.getName())
263 public void definition() {
264 WebService.Action definition = ws.getDef();
266 assertThat(definition.isPost()).isTrue();
267 assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
270 private void logInAsQProfileAdministrator() {
272 .logIn(db.users().insertUser())
273 .addPermission(ADMINISTER_QUALITY_PROFILES);
276 private void verifyProfileDoesNotExist(QProfileDto profile) {
277 assertThat(dbClient.qualityProfileDao().selectByUuid(dbSession, profile.getKee())).isNull();
278 assertThat(dbClient.qualityProfileDao().selectSelectedProjects(dbSession, profile, null)).isEmpty();
281 private void verifyProfileExists(QProfileDto profile) {
282 assertThat(dbClient.qualityProfileDao().selectByUuid(dbSession, profile.getKee())).isNotNull();
285 private QProfileDto createProfile() {
286 return db.qualityProfiles().insert(p -> p.setLanguage(A_LANGUAGE));