3 * Copyright (C) 2009-2024 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 java.util.Collection;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.RegisterExtension;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.MockitoAnnotations;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.api.rule.Severity;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.permission.GlobalPermission;
36 import org.sonar.db.qualityprofile.QProfileDto;
37 import org.sonar.db.rule.RuleDto;
38 import org.sonar.db.rule.RuleTesting;
39 import org.sonar.db.user.UserDto;
40 import org.sonar.server.exceptions.BadRequestException;
41 import org.sonar.server.exceptions.ForbiddenException;
42 import org.sonar.server.exceptions.UnauthorizedException;
43 import org.sonar.server.qualityprofile.QProfileRules;
44 import org.sonar.server.qualityprofile.RuleActivation;
45 import org.sonar.server.tester.UserSessionRule;
46 import org.sonar.server.ws.TestRequest;
47 import org.sonar.server.ws.TestResponse;
48 import org.sonar.server.ws.WsActionTester;
50 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatThrownBy;
53 import static org.mockito.ArgumentMatchers.any;
54 import static org.mockito.ArgumentMatchers.anyCollection;
55 import static org.mockito.Mockito.mock;
56 import static org.mockito.Mockito.verify;
57 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
58 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
59 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PRIORITIZED_RULE;
60 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
62 class ActivateRuleActionIT {
65 public DbTester db = DbTester.create();
67 public UserSessionRule userSession = UserSessionRule.standalone();
69 private final ArgumentCaptor<Collection<RuleActivation>> ruleActivationCaptor = ArgumentCaptor.forClass(Collection.class);
70 private final DbClient dbClient = db.getDbClient();
71 private final QProfileRules qProfileRules = mock(QProfileRules.class);
72 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
74 private final WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, qProfileRules, userSession, wsSupport));
78 MockitoAnnotations.initMocks(this);
82 void define_activate_rule_action() {
83 WebService.Action definition = ws.getDef();
84 assertThat(definition).isNotNull();
85 assertThat(definition.isPost()).isTrue();
86 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "prioritizedRule", "key",
87 "reset", "rule", "params");
91 void fail_if_not_logged_in() {
92 TestRequest request = ws.newRequest()
94 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
95 .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
97 assertThatThrownBy(() -> request.execute())
98 .isInstanceOf(UnauthorizedException.class);
102 void fail_if_not_global_quality_profile_administrator() {
103 userSession.logIn(db.users().insertUser());
104 QProfileDto qualityProfile = db.qualityProfiles().insert();
105 TestRequest request = ws.newRequest()
107 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
108 .setParam(PARAM_KEY, qualityProfile.getKee());
110 assertThatThrownBy(() -> request.execute())
111 .isInstanceOf(ForbiddenException.class);
115 void fail_activate_if_built_in_profile() {
116 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
118 QProfileDto qualityProfile = db.qualityProfiles().insert(profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage(
120 TestRequest request = ws.newRequest()
122 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
123 .setParam(PARAM_KEY, qualityProfile.getKee());
125 assertThatThrownBy(() -> request.execute())
126 .isInstanceOf(BadRequestException.class)
127 .hasMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
131 void fail_activate_external_rule() {
132 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
133 QProfileDto qualityProfile = db.qualityProfiles().insert();
134 RuleDto rule = db.rules().insert(r -> r.setIsExternal(true));
136 TestRequest request = ws.newRequest()
138 .setParam(PARAM_RULE, rule.getKey().toString())
139 .setParam(PARAM_KEY, qualityProfile.getKee());
141 assertThatThrownBy(() -> request.execute())
142 .isInstanceOf(BadRequestException.class)
143 .hasMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
147 void activate_rule() {
148 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
149 QProfileDto qualityProfile = db.qualityProfiles().insert();
150 RuleDto rule = db.rules().insert(RuleTesting.randomRuleKey());
151 TestRequest request = ws.newRequest()
153 .setParam(PARAM_RULE, rule.getKey().toString())
154 .setParam(PARAM_KEY, qualityProfile.getKee())
155 .setParam("severity", "BLOCKER")
156 .setParam(PARAM_PRIORITIZED_RULE, "true")
157 .setParam("params", "key1=v1;key2=v2")
158 .setParam("reset", "false");
160 TestResponse response = request.execute();
162 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
163 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
165 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
166 assertThat(activations).hasSize(1);
168 RuleActivation activation = activations.iterator().next();
169 assertThat(activation.getRuleUuid()).isEqualTo(rule.getUuid());
170 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
171 assertThat(activation.isPrioritizedRule()).isTrue();
172 assertThat(activation.isReset()).isFalse();
176 void as_qprofile_editor() {
177 UserDto user = db.users().insertUser();
178 QProfileDto qualityProfile = db.qualityProfiles().insert();
179 db.qualityProfiles().addUserPermission(qualityProfile, user);
180 userSession.logIn(user);
181 RuleKey ruleKey = RuleTesting.randomRuleKey();
182 db.rules().insert(ruleKey);
186 .setParam(PARAM_RULE, ruleKey.toString())
187 .setParam(PARAM_KEY, qualityProfile.getKee())
188 .setParam("severity", "BLOCKER")
189 .setParam("params", "key1=v1;key2=v2")
190 .setParam("reset", "false")
193 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());