]> source.dussan.org Git - sonarqube.git/blob
6fbba727fc6521cf8398d04948adc8aa1a8bc022
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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;
21
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;
48
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;
67
68 public class RegisterQualityProfilesNotificationTest {
69
70   private static final Random RANDOM = new Random();
71
72   private System2 system2 = mock(System2.class);
73   @Rule
74   public DbTester db = DbTester.create(system2);
75   @Rule
76   public UserSessionRule userSessionRule = UserSessionRule.standalone();
77   @Rule
78   public ExpectedException expectedException = ExpectedException.none();
79   @Rule
80   public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
81
82   @Rule
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,
89     userSessionRule);
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);
94
95   @Test
96   public void does_not_send_notification_on_new_profile() {
97     String language = newLanguageKey();
98     builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
99     builtInQProfileRepositoryRule.initialize();
100
101     underTest.start();
102
103     verifyZeroInteractions(builtInQualityProfilesNotification);
104   }
105
106   @Test
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();
114
115     underTest.start();
116
117     verifyZeroInteractions(builtInQualityProfilesNotification);
118   }
119
120   @Test
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();
129
130     underTest.start();
131
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));
141   }
142
143   @Test
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();
151
152     underTest.start();
153
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));
163   }
164
165   @Test
166   public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
167     String language = newLanguageKey();
168
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);
174
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();
181
182     underTest.start();
183
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));
197   }
198
199   @Test
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();
204
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();
209
210     underTest.start();
211
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));
221   }
222
223   @Test
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();
228
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);
233     db.commit();
234     addPluginProfile(builtInQProfileDto, ruleDefinitionDto);
235     builtInQProfileRepositoryRule.initialize();
236
237     underTest.start();
238
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));
248   }
249
250   @Test
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();
255
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);
262     db.commit();
263
264     addPluginProfile(builtInQProfileDto);
265     builtInQProfileRepositoryRule.initialize();
266
267     underTest.start();
268
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));
278   }
279
280   @Test
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);
292
293     underTest.start();
294
295     verify(builtInQualityProfilesNotification).onChange(any(), eq(startDate), eq(endDate));
296   }
297
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]));
302   }
303
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]));
308   }
309
310   private RulesProfileDto insertBuiltInProfile(String language) {
311     RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
312     db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
313     db.commit();
314     return ruleProfileDto;
315   }
316
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);
325     db.commit();
326   }
327
328   private QProfileDto insertProfile(OrganizationDto organization, Consumer<QProfileDto> consumer) {
329     QProfileDto builtInQProfileDto = db.qualityProfiles().insert(organization, consumer);
330     db.commit();
331     return builtInQProfileDto;
332   }
333
334   private static String newLanguageKey() {
335     return randomAlphanumeric(20).toLowerCase();
336   }
337 }