1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* SonarQube
* Copyright (C) 2009-2025 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.scanner.externalissue.sarif;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.issue.NewExternalIssue;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.rules.RuleType;
import org.sonar.sarif.pojo.Location;
import org.sonar.sarif.pojo.Message;
import org.sonar.sarif.pojo.Result;
import org.sonar.sarif.pojo.Stack;
import org.sonar.sarif.pojo.StackFrame;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.sonar.sarif.pojo.Result.Level.WARNING;
import static org.sonar.scanner.externalissue.sarif.ResultMapper.DEFAULT_IMPACT_SEVERITY;
import static org.sonar.scanner.externalissue.sarif.ResultMapper.DEFAULT_SEVERITY;
import static org.sonar.scanner.externalissue.sarif.ResultMapper.DEFAULT_SOFTWARE_QUALITY;
@RunWith(DataProviderRunner.class)
public class ResultMapperTest {
private static final String RULE_ID = "test_rules_id";
private static final String DRIVER_NAME = "driverName";
@Mock
private LocationMapper locationMapper;
@Mock
private SensorContext sensorContext;
@Mock
private NewExternalIssue mockExternalIssue;
@Mock
private NewIssueLocation newExternalIssueLocation;
private Result result;
@InjectMocks
ResultMapper resultMapper;
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
result = new Result().withMessage(new Message().withText("Result message"));
result.withRuleId(RULE_ID);
when(sensorContext.newExternalIssue()).thenReturn(mockExternalIssue);
when(locationMapper.fillIssueInFileLocation(any(), any())).thenReturn(true);
when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation);
}
@Test
public void mapResult_mapsSimpleFieldsCorrectly() {
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(newExternalIssue).type(RuleType.VULNERABILITY);
verify(newExternalIssue).engineId(DRIVER_NAME);
verify(newExternalIssue).severity(DEFAULT_SEVERITY);
verify(newExternalIssue).ruleId(RULE_ID);
}
@Test
public void mapResult_ifRuleIdMissing_fails() {
result.withRuleId(null);
assertThatNullPointerException()
.isThrownBy(() -> resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result))
.withMessage("No ruleId found for issue thrown by driver driverName");
}
@Test
public void mapResult_whenLocationExists_createsFileLocation() {
Location location = new Location();
result.withLocations(List.of(location));
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(locationMapper).fillIssueInFileLocation(newExternalIssueLocation, location);
verifyNoMoreInteractions(locationMapper);
verify(newExternalIssue).at(newExternalIssueLocation);
verify(newExternalIssue, never()).addLocation(any());
verify(newExternalIssue, never()).addFlow(any());
}
@Test
public void mapResult_useResultMessageForIssue() {
Location location = new Location();
result.withLocations(List.of(location));
resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(newExternalIssueLocation).message("Result message");
}
@Test
public void mapResult_concatResultMessageAndLocationMessageForIssue() {
Location location = new Location().withMessage(new Message().withText("Location message"));
result.withLocations(List.of(location));
resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(newExternalIssueLocation).message("Result message - Location message");
}
@Test
public void mapResult_whenRelatedLocationExists_createsSecondaryFileLocation() {
Location relatedLocation = new Location().withMessage(new Message().withText("Related location message"));
result.withRelatedLocations(Set.of(relatedLocation));
var newIssueLocationCall2 = mock(NewIssueLocation.class);
when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation, newIssueLocationCall2);
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(locationMapper).fillIssueInProjectLocation(newExternalIssueLocation);
verify(locationMapper).fillIssueInFileLocation(newIssueLocationCall2, relatedLocation);
verifyNoMoreInteractions(locationMapper);
verify(newExternalIssue).at(newExternalIssueLocation);
verify(newExternalIssue).addLocation(newIssueLocationCall2);
verify(newExternalIssue, never()).addFlow(any());
verify(newIssueLocationCall2).message("Related location message");
}
@Test
public void mapResult_whenRelatedLocationExists_createsSecondaryFileLocation_no_messages() {
Location relatedLocationWithoutMessage = new Location();
result.withRelatedLocations(Set.of(relatedLocationWithoutMessage));
var newIssueLocationCall2 = mock(NewIssueLocation.class);
when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation, newIssueLocationCall2);
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(newExternalIssue).addLocation(newIssueLocationCall2);
verify(newIssueLocationCall2, never()).message(anyString());
}
@Test
public void mapResult_whenStacksLocationExists_createsCodeFlowFileLocation() {
Location stackFrameLocation = new Location().withMessage(new Message().withText("Stack frame location message"));
var stackWithFrame = new Stack().withFrames(List.of(new StackFrame().withLocation(stackFrameLocation)));
var stackWithFrameNoLocation = new Stack().withFrames(List.of(new StackFrame()));
var stackWithoutFrame = new Stack().withFrames(List.of());
var stackWithoutFrame2 = new Stack();
result.withStacks(Set.of(stackWithFrame, stackWithFrameNoLocation, stackWithoutFrame, stackWithoutFrame2));
var newIssueLocationCall2 = mock(NewIssueLocation.class);
when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation, newIssueLocationCall2);
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(locationMapper).fillIssueInProjectLocation(newExternalIssueLocation);
verify(locationMapper).fillIssueInFileLocation(newIssueLocationCall2, stackFrameLocation);
verifyNoMoreInteractions(locationMapper);
verify(newExternalIssue).at(newExternalIssueLocation);
verify(newExternalIssue).addFlow(List.of(newIssueLocationCall2));
verify(newExternalIssue, never()).addLocation(any());
verify(newIssueLocationCall2).message("Stack frame location message");
}
@Test
public void mapResult_whenStacksLocationExists_createsCodeFlowFileLocation_no_text_messages() {
Location stackFrameLocationWithoutMessage = new Location().withMessage(new Message().withId("1"));
result.withStacks(Set.of(new Stack().withFrames(List.of(new StackFrame().withLocation(stackFrameLocationWithoutMessage)))));
var newIssueLocationCall2 = mock(NewIssueLocation.class);
when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation, newIssueLocationCall2);
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(newExternalIssue).addFlow(List.of(newIssueLocationCall2));
verify(newIssueLocationCall2, never()).message(anyString());
}
@Test
public void mapResult_whenLocationExistsButLocationMapperReturnsFalse_createsProjectLocation() {
Location location = new Location();
result.withLocations(List.of(location));
when(locationMapper.fillIssueInFileLocation(any(), any())).thenReturn(false);
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(locationMapper).fillIssueInProjectLocation(newExternalIssueLocation);
verify(newExternalIssue).at(newExternalIssueLocation);
verify(newExternalIssue, never()).addLocation(any());
verify(newExternalIssue, never()).addFlow(any());
}
@Test
public void mapResult_whenLocationsIsEmpty_createsProjectLocation() {
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
result.withLocations(List.of());
verify(locationMapper).fillIssueInProjectLocation(newExternalIssueLocation);
verifyNoMoreInteractions(locationMapper);
verify(newExternalIssue).at(newExternalIssueLocation);
verify(newExternalIssue, never()).addLocation(any());
verify(newExternalIssue, never()).addFlow(any());
}
@Test
public void mapResult_whenLocationsIsNull_createsProjectLocation() {
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(locationMapper).fillIssueInProjectLocation(newExternalIssueLocation);
verifyNoMoreInteractions(locationMapper);
verify(newExternalIssue).at(newExternalIssueLocation);
verify(newExternalIssue, never()).addLocation(any());
verify(newExternalIssue, never()).addFlow(any());
}
@DataProvider
public static Object[][] rule_severity_to_sonarqube_severity_mapping() {
return new Object[][] {
{Result.Level.ERROR, Severity.CRITICAL, org.sonar.api.issue.impact.Severity.HIGH},
{WARNING, Severity.MAJOR, org.sonar.api.issue.impact.Severity.MEDIUM},
{Result.Level.NOTE, Severity.MINOR, org.sonar.api.issue.impact.Severity.LOW},
{Result.Level.NONE, Severity.INFO, org.sonar.api.issue.impact.Severity.LOW},
{null, DEFAULT_SEVERITY, DEFAULT_IMPACT_SEVERITY},
};
}
@Test
@UseDataProvider("rule_severity_to_sonarqube_severity_mapping")
public void mapResult_mapsCorrectlyLevelToSeverity(Result.Level ruleSeverity, Severity sonarQubeSeverity, org.sonar.api.issue.impact.Severity impactSeverity) {
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, ruleSeverity, ruleSeverity, result);
verify(newExternalIssue).severity(sonarQubeSeverity);
verify(newExternalIssue).addImpact(DEFAULT_SOFTWARE_QUALITY, impactSeverity);
}
@Test
public void mapResult_mapsCorrectlyCleanCodeAttribute() {
NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
verify(newExternalIssue).cleanCodeAttribute(ResultMapper.DEFAULT_CLEAN_CODE_ATTRIBUTE);
}
}
|