]> source.dussan.org Git - sonarqube.git/blob
2bbfb98a9e2ece61aca418f4a15c1874fd429d6b
[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 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.organization.OrganizationDto;
38 import org.sonar.db.qualityprofile.ActiveRuleDto;
39 import org.sonar.db.qualityprofile.QProfileDto;
40 import org.sonar.db.qualityprofile.RulesProfileDto;
41 import org.sonar.db.rule.RuleDefinitionDto;
42 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
43 import org.sonar.server.rule.index.RuleIndex;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.util.TypeValidations;
46
47 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
48 import static org.apache.commons.lang.math.RandomUtils.nextLong;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.assertj.core.api.Assertions.tuple;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.verify;
53 import static org.mockito.Mockito.verifyZeroInteractions;
54 import static org.sonar.api.rules.Rule.create;
55 import static org.sonar.api.rules.RulePriority.MAJOR;
56 import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
57 import static org.sonar.server.language.LanguageTesting.newLanguage;
58 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
59 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
60
61 public class RegisterQualityProfilesNotificationTest {
62
63   private System2 system2 = new AlwaysIncreasingSystem2();
64   @Rule
65   public DbTester db = DbTester.create(system2);
66   @Rule
67   public UserSessionRule userSessionRule = UserSessionRule.standalone();
68   @Rule
69   public ExpectedException expectedException = ExpectedException.none();
70   @Rule
71   public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
72   @Rule
73   public LogTester logTester = new LogTester();
74
75   private DbClient dbClient = db.getDbClient();
76   private TypeValidations typeValidations = mock(TypeValidations.class);
77   private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
78   private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer);
79   private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, mock(RuleIndex.class), new RuleActivatorContextFactory(dbClient), typeValidations, activeRuleIndexer,
80     userSessionRule);
81   private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer);
82   private BuiltInQualityProfilesNotificationSender builtInQualityProfilesNotification = mock(BuiltInQualityProfilesNotificationSender.class);
83   private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
84     builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification);
85
86   @Test
87   public void does_not_send_notification_on_new_profile() {
88     String language = newLanguageKey();
89     builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
90     builtInQProfileRepositoryRule.initialize();
91
92     underTest.start();
93
94     verifyZeroInteractions(builtInQualityProfilesNotification);
95   }
96
97   @Test
98   public void does_not_send_notification_when_built_in_profile_is_not_updated() {
99     String language = newLanguageKey();
100     RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
101     RulesProfileDto dbProfile = insertBuiltInProfile(language);
102     activateRuleInDb(dbProfile, dbRule, MAJOR);
103     addPluginProfile(dbProfile, dbRule);
104     builtInQProfileRepositoryRule.initialize();
105
106     underTest.start();
107
108     verifyZeroInteractions(builtInQualityProfilesNotification);
109   }
110
111   @Test
112   public void send_notification_when_built_in_profile_contains_new_rule() {
113     String language = newLanguageKey();
114     RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
115     RulesProfileDto dbProfile = insertBuiltInProfile(language);
116     activateRuleInDb(dbProfile, existingRule, MAJOR);
117     RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
118     addPluginProfile(dbProfile, existingRule, newRule);
119     builtInQProfileRepositoryRule.initialize();
120
121     underTest.start();
122
123     ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
124     verify(builtInQualityProfilesNotification).send(captor.capture());
125     Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
126     assertThat(updatedProfiles.keySet())
127       .extracting(QProfileName::getName, QProfileName::getLanguage)
128       .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
129     assertThat(updatedProfiles.values())
130       .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
131       .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
132   }
133
134   @Test
135   public void send_notification_when_built_in_profile_contains_deactivated_rule() {
136     String language = newLanguageKey();
137     RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
138     RulesProfileDto dbProfile = insertBuiltInProfile(language);
139     activateRuleInDb(dbProfile, existingRule, MAJOR);
140     addPluginProfile(dbProfile);
141     builtInQProfileRepositoryRule.initialize();
142
143     underTest.start();
144
145     ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
146     verify(builtInQualityProfilesNotification).send(captor.capture());
147     Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
148     assertThat(updatedProfiles.keySet())
149       .extracting(QProfileName::getName, QProfileName::getLanguage)
150       .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
151     assertThat(updatedProfiles.values())
152       .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
153       .containsExactlyInAnyOrder(tuple(existingRule.getId(), DEACTIVATED));
154   }
155
156   @Test
157   public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
158     String language = newLanguageKey();
159
160     RuleDefinitionDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
161     RuleDefinitionDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
162     RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
163     activateRuleInDb(dbProfile1, existingRule1, MAJOR);
164     addPluginProfile(dbProfile1, existingRule1, newRule1);
165
166     RuleDefinitionDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
167     RuleDefinitionDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
168     RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
169     activateRuleInDb(dbProfile2, existingRule2, MAJOR);
170     addPluginProfile(dbProfile2, existingRule2, newRule2);
171     builtInQProfileRepositoryRule.initialize();
172
173     underTest.start();
174
175     ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
176     verify(builtInQualityProfilesNotification).send(captor.capture());
177     Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
178     assertThat(updatedProfiles.keySet())
179       .extracting(QProfileName::getName, QProfileName::getLanguage)
180       .containsExactlyInAnyOrder(
181         tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
182         tuple(dbProfile2.getName(), dbProfile2.getLanguage())
183       );
184     assertThat(updatedProfiles.values())
185       .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
186       .containsExactlyInAnyOrder(
187         tuple(newRule1.getId(), ACTIVATED),
188         tuple(newRule2.getId(), ACTIVATED)
189       );
190   }
191
192   @Test
193   public void do_not_include_inherited_quality_profile_changes() {
194     String language = newLanguageKey();
195
196     OrganizationDto organization = db.organizations().insert();
197     QProfileDto builtInQProfileDto = db.qualityProfiles().insert(organization, orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
198     QProfileDto customQProfileDto = db.qualityProfiles().insert(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
199     db.commit();
200     RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
201
202     RulesProfile pluginProfile = RulesProfile.create(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage());
203     pluginProfile.activateRule(create(newRule.getRepositoryKey(), newRule.getRuleKey()), MAJOR);
204     builtInQProfileRepositoryRule.add(newLanguage(builtInQProfileDto.getLanguage()), builtInQProfileDto.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
205     builtInQProfileRepositoryRule.initialize();
206
207     underTest.start();
208
209     ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
210     verify(builtInQualityProfilesNotification).send(captor.capture());
211     Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
212     assertThat(updatedProfiles.keySet())
213       .extracting(QProfileName::getName, QProfileName::getLanguage)
214       .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
215     assertThat(updatedProfiles.values())
216       .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
217       .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
218   }
219
220
221   private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
222     RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
223     Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));
224     builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
225   }
226
227   private RulesProfileDto insertBuiltInProfile(String language) {
228     RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
229     db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
230     db.commit();
231     return ruleProfileDto;
232   }
233
234   private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) {
235     ActiveRuleDto dto = new ActiveRuleDto()
236       .setProfileId(profile.getId())
237       .setSeverity(severity.name())
238       .setRuleId(rule.getId())
239       .setCreatedAt(nextLong())
240       .setUpdatedAt(nextLong());
241     db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
242     db.commit();
243   }
244
245   private static String newLanguageKey() {
246     return randomAlphanumeric(20).toLowerCase();
247   }
248 }