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 org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.mockito.ArgumentCaptor;
28 import org.sonar.api.profiles.RulesProfile;
29 import org.sonar.api.rules.ActiveRule;
30 import org.sonar.api.rules.RulePriority;
31 import org.sonar.api.utils.System2;
32 import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
33 import org.sonar.api.utils.log.LogTester;
34 import org.sonar.core.util.UuidFactoryFast;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.qualityprofile.ActiveRuleDto;
38 import org.sonar.db.qualityprofile.RulesProfileDto;
39 import org.sonar.db.rule.RuleDefinitionDto;
40 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
41 import org.sonar.server.rule.index.RuleIndex;
42 import org.sonar.server.tester.UserSessionRule;
43 import org.sonar.server.util.TypeValidations;
45 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
46 import static org.apache.commons.lang.math.RandomUtils.nextLong;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.assertj.core.api.Assertions.tuple;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.verify;
51 import static org.mockito.Mockito.verifyZeroInteractions;
52 import static org.sonar.api.rules.Rule.create;
53 import static org.sonar.api.rules.RulePriority.MAJOR;
54 import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
55 import static org.sonar.server.language.LanguageTesting.newLanguage;
56 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
57 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
59 public class RegisterQualityProfilesNotificationTest {
61 private System2 system2 = new AlwaysIncreasingSystem2();
63 public DbTester db = DbTester.create(system2);
65 public UserSessionRule userSessionRule = UserSessionRule.standalone();
67 public ExpectedException expectedException = ExpectedException.none();
69 public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
71 public LogTester logTester = new LogTester();
73 private DbClient dbClient = db.getDbClient();
74 private TypeValidations typeValidations = mock(TypeValidations.class);
75 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
76 private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer);
77 private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, mock(RuleIndex.class), new RuleActivatorContextFactory(dbClient), typeValidations, activeRuleIndexer,
79 private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer);
80 private BuiltInQualityProfilesNotificationSender builtInQualityProfilesNotification = mock(BuiltInQualityProfilesNotificationSender.class);
81 private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
82 builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification);
85 public void does_not_send_notification_on_new_profile() {
86 String language = newLanguageKey();
87 builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
88 builtInQProfileRepositoryRule.initialize();
92 verifyZeroInteractions(builtInQualityProfilesNotification);
96 public void does_not_send_notification_when_built_in_profile_is_not_updated() {
97 String language = newLanguageKey();
98 RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
99 RulesProfileDto dbProfile = insertBuiltInProfile(language);
100 activateRuleInDb(dbProfile, dbRule, MAJOR);
101 addPluginProfile(dbProfile, dbRule);
102 builtInQProfileRepositoryRule.initialize();
106 verifyZeroInteractions(builtInQualityProfilesNotification);
110 public void send_notification_when_built_in_profile_contains_new_rule() {
111 String language = newLanguageKey();
112 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
113 RulesProfileDto dbProfile = insertBuiltInProfile(language);
114 activateRuleInDb(dbProfile, existingRule, MAJOR);
115 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
116 addPluginProfile(dbProfile, existingRule, newRule);
117 builtInQProfileRepositoryRule.initialize();
121 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
122 verify(builtInQualityProfilesNotification).send(captor.capture());
123 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
124 assertThat(updatedProfiles.keySet())
125 .extracting(QProfileName::getName, QProfileName::getLanguage)
126 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
127 assertThat(updatedProfiles.values())
128 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
129 .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
133 public void send_notification_when_built_in_profile_contains_deactivated_rule() {
134 String language = newLanguageKey();
135 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
136 RulesProfileDto dbProfile = insertBuiltInProfile(language);
137 activateRuleInDb(dbProfile, existingRule, MAJOR);
138 addPluginProfile(dbProfile);
139 builtInQProfileRepositoryRule.initialize();
143 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
144 verify(builtInQualityProfilesNotification).send(captor.capture());
145 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
146 assertThat(updatedProfiles.keySet())
147 .extracting(QProfileName::getName, QProfileName::getLanguage)
148 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
149 assertThat(updatedProfiles.values())
150 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
151 .containsExactlyInAnyOrder(tuple(existingRule.getId(), DEACTIVATED));
155 public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
156 String language = newLanguageKey();
158 RuleDefinitionDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
159 RuleDefinitionDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
160 RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
161 activateRuleInDb(dbProfile1, existingRule1, MAJOR);
162 addPluginProfile(dbProfile1, existingRule1, newRule1);
164 RuleDefinitionDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
165 RuleDefinitionDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
166 RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
167 activateRuleInDb(dbProfile2, existingRule2, MAJOR);
168 addPluginProfile(dbProfile2, existingRule2, newRule2);
169 builtInQProfileRepositoryRule.initialize();
173 ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
174 verify(builtInQualityProfilesNotification).send(captor.capture());
175 Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
176 assertThat(updatedProfiles.keySet())
177 .extracting(QProfileName::getName, QProfileName::getLanguage)
178 .containsExactlyInAnyOrder(
179 tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
180 tuple(dbProfile2.getName(), dbProfile2.getLanguage())
182 assertThat(updatedProfiles.values())
183 .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
184 .containsExactlyInAnyOrder(
185 tuple(newRule1.getId(), ACTIVATED),
186 tuple(newRule2.getId(), ACTIVATED)
190 private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
191 RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
192 Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));
193 builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
196 private RulesProfileDto insertBuiltInProfile(String language) {
197 RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
198 db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
200 return ruleProfileDto;
203 private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) {
204 ActiveRuleDto dto = new ActiveRuleDto()
205 .setProfileId(profile.getId())
206 .setSeverity(severity.name())
207 .setRuleId(rule.getId())
208 .setCreatedAt(nextLong())
209 .setUpdatedAt(nextLong());
210 db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
214 private static String newLanguageKey() {
215 return randomAlphanumeric(20).toLowerCase();