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 java.util.Arrays;
23 import java.util.List;
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;
57 public class RegisterQualityProfilesNotificationTest {
59 private System2 system2 = new AlwaysIncreasingSystem2();
61 public DbTester db = DbTester.create(system2);
63 public UserSessionRule userSessionRule = UserSessionRule.standalone();
65 public ExpectedException expectedException = ExpectedException.none();
67 public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
69 public LogTester logTester = new LogTester();
71 private DbClient dbClient = db.getDbClient();
72 private TypeValidations typeValidations = mock(TypeValidations.class);
73 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
74 private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer);
75 private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, mock(RuleIndex.class), new RuleActivatorContextFactory(dbClient), typeValidations, activeRuleIndexer,
77 private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer);
78 private BuiltInQualityProfilesNotificationSender builtInQualityProfilesNotification = mock(BuiltInQualityProfilesNotificationSender.class);
79 private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
80 builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification);
83 public void does_not_send_notification_on_new_profile() {
84 String language = newLanguageKey();
85 builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
86 builtInQProfileRepositoryRule.initialize();
90 verifyZeroInteractions(builtInQualityProfilesNotification);
94 public void does_not_send_notification_when_built_in_profile_is_not_updated() {
95 String language = newLanguageKey();
96 RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
97 RulesProfileDto dbProfile = insertBuiltInProfile(language);
98 activateRuleInDb(dbProfile, dbRule, MAJOR);
99 addPluginProfile(dbProfile, dbRule);
100 builtInQProfileRepositoryRule.initialize();
104 verifyZeroInteractions(builtInQualityProfilesNotification);
108 public void send_notification_when_built_in_profile_contains_new_rule() {
109 String language = newLanguageKey();
110 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
111 RulesProfileDto dbProfile = insertBuiltInProfile(language);
112 activateRuleInDb(dbProfile, existingRule, MAJOR);
113 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
114 addPluginProfile(dbProfile, existingRule, newRule);
115 builtInQProfileRepositoryRule.initialize();
119 ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
120 verify(builtInQualityProfilesNotification).send(captor.capture());
121 List<QProfileName> updatedProfiles = captor.<List<QProfileName>>getValue();
122 assertThat(updatedProfiles)
123 .extracting(QProfileName::getName, QProfileName::getLanguage)
124 .containsExactlyInAnyOrder(tuple(dbProfile.getName(), dbProfile.getLanguage()));
128 public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
129 String language = newLanguageKey();
130 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
131 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
133 RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
134 activateRuleInDb(dbProfile1, existingRule, MAJOR);
135 addPluginProfile(dbProfile1, existingRule, newRule);
137 RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
138 activateRuleInDb(dbProfile2, existingRule, MAJOR);
139 addPluginProfile(dbProfile2, existingRule, newRule);
140 builtInQProfileRepositoryRule.initialize();
144 ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
145 verify(builtInQualityProfilesNotification).send(captor.capture());
146 List<QProfileName> updatedProfiles = captor.<List<QProfileName>>getValue();
147 assertThat(updatedProfiles)
148 .extracting(QProfileName::getName, QProfileName::getLanguage)
149 .containsExactlyInAnyOrder(
150 tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
151 tuple(dbProfile2.getName(), dbProfile2.getLanguage())
155 private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
156 RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
157 Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));
158 builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
161 private RulesProfileDto insertBuiltInProfile(String language) {
162 RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
163 db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
165 return ruleProfileDto;
168 private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) {
169 ActiveRuleDto dto = new ActiveRuleDto()
170 .setProfileId(profile.getId())
171 .setSeverity(severity.name())
172 .setRuleId(rule.getId())
173 .setCreatedAt(nextLong())
174 .setUpdatedAt(nextLong());
175 db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
179 private static String newLanguageKey() {
180 return randomAlphanumeric(20).toLowerCase();