3 * Copyright (C) 2009-2017 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.util.Arrays;
24 import java.util.Random;
25 import java.util.function.Consumer;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.mockito.ArgumentCaptor;
30 import org.sonar.api.profiles.RulesProfile;
31 import org.sonar.api.rule.Severity;
32 import org.sonar.api.rules.ActiveRule;
33 import org.sonar.api.rules.RulePriority;
34 import org.sonar.api.utils.System2;
35 import org.sonar.api.utils.log.LogTester;
36 import org.sonar.core.util.UuidFactoryFast;
37 import org.sonar.db.DbClient;
38 import org.sonar.db.DbTester;
39 import org.sonar.db.organization.OrganizationDto;
40 import org.sonar.db.qualityprofile.ActiveRuleDto;
41 import org.sonar.db.qualityprofile.QProfileDto;
42 import org.sonar.db.qualityprofile.RulesProfileDto;
43 import org.sonar.db.rule.RuleDefinitionDto;
44 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
45 import org.sonar.server.rule.index.RuleIndex;
46 import org.sonar.server.tester.UserSessionRule;
47 import org.sonar.server.util.TypeValidations;
49 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
50 import static org.apache.commons.lang.math.RandomUtils.nextLong;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.tuple;
53 import static org.mockito.Matchers.any;
54 import static org.mockito.Matchers.anyLong;
55 import static org.mockito.Matchers.eq;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.verify;
58 import static org.mockito.Mockito.verifyZeroInteractions;
59 import static org.mockito.Mockito.when;
60 import static org.sonar.api.rules.Rule.create;
61 import static org.sonar.api.rules.RulePriority.MAJOR;
62 import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
63 import static org.sonar.server.language.LanguageTesting.newLanguage;
64 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
65 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
66 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
68 public class RegisterQualityProfilesNotificationTest {
70 private static final Random RANDOM = new Random();
72 private System2 system2 = mock(System2.class);
74 public DbTester db = DbTester.create(system2);
76 public UserSessionRule userSessionRule = UserSessionRule.standalone();
78 public ExpectedException expectedException = ExpectedException.none();
80 public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
83 public LogTester logTester = new LogTester();
84 private DbClient dbClient = db.getDbClient();
85 private TypeValidations typeValidations = mock(TypeValidations.class);
86 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
87 private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer);
88 private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, mock(RuleIndex.class), new RuleActivatorContextFactory(dbClient), typeValidations, activeRuleIndexer,
90 private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer);
91 private BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification = mock(BuiltInQualityProfilesUpdateListener.class);
92 private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
93 builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification, system2);
96 public void does_not_send_notification_on_new_profile() {
97 String language = newLanguageKey();
98 builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
99 builtInQProfileRepositoryRule.initialize();
103 verifyZeroInteractions(builtInQualityProfilesNotification);
107 public void does_not_send_notification_when_built_in_profile_is_not_updated() {
108 String language = newLanguageKey();
109 RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
110 RulesProfileDto dbProfile = insertBuiltInProfile(language);
111 activateRuleInDb(dbProfile, dbRule, MAJOR);
112 addPluginProfile(dbProfile, dbRule);
113 builtInQProfileRepositoryRule.initialize();
117 verifyZeroInteractions(builtInQualityProfilesNotification);
121 public void send_notification_when_built_in_profile_contains_new_rule() {
122 String language = newLanguageKey();
123 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
124 RulesProfileDto dbProfile = insertBuiltInProfile(language);
125 activateRuleInDb(dbProfile, existingRule, MAJOR);
126 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
127 addPluginProfile(dbProfile, existingRule, newRule);
128 builtInQProfileRepositoryRule.initialize();
132 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
133 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
134 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
135 assertThat(updatedProfiles.keySet())
136 .extracting(QProfileName::getName, QProfileName::getLanguage)
137 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
138 assertThat(updatedProfiles.values())
139 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
140 .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
144 public void send_notification_when_built_in_profile_contains_deactivated_rule() {
145 String language = newLanguageKey();
146 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
147 RulesProfileDto dbProfile = insertBuiltInProfile(language);
148 activateRuleInDb(dbProfile, existingRule, MAJOR);
149 addPluginProfile(dbProfile);
150 builtInQProfileRepositoryRule.initialize();
154 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
155 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
156 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
157 assertThat(updatedProfiles.keySet())
158 .extracting(QProfileName::getName, QProfileName::getLanguage)
159 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
160 assertThat(updatedProfiles.values())
161 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
162 .containsExactlyInAnyOrder(tuple(existingRule.getId(), DEACTIVATED));
166 public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
167 String language = newLanguageKey();
169 RuleDefinitionDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
170 RuleDefinitionDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
171 RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
172 activateRuleInDb(dbProfile1, existingRule1, MAJOR);
173 addPluginProfile(dbProfile1, existingRule1, newRule1);
175 RuleDefinitionDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
176 RuleDefinitionDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
177 RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
178 activateRuleInDb(dbProfile2, existingRule2, MAJOR);
179 addPluginProfile(dbProfile2, existingRule2, newRule2);
180 builtInQProfileRepositoryRule.initialize();
184 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
185 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
186 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
187 assertThat(updatedProfiles.keySet())
188 .extracting(QProfileName::getName, QProfileName::getLanguage)
189 .containsExactlyInAnyOrder(
190 tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
191 tuple(dbProfile2.getName(), dbProfile2.getLanguage()));
192 assertThat(updatedProfiles.values())
193 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
194 .containsExactlyInAnyOrder(
195 tuple(newRule1.getId(), ACTIVATED),
196 tuple(newRule2.getId(), ACTIVATED));
200 public void do_not_include_inherited_quality_profile_change_on_new_rule() {
201 String language = newLanguageKey();
202 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
203 OrganizationDto organization = db.organizations().insert();
205 QProfileDto builtInQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
206 QProfileDto childQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
207 addPluginProfile(builtInQProfileDto, newRule);
208 builtInQProfileRepositoryRule.initialize();
212 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
213 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
214 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
215 assertThat(updatedProfiles.keySet())
216 .extracting(QProfileName::getName, QProfileName::getLanguage)
217 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
218 assertThat(updatedProfiles.values())
219 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
220 .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
224 public void do_not_include_inherited_quality_profile_change_on_existing_rule() {
225 String language = newLanguageKey();
226 RuleDefinitionDto ruleDefinitionDto = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
227 OrganizationDto organization = db.organizations().insert();
229 QProfileDto builtInQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
230 ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), builtInQProfileDto);
231 QProfileDto childQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
232 ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), childQProfileDto);
234 addPluginProfile(builtInQProfileDto, ruleDefinitionDto);
235 builtInQProfileRepositoryRule.initialize();
239 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
240 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
241 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
242 assertThat(updatedProfiles.keySet())
243 .extracting(QProfileName::getName, QProfileName::getLanguage)
244 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
245 assertThat(updatedProfiles.values())
246 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
247 .containsExactlyInAnyOrder(tuple(ruleDefinitionDto.getId(), UPDATED));
251 public void do_not_include_inherited_quality_profile_change_on_deactivated_rule() {
252 String language = newLanguageKey();
253 RuleDefinitionDto ruleDefinitionDto = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
254 OrganizationDto organization = db.organizations().insert();
256 QProfileDto builtInQProfileDto = insertProfile(organization,
257 orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
258 ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), builtInQProfileDto);
259 QProfileDto childQProfileDto = insertProfile(organization,
260 orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
261 ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), childQProfileDto);
264 addPluginProfile(builtInQProfileDto);
265 builtInQProfileRepositoryRule.initialize();
269 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
270 verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
271 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
272 assertThat(updatedProfiles.keySet())
273 .extracting(QProfileName::getName, QProfileName::getLanguage)
274 .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
275 assertThat(updatedProfiles.values())
276 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
277 .containsExactlyInAnyOrder(tuple(ruleDefinitionDto.getId(), DEACTIVATED));
281 public void send_start_and_end_date() {
282 String language = newLanguageKey();
283 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
284 RulesProfileDto dbProfile = insertBuiltInProfile(language);
285 activateRuleInDb(dbProfile, existingRule, MAJOR);
286 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
287 addPluginProfile(dbProfile, existingRule, newRule);
288 builtInQProfileRepositoryRule.initialize();
289 long startDate = RANDOM.nextInt(5000);
290 long endDate = startDate + RANDOM.nextInt(5000);
291 when(system2.now()).thenReturn(startDate, endDate);
295 verify(builtInQualityProfilesNotification).onChange(any(), eq(startDate), eq(endDate));
298 private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
299 RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
300 Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), null));
301 builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
304 private void addPluginProfile(QProfileDto profile, RuleDefinitionDto... dbRules) {
305 RulesProfile pluginProfile = RulesProfile.create(profile.getName(), profile.getLanguage());
306 Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), null));
307 builtInQProfileRepositoryRule.add(newLanguage(profile.getLanguage()), profile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
310 private RulesProfileDto insertBuiltInProfile(String language) {
311 RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
312 db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
314 return ruleProfileDto;
317 private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) {
318 ActiveRuleDto dto = new ActiveRuleDto()
319 .setProfileId(profile.getId())
320 .setSeverity(severity.name())
321 .setRuleId(rule.getId())
322 .setCreatedAt(nextLong())
323 .setUpdatedAt(nextLong());
324 db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
328 private QProfileDto insertProfile(OrganizationDto organization, Consumer<QProfileDto> consumer) {
329 QProfileDto builtInQProfileDto = db.qualityProfiles().insert(organization, consumer);
331 return builtInQProfileDto;
334 private static String newLanguageKey() {
335 return randomAlphanumeric(20).toLowerCase();