3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.externalissue.sarif;
22 import java.util.List;
24 import junit.framework.TestCase;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.slf4j.event.Level;
32 import org.sonar.api.batch.sensor.issue.NewExternalIssue;
33 import org.sonar.api.testfixtures.log.LogTester;
34 import org.sonar.core.sarif.Run;
35 import org.sonar.core.sarif.Sarif210;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatNullPointerException;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoInteractions;
42 import static org.mockito.Mockito.when;
44 @RunWith(MockitoJUnitRunner.class)
45 public class DefaultSarif210ImporterTest extends TestCase {
48 private RunMapper runMapper;
51 public LogTester logTester = new LogTester();
54 DefaultSarif210Importer sarif210Importer;
57 public void importSarif_shouldDelegateRunMapping_toRunMapper() {
58 Sarif210 sarif210 = mock(Sarif210.class);
60 Run run1 = mock(Run.class);
61 Run run2 = mock(Run.class);
62 when(sarif210.getRuns()).thenReturn(Set.of(run1, run2));
64 NewExternalIssue issue1run1 = mock(NewExternalIssue.class);
65 NewExternalIssue issue2run1 = mock(NewExternalIssue.class);
66 NewExternalIssue issue1run2 = mock(NewExternalIssue.class);
67 when(runMapper.mapRun(run1)).thenReturn(List.of(issue1run1, issue2run1));
68 when(runMapper.mapRun(run2)).thenReturn(List.of(issue1run2));
70 SarifImportResults sarifImportResults = sarif210Importer.importSarif(sarif210);
72 assertThat(sarifImportResults.getSuccessFullyImportedIssues()).isEqualTo(3);
73 assertThat(sarifImportResults.getSuccessFullyImportedRuns()).isEqualTo(2);
74 assertThat(sarifImportResults.getFailedRuns()).isZero();
75 verify(issue1run1).save();
76 verify(issue2run1).save();
77 verify(issue1run2).save();
81 public void importSarif_whenExceptionThrownByRunMapper_shouldLogAndContinueProcessing() {
82 Sarif210 sarif210 = mock(Sarif210.class);
84 Run run1 = mock(Run.class);
85 Run run2 = mock(Run.class);
86 when(sarif210.getRuns()).thenReturn(Set.of(run1, run2));
88 Exception testException = new RuntimeException("test");
89 when(runMapper.mapRun(run1)).thenThrow(testException);
90 NewExternalIssue issue1run2 = mock(NewExternalIssue.class);
91 when(runMapper.mapRun(run2)).thenReturn(List.of(issue1run2));
93 SarifImportResults sarifImportResults = sarif210Importer.importSarif(sarif210);
95 assertThat(sarifImportResults.getSuccessFullyImportedIssues()).isOne();
96 assertThat(sarifImportResults.getSuccessFullyImportedRuns()).isOne();
97 assertThat(sarifImportResults.getFailedRuns()).isOne();
98 assertThat(logTester.logs(Level.WARN)).containsOnly("Failed to import a sarif run, error: " + testException.getMessage());
99 verify(issue1run2).save();
103 public void importSarif_whenGetRunsReturnNull_shouldFailWithProperMessage() {
104 Sarif210 sarif210 = mock(Sarif210.class);
106 when(sarif210.getRuns()).thenReturn(null);
108 assertThatNullPointerException()
109 .isThrownBy(() -> sarif210Importer.importSarif(sarif210))
110 .withMessage("The runs section of the Sarif report is null");
112 verifyNoInteractions(runMapper);