3 * Copyright (C) 2009-2022 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.builtin;
22 import java.util.Collection;
23 import java.util.Collections;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.core.util.ParamChange;
29 import org.sonar.core.util.RuleChange;
30 import org.sonar.core.util.RuleSetChangedEvent;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.project.ProjectDto;
33 import org.sonar.db.qualityprofile.ActiveRuleDto;
34 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.db.qualityprofile.QualityProfileTesting;
37 import org.sonar.db.rule.RuleDefinitionDto;
38 import org.sonar.db.rule.RuleDto;
39 import org.sonar.db.rule.RuleParamDto;
40 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventServiceImpl;
41 import org.sonar.server.pushapi.qualityprofile.RuleActivatorEventsDistributor;
42 import org.sonar.server.qualityprofile.ActiveRuleChange;
44 import static java.util.List.of;
45 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.assertj.core.api.Assertions.tuple;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.verify;
50 import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
51 import static org.sonar.db.rule.RuleTesting.newCustomRule;
52 import static org.sonar.db.rule.RuleTesting.newTemplateRule;
53 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
55 public class QualityProfileChangeEventServiceImplTest {
58 public DbTester db = DbTester.create();
60 RuleActivatorEventsDistributor eventsDistributor = mock(RuleActivatorEventsDistributor.class);
62 public final QualityProfileChangeEventServiceImpl underTest = new QualityProfileChangeEventServiceImpl(db.getDbClient(), eventsDistributor);
65 public void distributeRuleChangeEvent() {
66 QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
69 RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
70 db.rules().insert(templateRule.getDefinition());
72 RuleDefinitionDto rule1 = newCustomRule(templateRule.getDefinition())
74 .setRepositoryKey("repo")
75 .setRuleKey("ruleKey")
76 .setDescriptionFormat(RuleDto.Format.MARKDOWN)
77 .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection("uuid", "<div>line1\nline2</div>"));
78 db.rules().insert(rule1);
80 ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
82 ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
83 activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
85 Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
87 ProjectDto project = db.components().insertPrivateProjectDto();
88 db.qualityProfiles().associateWithProject(project, qualityProfileDto);
90 underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
92 ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
93 verify(eventsDistributor).pushEvent(eventCaptor.capture());
95 RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
96 assertThat(ruleSetChangedEvent).isNotNull();
97 assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent,
98 RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects)
99 .containsExactly("RuleSetChanged", "xoo", new String[]{project.getKey()});
101 assertThat(ruleSetChangedEvent.getActivatedRules())
102 .extracting(RuleChange::getKey, RuleChange::getLanguage,
103 RuleChange::getSeverity, RuleChange::getTemplateKey)
104 .containsExactly(tuple("repo:ruleKey", "xoo", null, "xoo:template-key"));
106 assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
107 ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
108 assertThat(actualParamChange)
109 .extracting(ParamChange::getKey, ParamChange::getValue)
110 .containsExactly("paramChangeKey", "paramChangeValue");
112 assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
117 public void publishRuleActivationToSonarLintClients() {
118 ProjectDto projectDao = new ProjectDto();
119 QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
120 activatedQualityProfile.setLanguage("xoo");
121 db.qualityProfiles().insert(activatedQualityProfile);
122 RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo").setRuleKey("ruleKey"));
123 RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
125 ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(activatedQualityProfile, rule1);
126 ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param).setValue(randomAlphanumeric(20));
127 db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule1, activeRuleParam1);
128 db.getSession().commit();
130 QProfileDto deactivatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
131 db.qualityProfiles().insert(deactivatedQualityProfile);
132 RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo2").setRuleKey("ruleKey2"));
133 RuleParamDto rule2Param = db.rules().insertRuleParam(rule2);
135 ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(deactivatedQualityProfile, rule2);
136 ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule2Param).setValue(randomAlphanumeric(20));
137 db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule2, activeRuleParam2);
138 db.getSession().commit();
140 underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
142 ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
143 verify(eventsDistributor).pushEvent(eventCaptor.capture());
145 RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
146 assertThat(ruleSetChangedEvent).isNotNull();
147 assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent,
148 RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects)
149 .containsExactly("RuleSetChanged", "xoo", new String[]{null});
152 assertThat(ruleSetChangedEvent.getActivatedRules())
153 .extracting(RuleChange::getKey, RuleChange::getLanguage,
154 RuleChange::getSeverity, RuleChange::getTemplateKey)
155 .containsExactly(tuple("repo:ruleKey", "xoo", rule1.getSeverityString(), null));
157 assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
158 ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
159 assertThat(actualParamChange)
160 .extracting(ParamChange::getKey, ParamChange::getValue)
161 .containsExactly(activeRuleParam1.getKey(), activeRuleParam1.getValue());
164 assertThat(ruleSetChangedEvent.getDeactivatedRules())
165 .containsExactly("repo2:ruleKey2");