]> source.dussan.org Git - sonarqube.git/blob
80bb922ebaba8dd5ad0a81f25ebcedd303895313
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.ce.task.projectanalysis.step;
21
22 import org.assertj.core.data.MapEntry;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.RegisterExtension;
25 import org.sonar.api.rule.RuleStatus;
26 import org.sonar.api.rule.Severity;
27 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
28 import org.sonar.ce.task.projectanalysis.issue.DumbRule;
29 import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryRule;
30 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
31 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderImpl;
32 import org.sonar.ce.task.step.TestComputationStepContext;
33 import org.sonar.scanner.protocol.Constants;
34 import org.sonar.scanner.protocol.output.ScannerReport;
35
36 import static java.util.Arrays.asList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.db.rule.RuleTesting.XOO_X1;
39 import static org.sonar.db.rule.RuleTesting.XOO_X2;
40
41 public class LoadQualityProfilesStepTest {
42
43   @RegisterExtension
44   private final BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
45
46   @RegisterExtension
47   private final RuleRepositoryRule ruleRepository = new RuleRepositoryRule();
48
49   private final ActiveRulesHolderImpl activeRulesHolder = new ActiveRulesHolderImpl();
50   private final LoadQualityProfilesStep underTest = new LoadQualityProfilesStep(batchReportReader, activeRulesHolder, ruleRepository);
51
52   @Test
53   void feed_active_rules() {
54     ruleRepository.add(XOO_X1)
55       .setPluginKey("xoo");
56     ruleRepository.add(XOO_X2)
57       .setPluginKey("xoo");
58
59     ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
60       .setRuleRepository(XOO_X1.repository())
61       .setRuleKey(XOO_X1.rule())
62       .setSeverity(Constants.Severity.BLOCKER)
63       .setCreatedAt(1000L)
64       .setUpdatedAt(1200L);
65     batch1.getMutableParamsByKey().put("p1", "v1");
66
67     ScannerReport.ActiveRule.Builder batch2 = ScannerReport.ActiveRule.newBuilder()
68       .setRuleRepository(XOO_X2.repository()).setRuleKey(XOO_X2.rule()).setSeverity(Constants.Severity.MAJOR);
69     batchReportReader.putActiveRules(asList(batch1.build(), batch2.build()));
70
71     underTest.execute(new TestComputationStepContext());
72
73     assertThat(activeRulesHolder.getAll()).hasSize(2);
74
75     ActiveRule ar1 = activeRulesHolder.get(XOO_X1).get();
76     assertThat(ar1.getSeverity()).isEqualTo(Severity.BLOCKER);
77     assertThat(ar1.getParams()).containsExactly(MapEntry.entry("p1", "v1"));
78     assertThat(ar1.getPluginKey()).isEqualTo("xoo");
79     assertThat(ar1.getUpdatedAt()).isEqualTo(1200L);
80
81     ActiveRule ar2 = activeRulesHolder.get(XOO_X2).get();
82     assertThat(ar2.getSeverity()).isEqualTo(Severity.MAJOR);
83     assertThat(ar2.getParams()).isEmpty();
84     assertThat(ar2.getPluginKey()).isEqualTo("xoo");
85     assertThat(ar1.getUpdatedAt()).isEqualTo(1200L);
86   }
87
88   @Test
89   void ignore_rules_with_status_REMOVED() {
90     ruleRepository.add(new DumbRule(XOO_X1).setStatus(RuleStatus.REMOVED));
91
92     ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
93       .setRuleRepository(XOO_X1.repository()).setRuleKey(XOO_X1.rule())
94       .setSeverity(Constants.Severity.BLOCKER);
95     batchReportReader.putActiveRules(asList(batch1.build()));
96
97     underTest.execute(new TestComputationStepContext());
98
99     assertThat(activeRulesHolder.getAll()).isEmpty();
100   }
101
102   @Test
103   void ignore_not_found_rules() {
104     ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
105       .setRuleRepository(XOO_X1.repository()).setRuleKey(XOO_X1.rule())
106       .setSeverity(Constants.Severity.BLOCKER);
107     batchReportReader.putActiveRules(asList(batch1.build()));
108
109     underTest.execute(new TestComputationStepContext());
110
111     assertThat(activeRulesHolder.getAll()).isEmpty();
112   }
113 }