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 java.util.Collection;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.MockitoAnnotations;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.api.rule.Severity;
32 import org.sonar.api.server.ws.WebService;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.permission.GlobalPermission;
37 import org.sonar.db.qualityprofile.QProfileDto;
38 import org.sonar.db.rule.RuleDefinitionDto;
39 import org.sonar.db.rule.RuleTesting;
40 import org.sonar.db.user.UserDto;
41 import org.sonar.server.exceptions.BadRequestException;
42 import org.sonar.server.exceptions.ForbiddenException;
43 import org.sonar.server.exceptions.UnauthorizedException;
44 import org.sonar.server.qualityprofile.QProfileRules;
45 import org.sonar.server.qualityprofile.RuleActivation;
46 import org.sonar.server.tester.UserSessionRule;
47 import org.sonar.server.ws.TestRequest;
48 import org.sonar.server.ws.TestResponse;
49 import org.sonar.server.ws.WsActionTester;
51 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
52 import static org.assertj.core.api.Assertions.assertThat;
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 public ExpectedException expectedException = ExpectedException.none();
70 private ArgumentCaptor<Collection<RuleActivation>> ruleActivationCaptor = ArgumentCaptor.forClass(Collection.class);
71 private DbClient dbClient = db.getDbClient();
72 private QProfileRules qProfileRules = mock(QProfileRules.class);
73 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
75 private WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, qProfileRules, userSession, wsSupport));
78 public void before() {
79 MockitoAnnotations.initMocks(this);
83 public void define_activate_rule_action() {
84 WebService.Action definition = ws.getDef();
85 assertThat(definition).isNotNull();
86 assertThat(definition.isPost()).isTrue();
87 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "key", "reset", "rule", "params");
91 public 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 expectedException.expect(UnauthorizedException.class);
103 public void fail_if_not_organization_quality_profile_administrator() {
104 userSession.logIn(db.users().insertUser());
105 QProfileDto qualityProfile = db.qualityProfiles().insert();
106 TestRequest request = ws.newRequest()
108 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
109 .setParam(PARAM_KEY, qualityProfile.getKee());
111 expectedException.expect(ForbiddenException.class);
117 public void fail_activate_if_built_in_profile() {
118 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
120 QProfileDto qualityProfile = db.qualityProfiles().insert(profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo"));
121 TestRequest request = ws.newRequest()
123 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
124 .setParam(PARAM_KEY, qualityProfile.getKee());
126 expectedException.expect(BadRequestException.class);
127 expectedException.expectMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
133 public void fail_activate_external_rule() {
134 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
135 QProfileDto qualityProfile = db.qualityProfiles().insert();
136 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
138 TestRequest request = ws.newRequest()
140 .setParam(PARAM_RULE, rule.getKey().toString())
141 .setParam(PARAM_KEY, qualityProfile.getKee());
143 expectedException.expect(BadRequestException.class);
144 expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
150 public void activate_rule() {
151 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
152 QProfileDto qualityProfile = db.qualityProfiles().insert();
153 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
154 TestRequest request = ws.newRequest()
156 .setParam(PARAM_RULE, rule.getKey().toString())
157 .setParam(PARAM_KEY, qualityProfile.getKee())
158 .setParam("severity", "BLOCKER")
159 .setParam("params", "key1=v1;key2=v2")
160 .setParam("reset", "false");
162 TestResponse response = request.execute();
164 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
165 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
167 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
168 assertThat(activations).hasSize(1);
170 RuleActivation activation = activations.iterator().next();
171 assertThat(activation.getRuleUuid()).isEqualTo(rule.getUuid());
172 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
173 assertThat(activation.isReset()).isFalse();
177 public void as_qprofile_editor() {
178 UserDto user = db.users().insertUser();
179 QProfileDto qualityProfile = db.qualityProfiles().insert();
180 db.qualityProfiles().addUserPermission(qualityProfile, user);
181 userSession.logIn(user);
182 RuleKey ruleKey = RuleTesting.randomRuleKey();
183 db.rules().insert(ruleKey);
187 .setParam(PARAM_RULE, ruleKey.toString())
188 .setParam(PARAM_KEY, qualityProfile.getKee())
189 .setParam("severity", "BLOCKER")
190 .setParam("params", "key1=v1;key2=v2")
191 .setParam("reset", "false")
194 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());