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,
41 RuleType.SECURITY_HOTSPOT);
43 assertThat(result).isEmpty();
47 void mapImpactSeverities_whenSeverityIsNull_shouldReturnRuleImpacts() {
48 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH);
49 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(null,
51 RuleType.SECURITY_HOTSPOT);
53 assertThat(result).isEqualTo(impacts);
57 void mapImpactSeverities_whenOneImpact_shouldReturnOverriddenImpact() {
58 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.INFO,
59 Map.of(SoftwareQuality.MAINTAINABILITY,
60 org.sonar.api.issue.impact.Severity.HIGH),
63 assertThat(result).hasSize(1).containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.INFO);
67 void mapImpactSeverities_whenOneDifferentImpact_shouldReturnOverriddenImpact() {
68 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.INFO,
69 Map.of(SoftwareQuality.RELIABILITY,
70 org.sonar.api.issue.impact.Severity.HIGH),
73 assertThat(result).hasSize(1).containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.INFO);
77 void mapImpactSeverities_whenMultipleImpact_shouldReturnOverriddenImpactMatchingCodeSmell() {
79 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.CODE_SMELL);
81 assertThat(result).hasSize(3)
82 .containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER)
83 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)
84 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
86 result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.BUG);
88 assertThat(result).hasSize(3)
89 .containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH)
90 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.BLOCKER)
91 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
93 result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.VULNERABILITY);
95 assertThat(result).hasSize(3)
96 .containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH)
97 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)
98 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER);
102 void mapImpactSeverities_whenMultipleImpactNotMatchingRuleType_shouldReturnRuleImpacts() {
103 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = Map.of(
104 SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW,
105 SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
107 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, impacts, RuleType.CODE_SMELL);
108 assertThat(result).hasSize(2)
109 .containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)
110 .containsEntry(SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
114 void mapSeverity_whenOneImpact_ShouldReturnMappedImpactSeverity() {
115 String severity = QProfileImpactSeverityMapper.mapSeverity(
116 Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH),
117 RuleType.BUG, Severity.BLOCKER);
119 assertThat(severity).isEqualTo(Severity.CRITICAL);
123 void mapSeverity_whenMultipleImpacts_ShouldReturnMappedImpactSeverity() {
124 String severity = QProfileImpactSeverityMapper.mapSeverity(
126 RuleType.BUG, Severity.BLOCKER);
128 assertThat(severity).isEqualTo(Severity.MINOR);
130 severity = QProfileImpactSeverityMapper.mapSeverity(
132 RuleType.VULNERABILITY, Severity.BLOCKER);
134 assertThat(severity).isEqualTo(Severity.INFO);
136 severity = QProfileImpactSeverityMapper.mapSeverity(
138 RuleType.CODE_SMELL, Severity.BLOCKER);
140 assertThat(severity).isEqualTo(Severity.CRITICAL);
144 void mapImpactSeverities_whenMultipleImpactNotMatchingRuleType_shouldReturnRuleSeverity() {
145 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = Map.of(
146 SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW,
147 SoftwareQuality.SECURITY, org.sonar.api.issue.impact.Severity.INFO);
149 String severity = QProfileImpactSeverityMapper.mapSeverity(
151 RuleType.CODE_SMELL, Severity.BLOCKER);
153 assertThat(severity).isEqualTo(Severity.BLOCKER);