]> source.dussan.org Git - sonarqube.git/blob
c38b07d31f2ec1226807bf7876293f73fadbabfd
[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.server.qualityprofile;
21
22 import java.util.Map;
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;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29
30 class QProfileImpactSeverityMapperTest {
31
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);
36
37   @Test
38   void mapImpactSeverities_whenSecurityHotspot_shouldReturnEmptyMap() {
39     Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.MAJOR,
40       Map.of(),
41       RuleType.SECURITY_HOTSPOT);
42
43     assertThat(result).isEmpty();
44   }
45
46   @Test
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,
50       impacts,
51       RuleType.SECURITY_HOTSPOT);
52
53     assertThat(result).isEqualTo(impacts);
54   }
55
56   @Test
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),
61       RuleType.CODE_SMELL);
62
63     assertThat(result).hasSize(1).containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.INFO);
64   }
65
66   @Test
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),
71       RuleType.CODE_SMELL);
72
73     assertThat(result).hasSize(1).containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.INFO);
74   }
75
76   @Test
77   void mapImpactSeverities_whenMultipleImpact_shouldReturnOverriddenImpactMatchingCodeSmell() {
78
79     Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.CODE_SMELL);
80
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);
85
86     result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.BUG);
87
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);
92
93     result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.VULNERABILITY);
94
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);
99   }
100
101   @Test
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);
106
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);
111   }
112
113   @Test
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);
118
119     assertThat(severity).isEqualTo(Severity.CRITICAL);
120   }
121
122   @Test
123   void mapSeverity_whenMultipleImpacts_ShouldReturnMappedImpactSeverity() {
124     String severity = QProfileImpactSeverityMapper.mapSeverity(
125       IMPACTS,
126       RuleType.BUG, Severity.BLOCKER);
127
128     assertThat(severity).isEqualTo(Severity.MINOR);
129
130     severity = QProfileImpactSeverityMapper.mapSeverity(
131       IMPACTS,
132       RuleType.VULNERABILITY, Severity.BLOCKER);
133
134     assertThat(severity).isEqualTo(Severity.INFO);
135
136     severity = QProfileImpactSeverityMapper.mapSeverity(
137       IMPACTS,
138       RuleType.CODE_SMELL, Severity.BLOCKER);
139
140     assertThat(severity).isEqualTo(Severity.CRITICAL);
141   }
142
143   @Test
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);
148
149     String severity = QProfileImpactSeverityMapper.mapSeverity(
150       impacts,
151       RuleType.CODE_SMELL, Severity.BLOCKER);
152
153     assertThat(severity).isEqualTo(Severity.BLOCKER);
154   }
155
156 }