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.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.qualityprofile.ActiveRuleDto;
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.server.es.EsClient;
43 import org.sonar.server.es.EsTester;
44 import org.sonar.server.exceptions.NotFoundException;
45 import org.sonar.server.organization.TestDefaultOrganizationProvider;
46 import org.sonar.server.qualityprofile.QProfileRules;
47 import org.sonar.server.qualityprofile.QProfileRulesImpl;
48 import org.sonar.server.qualityprofile.QProfileTree;
49 import org.sonar.server.qualityprofile.QProfileTreeImpl;
50 import org.sonar.server.qualityprofile.RuleActivation;
51 import org.sonar.server.qualityprofile.RuleActivator;
52 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
53 import org.sonar.server.rule.index.RuleIndex;
54 import org.sonar.server.rule.index.RuleIndexer;
55 import org.sonar.server.tester.UserSessionRule;
56 import org.sonar.server.util.TypeValidations;
57 import org.sonar.server.ws.WsActionTester;
58 import org.sonarqube.ws.Qualityprofiles.InheritanceWsResponse;
60 import static java.util.Arrays.asList;
61 import static java.util.Collections.singleton;
62 import static org.assertj.core.api.Assertions.assertThat;
63 import static org.sonar.test.JsonAssert.assertJson;
64 import static org.sonarqube.ws.MediaTypes.PROTOBUF;
65 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
66 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
68 public class InheritanceActionTest {
71 public DbTester db = DbTester.create();
73 public EsTester es = EsTester.create();
75 public UserSessionRule userSession = UserSessionRule.standalone();
77 public ExpectedException expectedException = ExpectedException.none();
79 private DbClient dbClient = db.getDbClient();
80 private DbSession dbSession = db.getSession();
81 private EsClient esClient = es.client();
82 private RuleIndexer ruleIndexer = new RuleIndexer(esClient, dbClient);
83 private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(dbClient, esClient);
85 private RuleIndex ruleIndex = new RuleIndex(esClient, System2.INSTANCE);
86 private RuleActivator ruleActivator = new RuleActivator(System2.INSTANCE, dbClient, new TypeValidations(new ArrayList<>()), userSession);
87 private QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, ruleIndex, activeRuleIndexer);
88 private QProfileTree qProfileTree = new QProfileTreeImpl(dbClient, ruleActivator, System2.INSTANCE, activeRuleIndexer);
90 private WsActionTester ws = new WsActionTester(new InheritanceAction(
92 new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db)),
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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"));
208 @Test(expected = NotFoundException.class)
209 public void fail_if_not_found() {
211 .setParam(PARAM_LANGUAGE, "xoo")
212 .setParam(PARAM_QUALITY_PROFILE, "asd")
217 public void definition() {
218 WebService.Action definition = ws.getDef();
220 assertThat(definition.key()).isEqualTo("inheritance");
221 assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
224 private QProfileDto createProfile(String lang, String name, String key) {
225 return db.qualityProfiles().insert(qp -> qp.setKee(key).setName(name).setLanguage(lang));
228 private void setParent(QProfileDto profile, QProfileDto parent) {
229 qProfileTree.setParentAndCommit(dbSession, parent, profile);
232 private RuleDefinitionDto createRule(String lang, String id) {
233 long now = new Date().getTime();
234 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("blah", id))
236 .setSeverity(Severity.BLOCKER)
237 .setStatus(RuleStatus.READY)
240 dbClient.ruleDao().insert(dbSession, rule);
241 ruleIndexer.commitAndIndex(dbSession, rule.getUuid());
245 private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
246 long now = new Date().getTime();
247 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
248 .setSeverity(rule.getSeverityString())
251 dbClient.activeRuleDao().insert(dbSession, activeRule);
255 private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
256 qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getUuid(), severity, null)));