3 * Copyright (C) 2009-2019 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");
97 WebService.Param profileKey = definition.param("key");
98 assertThat(profileKey.deprecatedKey()).isEqualTo("profile_key");
99 WebService.Param ruleKey = definition.param("rule");
100 assertThat(ruleKey.deprecatedKey()).isEqualTo("rule_key");
104 public void fail_if_not_logged_in() {
105 TestRequest request = ws.newRequest()
107 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
108 .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
110 expectedException.expect(UnauthorizedException.class);
116 public void fail_if_not_organization_quality_profile_administrator() {
117 userSession.logIn(db.users().insertUser());
118 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
119 TestRequest request = ws.newRequest()
121 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
122 .setParam(PARAM_KEY, qualityProfile.getKee());
124 expectedException.expect(ForbiddenException.class);
130 public void fail_activate_if_built_in_profile() {
131 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
133 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo"));
134 TestRequest request = ws.newRequest()
136 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
137 .setParam(PARAM_KEY, qualityProfile.getKee());
139 expectedException.expect(BadRequestException.class);
140 expectedException.expectMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
146 public void fail_activate_external_rule() {
147 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
148 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
149 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
151 TestRequest request = ws.newRequest()
153 .setParam(PARAM_RULE, rule.getKey().toString())
154 .setParam(PARAM_KEY, qualityProfile.getKee());
156 expectedException.expect(BadRequestException.class);
157 expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
163 public void activate_rule_in_default_organization() {
164 userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
165 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
166 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
167 TestRequest request = ws.newRequest()
169 .setParam(PARAM_RULE, rule.getKey().toString())
170 .setParam(PARAM_KEY, qualityProfile.getKee())
171 .setParam("severity", "BLOCKER")
172 .setParam("params", "key1=v1;key2=v2")
173 .setParam("reset", "false");
175 TestResponse response = request.execute();
177 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
178 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
180 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
181 assertThat(activations).hasSize(1);
183 RuleActivation activation = activations.iterator().next();
184 assertThat(activation.getRuleId()).isEqualTo(rule.getId());
185 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
186 assertThat(activation.isReset()).isFalse();
190 public void activate_rule_in_specific_organization() {
191 userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
192 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
193 RuleKey ruleKey = RuleTesting.randomRuleKey();
194 Integer ruleId = db.rules().insert(ruleKey).getId();
195 TestRequest request = ws.newRequest()
197 .setParam(PARAM_RULE, ruleKey.toString())
198 .setParam(PARAM_KEY, qualityProfile.getKee())
199 .setParam("severity", "BLOCKER")
200 .setParam("params", "key1=v1;key2=v2")
201 .setParam("reset", "false");
203 TestResponse response = request.execute();
205 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
206 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
208 Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
209 assertThat(activations).hasSize(1);
210 RuleActivation activation = activations.iterator().next();
211 assertThat(activation.getRuleId()).isEqualTo(ruleId);
212 assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
213 assertThat(activation.isReset()).isFalse();
217 public void as_qprofile_editor() {
218 UserDto user = db.users().insertUser();
219 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
220 db.qualityProfiles().addUserPermission(qualityProfile, user);
221 userSession.logIn(user);
222 RuleKey ruleKey = RuleTesting.randomRuleKey();
223 db.rules().insert(ruleKey);
227 .setParam(PARAM_RULE, ruleKey.toString())
228 .setParam(PARAM_KEY, qualityProfile.getKee())
229 .setParam("severity", "BLOCKER")
230 .setParam("params", "key1=v1;key2=v2")
231 .setParam("reset", "false")
234 verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());