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.pushapi.qualityprofile;
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.rule.RuleChange;
30 import org.sonar.core.util.rule.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.RuleDto;
38 import org.sonar.db.rule.RuleParamDto;
39 import org.sonar.server.qualityprofile.ActiveRuleChange;
41 import static java.util.List.of;
42 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.assertj.core.api.Assertions.tuple;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.verify;
47 import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
48 import static org.sonar.db.rule.RuleTesting.newCustomRule;
49 import static org.sonar.db.rule.RuleTesting.newTemplateRule;
50 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
52 public class QualityProfileChangeEventServiceImplTest {
55 public DbTester db = DbTester.create();
57 RuleActivatorEventsDistributor eventsDistributor = mock(RuleActivatorEventsDistributor.class);
59 public final QualityProfileChangeEventServiceImpl underTest = new QualityProfileChangeEventServiceImpl(db.getDbClient(), eventsDistributor);
62 public void distributeRuleChangeEvent() {
63 QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
66 RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
67 db.rules().insert(templateRule);
69 RuleDto rule1 = newCustomRule(templateRule)
71 .setRepositoryKey("repo")
72 .setRuleKey("ruleKey")
73 .setDescriptionFormat(RuleDto.Format.MARKDOWN)
74 .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection("uuid", "<div>line1\nline2</div>"));
75 db.rules().insert(rule1);
77 ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
79 ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
80 activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
82 Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
84 ProjectDto project = db.components().insertPrivateProjectDto();
85 db.qualityProfiles().associateWithProject(project, qualityProfileDto);
87 underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
89 ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
90 verify(eventsDistributor).pushEvent(eventCaptor.capture());
92 RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
93 assertThat(ruleSetChangedEvent).isNotNull();
94 assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent,
95 RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects)
96 .containsExactly("RuleSetChanged", "xoo", new String[]{project.getKey()});
98 assertThat(ruleSetChangedEvent.getActivatedRules())
99 .extracting(RuleChange::getKey, RuleChange::getLanguage,
100 RuleChange::getSeverity, RuleChange::getTemplateKey)
101 .containsExactly(tuple("repo:ruleKey", "xoo", null, "xoo:template-key"));
103 assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
104 ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
105 assertThat(actualParamChange)
106 .extracting(ParamChange::getKey, ParamChange::getValue)
107 .containsExactly("paramChangeKey", "paramChangeValue");
109 assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
114 public void publishRuleActivationToSonarLintClients() {
115 ProjectDto projectDao = new ProjectDto();
116 QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
117 activatedQualityProfile.setLanguage("xoo");
118 db.qualityProfiles().insert(activatedQualityProfile);
119 RuleDto rule1 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo").setRuleKey("ruleKey"));
120 RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
122 ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(activatedQualityProfile, rule1);
123 ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param).setValue(randomAlphanumeric(20));
124 db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule1, activeRuleParam1);
125 db.getSession().commit();
127 QProfileDto deactivatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
128 db.qualityProfiles().insert(deactivatedQualityProfile);
129 RuleDto rule2 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo2").setRuleKey("ruleKey2"));
130 RuleParamDto rule2Param = db.rules().insertRuleParam(rule2);
132 ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(deactivatedQualityProfile, rule2);
133 ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule2Param).setValue(randomAlphanumeric(20));
134 db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule2, activeRuleParam2);
135 db.getSession().commit();
137 underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
139 ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
140 verify(eventsDistributor).pushEvent(eventCaptor.capture());
142 RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
143 assertThat(ruleSetChangedEvent).isNotNull();
144 assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent,
145 RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects)
146 .containsExactly("RuleSetChanged", "xoo", new String[]{null});
149 assertThat(ruleSetChangedEvent.getActivatedRules())
150 .extracting(RuleChange::getKey, RuleChange::getLanguage,
151 RuleChange::getSeverity, RuleChange::getTemplateKey)
152 .containsExactly(tuple("repo:ruleKey", "xoo", rule1.getSeverityString(), null));
154 assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
155 ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
156 assertThat(actualParamChange)
157 .extracting(ParamChange::getKey, ParamChange::getValue)
158 .containsExactly(activeRuleParam1.getKey(), activeRuleParam1.getValue());
161 assertThat(ruleSetChangedEvent.getDeactivatedRules())
162 .containsExactly("repo2:ruleKey2");