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.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.sonar.api.resources.Languages;
28 import org.sonar.api.rule.RuleKey;
29 import org.sonar.api.rule.RuleStatus;
30 import org.sonar.api.rule.Severity;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.api.server.ws.WebService.Param;
33 import org.sonar.api.utils.System2;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.qualityprofile.ActiveRuleDto;
38 import org.sonar.db.qualityprofile.QProfileDto;
39 import org.sonar.db.rule.RuleDefinitionDto;
40 import org.sonar.db.rule.RuleTesting;
41 import org.sonar.server.es.EsClient;
42 import org.sonar.server.es.EsTester;
43 import org.sonar.server.exceptions.NotFoundException;
44 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService;
45 import org.sonar.server.qualityprofile.QProfileRules;
46 import org.sonar.server.qualityprofile.QProfileRulesImpl;
47 import org.sonar.server.qualityprofile.QProfileTree;
48 import org.sonar.server.qualityprofile.QProfileTreeImpl;
49 import org.sonar.server.qualityprofile.RuleActivation;
50 import org.sonar.server.qualityprofile.builtin.RuleActivator;
51 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
52 import org.sonar.server.rule.index.RuleIndex;
53 import org.sonar.server.rule.index.RuleIndexer;
54 import org.sonar.server.tester.UserSessionRule;
55 import org.sonar.server.util.TypeValidations;
56 import org.sonar.server.ws.WsActionTester;
57 import org.sonarqube.ws.Qualityprofiles.InheritanceWsResponse;
59 import static java.util.Arrays.asList;
60 import static java.util.Collections.singleton;
61 import static org.assertj.core.api.Assertions.assertThat;
62 import static org.assertj.core.api.Assertions.assertThatThrownBy;
63 import static org.mockito.Mockito.mock;
64 import static org.sonar.test.JsonAssert.assertJson;
65 import static org.sonarqube.ws.MediaTypes.PROTOBUF;
66 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
67 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
69 public class InheritanceActionTest {
72 public DbTester db = DbTester.create();
74 public EsTester es = EsTester.create();
76 public UserSessionRule userSession = UserSessionRule.standalone();
78 private DbClient dbClient = db.getDbClient();
79 private DbSession dbSession = db.getSession();
80 private EsClient esClient = es.client();
81 private RuleIndexer ruleIndexer = new RuleIndexer(esClient, dbClient);
82 private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(dbClient, esClient);
84 private RuleIndex ruleIndex = new RuleIndex(esClient, System2.INSTANCE);
85 private QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
86 private RuleActivator ruleActivator = new RuleActivator(System2.INSTANCE, dbClient, new TypeValidations(new ArrayList<>()), userSession);
87 private QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, ruleIndex, activeRuleIndexer, qualityProfileChangeEventService);
88 private QProfileTree qProfileTree = new QProfileTreeImpl(dbClient, ruleActivator, System2.INSTANCE, activeRuleIndexer, mock(QualityProfileChangeEventService.class));
90 private WsActionTester ws = new WsActionTester(new InheritanceAction(
92 new QProfileWsSupport(dbClient, userSession),
96 public void inheritance_nominal() {
97 RuleDefinitionDto rule1 = createRule("xoo", "rule1");
98 RuleDefinitionDto rule2 = createRule("xoo", "rule2");
99 RuleDefinitionDto rule3 = createRule("xoo", "rule3");
102 * sonar way (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2))
104 QProfileDto sonarway = db.qualityProfiles().insert(p -> p.setKee("xoo-sonar-way").setLanguage("xoo").setName("Sonar way").setIsBuiltIn(true));
105 createActiveRule(rule1, sonarway);
106 createActiveRule(rule2, sonarway);
109 activeRuleIndexer.indexAll();
111 QProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345");
112 setParent(sonarway, companyWide);
114 QProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456");
115 setParent(companyWide, buWide);
116 overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL);
118 QProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567");
119 setParent(buWide, forProject1);
120 createActiveRule(rule3, forProject1);
122 activeRuleIndexer.indexAll();
124 QProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678");
125 setParent(buWide, forProject2);
126 overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL);
128 String response = ws.newRequest()
129 .setParam(PARAM_LANGUAGE, buWide.getLanguage())
130 .setParam(PARAM_QUALITY_PROFILE, buWide.getName())
134 assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-buWide.json"));
138 public void inheritance_parent_child() throws Exception {
139 RuleDefinitionDto rule1 = db.rules().insert();
140 RuleDefinitionDto rule2 = db.rules().insert();
141 RuleDefinitionDto rule3 = db.rules().insert();
142 ruleIndexer.commitAndIndex(db.getSession(), asList(rule1.getUuid(), rule2.getUuid(), rule3.getUuid()));
144 QProfileDto parent = db.qualityProfiles().insert();
145 db.qualityProfiles().activateRule(parent, rule1);
146 db.qualityProfiles().activateRule(parent, rule2);
147 long parentRules = 2;
149 QProfileDto child = db.qualityProfiles().insert(q -> q.setParentKee(parent.getKee()));
150 db.qualityProfiles().activateRule(child, rule3);
153 activeRuleIndexer.indexAll();
155 InputStream response = ws.newRequest()
156 .setMediaType(PROTOBUF)
157 .setParam(PARAM_LANGUAGE, child.getLanguage())
158 .setParam(PARAM_QUALITY_PROFILE, child.getName())
162 InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
164 assertThat(result.getProfile().getKey()).isEqualTo(child.getKee());
165 assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(childRules);
167 assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getKey).containsExactly(parent.getKee());
168 assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getActiveRuleCount).containsExactly(parentRules);
172 public void inheritance_ignores_removed_rules() throws Exception {
173 RuleDefinitionDto rule = db.rules().insert(r -> r.setStatus(RuleStatus.REMOVED));
174 ruleIndexer.commitAndIndex(db.getSession(), rule.getUuid());
176 QProfileDto profile = db.qualityProfiles().insert();
177 db.qualityProfiles().activateRule(profile, rule);
178 long activeRules = 0;
180 activeRuleIndexer.indexAll();
182 InputStream response = ws.newRequest()
183 .setMediaType(PROTOBUF)
184 .setParam(PARAM_LANGUAGE, profile.getLanguage())
185 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
189 InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
190 assertThat(result.getProfile().getKey()).isEqualTo(profile.getKee());
191 assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(activeRules);
195 public void inheritance_no_family() {
196 // Simple profile, no parent, no child
197 QProfileDto remi = createProfile("xoo", "Nobodys Boy", "xoo-nobody-s-boy-01234");
199 String response = ws.newRequest()
200 .setParam(PARAM_LANGUAGE, remi.getLanguage())
201 .setParam(PARAM_QUALITY_PROFILE, remi.getName())
205 assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-simple.json"));
209 public void fail_if_not_found() {
210 assertThatThrownBy(() -> {
212 .setParam(PARAM_LANGUAGE, "xoo")
213 .setParam(PARAM_QUALITY_PROFILE, "asd")
216 .isInstanceOf(NotFoundException.class);
220 public void definition() {
221 WebService.Action definition = ws.getDef();
223 assertThat(definition.key()).isEqualTo("inheritance");
224 assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
227 private QProfileDto createProfile(String lang, String name, String key) {
228 return db.qualityProfiles().insert(qp -> qp.setKee(key).setName(name).setLanguage(lang));
231 private void setParent(QProfileDto profile, QProfileDto parent) {
232 qProfileTree.setParentAndCommit(dbSession, parent, profile);
235 private RuleDefinitionDto createRule(String lang, String id) {
236 long now = new Date().getTime();
237 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("blah", id))
239 .setSeverity(Severity.BLOCKER)
240 .setStatus(RuleStatus.READY)
243 dbClient.ruleDao().insert(dbSession, rule);
244 ruleIndexer.commitAndIndex(dbSession, rule.getUuid());
248 private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
249 long now = new Date().getTime();
250 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
251 .setSeverity(rule.getSeverityString())
254 dbClient.activeRuleDao().insert(dbSession, activeRule);
258 private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
259 qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getUuid(), severity, null)));