aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java
blob: 4f318a488e81111aaed05a00b4f9be73c7acf71b (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * 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;

import java.io.File;
import java.security.SecureRandom;
import java.util.Map;
import java.util.Random;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.batch.sensor.rule.AdHocRule;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.CleanCodeAttribute;
import org.sonar.api.rules.RuleType;
import org.sonar.api.testfixtures.log.LogTester;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
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.LOW;
import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY;
import static org.sonar.api.issue.impact.SoftwareQuality.SECURITY;

public class ExternalIssueImporterTest {

  public static final String RULE_ENGINE_ID = "some_rule_engine_id";
  public static final String RULE_ID = "some_rule_id";
  public static final String RULE_NAME = "some_rule_name";
  public static final CleanCodeAttribute RULE_ATTRIBUTE = CleanCodeAttribute.FORMATTED;

  private final Random random = new SecureRandom();

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();
  @Rule
  public LogTester logs = new LogTester();

  private DefaultInputFile sourceFile;
  private SensorContextTester context;

  @Before
  public void prepare() throws Exception {
    File baseDir = temp.newFolder();
    context = SensorContextTester.create(baseDir);
    sourceFile = new TestInputFileBuilder("foo", "src/Foo.java")
      .setModuleBaseDir(baseDir.toPath())
      .initMetadata("the first line\nthe second line\n\n")
      .setCharset(UTF_8)
      .setLanguage("java")
      .build();
    context.fileSystem().add(sourceFile);
  }

  @Test
  public void execute_whenNewFormatWithZeroIssues() {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Rule rule = createRule();
    report.issues = new ExternalIssueReport.Issue[0];
    report.rules = new ExternalIssueReport.Rule[] {rule};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);
    underTest.execute();

    assertThat(context.allExternalIssues()).isEmpty();
    assertThat(context.allIssues()).isEmpty();
    assertThat(logs.logs(Level.INFO)).contains("Imported 0 issues in 0 files");
  }

  @Test
  public void execute_whenNewFormatWithMinimalInfo() {
    ExternalIssueReport.Issue input = new ExternalIssueReport.Issue();
    input.primaryLocation = new ExternalIssueReport.Location();
    input.primaryLocation.filePath = sourceFile.getProjectRelativePath();
    input.primaryLocation.message = secure().nextAlphabetic(5);

    runOn(input);

    assertThat(context.allExternalIssues()).hasSize(1);
    ExternalIssue output = context.allExternalIssues().iterator().next();
    assertThat(output.engineId()).isEqualTo(RULE_ENGINE_ID);
    assertThat(output.ruleId()).isEqualTo(RULE_ID);
    assertThat(output.severity()).isNull();
    assertThat(output.type()).isNull();
    assertThat(output.remediationEffort()).isNull();
    assertThat(logs.logs(Level.INFO)).contains("Imported 1 issue in 1 file");
    assertThat(context.allAdHocRules()).hasSize(1);

    AdHocRule output1 = context.allAdHocRules().iterator().next();
    assertThat(output1.ruleId()).isEqualTo(RULE_ID);
    assertThat(output1.name()).isEqualTo(RULE_NAME);
    assertThat(output1.engineId()).isEqualTo(RULE_ENGINE_ID);
    assertThat(output1.severity()).isNull();
    assertThat(output1.type()).isNull();
    assertThat(output1.cleanCodeAttribute()).isEqualTo(RULE_ATTRIBUTE);
    assertThat(output1.defaultImpacts()).containsExactlyInAnyOrderEntriesOf(Map.of(SECURITY, BLOCKER, MAINTAINABILITY, LOW));
  }

  @Test
  public void execute_whenNewFormatWithCompletePrimaryLocation() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = 4;
    input.endLine = 2;
    input.endColumn = 3;

    runOn(newIssue(input));

    assertThat(context.allExternalIssues()).hasSize(1);
    ExternalIssue output = context.allExternalIssues().iterator().next();
    assertSameRange(input, output.primaryLocation().textRange());
  }

  @Test
  public void execute_whenNewFormatWithNoColumns() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = null;
    input.endLine = 2;
    input.endColumn = null;

    runOn(newIssue(input));

    assertThat(context.allExternalIssues()).hasSize(1);
    TextRange got = context.allExternalIssues().iterator().next().primaryLocation().textRange();
    assertThat(got.start().line()).isEqualTo(input.startLine);
    assertThat(got.start().lineOffset()).isZero();
    assertThat(got.end().line()).isEqualTo(input.startLine);
    assertThat(got.end().lineOffset()).isEqualTo(sourceFile.selectLine(input.startLine).end().lineOffset());
  }

  @Test
  public void execute_whenNewFormatWithStartButNotEndColumn() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = 3;
    input.endLine = 2;
    input.endColumn = null;

    runOn(newIssue(input));

    assertThat(context.allExternalIssues()).hasSize(1);
    TextRange got = context.allExternalIssues().iterator().next().primaryLocation().textRange();
    assertThat(got.start().line()).isEqualTo(input.startLine);
    assertThat(got.start().lineOffset()).isEqualTo(3);
    assertThat(got.end().line()).isEqualTo(input.endLine);
    assertThat(got.end().lineOffset()).isEqualTo(sourceFile.selectLine(input.endLine).end().lineOffset());
  }

  @Test
  public void execute_whenNewFormatWithStartColumnOnEmptyLine_shouldThrowException() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 3;
    input.startColumn = 0;

    ExternalIssueReport.Issue issue = newIssue(input);
    assertThatThrownBy(() -> runOn(issue))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage("A 'startColumn' [line=3, lineOffset=0] cannot be provided when the line is empty");
  }

  @Test
  public void execute_whenNewFormatContainsNonExistentCleanCodeAttribute_shouldThrowException() {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Rule rule = createRule("not_existent_attribute", MAINTAINABILITY.name(), HIGH.name());
    report.issues = new ExternalIssueReport.Issue[] {};
    report.rules = new ExternalIssueReport.Rule[] {rule};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);

    assertThatThrownBy(underTest::execute)
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage("No enum constant org.sonar.api.rules.CleanCodeAttribute.not_existent_attribute");
  }

  @Test
  public void execute_whenCleanCodeAttributeIsNotProvided_shouldBeNull() {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Rule rule = createRule(RuleType.CODE_SMELL, Severity.MAJOR);
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = 4;
    input.endLine = 2;
    input.endColumn = 3;

    ExternalIssueReport.Issue issue = newIssue(input);
    issue.engineId = rule.engineId;
    issue.ruleId = rule.id;

    report.issues = new ExternalIssueReport.Issue[] {issue};
    report.rules = new ExternalIssueReport.Rule[] {rule};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);

    underTest.execute();
    assertThat(context.allExternalIssues()).hasSize(1);
    ExternalIssue externalIssue = context.allExternalIssues().iterator().next();
    assertThat(externalIssue.type()).isEqualTo(RuleType.CODE_SMELL);
    assertThat(externalIssue.severity()).isEqualTo(Severity.MAJOR);
  }

  @Test
  public void execute_whenNewFormatContainsNonExistentSoftwareQuality_shouldThrowException() {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Rule rule = createRule(CleanCodeAttribute.CONVENTIONAL.name(), "not_existent_software_quality", HIGH.name());
    report.issues = new ExternalIssueReport.Issue[] {};
    report.rules = new ExternalIssueReport.Rule[] {rule};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);

    assertThatThrownBy(underTest::execute)
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage("No enum constant org.sonar.api.issue.impact.SoftwareQuality.not_existent_software_quality");
  }

  @Test
  public void execute_whenNewFormatContainsNonExistentImpactSeverity_shouldThrowException() {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Rule rule = createRule(CleanCodeAttribute.CONVENTIONAL.name(), SoftwareQuality.RELIABILITY.name(),
      "not_existent_impact_severity");
    report.issues = new ExternalIssueReport.Issue[] {};
    report.rules = new ExternalIssueReport.Rule[] {rule};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);

    assertThatThrownBy(underTest::execute)
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage("No enum constant org.sonar.api.issue.impact.Severity.not_existent_impact_severity");
  }

  @Test
  public void execute_whenDeprecatedFormatWithZeroIssues() {
    ExternalIssueReport report = new ExternalIssueReport();
    report.issues = new ExternalIssueReport.Issue[0];

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);
    underTest.execute();
    assertThat(context.allExternalIssues()).isEmpty();
    assertThat(context.allIssues()).isEmpty();
    assertThat(logs.logs(Level.INFO)).contains("Imported 0 issues in 0 files");
  }

  @Test
  public void execute_whenDeprecatedFormatWithMinimalInfo() {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Issue input = new ExternalIssueReport.Issue();
    input.engineId = "findbugs";
    input.ruleId = "123";
    input.severity = "CRITICAL";
    input.type = "BUG";
    input.primaryLocation = new ExternalIssueReport.Location();
    input.primaryLocation.filePath = sourceFile.getProjectRelativePath();
    input.primaryLocation.message = secure().nextAlphabetic(5);
    report.issues = new ExternalIssueReport.Issue[] {input};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);
    underTest.execute();

    assertThat(context.allExternalIssues()).hasSize(1);
    ExternalIssue output = context.allExternalIssues().iterator().next();
    assertThat(output.engineId()).isEqualTo(input.engineId);
    assertThat(output.ruleId()).isEqualTo(input.ruleId);
    assertThat(output.severity()).isEqualTo(Severity.valueOf(input.severity));
    assertThat(output.remediationEffort()).isNull();
    assertThat(logs.logs(Level.INFO)).contains("Imported 1 issue in 1 file");
  }

  @Test
  public void execute_whenDeprecatedFormatWithCompletePrimaryLocation() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = 4;
    input.endLine = 2;
    input.endColumn = 3;

    runOnDeprecatedFormat(newIssue(input));

    assertThat(context.allExternalIssues()).hasSize(1);
    ExternalIssue output = context.allExternalIssues().iterator().next();
    assertSameRange(input, output.primaryLocation().textRange());
  }

  @Test
  public void execute_whenDeprecatedFormatWithNoColumns() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = null;
    input.endLine = 2;
    input.endColumn = null;

    runOnDeprecatedFormat(newIssue(input));

    assertThat(context.allExternalIssues()).hasSize(1);
    TextRange got = context.allExternalIssues().iterator().next().primaryLocation().textRange();
    assertThat(got.start().line()).isEqualTo(input.startLine);
    assertThat(got.start().lineOffset()).isZero();
    assertThat(got.end().line()).isEqualTo(input.startLine);
    assertThat(got.end().lineOffset()).isEqualTo(sourceFile.selectLine(input.startLine).end().lineOffset());
  }

  @Test
  public void execute_whenDeprecatedFormatWithStartButNotEndColumn() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 1;
    input.startColumn = 3;
    input.endLine = 2;
    input.endColumn = null;

    runOnDeprecatedFormat(newIssue(input));

    assertThat(context.allExternalIssues()).hasSize(1);
    TextRange got = context.allExternalIssues().iterator().next().primaryLocation().textRange();
    assertThat(got.start().line()).isEqualTo(input.startLine);
    assertThat(got.start().lineOffset()).isEqualTo(3);
    assertThat(got.end().line()).isEqualTo(input.endLine);
    assertThat(got.end().lineOffset()).isEqualTo(sourceFile.selectLine(input.endLine).end().lineOffset());
  }

  @Test
  public void execute_whenDeprecatedFormatWithStartColumnOnEmptyLine_shouldThrowException() {
    ExternalIssueReport.TextRange input = new ExternalIssueReport.TextRange();
    input.startLine = 3;
    input.startColumn = 0;

    ExternalIssueReport.Issue issue = newIssue(input);
    assertThatThrownBy(() -> runOnDeprecatedFormat(issue))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage("A 'startColumn' [line=3, lineOffset=0] cannot be provided when the line is empty");
  }

  private static ExternalIssueReport.Rule createRule() {
    return createRule(RULE_ATTRIBUTE.name(), SECURITY.name(), BLOCKER.name());
  }

  private static ExternalIssueReport.Rule createRule(@Nullable String cleanCodeAttribute, String softwareQuality, String impactSeverity) {
    ExternalIssueReport.Rule rule = new ExternalIssueReport.Rule();
    rule.id = RULE_ID;
    rule.name = RULE_NAME;
    rule.engineId = RULE_ENGINE_ID;
    rule.cleanCodeAttribute = cleanCodeAttribute;
    ExternalIssueReport.Impact impact1 = new ExternalIssueReport.Impact();
    impact1.severity = impactSeverity;
    impact1.softwareQuality = softwareQuality;
    ExternalIssueReport.Impact impact2 = new ExternalIssueReport.Impact();
    impact2.severity = LOW.name();
    impact2.softwareQuality = MAINTAINABILITY.name();
    rule.impacts = new ExternalIssueReport.Impact[] {impact1, impact2};
    return rule;
  }

  private static ExternalIssueReport.Rule createRule(RuleType ruleType, Severity severity) {
    ExternalIssueReport.Rule rule = new ExternalIssueReport.Rule();
    rule.id = RULE_ID;
    rule.name = RULE_NAME;
    rule.engineId = RULE_ENGINE_ID;
    rule.type = ruleType.name();
    rule.severity = severity.name();
    return rule;
  }

  private void assertSameRange(ExternalIssueReport.TextRange expected, TextRange got) {
    assertThat(got.start().line()).isEqualTo(expected.startLine);
    assertThat(got.start().lineOffset()).isEqualTo(defaultIfNull(expected.startColumn, 0));
    assertThat(got.end().line()).isEqualTo(expected.endLine);
    assertThat(got.end().lineOffset()).isEqualTo(defaultIfNull(expected.endColumn, 0));
  }

  private void runOnDeprecatedFormat(ExternalIssueReport.Issue input) {
    ExternalIssueReport report = new ExternalIssueReport();
    report.issues = new ExternalIssueReport.Issue[] {input};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);
    underTest.execute();
  }

  private void runOn(ExternalIssueReport.Issue input) {
    ExternalIssueReport report = new ExternalIssueReport();
    ExternalIssueReport.Rule rule = createRule();
    input.ruleId = rule.id;
    report.issues = new ExternalIssueReport.Issue[] {input};
    report.rules = new ExternalIssueReport.Rule[] {rule};

    ExternalIssueImporter underTest = new ExternalIssueImporter(this.context, report);
    underTest.execute();
  }

  private ExternalIssueReport.Issue newIssue(@Nullable ExternalIssueReport.TextRange textRange) {
    ExternalIssueReport.Issue input = new ExternalIssueReport.Issue();
    input.engineId = secure().nextAlphabetic(5);
    input.ruleId = secure().nextAlphabetic(5);
    input.severity = "CRITICAL";
    input.type = "BUG";
    input.effortMinutes = random.nextInt(Integer.MAX_VALUE);
    input.primaryLocation = new ExternalIssueReport.Location();
    input.primaryLocation.filePath = sourceFile.getProjectRelativePath();
    input.primaryLocation.message = secure().nextAlphabetic(5);
    input.primaryLocation.textRange = textRange;
    return input;
  }
}