3 * Copyright (C) 2009-2021 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 org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.mockito.ArgumentCaptor;
33 import org.sonar.api.rule.RuleKey;
34 import org.sonar.api.rule.Severity;
35 import org.sonar.api.rules.RulePriority;
36 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
37 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
38 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
39 import org.sonar.api.utils.System2;
40 import org.sonar.api.utils.log.LogTester;
41 import org.sonar.core.util.UuidFactoryFast;
42 import org.sonar.core.util.stream.MoreCollectors;
43 import org.sonar.db.DbClient;
44 import org.sonar.db.DbTester;
45 import org.sonar.db.qualityprofile.ActiveRuleDto;
46 import org.sonar.db.qualityprofile.QProfileDto;
47 import org.sonar.db.qualityprofile.RulesProfileDto;
48 import org.sonar.db.rule.RuleDefinitionDto;
49 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
50 import org.sonar.server.rule.index.RuleIndex;
51 import org.sonar.server.tester.UserSessionRule;
52 import org.sonar.server.util.TypeValidations;
54 import static com.google.common.base.Preconditions.checkState;
55 import static java.util.Collections.singleton;
56 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
57 import static org.apache.commons.lang.math.RandomUtils.nextLong;
58 import static org.assertj.core.api.Assertions.assertThat;
59 import static org.assertj.core.api.Assertions.tuple;
60 import static org.mockito.ArgumentMatchers.any;
61 import static org.mockito.ArgumentMatchers.anyLong;
62 import static org.mockito.ArgumentMatchers.eq;
63 import static org.mockito.Mockito.mock;
64 import static org.mockito.Mockito.verify;
65 import static org.mockito.Mockito.verifyZeroInteractions;
66 import static org.mockito.Mockito.when;
67 import static org.sonar.api.rules.RulePriority.MAJOR;
68 import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
69 import static org.sonar.server.language.LanguageTesting.newLanguage;
70 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
71 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
72 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
74 public class RegisterQualityProfilesNotificationTest {
76 private static final Random RANDOM = new SecureRandom();
78 private System2 system2 = mock(System2.class);
80 public DbTester db = DbTester.create(system2);
82 public UserSessionRule userSessionRule = UserSessionRule.standalone();
84 public ExpectedException expectedException = ExpectedException.none();
86 public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
88 public LogTester logTester = new LogTester();
90 private DbClient dbClient = db.getDbClient();
91 private TypeValidations typeValidations = mock(TypeValidations.class);
92 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
93 private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer);
94 private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, typeValidations, userSessionRule);
95 private QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, mock(RuleIndex.class), activeRuleIndexer);
96 private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer);
97 private BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification = mock(BuiltInQualityProfilesUpdateListener.class);
98 private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
99 builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification, system2);
102 public void do_not_send_notification_on_new_profile() {
103 String language = newLanguageKey();
104 builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
105 builtInQProfileRepositoryRule.initialize();
109 verifyZeroInteractions(builtInQualityProfilesNotification);
113 public void do_not_send_notification_when_profile_is_not_updated() {
114 String language = newLanguageKey();
115 RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
116 RulesProfileDto dbProfile = insertBuiltInProfile(language);
117 activateRuleInDb(dbProfile, dbRule, MAJOR);
118 addPluginProfile(dbProfile, dbRule);
119 builtInQProfileRepositoryRule.initialize();
123 verifyZeroInteractions(builtInQualityProfilesNotification);
127 public void send_notification_when_a_new_rule_is_activated() {
128 String language = newLanguageKey();
129 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
130 RulesProfileDto dbProfile = insertBuiltInProfile(language);
131 activateRuleInDb(dbProfile, existingRule, MAJOR);
132 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
133 addPluginProfile(dbProfile, existingRule, newRule);
134 builtInQProfileRepositoryRule.initialize();
138 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
139 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
140 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
141 assertThat(updatedProfiles.keySet())
142 .extracting(QProfileName::getName, QProfileName::getLanguage)
143 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
144 assertThat(updatedProfiles.values())
145 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
146 .containsExactlyInAnyOrder(tuple(newRule.getUuid(), ACTIVATED));
150 public void send_notification_when_a_rule_is_deactivated() {
151 String language = newLanguageKey();
152 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
153 RulesProfileDto dbProfile = insertBuiltInProfile(language);
154 activateRuleInDb(dbProfile, existingRule, MAJOR);
155 addPluginProfile(dbProfile);
156 builtInQProfileRepositoryRule.initialize();
160 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
161 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
162 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
163 assertThat(updatedProfiles.keySet())
164 .extracting(QProfileName::getName, QProfileName::getLanguage)
165 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
166 assertThat(updatedProfiles.values())
167 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
168 .containsExactlyInAnyOrder(tuple(existingRule.getUuid(), DEACTIVATED));
172 public void send_a_single_notification_when_multiple_rules_are_activated() {
173 String language = newLanguageKey();
175 RuleDefinitionDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
176 RuleDefinitionDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
177 RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
178 activateRuleInDb(dbProfile1, existingRule1, MAJOR);
179 addPluginProfile(dbProfile1, existingRule1, newRule1);
181 RuleDefinitionDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
182 RuleDefinitionDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
183 RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
184 activateRuleInDb(dbProfile2, existingRule2, MAJOR);
185 addPluginProfile(dbProfile2, existingRule2, newRule2);
186 builtInQProfileRepositoryRule.initialize();
190 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
191 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
192 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
193 assertThat(updatedProfiles.keySet())
194 .extracting(QProfileName::getName, QProfileName::getLanguage)
195 .containsExactlyInAnyOrder(
196 tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
197 tuple(dbProfile2.getName(), dbProfile2.getLanguage()));
198 assertThat(updatedProfiles.values())
199 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
200 .containsExactlyInAnyOrder(
201 tuple(newRule1.getUuid(), ACTIVATED),
202 tuple(newRule2.getUuid(), ACTIVATED));
206 public void notification_does_not_include_inherited_profiles_when_rule_is_added() {
207 String language = newLanguageKey();
208 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
210 QProfileDto builtInQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
211 QProfileDto childQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
212 addPluginProfile(builtInQProfileDto, newRule);
213 builtInQProfileRepositoryRule.initialize();
217 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
218 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
219 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
220 assertThat(updatedProfiles.keySet())
221 .extracting(QProfileName::getName, QProfileName::getLanguage)
222 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
223 assertThat(updatedProfiles.values())
224 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
225 .containsExactlyInAnyOrder(tuple(newRule.getUuid(), ACTIVATED));
229 public void notification_does_not_include_inherited_profiled_when_rule_is_changed() {
230 String language = newLanguageKey();
231 RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
233 QProfileDto builtInProfile = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
234 db.qualityProfiles().activateRule(builtInProfile, rule, ar -> ar.setSeverity(Severity.MINOR));
235 QProfileDto childProfile = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInProfile.getKee()));
236 db.qualityProfiles().activateRule(childProfile, rule, ar -> ar.setInheritance(ActiveRuleDto.INHERITED).setSeverity(Severity.MINOR));
237 addPluginProfile(builtInProfile, rule);
238 builtInQProfileRepositoryRule.initialize();
243 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
244 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
245 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
246 assertThat(updatedProfiles.keySet())
247 .extracting(QProfileName::getName, QProfileName::getLanguage)
248 .containsExactlyInAnyOrder(tuple(builtInProfile.getName(), builtInProfile.getLanguage()));
249 assertThat(updatedProfiles.values())
250 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
251 .containsExactlyInAnyOrder(tuple(rule.getUuid(), UPDATED));
255 public void notification_does_not_include_inherited_profiles_when_rule_is_deactivated() {
256 String language = newLanguageKey();
257 RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
259 QProfileDto builtInQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
260 db.qualityProfiles().activateRule(builtInQProfileDto, rule);
261 QProfileDto childQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
262 qProfileRules.activateAndCommit(db.getSession(), childQProfileDto, singleton(RuleActivation.create(rule.getUuid())));
265 addPluginProfile(builtInQProfileDto);
266 builtInQProfileRepositoryRule.initialize();
270 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
271 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
272 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
273 assertThat(updatedProfiles.keySet())
274 .extracting(QProfileName::getName, QProfileName::getLanguage)
275 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
276 assertThat(updatedProfiles.values())
277 .extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType)
278 .containsExactlyInAnyOrder(tuple(rule.getUuid(), DEACTIVATED));
282 public void notification_contains_send_start_and_end_date() {
283 String language = newLanguageKey();
284 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
285 RulesProfileDto dbProfile = insertBuiltInProfile(language);
286 activateRuleInDb(dbProfile, existingRule, MAJOR);
287 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
288 addPluginProfile(dbProfile, existingRule, newRule);
289 builtInQProfileRepositoryRule.initialize();
290 long startDate = RANDOM.nextInt(5000);
291 long endDate = startDate + RANDOM.nextInt(5000);
292 when(system2.now()).thenReturn(startDate, endDate);
296 verify(builtInQualityProfilesNotification).onChange(any(), eq(startDate), eq(endDate));
299 private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
300 BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
301 NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(dbProfile.getName(), dbProfile.getLanguage());
303 Arrays.stream(dbRules).forEach(dbRule -> newQp.activateRule(dbRule.getRepositoryKey(), dbRule.getRuleKey()).overrideSeverity(Severity.MAJOR));
305 List<BuiltInActiveRule> rules = context.profile(dbProfile.getLanguage(), dbProfile.getName()).rules();
306 BuiltInQProfile.ActiveRule[] activeRules = toActiveRules(rules, dbRules);
307 builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, activeRules);
310 private static BuiltInQProfile.ActiveRule[] toActiveRules(List<BuiltInActiveRule> rules, RuleDefinitionDto[] dbRules) {
311 Map<RuleKey, RuleDefinitionDto> dbRulesByRuleKey = Arrays.stream(dbRules)
312 .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey));
313 return rules.stream()
315 RuleKey ruleKey = RuleKey.of(r.repoKey(), r.ruleKey());
316 RuleDefinitionDto ruleDefinitionDto = dbRulesByRuleKey.get(ruleKey);
317 checkState(ruleDefinitionDto != null, "Rule '%s' not found", ruleKey);
318 return new BuiltInQProfile.ActiveRule(ruleDefinitionDto.getUuid(), r);
319 }).toArray(BuiltInQProfile.ActiveRule[]::new);
322 private void addPluginProfile(QProfileDto profile, RuleDefinitionDto... dbRules) {
323 BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
324 NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage());
326 Arrays.stream(dbRules).forEach(dbRule -> newQp.activateRule(dbRule.getRepositoryKey(), dbRule.getRuleKey()).overrideSeverity(Severity.MAJOR));
328 BuiltInQProfile.ActiveRule[] activeRules = toActiveRules(context.profile(profile.getLanguage(), profile.getName()).rules(), dbRules);
329 builtInQProfileRepositoryRule.add(newLanguage(profile.getLanguage()), profile.getName(), false, activeRules);
332 private RulesProfileDto insertBuiltInProfile(String language) {
333 RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
334 db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
336 return ruleProfileDto;
339 private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) {
340 ActiveRuleDto dto = new ActiveRuleDto()
341 .setProfileUuid(profile.getUuid())
342 .setSeverity(severity.name())
343 .setRuleUuid(rule.getUuid())
344 .setCreatedAt(nextLong())
345 .setUpdatedAt(nextLong());
346 db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
350 private QProfileDto insertProfile(Consumer<QProfileDto> consumer) {
351 QProfileDto builtInQProfileDto = db.qualityProfiles().insert(consumer);
353 return builtInQProfileDto;
356 private static String newLanguageKey() {
357 return randomAlphanumeric(20).toLowerCase();