]> source.dussan.org Git - sonarqube.git/blob
a67e8ded25882c6e2c4886eb28fa4a36fac3b605
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.builtin;
21
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.RuleDto;
38 import org.sonar.db.rule.RuleParamDto;
39 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventServiceImpl;
40 import org.sonar.server.pushapi.qualityprofile.RuleActivatorEventsDistributor;
41 import org.sonar.server.qualityprofile.ActiveRuleChange;
42
43 import static java.util.List.of;
44 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.assertj.core.api.Assertions.tuple;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.verify;
49 import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
50 import static org.sonar.db.rule.RuleTesting.newCustomRule;
51 import static org.sonar.db.rule.RuleTesting.newTemplateRule;
52 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
53
54 public class QualityProfileChangeEventServiceImplTest {
55
56   @Rule
57   public DbTester db = DbTester.create();
58
59   RuleActivatorEventsDistributor eventsDistributor = mock(RuleActivatorEventsDistributor.class);
60
61   public final QualityProfileChangeEventServiceImpl underTest = new QualityProfileChangeEventServiceImpl(db.getDbClient(), eventsDistributor);
62
63   @Test
64   public void distributeRuleChangeEvent() {
65     QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
66
67     // Template rule
68     RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
69     db.rules().insert(templateRule);
70     // Custom rule
71     RuleDto rule1 = newCustomRule(templateRule)
72       .setLanguage("xoo")
73       .setRepositoryKey("repo")
74       .setRuleKey("ruleKey")
75       .setDescriptionFormat(RuleDto.Format.MARKDOWN)
76       .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection("uuid", "<div>line1\nline2</div>"));
77     db.rules().insert(rule1);
78
79     ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
80
81     ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
82     activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
83
84     Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
85
86     ProjectDto project = db.components().insertPrivateProjectDto();
87     db.qualityProfiles().associateWithProject(project, qualityProfileDto);
88
89     underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
90
91     ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
92     verify(eventsDistributor).pushEvent(eventCaptor.capture());
93
94     RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
95     assertThat(ruleSetChangedEvent).isNotNull();
96     assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent,
97         RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects)
98       .containsExactly("RuleSetChanged", "xoo", new String[]{project.getKey()});
99
100     assertThat(ruleSetChangedEvent.getActivatedRules())
101       .extracting(RuleChange::getKey, RuleChange::getLanguage,
102         RuleChange::getSeverity, RuleChange::getTemplateKey)
103       .containsExactly(tuple("repo:ruleKey", "xoo", null, "xoo:template-key"));
104
105     assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
106     ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
107     assertThat(actualParamChange)
108       .extracting(ParamChange::getKey, ParamChange::getValue)
109       .containsExactly("paramChangeKey", "paramChangeValue");
110
111     assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
112
113   }
114
115   @Test
116   public void publishRuleActivationToSonarLintClients() {
117     ProjectDto projectDao = new ProjectDto();
118     QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
119     activatedQualityProfile.setLanguage("xoo");
120     db.qualityProfiles().insert(activatedQualityProfile);
121     RuleDto rule1 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo").setRuleKey("ruleKey"));
122     RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
123
124     ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(activatedQualityProfile, rule1);
125     ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param).setValue(randomAlphanumeric(20));
126     db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule1, activeRuleParam1);
127     db.getSession().commit();
128
129     QProfileDto deactivatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
130     db.qualityProfiles().insert(deactivatedQualityProfile);
131     RuleDto rule2 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo2").setRuleKey("ruleKey2"));
132     RuleParamDto rule2Param = db.rules().insertRuleParam(rule2);
133
134     ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(deactivatedQualityProfile, rule2);
135     ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule2Param).setValue(randomAlphanumeric(20));
136     db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule2, activeRuleParam2);
137     db.getSession().commit();
138
139     underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
140
141     ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
142     verify(eventsDistributor).pushEvent(eventCaptor.capture());
143
144     RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
145     assertThat(ruleSetChangedEvent).isNotNull();
146     assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent,
147         RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects)
148       .containsExactly("RuleSetChanged", "xoo", new String[]{null});
149
150     // activated rule
151     assertThat(ruleSetChangedEvent.getActivatedRules())
152       .extracting(RuleChange::getKey, RuleChange::getLanguage,
153         RuleChange::getSeverity, RuleChange::getTemplateKey)
154       .containsExactly(tuple("repo:ruleKey", "xoo", rule1.getSeverityString(), null));
155
156     assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
157     ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
158     assertThat(actualParamChange)
159       .extracting(ParamChange::getKey, ParamChange::getValue)
160       .containsExactly(activeRuleParam1.getKey(), activeRuleParam1.getValue());
161
162     // deactivated rule
163     assertThat(ruleSetChangedEvent.getDeactivatedRules())
164       .containsExactly("repo2:ruleKey2");
165   }
166
167 }