]> source.dussan.org Git - sonarqube.git/blob
a13fb9cc04fd7b18c809a4490e3cf47d1988d03f
[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(SoftwareQuality.MAINTAINABILITY,
41         org.sonar.api.issue.impact.Severity.HIGH),
42       RuleType.SECURITY_HOTSPOT);
43
44     assertThat(result).isEmpty();
45   }
46
47   @Test
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),
52       RuleType.CODE_SMELL);
53
54     assertThat(result).hasSize(1).containsEntry(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.INFO);
55   }
56
57   @Test
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),
62       RuleType.CODE_SMELL);
63
64     assertThat(result).hasSize(1).containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.INFO);
65   }
66
67   @Test
68   void mapImpactSeverities_whenMultipleImpact_shouldReturnOverriddenImpactMatchingCodeSmell() {
69
70     Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.CODE_SMELL);
71
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);
76
77     result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.BUG);
78
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);
83
84     result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.BLOCKER, IMPACTS, RuleType.VULNERABILITY);
85
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);
90   }
91
92   @Test
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);
97
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);
102   }
103
104   @Test
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);
109
110     assertThat(severity).isEqualTo(Severity.CRITICAL);
111   }
112
113   @Test
114   void mapSeverity_whenMultipleImpacts_ShouldReturnMappedImpactSeverity() {
115     String severity = QProfileImpactSeverityMapper.mapSeverity(
116       IMPACTS,
117       RuleType.BUG, Severity.BLOCKER);
118
119     assertThat(severity).isEqualTo(Severity.MINOR);
120
121     severity = QProfileImpactSeverityMapper.mapSeverity(
122       IMPACTS,
123       RuleType.VULNERABILITY, Severity.BLOCKER);
124
125     assertThat(severity).isEqualTo(Severity.INFO);
126
127     severity = QProfileImpactSeverityMapper.mapSeverity(
128       IMPACTS,
129       RuleType.CODE_SMELL, Severity.BLOCKER);
130
131     assertThat(severity).isEqualTo(Severity.CRITICAL);
132   }
133
134   @Test
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);
139
140     String severity = QProfileImpactSeverityMapper.mapSeverity(
141       impacts,
142       RuleType.CODE_SMELL, Severity.BLOCKER);
143
144     assertThat(severity).isEqualTo(Severity.BLOCKER);
145   }
146
147 }