]> source.dussan.org Git - sonarqube.git/blob
e8cf14b5adbc14d18d376ca5e4349ccecfe6b298
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.scanner.externalissue.sarif;
21
22 import java.util.List;
23 import java.util.Set;
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;
36
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;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class DefaultSarif210ImporterTest extends TestCase {
46
47   @Mock
48   private RunMapper runMapper;
49
50   @Rule
51   public LogTester logTester = new LogTester();
52
53   @InjectMocks
54   DefaultSarif210Importer sarif210Importer;
55
56   @Test
57   public void importSarif_shouldDelegateRunMapping_toRunMapper() {
58     Sarif210 sarif210 = mock(Sarif210.class);
59
60     Run run1 = mock(Run.class);
61     Run run2 = mock(Run.class);
62     when(sarif210.getRuns()).thenReturn(Set.of(run1, run2));
63
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));
69
70     SarifImportResults sarifImportResults = sarif210Importer.importSarif(sarif210);
71
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();
78   }
79
80   @Test
81   public void importSarif_whenExceptionThrownByRunMapper_shouldLogAndContinueProcessing() {
82     Sarif210 sarif210 = mock(Sarif210.class);
83
84     Run run1 = mock(Run.class);
85     Run run2 = mock(Run.class);
86     when(sarif210.getRuns()).thenReturn(Set.of(run1, run2));
87
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));
92
93     SarifImportResults sarifImportResults = sarif210Importer.importSarif(sarif210);
94
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();
100   }
101
102   @Test
103   public void importSarif_whenGetRunsReturnNull_shouldFailWithProperMessage() {
104     Sarif210 sarif210 = mock(Sarif210.class);
105
106     when(sarif210.getRuns()).thenReturn(null);
107
108     assertThatNullPointerException()
109       .isThrownBy(() -> sarif210Importer.importSarif(sarif210))
110       .withMessage("The runs section of the Sarif report is null");
111
112     verifyNoInteractions(runMapper);
113   }
114
115 }