]> source.dussan.org Git - sonarqube.git/blob
77b5db39783d2ecc0d57b6ff64a659daf868987d
[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.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;
43
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;
54
55 public class QualityProfileChangeEventServiceImplTest {
56
57   @Rule
58   public DbTester db = DbTester.create();
59
60   RuleActivatorEventsDistributor eventsDistributor = mock(RuleActivatorEventsDistributor.class);
61
62   public final QualityProfileChangeEventServiceImpl underTest = new QualityProfileChangeEventServiceImpl(db.getDbClient(), eventsDistributor);
63
64   @Test
65   public void distributeRuleChangeEvent() {
66     QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
67
68     // Template rule
69     RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
70     db.rules().insert(templateRule.getDefinition());
71     // Custom rule
72     RuleDefinitionDto rule1 = newCustomRule(templateRule.getDefinition())
73       .setLanguage("xoo")
74       .setRepositoryKey("repo")
75       .setRuleKey("ruleKey")
76       .setDescriptionFormat(RuleDto.Format.MARKDOWN)
77       .addRuleDescriptionSectionDto(createDefaultRuleDescriptionSection("<div>line1\nline2</div>"));
78     db.rules().insert(rule1);
79
80     ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
81
82     ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
83     activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
84
85     Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
86
87     ProjectDto project = db.components().insertPrivateProjectDto();
88     db.qualityProfiles().associateWithProject(project, qualityProfileDto);
89
90     underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
91
92     ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
93     verify(eventsDistributor).pushEvent(eventCaptor.capture());
94
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()});
100
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"));
105
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");
111
112     assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
113
114   }
115
116   @Test
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);
124
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();
129
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);
134
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();
139
140     underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
141
142     ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
143     verify(eventsDistributor).pushEvent(eventCaptor.capture());
144
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});
150
151     // activated rule
152     assertThat(ruleSetChangedEvent.getActivatedRules())
153       .extracting(RuleChange::getKey, RuleChange::getLanguage,
154         RuleChange::getSeverity, RuleChange::getTemplateKey)
155       .containsExactly(tuple("repo:ruleKey", "xoo", rule1.getSeverityString(), null));
156
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());
162
163     // deactivated rule
164     assertThat(ruleSetChangedEvent.getDeactivatedRules())
165       .containsExactly("repo2:ruleKey2");
166   }
167
168 }