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 org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.profiles.RulesProfile;
27 import org.sonar.api.rules.ActiveRule;
28 import org.sonar.api.rules.RulePriority;
29 import org.sonar.api.utils.System2;
30 import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
31 import org.sonar.api.utils.log.LogTester;
32 import org.sonar.core.util.UuidFactoryFast;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.qualityprofile.ActiveRuleDto;
36 import org.sonar.db.qualityprofile.RulesProfileDto;
37 import org.sonar.db.rule.RuleDefinitionDto;
38 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
39 import org.sonar.server.rule.index.RuleIndex;
40 import org.sonar.server.tester.UserSessionRule;
41 import org.sonar.server.util.TypeValidations;
43 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
44 import static org.apache.commons.lang.math.RandomUtils.nextLong;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.verifyZeroInteractions;
48 import static org.sonar.api.rules.Rule.create;
49 import static org.sonar.api.rules.RulePriority.MAJOR;
50 import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
51 import static org.sonar.server.language.LanguageTesting.newLanguage;
53 public class RegisterQualityProfilesNotificationTest {
55 private System2 system2 = new AlwaysIncreasingSystem2();
57 public DbTester db = DbTester.create(system2);
59 public UserSessionRule userSessionRule = UserSessionRule.standalone();
61 public ExpectedException expectedException = ExpectedException.none();
63 public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
65 public LogTester logTester = new LogTester();
67 private DbClient dbClient = db.getDbClient();
68 private TypeValidations typeValidations = mock(TypeValidations.class);
69 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
70 private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer);
71 private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, mock(RuleIndex.class), new RuleActivatorContextFactory(dbClient), typeValidations, activeRuleIndexer,
73 private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer);
74 private BuiltInQualityProfilesNotification builtInQualityProfilesNotification = mock(BuiltInQualityProfilesNotification.class);
75 private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
76 builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification);
79 public void does_not_send_notification_on_new_profile() {
80 String language = newLanguageKey();
81 builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
82 builtInQProfileRepositoryRule.initialize();
86 verifyZeroInteractions(builtInQualityProfilesNotification);
90 public void does_not_send_notification_when_built_in_profile_is_not_updated() {
91 String language = newLanguageKey();
92 RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
93 RulesProfileDto dbProfile = insertBuiltInProfile(language);
94 activateRuleInDb(dbProfile, dbRule, MAJOR);
95 addPluginProfile(dbProfile, dbRule);
96 builtInQProfileRepositoryRule.initialize();
100 verifyZeroInteractions(builtInQualityProfilesNotification);
104 public void send_notification_when_built_in_profile_contains_new_rule() {
105 String language = newLanguageKey();
106 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
107 RulesProfileDto dbProfile = insertBuiltInProfile(language);
108 activateRuleInDb(dbProfile, existingRule, MAJOR);
109 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
110 addPluginProfile(dbProfile, existingRule, newRule);
111 builtInQProfileRepositoryRule.initialize();
115 verify(builtInQualityProfilesNotification).send();
118 private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
119 RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
120 Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));
121 builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
124 private RulesProfileDto insertBuiltInProfile(String language) {
125 RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
126 db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
128 return ruleProfileDto;
131 private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) {
132 ActiveRuleDto dto = new ActiveRuleDto()
133 .setProfileId(profile.getId())
134 .setSeverity(severity.name())
135 .setRuleId(rule.getId())
136 .setCreatedAt(nextLong())
137 .setUpdatedAt(nextLong());
138 db.getDbClient().activeRuleDao().insert(db.getSession(), dto);
142 private static String newLanguageKey() {
143 return randomAlphanumeric(20).toLowerCase();