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.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.resources.Languages;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.api.rule.RuleStatus;
31 import org.sonar.api.rule.Severity;
32 import org.sonar.api.server.ws.WebService;
33 import org.sonar.api.server.ws.WebService.Param;
34 import org.sonar.api.utils.System2;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.organization.OrganizationDto;
39 import org.sonar.db.qualityprofile.ActiveRuleDto;
40 import org.sonar.db.qualityprofile.QProfileDto;
41 import org.sonar.db.rule.RuleDefinitionDto;
42 import org.sonar.db.rule.RuleTesting;
43 import org.sonar.db.user.UserDto;
44 import org.sonar.server.es.EsClient;
45 import org.sonar.server.es.EsTester;
46 import org.sonar.server.exceptions.ForbiddenException;
47 import org.sonar.server.exceptions.NotFoundException;
48 import org.sonar.server.organization.TestDefaultOrganizationProvider;
49 import org.sonar.server.qualityprofile.QProfileRules;
50 import org.sonar.server.qualityprofile.QProfileRulesImpl;
51 import org.sonar.server.qualityprofile.QProfileTree;
52 import org.sonar.server.qualityprofile.QProfileTreeImpl;
53 import org.sonar.server.qualityprofile.RuleActivation;
54 import org.sonar.server.qualityprofile.RuleActivator;
55 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
56 import org.sonar.server.rule.index.RuleIndex;
57 import org.sonar.server.rule.index.RuleIndexer;
58 import org.sonar.server.tester.UserSessionRule;
59 import org.sonar.server.util.TypeValidations;
60 import org.sonar.server.ws.WsActionTester;
61 import org.sonarqube.ws.Qualityprofiles.InheritanceWsResponse;
63 import static java.lang.String.format;
64 import static java.util.Arrays.asList;
65 import static java.util.Collections.singleton;
66 import static org.assertj.core.api.Assertions.assertThat;
67 import static org.sonar.db.organization.OrganizationDto.Subscription.PAID;
68 import static org.sonar.test.JsonAssert.assertJson;
69 import static org.sonarqube.ws.MediaTypes.PROTOBUF;
70 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
71 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
72 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
73 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
75 public class InheritanceActionTest {
78 public DbTester db = DbTester.create();
80 public EsTester es = EsTester.create();
82 public UserSessionRule userSession = UserSessionRule.standalone();
84 public ExpectedException expectedException = ExpectedException.none();
86 private DbClient dbClient = db.getDbClient();
87 private DbSession dbSession = db.getSession();
88 private EsClient esClient = es.client();
89 private RuleIndexer ruleIndexer = new RuleIndexer(esClient, dbClient);
90 private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(dbClient, esClient);
92 private RuleIndex ruleIndex = new RuleIndex(esClient, System2.INSTANCE);
93 private RuleActivator ruleActivator = new RuleActivator(System2.INSTANCE, dbClient, new TypeValidations(new ArrayList<>()), userSession);
94 private QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, ruleIndex, activeRuleIndexer);
95 private QProfileTree qProfileTree = new QProfileTreeImpl(dbClient, ruleActivator, System2.INSTANCE, activeRuleIndexer);
97 private WsActionTester ws = new WsActionTester(new InheritanceAction(
99 new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db)),
103 public void inheritance_nominal() {
104 OrganizationDto organization = db.organizations().insert();
105 RuleDefinitionDto rule1 = createRule("xoo", "rule1");
106 RuleDefinitionDto rule2 = createRule("xoo", "rule2");
107 RuleDefinitionDto rule3 = createRule("xoo", "rule3");
110 * sonar way (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2))
112 QProfileDto sonarway = db.qualityProfiles().insert(organization, p -> p.setKee("xoo-sonar-way").setLanguage("xoo").setName("Sonar way").setIsBuiltIn(true));
113 createActiveRule(rule1, sonarway);
114 createActiveRule(rule2, sonarway);
117 activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());
119 QProfileDto companyWide = createProfile(organization, "xoo", "My Company Profile", "xoo-my-company-profile-12345");
120 setParent(sonarway, companyWide);
122 QProfileDto buWide = createProfile(organization, "xoo", "My BU Profile", "xoo-my-bu-profile-23456");
123 setParent(companyWide, buWide);
124 overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL);
126 QProfileDto forProject1 = createProfile(organization, "xoo", "For Project One", "xoo-for-project-one-34567");
127 setParent(buWide, forProject1);
128 createActiveRule(rule3, forProject1);
130 activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());
132 QProfileDto forProject2 = createProfile(organization, "xoo", "For Project Two", "xoo-for-project-two-45678");
133 setParent(buWide, forProject2);
134 overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL);
136 String response = ws.newRequest()
137 .setParam(PARAM_KEY, buWide.getKee())
141 assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-buWide.json"));
145 public void inheritance_parent_child() throws Exception {
146 OrganizationDto organization = db.organizations().insert();
147 RuleDefinitionDto rule1 = db.rules().insert();
148 RuleDefinitionDto rule2 = db.rules().insert();
149 RuleDefinitionDto rule3 = db.rules().insert();
150 ruleIndexer.commitAndIndex(db.getSession(), asList(rule1.getId(), rule2.getId(), rule3.getId()));
152 QProfileDto parent = db.qualityProfiles().insert(organization);
153 db.qualityProfiles().activateRule(parent, rule1);
154 db.qualityProfiles().activateRule(parent, rule2);
155 long parentRules = 2;
157 QProfileDto child = db.qualityProfiles().insert(organization, q -> q.setParentKee(parent.getKee()));
158 db.qualityProfiles().activateRule(child, rule3);
161 activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());
163 InputStream response = ws.newRequest()
164 .setMediaType(PROTOBUF)
165 .setParam(PARAM_KEY, child.getKee())
169 InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
171 assertThat(result.getProfile().getKey()).isEqualTo(child.getKee());
172 assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(childRules);
174 assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getKey).containsExactly(parent.getKee());
175 assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getActiveRuleCount).containsExactly(parentRules);
179 public void inheritance_ignores_removed_rules() throws Exception {
180 OrganizationDto organization = db.organizations().insert();
181 RuleDefinitionDto rule = db.rules().insert(r -> r.setStatus(RuleStatus.REMOVED));
182 ruleIndexer.commitAndIndex(db.getSession(), rule.getId());
184 QProfileDto profile = db.qualityProfiles().insert(organization);
185 db.qualityProfiles().activateRule(profile, rule);
186 long activeRules = 0;
188 activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());
190 InputStream response = ws.newRequest()
191 .setMediaType(PROTOBUF)
192 .setParam(PARAM_KEY, profile.getKee())
196 InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
197 assertThat(result.getProfile().getKey()).isEqualTo(profile.getKee());
198 assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(activeRules);
202 public void inheritance_no_family() {
203 // Simple profile, no parent, no child
204 OrganizationDto organization = db.organizations().insert();
205 QProfileDto remi = createProfile(organization,"xoo", "Nobodys Boy", "xoo-nobody-s-boy-01234");
207 String response = ws.newRequest()
208 .setParam(PARAM_KEY, remi.getKee())
212 assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-simple.json"));
216 public void inheritance_on_paid_organization() {
217 OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID));
218 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
219 UserDto user = db.users().insertUser();
220 userSession.logIn(user).addMembership(organization);
223 .setParam(PARAM_ORGANIZATION, organization.getKey())
224 .setParam(PARAM_QUALITY_PROFILE, qualityProfile.getName())
225 .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
229 @Test(expected = NotFoundException.class)
230 public void fail_if_not_found() {
231 ws.newRequest().setParam(PARAM_KEY, "polop").execute();
235 public void fail_on_paid_organization_when_not_member() {
236 OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID));
237 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
239 expectedException.expect(ForbiddenException.class);
240 expectedException.expectMessage(format("You're not member of organization '%s'", organization.getKey()));
243 .setParam(PARAM_ORGANIZATION, organization.getKey())
244 .setParam(PARAM_QUALITY_PROFILE, qualityProfile.getName())
245 .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
250 public void definition() {
251 WebService.Action definition = ws.getDef();
253 assertThat(definition.key()).isEqualTo("inheritance");
254 assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("key", "language", "qualityProfile", "organization");
255 Param key = definition.param("key");
256 assertThat(key.deprecatedKey()).isEqualTo("profileKey");
257 assertThat(key.deprecatedSince()).isEqualTo("6.6");
258 Param profileName = definition.param("qualityProfile");
259 assertThat(profileName.deprecatedSince()).isNullOrEmpty();
260 Param language = definition.param("language");
261 assertThat(language.deprecatedSince()).isNullOrEmpty();
264 private QProfileDto createProfile(OrganizationDto organization, String lang, String name, String key) {
265 return db.qualityProfiles().insert(organization, qp -> qp.setKee(key).setName(name).setLanguage(lang));
268 private void setParent(QProfileDto profile, QProfileDto parent) {
269 qProfileTree.setParentAndCommit(dbSession, parent, profile);
272 private RuleDefinitionDto createRule(String lang, String id) {
273 long now = new Date().getTime();
274 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("blah", id))
276 .setSeverity(Severity.BLOCKER)
277 .setStatus(RuleStatus.READY)
280 dbClient.ruleDao().insert(dbSession, rule);
281 ruleIndexer.commitAndIndex(dbSession, rule.getId());
285 private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
286 long now = new Date().getTime();
287 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
288 .setSeverity(rule.getSeverityString())
291 dbClient.activeRuleDao().insert(dbSession, activeRule);
295 private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
296 qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getId(), severity, null)));