3 * Copyright (C) 2009-2024 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;
22 import com.google.common.collect.Multimap;
23 import java.security.SecureRandom;
24 import java.util.Arrays;
25 import java.util.List;
27 import java.util.Random;
28 import java.util.function.Consumer;
29 import java.util.function.Function;
30 import java.util.stream.Collectors;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.sonar.api.config.Configuration;
35 import org.sonar.api.resources.Languages;
36 import org.sonar.api.rule.RuleKey;
37 import org.sonar.api.rule.Severity;
38 import org.sonar.api.rules.RulePriority;
39 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
40 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
41 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
42 import org.sonar.api.testfixtures.log.LogTester;
43 import org.sonar.api.utils.System2;
44 import org.sonar.api.utils.Version;
45 import org.sonar.core.platform.SonarQubeVersion;
46 import org.sonar.core.util.UuidFactoryFast;
47 import org.sonar.db.DbClient;
48 import org.sonar.db.DbTester;
49 import org.sonar.db.qualityprofile.ActiveRuleDto;
50 import org.sonar.db.qualityprofile.QProfileDto;
51 import org.sonar.db.qualityprofile.RulesProfileDto;
52 import org.sonar.db.rule.RuleDto;
53 import org.sonar.server.language.LanguageTesting;
54 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService;
55 import org.sonar.server.qualityprofile.builtin.BuiltInQProfile;
56 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileInsert;
57 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileInsertImpl;
58 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileRepositoryRule;
59 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdate;
60 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdateImpl;
61 import org.sonar.server.qualityprofile.builtin.BuiltInQualityProfilesUpdateListener;
62 import org.sonar.server.qualityprofile.builtin.QProfileName;
63 import org.sonar.server.qualityprofile.builtin.RuleActivator;
64 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
65 import org.sonar.server.rule.DefaultRuleFinder;
66 import org.sonar.server.rule.RuleDescriptionFormatter;
67 import org.sonar.server.rule.ServerRuleFinder;
68 import org.sonar.server.rule.index.RuleIndex;
69 import org.sonar.server.tester.UserSessionRule;
70 import org.sonar.server.util.TypeValidations;
72 import static com.google.common.base.Preconditions.checkState;
73 import static java.util.Collections.singleton;
74 import static org.apache.commons.lang3.RandomStringUtils.secure;
75 import static org.assertj.core.api.Assertions.assertThat;
76 import static org.assertj.core.api.Assertions.tuple;
77 import static org.mockito.ArgumentMatchers.any;
78 import static org.mockito.ArgumentMatchers.anyLong;
79 import static org.mockito.ArgumentMatchers.eq;
80 import static org.mockito.Mockito.mock;
81 import static org.mockito.Mockito.verify;
82 import static org.mockito.Mockito.verifyNoInteractions;
83 import static org.mockito.Mockito.when;
84 import static org.sonar.api.rules.RulePriority.MAJOR;
85 import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
86 import static org.sonar.server.language.LanguageTesting.newLanguage;
87 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
88 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
89 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
91 public class RegisterQualityProfilesNotificationIT {
93 private static final Random RANDOM = new SecureRandom();
95 private System2 system2 = mock(System2.class);
97 public DbTester db = DbTester.create(system2);
99 public UserSessionRule userSessionRule = UserSessionRule.standalone();
101 public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
103 public LogTester logTester = new LogTester();
105 private DbClient dbClient = db.getDbClient();
106 private TypeValidations typeValidations = mock(TypeValidations.class);
107 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
108 private QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
109 private ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class));
110 private SonarQubeVersion sonarQubeVersion = new SonarQubeVersion(Version.create(10, 3));
111 private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, ruleFinder, system2, UuidFactoryFast.getInstance(),
112 typeValidations, activeRuleIndexer, sonarQubeVersion);
113 private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, typeValidations, userSessionRule, mock(Configuration.class), sonarQubeVersion);
114 private QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, mock(RuleIndex.class), activeRuleIndexer, qualityProfileChangeEventService);
115 private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer, qualityProfileChangeEventService);
116 private BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification = mock(BuiltInQualityProfilesUpdateListener.class);
117 private final Languages languages = LanguageTesting.newLanguages();
118 private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
119 builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification, system2, languages);
122 public void do_not_send_notification_on_new_profile() {
123 String language = newLanguageKey();
124 builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
125 builtInQProfileRepositoryRule.initialize();
129 verifyNoInteractions(builtInQualityProfilesNotification);
133 public void do_not_send_notification_when_profile_is_not_updated() {
134 String language = newLanguageKey();
135 RuleDto dbRule = db.rules().insert(r -> r.setLanguage(language));
136 RulesProfileDto dbProfile = insertBuiltInProfile(language);
137 activateRuleInDb(dbProfile, dbRule, MAJOR);
138 addPluginProfile(dbProfile, dbRule);
139 builtInQProfileRepositoryRule.initialize();
143 verifyNoInteractions(builtInQualityProfilesNotification);
147 public void send_notification_when_a_new_rule_is_activated() {
148 String language = newLanguageKey();
149 RuleDto existingRule = db.rules().insert(r -> r.setLanguage(language));
150 RulesProfileDto dbProfile = insertBuiltInProfile(language);
151 activateRuleInDb(dbProfile, existingRule, MAJOR);
152 RuleDto newRule = db.rules().insert(r -> r.setLanguage(language));
153 addPluginProfile(dbProfile, existingRule, newRule);
154 builtInQProfileRepositoryRule.initialize();
158 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
159 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
160 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
161 assertThat(updatedProfiles.keySet())
162 .extracting(QProfileName::getName, QProfileName::getLanguage)
163 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
164 assertThat(updatedProfiles.values())
165 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
166 .containsExactlyInAnyOrder(tuple(newRule.getUuid(), ACTIVATED));
170 public void send_notification_when_a_rule_is_deactivated() {
171 String language = newLanguageKey();
172 RuleDto existingRule = db.rules().insert(r -> r.setLanguage(language));
173 RulesProfileDto dbProfile = insertBuiltInProfile(language);
174 activateRuleInDb(dbProfile, existingRule, MAJOR);
175 addPluginProfile(dbProfile);
176 builtInQProfileRepositoryRule.initialize();
180 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
181 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
182 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
183 assertThat(updatedProfiles.keySet())
184 .extracting(QProfileName::getName, QProfileName::getLanguage)
185 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
186 assertThat(updatedProfiles.values())
187 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
188 .containsExactlyInAnyOrder(tuple(existingRule.getUuid(), DEACTIVATED));
192 public void send_a_single_notification_when_multiple_rules_are_activated() {
193 String language = newLanguageKey();
195 RuleDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
196 RuleDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
197 RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
198 activateRuleInDb(dbProfile1, existingRule1, MAJOR);
199 addPluginProfile(dbProfile1, existingRule1, newRule1);
201 RuleDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
202 RuleDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
203 RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
204 activateRuleInDb(dbProfile2, existingRule2, MAJOR);
205 addPluginProfile(dbProfile2, existingRule2, newRule2);
206 builtInQProfileRepositoryRule.initialize();
210 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
211 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
212 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
213 assertThat(updatedProfiles.keySet())
214 .extracting(QProfileName::getName, QProfileName::getLanguage)
215 .containsExactlyInAnyOrder(
216 tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
217 tuple(dbProfile2.getName(), dbProfile2.getLanguage()));
218 assertThat(updatedProfiles.values())
219 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
220 .containsExactlyInAnyOrder(
221 tuple(newRule1.getUuid(), ACTIVATED),
222 tuple(newRule2.getUuid(), ACTIVATED));
226 public void notification_does_not_include_inherited_profiles_when_rule_is_added() {
227 String language = newLanguageKey();
228 RuleDto newRule = db.rules().insert(r -> r.setLanguage(language));
230 QProfileDto builtInQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
231 QProfileDto childQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
232 addPluginProfile(builtInQProfileDto, newRule);
233 builtInQProfileRepositoryRule.initialize();
237 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
238 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
239 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
240 assertThat(updatedProfiles.keySet())
241 .extracting(QProfileName::getName, QProfileName::getLanguage)
242 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
243 assertThat(updatedProfiles.values())
244 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
245 .containsExactlyInAnyOrder(tuple(newRule.getUuid(), ACTIVATED));
249 public void notification_does_not_include_inherited_profiled_when_rule_is_changed() {
250 String language = newLanguageKey();
251 RuleDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
253 QProfileDto builtInProfile = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
254 db.qualityProfiles().activateRule(builtInProfile, rule, ar -> ar.setSeverity(Severity.MINOR));
255 QProfileDto childProfile = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInProfile.getKee()));
256 db.qualityProfiles().activateRule(childProfile, rule, ar -> ar.setInheritance(ActiveRuleDto.INHERITED).setSeverity(Severity.MINOR));
257 addPluginProfile(builtInProfile, rule);
258 builtInQProfileRepositoryRule.initialize();
263 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
264 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
265 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
266 assertThat(updatedProfiles.keySet())
267 .extracting(QProfileName::getName, QProfileName::getLanguage)
268 .containsExactlyInAnyOrder(tuple(builtInProfile.getName(), builtInProfile.getLanguage()));
269 assertThat(updatedProfiles.values())
270 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
271 .containsExactlyInAnyOrder(tuple(rule.getUuid(), UPDATED));
275 public void notification_does_not_include_inherited_profiles_when_rule_is_deactivated() {
276 String language = newLanguageKey();
277 RuleDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
279 QProfileDto builtInQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
280 db.qualityProfiles().activateRule(builtInQProfileDto, rule);
281 QProfileDto childQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
282 qProfileRules.activateAndCommit(db.getSession(), childQProfileDto, singleton(RuleActivation.create(rule.getUuid())));
285 addPluginProfile(builtInQProfileDto);
286 builtInQProfileRepositoryRule.initialize();
290 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
291 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
292 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
293 assertThat(updatedProfiles.keySet())
294 .extracting(QProfileName::getName, QProfileName::getLanguage)
295 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
296 assertThat(updatedProfiles.values())
297 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
298 .containsExactlyInAnyOrder(tuple(rule.getUuid(), DEACTIVATED));
302 public void notification_contains_send_start_and_end_date() {
303 String language = newLanguageKey();
304 RuleDto existingRule = db.rules().insert(r -> r.setLanguage(language));
305 RulesProfileDto dbProfile = insertBuiltInProfile(language);
306 activateRuleInDb(dbProfile, existingRule, MAJOR);
307 RuleDto newRule = db.rules().insert(r -> r.setLanguage(language));
308 addPluginProfile(dbProfile, existingRule, newRule);
309 builtInQProfileRepositoryRule.initialize();
310 long startDate = RANDOM.nextInt(5000);
311 long endDate = startDate + RANDOM.nextInt(5000);
312 when(system2.now()).thenReturn(startDate, endDate);
316 verify(builtInQualityProfilesNotification).onChange(any(), eq(startDate), eq(endDate));
319 private void addPluginProfile(RulesProfileDto dbProfile, RuleDto... dbRules) {
320 BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
321 NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(dbProfile.getName(), dbProfile.getLanguage());
323 Arrays.stream(dbRules).forEach(dbRule -> newQp.activateRule(dbRule.getRepositoryKey(), dbRule.getRuleKey()).overrideSeverity(Severity.MAJOR));
325 List<BuiltInActiveRule> rules = context.profile(dbProfile.getLanguage(), dbProfile.getName()).rules();
326 BuiltInQProfile.ActiveRule[] activeRules = toActiveRules(rules, dbRules);
327 builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, activeRules);
330 private static BuiltInQProfile.ActiveRule[] toActiveRules(List<BuiltInActiveRule> rules, RuleDto[] dbRules) {
331 Map<RuleKey, RuleDto> dbRulesByRuleKey = Arrays.stream(dbRules)
332 .collect(Collectors.toMap(RuleDto::getKey, Function.identity()));
333 return rules.stream()
335 RuleKey ruleKey = RuleKey.of(r.repoKey(), r.ruleKey());
336 RuleDto ruleDefinitionDto = dbRulesByRuleKey.get(ruleKey);
337 checkState(ruleDefinitionDto != null, "Rule '%s' not found", ruleKey);
338 return new BuiltInQProfile.ActiveRule(ruleDefinitionDto.getUuid(), r);
339 }).toArray(BuiltInQProfile.ActiveRule[]::new);
342 private void addPluginProfile(QProfileDto profile, RuleDto... dbRules) {
343 BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
344 NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage());
346 Arrays.stream(dbRules).forEach(dbRule -> newQp.activateRule(dbRule.getRepositoryKey(), dbRule.getRuleKey()).overrideSeverity(Severity.MAJOR));
348 BuiltInQProfile.ActiveRule[] activeRules = toActiveRules(context.profile(profile.getLanguage(), profile.getName()).rules(), dbRules);
349 builtInQProfileRepositoryRule.add(newLanguage(profile.getLanguage()), profile.getName(), false, activeRules);
352 private RulesProfileDto insertBuiltInProfile(String language) {
353 RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
354 db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
356 return ruleProfileDto;
359 private void activateRuleInDb(RulesProfileDto profile, RuleDto rule, RulePriority severity) {
360 ActiveRuleDto dto = new ActiveRuleDto()
361 .setProfileUuid(profile.getUuid())
362 .setSeverity(severity.name())
363 .setRuleUuid(rule.getUuid())
364 .setCreatedAt(RANDOM.nextLong(Long.MAX_VALUE))
365 .setUpdatedAt(RANDOM.nextLong(Long.MAX_VALUE));
366 db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
370 private QProfileDto insertProfile(Consumer<QProfileDto> consumer) {
371 QProfileDto builtInQProfileDto = db.qualityProfiles().insert(consumer);
373 return builtInQProfileDto;
376 private static String newLanguageKey() {
377 return secure().nextAlphanumeric(20).toLowerCase();