]> source.dussan.org Git - sonarqube.git/blob
a4feaed3b9eec59527602e4419c3d9b064b2efdf
[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.builtin;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.impl.utils.TestSystem2;
28 import org.sonar.api.rule.RuleKey;
29 import org.sonar.api.rule.Severity;
30 import org.sonar.api.rules.RulePriority;
31 import org.sonar.api.utils.System2;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.qualityprofile.ActiveRuleDto;
35 import org.sonar.db.qualityprofile.ActiveRuleKey;
36 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
37 import org.sonar.db.qualityprofile.QProfileDto;
38 import org.sonar.db.qualityprofile.RulesProfileDto;
39 import org.sonar.db.rule.RuleDefinitionDto;
40 import org.sonar.db.rule.RuleParamDto;
41 import org.sonar.server.exceptions.BadRequestException;
42 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService;
43 import org.sonar.server.qualityprofile.ActiveRuleChange;
44 import org.sonar.server.qualityprofile.ActiveRuleInheritance;
45 import org.sonar.server.qualityprofile.RuleActivation;
46 import org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.util.IntegerTypeValidation;
49 import org.sonar.server.util.StringTypeValidation;
50 import org.sonar.server.util.TypeValidations;
51
52 import static java.util.Arrays.asList;
53 import static java.util.Collections.emptyList;
54 import static java.util.Collections.singletonList;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.junit.Assert.assertThrows;
57 import static org.mockito.Mockito.mock;
58 import static org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED;
59 import static org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES;
60
61 /**
62  * Class org.sonar.server.qualityprofile.builtin.RuleActivator is mostly covered in
63  * org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdateImplTest
64  */
65 public class RuleActivatorTest {
66   @Rule
67   public final DbTester db = DbTester.create();
68
69   @Rule
70   public final UserSessionRule userSession = UserSessionRule.standalone();
71
72   private static final long NOW = 1_000;
73   private static final long PAST = NOW - 100;
74   private final System2 system2 = new TestSystem2().setNow(NOW);
75   private final TypeValidations typeValidations = new TypeValidations(asList(new StringTypeValidation(), new IntegerTypeValidation()));
76
77   private final QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
78   private final RuleActivator underTest = new RuleActivator(system2, db.getDbClient(), typeValidations, userSession);
79
80   @Test
81   public void reset_overridden_active_rule() {
82     RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
83     RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
84
85     QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
86     ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
87       RulePriority.valueOf(Severity.BLOCKER), null);
88     ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
89
90     QProfileDto childProfile = createChildProfile(parentProfile);
91     ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
92       RulePriority.valueOf(Severity.MINOR), OVERRIDES);
93     ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "15");
94
95     DbSession session = db.getSession();
96     RuleActivation resetRequest = RuleActivation.createReset(rule.getUuid());
97     RuleActivationContext context = new RuleActivationContext.Builder()
98       .setProfiles(asList(parentProfile, childProfile))
99       .setBaseProfile(RulesProfileDto.from(childProfile))
100       .setDate(NOW)
101       .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
102       .setRules(singletonList(rule))
103       .setRuleParams(singletonList(ruleParam))
104       .setActiveRules(asList(parentActiveRuleDto, childActiveRuleDto))
105       .setActiveRuleParams(asList(parentActiveRuleParam, childActiveRuleParam))
106       .build();
107
108     List<ActiveRuleChange> result = underTest.activate(session, resetRequest, context);
109
110     assertThat(result).hasSize(1);
111     assertThat(result.get(0).getParameters()).containsEntry("min", "10");
112     assertThat(result.get(0).getSeverity()).isEqualTo(Severity.BLOCKER);
113     assertThat(result.get(0).getInheritance()).isEqualTo(ActiveRuleInheritance.INHERITED);
114   }
115
116   @Test
117   public void request_new_severity_and_param_for_child_rule() {
118     RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
119     RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
120
121     QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
122     ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
123         RulePriority.valueOf(Severity.BLOCKER), null);
124     ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
125
126     QProfileDto childProfile = createChildProfile(parentProfile);
127     ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
128         RulePriority.valueOf(Severity.BLOCKER), INHERITED);
129     ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "10");
130
131     DbSession session = db.getSession();
132     RuleActivation resetRequest = RuleActivation.create(rule.getUuid(), Severity.MINOR, ImmutableMap.of("min", "15"));
133     RuleActivationContext context = new RuleActivationContext.Builder()
134         .setProfiles(asList(parentProfile, childProfile))
135         .setBaseProfile(RulesProfileDto.from(childProfile))
136         .setDate(NOW)
137         .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
138         .setRules(singletonList(rule))
139         .setRuleParams(singletonList(ruleParam))
140         .setActiveRules(asList(parentActiveRuleDto, childActiveRuleDto))
141         .setActiveRuleParams(asList(parentActiveRuleParam, childActiveRuleParam))
142         .build();
143
144     List<ActiveRuleChange> result = underTest.activate(session, resetRequest, context);
145
146     assertThat(result).hasSize(1);
147     assertThat(result.get(0).getParameters()).containsEntry("min", "15");
148     assertThat(result.get(0).getSeverity()).isEqualTo(Severity.MINOR);
149     assertThat(result.get(0).getInheritance()).isEqualTo(OVERRIDES);
150   }
151
152   @Test
153   public void set_severity_and_param_for_child_rule_when_activating() {
154     RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
155     RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
156
157     QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
158
159     QProfileDto childProfile = createChildProfile(parentProfile);
160
161     DbSession session = db.getSession();
162     RuleActivation resetRequest = RuleActivation.create(rule.getUuid());
163     RuleActivationContext context = new RuleActivationContext.Builder()
164         .setProfiles(asList(parentProfile, childProfile))
165         .setBaseProfile(RulesProfileDto.from(childProfile))
166         .setDate(NOW)
167         .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
168         .setRules(singletonList(rule))
169         .setRuleParams(singletonList(ruleParam))
170         .setActiveRules(emptyList())
171         .setActiveRuleParams(emptyList())
172         .build();
173
174     List<ActiveRuleChange> result = underTest.activate(session, resetRequest, context);
175
176     assertThat(result).hasSize(1);
177     assertThat(result.get(0).getParameters()).containsEntry("min", "10");
178     assertThat(result.get(0).getSeverity()).isEqualTo(Severity.BLOCKER);
179     assertThat(result.get(0).getInheritance()).isNull();
180   }
181
182   @Test
183   public void fail_if_rule_language_doesnt_match_qp() {
184     RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo")
185       .setRepositoryKey("repo")
186       .setRuleKey("rule")
187       .setSeverity(Severity.BLOCKER));
188     QProfileDto qp = db.qualityProfiles().insert(p -> p.setLanguage("xoo2").setKee("qp").setIsBuiltIn(true));
189
190     DbSession session = db.getSession();
191     RuleActivation resetRequest = RuleActivation.create(rule.getUuid());
192     RuleActivationContext context = new RuleActivationContext.Builder()
193       .setProfiles(singletonList(qp))
194       .setBaseProfile(RulesProfileDto.from(qp))
195       .setDate(NOW)
196       .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
197       .setRules(singletonList(rule))
198       .setRuleParams(emptyList())
199       .setActiveRules(emptyList())
200       .setActiveRuleParams(emptyList())
201       .build();
202
203
204     assertThrows("xoo rule repo:rule cannot be activated on xoo2 profile qp", BadRequestException.class,
205       () -> underTest.activate(session, resetRequest, context));
206   }
207
208
209   private ActiveRuleDto activateRuleInDb(RulesProfileDto ruleProfile, RuleDefinitionDto rule, RulePriority severity, @Nullable ActiveRuleInheritance inheritance) {
210     ActiveRuleDto dto = new ActiveRuleDto()
211       .setKey(ActiveRuleKey.of(ruleProfile, RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey())))
212       .setProfileUuid(ruleProfile.getUuid())
213       .setSeverity(severity.name())
214       .setRuleUuid(rule.getUuid())
215       .setInheritance(inheritance != null ? inheritance.name() : null)
216       .setCreatedAt(PAST)
217       .setUpdatedAt(PAST);
218     db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
219     db.commit();
220     return dto;
221   }
222
223   private ActiveRuleParamDto activateRuleParamInDb(ActiveRuleDto activeRuleDto, RuleParamDto ruleParamDto, String value) {
224     ActiveRuleParamDto dto = new ActiveRuleParamDto()
225       .setActiveRuleUuid(activeRuleDto.getUuid())
226       .setRulesParameterUuid(ruleParamDto.getUuid())
227       .setKey(ruleParamDto.getName())
228       .setValue(value);
229     db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRuleDto, dto);
230     db.commit();
231     return dto;
232   }
233
234   private QProfileDto createChildProfile(QProfileDto parent) {
235     return db.qualityProfiles().insert(p -> p
236       .setLanguage(parent.getLanguage())
237       .setParentKee(parent.getKee())
238       .setName("Child of " + parent.getName()))
239       .setIsBuiltIn(false);
240   }
241 }