3 * Copyright (C) 2009-2022 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.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
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.RuleDefinitionDto;
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.lang.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_RULE;
61 public class ActivateRuleActionTest {
64 public DbTester db = DbTester.create();
66 public UserSessionRule userSession = UserSessionRule.standalone();
68 private final ArgumentCaptor<Collection<RuleActivation>> ruleActivationCaptor = ArgumentCaptor.forClass(Collection.class);
69 private final DbClient dbClient = db.getDbClient();
70 private final QProfileRules qProfileRules = mock(QProfileRules.class);
71 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
73 private final WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, qProfileRules, userSession, wsSupport));
76 public void before() {
77 MockitoAnnotations.initMocks(this);
81 public void define_activate_rule_action() {
82 WebService.Action definition = ws.getDef();
83 assertThat(definition).isNotNull();
84 assertThat(definition.isPost()).isTrue();
85 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "key", "reset", "rule", "params");
89 public void fail_if_not_logged_in() {
90 TestRequest request = ws.newRequest()
92 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
93 .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
95 assertThatThrownBy(() -> request.execute())
96 .isInstanceOf(UnauthorizedException.class);
100 public void fail_if_not_global_quality_profile_administrator() {
101 userSession.logIn(db.users().insertUser());
102 QProfileDto qualityProfile = db.qualityProfiles().insert();
103 TestRequest request = ws.newRequest()
105 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
106 .setParam(PARAM_KEY, qualityProfile.getKee());
108 assertThatThrownBy(() -> request.execute())
109 .isInstanceOf(ForbiddenException.class);
113 public void fail_activate_if_built_in_profile() {
114 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
116 QProfileDto qualityProfile = db.qualityProfiles().insert(profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo"));
117 TestRequest request = ws.newRequest()
119 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
120 .setParam(PARAM_KEY, qualityProfile.getKee());
122 assertThatThrownBy(() -> request.execute())
123 .isInstanceOf(BadRequestException.class)
124 .hasMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
128 public void fail_activate_external_rule() {
129 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
130 QProfileDto qualityProfile = db.qualityProfiles().insert();
131 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
133 TestRequest request = ws.newRequest()
135 .setParam(PARAM_RULE, rule.getKey().toString())
136 .setParam(PARAM_KEY, qualityProfile.getKee());
138 assertThatThrownBy(() -> request.execute())
139 .isInstanceOf(BadRequestException.class)
140 .hasMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
144 public void activate_rule() {
145 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
146 QProfileDto qualityProfile = db.qualityProfiles().insert();
147 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
148 TestRequest request = ws.newRequest()
150 .setParam(PARAM_RULE, rule.getKey().toString())
151 .setParam(PARAM_KEY, qualityProfile.getKee())
152 .setParam("severity", "BLOCKER")
153 .setParam("params", "key1=v1;key2=v2")
154 .setParam("reset", "false");
156 TestResponse response = request.execute();
158 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
159 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
161 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
162 assertThat(activations).hasSize(1);
164 RuleActivation activation = activations.iterator().next();
165 assertThat(activation.getRuleUuid()).isEqualTo(rule.getUuid());
166 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
167 assertThat(activation.isReset()).isFalse();
171 public void as_qprofile_editor() {
172 UserDto user = db.users().insertUser();
173 QProfileDto qualityProfile = db.qualityProfiles().insert();
174 db.qualityProfiles().addUserPermission(qualityProfile, user);
175 userSession.logIn(user);
176 RuleKey ruleKey = RuleTesting.randomRuleKey();
177 db.rules().insert(ruleKey);
181 .setParam(PARAM_RULE, ruleKey.toString())
182 .setParam(PARAM_KEY, qualityProfile.getKee())
183 .setParam("severity", "BLOCKER")
184 .setParam("params", "key1=v1;key2=v2")
185 .setParam("reset", "false")
188 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());