aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2024-11-06 10:51:12 +0100
committersonartech <sonartech@sonarsource.com>2024-11-11 20:02:44 +0000
commit8fae1ede3f39a29731237c0d89b76f0b447d9547 (patch)
treebe15d550446280704733df1131d082c3c9d1a3f5 /sonar-core
parent7d7ed0de5f3e546063b81f6c38d7fd04f1d4d62a (diff)
downloadsonarqube-8fae1ede3f39a29731237c0d89b76f0b447d9547.tar.gz
sonarqube-8fae1ede3f39a29731237c0d89b76f0b447d9547.zip
SONAR-23527 impacts severity is updated when issue is set on manual severity
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/ImpactSeverityMapper.java50
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/ImpactSeverityMapperTest.java56
2 files changed, 106 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/ImpactSeverityMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/ImpactSeverityMapper.java
new file mode 100644
index 00000000000..b68057cf429
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/rule/ImpactSeverityMapper.java
@@ -0,0 +1,50 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.rule;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.ImmutableBiMap;
+import java.util.Optional;
+import org.sonar.api.issue.impact.Severity;
+
+public class ImpactSeverityMapper {
+ private static final BiMap<Severity, String> SEVERITY_MAPPING = new ImmutableBiMap.Builder<Severity, String>()
+ .put(Severity.INFO, org.sonar.api.rule.Severity.INFO)
+ .put(Severity.LOW, org.sonar.api.rule.Severity.MINOR)
+ .put(Severity.MEDIUM, org.sonar.api.rule.Severity.MAJOR)
+ .put(Severity.HIGH, org.sonar.api.rule.Severity.CRITICAL)
+ .put(Severity.BLOCKER, org.sonar.api.rule.Severity.BLOCKER)
+ .build();
+
+ private ImpactSeverityMapper() {
+
+ }
+
+ public static Severity mapImpactSeverity(String severity) {
+ return Optional.ofNullable(SEVERITY_MAPPING.inverse().get(severity))
+ .orElseThrow(() -> new IllegalArgumentException("Severity not supported: " + severity));
+ }
+
+ public static String mapRuleSeverity(Severity severity) {
+ return Optional.ofNullable(SEVERITY_MAPPING.get(severity))
+ .orElseThrow(() -> new IllegalArgumentException("Impact Severity not supported: " + severity));
+ }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/rule/ImpactSeverityMapperTest.java b/sonar-core/src/test/java/org/sonar/core/rule/ImpactSeverityMapperTest.java
new file mode 100644
index 00000000000..300c3376196
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/rule/ImpactSeverityMapperTest.java
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.rule;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.api.issue.impact.Severity;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class ImpactSeverityMapperTest {
+
+ @Test
+ void mapImpactSeverity_shouldReturnExpectedValues() {
+
+ assertThat(ImpactSeverityMapper.mapRuleSeverity(Severity.INFO)).isEqualTo(org.sonar.api.rule.Severity.INFO);
+ }
+
+ @Test
+ void mapImpactSeverity_shouldThrowExceptionWhenValueIsUnknownOrNull() {
+
+ assertThatThrownBy(() -> ImpactSeverityMapper.mapImpactSeverity("unknown")).isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Severity not supported: unknown");
+
+ assertThatThrownBy(() -> ImpactSeverityMapper.mapImpactSeverity(null)).isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Severity not supported: null");
+ }
+
+ @Test
+ void mapRuleSeverity_shouldReturnExpectedValues() {
+ assertThat(ImpactSeverityMapper.mapImpactSeverity(org.sonar.api.rule.Severity.INFO)).isEqualTo(Severity.INFO);
+ }
+
+ @Test
+ void mapRuleSeverity_shouldThrowExceptionWhenValueIsUnknownOrNull() {
+ assertThatThrownBy(() -> ImpactSeverityMapper.mapRuleSeverity(null)).isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Impact Severity not supported: null");
+ }
+}