aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2024-10-10 18:14:38 +0200
committersonartech <sonartech@sonarsource.com>2024-10-16 20:03:01 +0000
commite2cb22069d25733459a1d058be1a7c1f3ca370ef (patch)
tree8174db0874a9c60e1ed26ae7cea13fb025d6d9e6 /sonar-core
parent5038a353ca5917a647e5613859fbf62e756f4a21 (diff)
downloadsonarqube-e2cb22069d25733459a1d058be1a7c1f3ca370ef.tar.gz
sonarqube-e2cb22069d25733459a1d058be1a7c1f3ca370ef.zip
SONAR-23250 handle impacts on active rules in scanner engine
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/ImpactFormatter.java55
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/ImpactFormatterTest.java50
2 files changed, 105 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/ImpactFormatter.java b/sonar-core/src/main/java/org/sonar/core/rule/ImpactFormatter.java
new file mode 100644
index 00000000000..73120079f0f
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/rule/ImpactFormatter.java
@@ -0,0 +1,55 @@
+/*
+ * 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.sonar.api.issue.impact.Severity;
+import org.sonarqube.ws.Common;
+
+import static org.sonar.api.issue.impact.Severity.BLOCKER;
+import static org.sonar.api.issue.impact.Severity.HIGH;
+import static org.sonar.api.issue.impact.Severity.INFO;
+import static org.sonar.api.issue.impact.Severity.LOW;
+import static org.sonar.api.issue.impact.Severity.MEDIUM;
+
+public class ImpactFormatter {
+ private ImpactFormatter() {
+ }
+
+ public static Common.ImpactSeverity mapImpactSeverity(Severity severity) {
+ return switch (severity) {
+ case BLOCKER -> Common.ImpactSeverity.ImpactSeverity_BLOCKER;
+ case HIGH -> Common.ImpactSeverity.HIGH;
+ case MEDIUM -> Common.ImpactSeverity.MEDIUM;
+ case LOW -> Common.ImpactSeverity.LOW;
+ case INFO -> Common.ImpactSeverity.ImpactSeverity_INFO;
+ };
+ }
+
+ public static Severity mapImpactSeverity(Common.ImpactSeverity severity) {
+ return switch (severity) {
+ case ImpactSeverity_BLOCKER -> BLOCKER;
+ case HIGH -> HIGH;
+ case MEDIUM -> MEDIUM;
+ case LOW -> LOW;
+ case ImpactSeverity_INFO -> INFO;
+ case UNKNOWN_IMPACT_SEVERITY -> throw new UnsupportedOperationException("Impact severity not supported");
+ };
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/rule/ImpactFormatterTest.java b/sonar-core/src/test/java/org/sonar/core/rule/ImpactFormatterTest.java
new file mode 100644
index 00000000000..6883ef15ae2
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/rule/ImpactFormatterTest.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 org.junit.jupiter.api.Test;
+import org.sonar.api.issue.impact.Severity;
+import org.sonarqube.ws.Common;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class ImpactFormatterTest {
+
+ @Test
+ void mapImpactSeverity_whenMappingStandardSeverity_shouldReturnExpectedValue() {
+ assertThat(ImpactFormatter.mapImpactSeverity(Severity.BLOCKER)).isEqualTo(Common.ImpactSeverity.ImpactSeverity_BLOCKER);
+ assertThat(ImpactFormatter.mapImpactSeverity(Severity.HIGH)).isEqualTo(Common.ImpactSeverity.HIGH);
+ assertThat(ImpactFormatter.mapImpactSeverity(Severity.MEDIUM)).isEqualTo(Common.ImpactSeverity.MEDIUM);
+ assertThat(ImpactFormatter.mapImpactSeverity(Severity.LOW)).isEqualTo(Common.ImpactSeverity.LOW);
+ assertThat(ImpactFormatter.mapImpactSeverity(Severity.INFO)).isEqualTo(Common.ImpactSeverity.ImpactSeverity_INFO);
+ }
+
+ @Test
+ void mapImpactSeverity_whenMappingProtobufSeverity_shouldReturnExpectedValue() {
+ assertThat(ImpactFormatter.mapImpactSeverity(Common.ImpactSeverity.ImpactSeverity_BLOCKER)).isEqualTo(Severity.BLOCKER);
+ assertThat(ImpactFormatter.mapImpactSeverity(Common.ImpactSeverity.HIGH)).isEqualTo(Severity.HIGH);
+ assertThat(ImpactFormatter.mapImpactSeverity(Common.ImpactSeverity.MEDIUM)).isEqualTo(Severity.MEDIUM);
+ assertThat(ImpactFormatter.mapImpactSeverity(Common.ImpactSeverity.LOW)).isEqualTo(Severity.LOW);
+ assertThat(ImpactFormatter.mapImpactSeverity(Common.ImpactSeverity.ImpactSeverity_INFO)).isEqualTo(Severity.INFO);
+ assertThatThrownBy(() -> ImpactFormatter.mapImpactSeverity(Common.ImpactSeverity.UNKNOWN_IMPACT_SEVERITY))
+ .isInstanceOf(UnsupportedOperationException.class);
+ }
+}