]> source.dussan.org Git - sonarqube.git/blob
1cab5c35a699e18b6f3aa8d288bd01875490ca51
[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.pushapi.qualityprofile;
21
22 import java.nio.charset.StandardCharsets;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Deque;
26 import java.util.Set;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.project.ProjectDto;
32 import org.sonar.db.pushevent.PushEventDto;
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;
40 import org.sonarqube.ws.Common;
41
42 import static java.util.List.of;
43 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.sonar.db.rule.RuleTesting.newCustomRule;
46 import static org.sonar.db.rule.RuleTesting.newTemplateRule;
47 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
48
49 public class QualityProfileChangeEventServiceImplTest {
50
51   @Rule
52   public DbTester db = DbTester.create();
53
54   public final QualityProfileChangeEventServiceImpl underTest = new QualityProfileChangeEventServiceImpl(db.getDbClient());
55
56   @Test
57   public void distributeRuleChangeEvent() {
58     QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
59
60     RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
61     db.rules().insert(templateRule);
62
63     RuleDto rule1 = newCustomRule(templateRule, "<div>line1\nline2</div>")
64       .setLanguage("xoo")
65       .setRepositoryKey("repo")
66       .setRuleKey("ruleKey")
67       .setDescriptionFormat(RuleDto.Format.MARKDOWN);
68     db.rules().insert(rule1);
69
70     ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
71
72     ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
73     activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
74
75     Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
76
77     ProjectDto project = db.components().insertPrivateProjectDto();
78     db.qualityProfiles().associateWithProject(project, qualityProfileDto);
79
80     underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
81
82     Deque<PushEventDto> events = db.getDbClient().pushEventDao()
83       .selectChunkByProjectUuids(db.getSession(), Set.of(project.getUuid()), 1l, null, 1);
84
85     assertThat(events).isNotEmpty().hasSize(1);
86     assertThat(events.getFirst())
87       .extracting(PushEventDto::getName, PushEventDto::getLanguage)
88       .contains("RuleSetChanged", "xoo");
89
90     String ruleSetChangedEvent = new String(events.getFirst().getPayload(), StandardCharsets.UTF_8);
91
92     assertThat(ruleSetChangedEvent)
93       .contains("\"activatedRules\":[{\"key\":\"repo:ruleKey\"," +
94         "\"language\":\"xoo\"," +
95         "\"templateKey\":\"xoo:template-key\"," +
96         "\"params\":[{\"key\":\"paramChangeKey\",\"value\":\"paramChangeValue\"}]}]," +
97         "\"deactivatedRules\":[]");
98   }
99
100   @Test
101   public void publishRuleActivationToSonarLintClients() {
102     ProjectDto projectDao = new ProjectDto().setUuid("project-uuid");
103     QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
104     activatedQualityProfile.setLanguage("xoo");
105     db.qualityProfiles().insert(activatedQualityProfile);
106     RuleDto rule1 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo").setRuleKey("ruleKey"));
107     RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
108
109     ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(activatedQualityProfile, rule1);
110     ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param).setValue(randomAlphanumeric(20));
111     db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule1, activeRuleParam1);
112     db.getSession().commit();
113
114     QProfileDto deactivatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
115     db.qualityProfiles().insert(deactivatedQualityProfile);
116     RuleDto rule2 = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo2").setRuleKey("ruleKey2"));
117     RuleParamDto rule2Param = db.rules().insertRuleParam(rule2);
118
119     ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(deactivatedQualityProfile, rule2);
120     ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule2Param).setValue(randomAlphanumeric(20));
121     db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule2, activeRuleParam2);
122     db.getSession().commit();
123
124     underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
125
126     Deque<PushEventDto> events = db.getDbClient().pushEventDao()
127       .selectChunkByProjectUuids(db.getSession(), Set.of(projectDao.getUuid()), 1l, null, 1);
128
129     assertThat(events).isNotEmpty().hasSize(1);
130     assertThat(events.getFirst())
131       .extracting(PushEventDto::getName, PushEventDto::getLanguage)
132       .contains("RuleSetChanged", "xoo");
133
134     String ruleSetChangedEvent = new String(events.getFirst().getPayload(), StandardCharsets.UTF_8);
135
136     assertThat(ruleSetChangedEvent)
137       .contains("\"activatedRules\":[{\"key\":\"repo:ruleKey\"," +
138         "\"language\":\"xoo\",\"severity\":\"" + Common.Severity.forNumber(rule1.getSeverity()).name() + "\"," +
139         "\"params\":[{\"key\":\"" + activeRuleParam1.getKey() + "\",\"value\":\"" + activeRuleParam1.getValue() + "\"}]}]," +
140         "\"deactivatedRules\":[\"repo2:ruleKey2\"]");
141   }
142
143 }