aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test
diff options
context:
space:
mode:
authorantoine.vinot <antoine.vinot@sonarsource.com>2024-04-05 11:31:23 +0200
committersonartech <sonartech@sonarsource.com>2024-04-05 20:02:38 +0000
commit118a1b07a78bb95a84defa9535f9999d8f4bd2c5 (patch)
treed98e9956f301aa6b9b446fb3f6dded40e5399add /sonar-scanner-engine/src/test
parent54ad2b1a3d5746ed4702daa1a10845d7b27a0026 (diff)
downloadsonarqube-118a1b07a78bb95a84defa9535f9999d8f4bd2c5.tar.gz
sonarqube-118a1b07a78bb95a84defa9535f9999d8f4bd2c5.zip
SONAR-22009 Fix NPE on SARIF import when Extensions don't have Rules
Diffstat (limited to 'sonar-scanner-engine/src/test')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RunMapperTest.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RunMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RunMapperTest.java
index 67f52b00556..8c03e6cd99a 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RunMapperTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/RunMapperTest.java
@@ -42,6 +42,7 @@ import org.sonar.scanner.externalissue.sarif.RunMapper.RunMapperResult;
import static java.util.Collections.emptySet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
@@ -132,6 +133,36 @@ public class RunMapperTest {
}
@Test
+ public void mapRun_shouldNotFail_whenExtensionsDontHaveRules() {
+ when(run.getTool().getDriver().getRules()).thenReturn(Set.of(rule));
+ Extension extension = mock(Extension.class);
+ when(extension.getRules()).thenReturn(null);
+ when(run.getTool().getExtensions()).thenReturn(Set.of(extension));
+
+ try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
+ detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
+ detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
+
+ assertThatNoException().isThrownBy(() -> runMapper.mapRun(run));
+ }
+ }
+
+ @Test
+ public void mapRun_shouldNotFail_whenExtensionsHaveEmptyRules() {
+ when(run.getTool().getDriver().getRules()).thenReturn(Set.of(rule));
+ Extension extension = mock(Extension.class);
+ when(extension.getRules()).thenReturn(Set.of());
+ when(run.getTool().getExtensions()).thenReturn(Set.of(extension));
+
+ try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
+ detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
+ detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
+
+ assertThatNoException().isThrownBy(() -> runMapper.mapRun(run));
+ }
+ }
+
+ @Test
public void mapRun_ifRunIsEmpty_returnsEmptyList() {
when(run.getResults()).thenReturn(emptySet());