3 * Copyright (C) 2009-2024 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.ce.task.projectanalysis.step;
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;
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;
41 public class LoadQualityProfilesStepTest {
44 private final BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
47 private final RuleRepositoryRule ruleRepository = new RuleRepositoryRule();
49 private final ActiveRulesHolderImpl activeRulesHolder = new ActiveRulesHolderImpl();
50 private final LoadQualityProfilesStep underTest = new LoadQualityProfilesStep(batchReportReader, activeRulesHolder, ruleRepository);
53 void feed_active_rules() {
54 ruleRepository.add(XOO_X1)
56 ruleRepository.add(XOO_X2)
59 ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
60 .setRuleRepository(XOO_X1.repository())
61 .setRuleKey(XOO_X1.rule())
62 .setSeverity(Constants.Severity.BLOCKER)
65 batch1.getMutableParamsByKey().put("p1", "v1");
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()));
71 underTest.execute(new TestComputationStepContext());
73 assertThat(activeRulesHolder.getAll()).hasSize(2);
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);
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);
89 void ignore_rules_with_status_REMOVED() {
90 ruleRepository.add(new DumbRule(XOO_X1).setStatus(RuleStatus.REMOVED));
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()));
97 underTest.execute(new TestComputationStepContext());
99 assertThat(activeRulesHolder.getAll()).isEmpty();
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()));
109 underTest.execute(new TestComputationStepContext());
111 assertThat(activeRulesHolder.getAll()).isEmpty();