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.Captor;
30 import org.mockito.MockitoAnnotations;
31 import org.sonar.api.rule.RuleKey;
32 import org.sonar.api.rule.Severity;
33 import org.sonar.api.server.ws.WebService;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.organization.OrganizationDto;
38 import org.sonar.db.permission.OrganizationPermission;
39 import org.sonar.db.qualityprofile.QProfileDto;
40 import org.sonar.db.rule.RuleDefinitionDto;
41 import org.sonar.db.rule.RuleTesting;
42 import org.sonar.db.user.UserDto;
43 import org.sonar.server.exceptions.BadRequestException;
44 import org.sonar.server.exceptions.ForbiddenException;
45 import org.sonar.server.exceptions.UnauthorizedException;
46 import org.sonar.server.organization.TestDefaultOrganizationProvider;
47 import org.sonar.server.qualityprofile.QProfileRules;
48 import org.sonar.server.qualityprofile.RuleActivation;
49 import org.sonar.server.tester.UserSessionRule;
50 import org.sonar.server.ws.TestRequest;
51 import org.sonar.server.ws.TestResponse;
52 import org.sonar.server.ws.WsActionTester;
54 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.mockito.ArgumentMatchers.any;
57 import static org.mockito.ArgumentMatchers.anyCollection;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.verify;
60 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
61 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
62 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
64 public class ActivateRuleActionTest {
67 public DbTester db = DbTester.create();
69 public UserSessionRule userSession = UserSessionRule.standalone();
71 public ExpectedException expectedException = ExpectedException.none();
73 public ArgumentCaptor<Collection<RuleActivation>> ruleActivationCaptor;
75 private DbClient dbClient = db.getDbClient();
76 private QProfileRules qProfileRules = mock(QProfileRules.class);
77 private QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db));
79 private WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, qProfileRules, userSession, wsSupport));
81 private OrganizationDto defaultOrganization;
82 private OrganizationDto organization;
85 public void before() {
86 MockitoAnnotations.initMocks(this);
87 defaultOrganization = db.getDefaultOrganization();
88 organization = db.organizations().insert();
92 public void define_activate_rule_action() {
93 WebService.Action definition = ws.getDef();
94 assertThat(definition).isNotNull();
95 assertThat(definition.isPost()).isTrue();
96 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "key", "reset", "rule", "params");
100 public void fail_if_not_logged_in() {
101 TestRequest request = ws.newRequest()
103 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
104 .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
106 expectedException.expect(UnauthorizedException.class);
112 public void fail_if_not_organization_quality_profile_administrator() {
113 userSession.logIn(db.users().insertUser());
114 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
115 TestRequest request = ws.newRequest()
117 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
118 .setParam(PARAM_KEY, qualityProfile.getKee());
120 expectedException.expect(ForbiddenException.class);
126 public void fail_activate_if_built_in_profile() {
127 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
129 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo"));
130 TestRequest request = ws.newRequest()
132 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
133 .setParam(PARAM_KEY, qualityProfile.getKee());
135 expectedException.expect(BadRequestException.class);
136 expectedException.expectMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
142 public void fail_activate_external_rule() {
143 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
144 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
145 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
147 TestRequest request = ws.newRequest()
149 .setParam(PARAM_RULE, rule.getKey().toString())
150 .setParam(PARAM_KEY, qualityProfile.getKee());
152 expectedException.expect(BadRequestException.class);
153 expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
159 public void activate_rule_in_default_organization() {
160 userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
161 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
162 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
163 TestRequest request = ws.newRequest()
165 .setParam(PARAM_RULE, rule.getKey().toString())
166 .setParam(PARAM_KEY, qualityProfile.getKee())
167 .setParam("severity", "BLOCKER")
168 .setParam("params", "key1=v1;key2=v2")
169 .setParam("reset", "false");
171 TestResponse response = request.execute();
173 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
174 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
176 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
177 assertThat(activations).hasSize(1);
179 RuleActivation activation = activations.iterator().next();
180 assertThat(activation.getRuleId()).isEqualTo(rule.getId());
181 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
182 assertThat(activation.isReset()).isFalse();
186 public void activate_rule_in_specific_organization() {
187 userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
188 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
189 RuleKey ruleKey = RuleTesting.randomRuleKey();
190 Integer ruleId = db.rules().insert(ruleKey).getId();
191 TestRequest request = ws.newRequest()
193 .setParam(PARAM_RULE, ruleKey.toString())
194 .setParam(PARAM_KEY, qualityProfile.getKee())
195 .setParam("severity", "BLOCKER")
196 .setParam("params", "key1=v1;key2=v2")
197 .setParam("reset", "false");
199 TestResponse response = request.execute();
201 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
202 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
204 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
205 assertThat(activations).hasSize(1);
206 RuleActivation activation = activations.iterator().next();
207 assertThat(activation.getRuleId()).isEqualTo(ruleId);
208 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
209 assertThat(activation.isReset()).isFalse();
213 public void as_qprofile_editor() {
214 UserDto user = db.users().insertUser();
215 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
216 db.qualityProfiles().addUserPermission(qualityProfile, user);
217 userSession.logIn(user);
218 RuleKey ruleKey = RuleTesting.randomRuleKey();
219 db.rules().insert(ruleKey);
223 .setParam(PARAM_RULE, ruleKey.toString())
224 .setParam(PARAM_KEY, qualityProfile.getKee())
225 .setParam("severity", "BLOCKER")
226 .setParam("params", "key1=v1;key2=v2")
227 .setParam("reset", "false")
230 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());