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.server.qualityprofile;
23 import org.junit.jupiter.api.Test;
24 import org.sonar.api.issue.impact.SoftwareQuality;
25 import org.sonar.api.rule.Severity;
26 import org.sonar.api.rules.RuleType;
28 import static org.assertj.core.api.Assertions.assertThat;
30 class QProfileImpactSeverityMapperTest {
32 public static final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> IMPACTS = Map.of(
33 SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH,
34 SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW,
35 SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
38 void mapImpactSeverities_whenSecurityHotspot_shouldReturnEmptyMap() {
39 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.MAJOR,
40 Map.of(SoftwareQuality.MAINTAINABILITY,
41 org.sonar.api.issue.impact.Severity.HIGH),
42 RuleType.SECURITY_HOTSPOT);
44 assertThat(result).isEmpty();
48 void mapImpactSeverities_whenOneImpact_shouldReturnOverriddenImpact() {
49 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.INFO,
50 Map.of(SoftwareQuality.MAINTAINABILITY,
51 org.sonar.api.issue.impact.Severity.HIGH),
54 assertThat(result).hasSize(1).containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.INFO);
58 void mapImpactSeverities_whenOneDifferentImpact_shouldReturnOverriddenImpact() {
59 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.INFO,
60 Map.of(SoftwareQuality.RELIABILITY,
61 org.sonar.api.issue.impact.Severity.HIGH),
64 assertThat(result).hasSize(1).containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.INFO);
68 void mapImpactSeverities_whenMultipleImpact_shouldReturnOverriddenImpactMatchingCodeSmell() {
70 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.CODE_SMELL);
72 assertThat(result).hasSize(3)
73 .containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER)
74 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)
75 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
77 result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.BUG);
79 assertThat(result).hasSize(3)
80 .containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH)
81 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.BLOCKER)
82 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
84 result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.VULNERABILITY);
86 assertThat(result).hasSize(3)
87 .containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH)
88 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)
89 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER);
93 void mapImpactSeverities_whenMultipleImpactNotMatchingRuleType_shouldReturnRuleImpacts() {
94 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = Map.of(
95 SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW,
96 SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
98 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, impacts, RuleType.CODE_SMELL);
99 assertThat(result).hasSize(2)
100 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)
101 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
105 void mapSeverity_whenOneImpact_ShouldReturnMappedImpactSeverity() {
106 String severity = QProfileImpactSeverityMapper.mapSeverity(
107 Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH),
108 RuleType.BUG, Severity.BLOCKER);
110 assertThat(severity).isEqualTo(Severity.CRITICAL);
114 void mapSeverity_whenMultipleImpacts_ShouldReturnMappedImpactSeverity() {
115 String severity = QProfileImpactSeverityMapper.mapSeverity(
117 RuleType.BUG, Severity.BLOCKER);
119 assertThat(severity).isEqualTo(Severity.MINOR);
121 severity = QProfileImpactSeverityMapper.mapSeverity(
123 RuleType.VULNERABILITY, Severity.BLOCKER);
125 assertThat(severity).isEqualTo(Severity.INFO);
127 severity = QProfileImpactSeverityMapper.mapSeverity(
129 RuleType.CODE_SMELL, Severity.BLOCKER);
131 assertThat(severity).isEqualTo(Severity.CRITICAL);
135 void mapImpactSeverities_whenMultipleImpactNotMatchingRuleType_shouldReturnRuleSeverity() {
136 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = Map.of(
137 SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW,
138 SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
140 String severity = QProfileImpactSeverityMapper.mapSeverity(
142 RuleType.CODE_SMELL, Severity.BLOCKER);
144 assertThat(severity).isEqualTo(Severity.BLOCKER);