]> source.dussan.org Git - sonarqube.git/blob
a533309f3bc2dd3f254450776742ec092b19c5bc
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.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;
59
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;
67
68 public class InheritanceActionTest {
69
70   @Rule
71   public DbTester db = DbTester.create();
72   @Rule
73   public EsTester es = EsTester.create();
74   @Rule
75   public UserSessionRule userSession = UserSessionRule.standalone();
76   @Rule
77   public ExpectedException expectedException = ExpectedException.none();
78
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);
84
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);
89
90   private WsActionTester ws = new WsActionTester(new InheritanceAction(
91     dbClient,
92     new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db)),
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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.indexOnStartup(activeRuleIndexer.getIndexTypes());
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(expected = NotFoundException.class)
209   public void fail_if_not_found() {
210     ws.newRequest()
211       .setParam(PARAM_LANGUAGE, "xoo")
212       .setParam(PARAM_QUALITY_PROFILE, "asd")
213       .execute();
214   }
215
216   @Test
217   public void definition() {
218     WebService.Action definition = ws.getDef();
219
220     assertThat(definition.key()).isEqualTo("inheritance");
221     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
222   }
223
224   private QProfileDto createProfile(String lang, String name, String key) {
225     return db.qualityProfiles().insert(qp -> qp.setKee(key).setName(name).setLanguage(lang));
226   }
227
228   private void setParent(QProfileDto profile, QProfileDto parent) {
229     qProfileTree.setParentAndCommit(dbSession, parent, profile);
230   }
231
232   private RuleDefinitionDto createRule(String lang, String id) {
233     long now = new Date().getTime();
234     RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("blah", id))
235       .setLanguage(lang)
236       .setSeverity(Severity.BLOCKER)
237       .setStatus(RuleStatus.READY)
238       .setUpdatedAt(now)
239       .setCreatedAt(now);
240     dbClient.ruleDao().insert(dbSession, rule);
241     ruleIndexer.commitAndIndex(dbSession, rule.getUuid());
242     return rule;
243   }
244
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())
249       .setUpdatedAt(now)
250       .setCreatedAt(now);
251     dbClient.activeRuleDao().insert(dbSession, activeRule);
252     return activeRule;
253   }
254
255   private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) {
256     qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getUuid(), severity, null)));
257   }
258 }