]> source.dussan.org Git - sonarqube.git/blob
f8579c35740097e263d0fa2fb45a237db56d88bf
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualityprofile.ws;
21
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;
58
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;
68
69 public class InheritanceActionTest {
70
71   @Rule
72   public DbTester db = DbTester.create();
73   @Rule
74   public EsTester es = EsTester.create();
75   @Rule
76   public UserSessionRule userSession = UserSessionRule.standalone();
77
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);
83
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));
89
90   private WsActionTester ws = new WsActionTester(new InheritanceAction(
91     dbClient,
92     new QProfileWsSupport(dbClient, userSession),
93     new Languages()));
94
95   @Test
96   public void inheritance_nominal() {
97     RuleDefinitionDto rule1 = createRule("xoo", "rule1");
98     RuleDefinitionDto rule2 = createRule("xoo", "rule2");
99     RuleDefinitionDto rule3 = createRule("xoo", "rule3");
100
101     /*
102      * sonar way (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2))
103      */
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);
107
108     dbSession.commit();
109     activeRuleIndexer.indexAll();
110
111     QProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345");
112     setParent(sonarway, companyWide);
113
114     QProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456");
115     setParent(companyWide, buWide);
116     overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL);
117
118     QProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567");
119     setParent(buWide, forProject1);
120     createActiveRule(rule3, forProject1);
121     dbSession.commit();
122     activeRuleIndexer.indexAll();
123
124     QProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678");
125     setParent(buWide, forProject2);
126     overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL);
127
128     String response = ws.newRequest()
129       .setParam(PARAM_LANGUAGE, buWide.getLanguage())
130       .setParam(PARAM_QUALITY_PROFILE, buWide.getName())
131       .execute()
132       .getInput();
133
134     assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-buWide.json"));
135   }
136
137   @Test
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()));
143
144     QProfileDto parent = db.qualityProfiles().insert();
145     db.qualityProfiles().activateRule(parent, rule1);
146     db.qualityProfiles().activateRule(parent, rule2);
147     long parentRules = 2;
148
149     QProfileDto child = db.qualityProfiles().insert(q -> q.setParentKee(parent.getKee()));
150     db.qualityProfiles().activateRule(child, rule3);
151     long childRules = 1;
152
153     activeRuleIndexer.indexAll();
154
155     InputStream response = ws.newRequest()
156       .setMediaType(PROTOBUF)
157       .setParam(PARAM_LANGUAGE, child.getLanguage())
158       .setParam(PARAM_QUALITY_PROFILE, child.getName())
159       .execute()
160       .getInputStream();
161
162     InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
163
164     assertThat(result.getProfile().getKey()).isEqualTo(child.getKee());
165     assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(childRules);
166
167     assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getKey).containsExactly(parent.getKee());
168     assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getActiveRuleCount).containsExactly(parentRules);
169   }
170
171   @Test
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());
175
176     QProfileDto profile = db.qualityProfiles().insert();
177     db.qualityProfiles().activateRule(profile, rule);
178     long activeRules = 0;
179
180     activeRuleIndexer.indexAll();
181
182     InputStream response = ws.newRequest()
183       .setMediaType(PROTOBUF)
184       .setParam(PARAM_LANGUAGE, profile.getLanguage())
185       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
186       .execute()
187       .getInputStream();
188
189     InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
190     assertThat(result.getProfile().getKey()).isEqualTo(profile.getKee());
191     assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(activeRules);
192   }
193
194   @Test
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");
198
199     String response = ws.newRequest()
200       .setParam(PARAM_LANGUAGE, remi.getLanguage())
201       .setParam(PARAM_QUALITY_PROFILE, remi.getName())
202       .execute()
203       .getInput();
204
205     assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-simple.json"));
206   }
207
208   @Test
209   public void fail_if_not_found() {
210     assertThatThrownBy(() -> {
211       ws.newRequest()
212         .setParam(PARAM_LANGUAGE, "xoo")
213         .setParam(PARAM_QUALITY_PROFILE, "asd")
214         .execute();
215     })
216       .isInstanceOf(NotFoundException.class);
217   }
218
219   @Test
220   public void definition() {
221     WebService.Action definition = ws.getDef();
222
223     assertThat(definition.key()).isEqualTo("inheritance");
224     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
225   }
226
227   private QProfileDto createProfile(String lang, String name, String key) {
228     return db.qualityProfiles().insert(qp -> qp.setKee(key).setName(name).setLanguage(lang));
229   }
230
231   private void setParent(QProfileDto profile, QProfileDto parent) {
232     qProfileTree.setParentAndCommit(dbSession, parent, profile);
233   }
234
235   private RuleDefinitionDto createRule(String lang, String id) {
236     long now = new Date().getTime();
237     RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("blah", id))
238       .setLanguage(lang)
239       .setSeverity(Severity.BLOCKER)
240       .setStatus(RuleStatus.READY)
241       .setUpdatedAt(now)
242       .setCreatedAt(now);
243     dbClient.ruleDao().insert(dbSession, rule);
244     ruleIndexer.commitAndIndex(dbSession, rule.getUuid());
245     return rule;
246   }
247
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())
252       .setUpdatedAt(now)
253       .setCreatedAt(now);
254     dbClient.activeRuleDao().insert(dbSession, activeRule);
255     return activeRule;
256   }
257
258   private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
259     qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getUuid(), severity, null)));
260   }
261 }