aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/issues/ExternalIssuesMediumIT.java
blob: d2e704831459bd60d2ecb07d8c4ef8ce1db904f9 (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
/*
 * 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.mediumtest.issues;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.scanner.mediumtest.AnalysisResult;
import org.sonar.scanner.mediumtest.ScannerMediumTester;
import org.sonar.scanner.protocol.Constants.Severity;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.ExternalIssue;
import org.sonar.scanner.protocol.output.ScannerReport.Issue;
import org.sonar.scanner.protocol.output.ScannerReport.IssueType;
import org.sonar.xoo.XooPlugin;
import org.sonar.xoo.rule.OneExternalIssuePerLineSensor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

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

  @Rule
  public ScannerMediumTester tester = new ScannerMediumTester()
    .registerPlugin("xoo", new XooPlugin());

  @Test
  public void testOneIssuePerLine() throws Exception {
    File projectDir = new File("test-resources/mediumtest/xoo/sample");
    File tmpDir = temp.newFolder();
    FileUtils.copyDirectory(projectDir, tmpDir);

    AnalysisResult result = tester
      .newAnalysis(new File(tmpDir, "sonar-project.properties"))
      .property(OneExternalIssuePerLineSensor.ACTIVATE, "true")
      .execute();

    List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
    assertThat(issues).isEmpty();

    List<ExternalIssue> externalIssues = result.externalIssuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
    assertThat(externalIssues).hasSize(8 /* lines */);

    ExternalIssue issue = externalIssues.get(0);
    assertThat(issue.getTextRange().getStartLine()).isEqualTo(issue.getTextRange().getStartLine());

    assertThat(result.adHocRules()).isEmpty();
  }

  @Test
  public void testOneIssuePerLine_register_ad_hoc_rule() throws Exception {
    File projectDir = new File("test-resources/mediumtest/xoo/sample");
    File tmpDir = temp.newFolder();
    FileUtils.copyDirectory(projectDir, tmpDir);

    AnalysisResult result = tester
      .newAnalysis(new File(tmpDir, "sonar-project.properties"))
      .property(OneExternalIssuePerLineSensor.ACTIVATE, "true")
      .property(OneExternalIssuePerLineSensor.REGISTER_AD_HOC_RULE, "true")
      .execute();

    assertThat(result.adHocRules()).extracting(
      ScannerReport.AdHocRule::getEngineId,
      ScannerReport.AdHocRule::getRuleId,
      ScannerReport.AdHocRule::getName,
      ScannerReport.AdHocRule::getDescription,
      ScannerReport.AdHocRule::getSeverity,
      ScannerReport.AdHocRule::getType)
      .containsExactlyInAnyOrder(
        tuple(
          OneExternalIssuePerLineSensor.ENGINE_ID,
          OneExternalIssuePerLineSensor.RULE_ID,
          "An ad hoc rule",
          "blah blah",
          Severity.BLOCKER,
          IssueType.BUG));
  }

  @Test
  public void testLoadIssuesFromJsonReport() throws URISyntaxException, IOException {
    logs.setLevel(Level.DEBUG);
    File projectDir = new File("test-resources/mediumtest/xoo/sample");
    File tmpDir = temp.newFolder();
    FileUtils.copyDirectory(projectDir, tmpDir);

    AnalysisResult result = tester
      .newAnalysis(new File(tmpDir, "sonar-project.properties"))
      .property("sonar.externalIssuesReportPaths", "externalIssues.json")
      .execute();

    List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
    assertThat(issues).isEmpty();

    List<ExternalIssue> externalIssues = result.externalIssuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
    assertThat(externalIssues).hasSize(2);

    // precise issue location
    ExternalIssue issue = externalIssues.get(0);
    assertPreciseIssueLocation(issue);

    // location on a line
    issue = externalIssues.get(1);
    assertIssueLocationLine(issue);

    // One file-level issue in helloscala, with secondary location
    List<ExternalIssue> externalIssues2 = result.externalIssuesFor(result.inputFile("xources/hello/helloscala.xoo"));
    assertThat(externalIssues2).hasSize(1);

    issue = externalIssues2.iterator().next();
    assertSecondaryLocation(issue);

    // one issue is located in a non-existing file
    assertThat(logs.logs()).contains("External issues ignored for 1 unknown files, including: invalidFile");

  }

  private void assertSecondaryLocation(ExternalIssue issue) {
    assertThat(issue.getFlowCount()).isEqualTo(2);
    assertThat(issue.getMsg()).isEqualTo("fix the bug here");
    assertThat(issue.getEngineId()).isEqualTo("externalXoo");
    assertThat(issue.getRuleId()).isEqualTo("rule3");
    assertThat(issue.getSeverity()).isEqualTo(Severity.MAJOR);
    assertThat(issue.getType()).isEqualTo(IssueType.BUG);
    assertThat(issue.hasTextRange()).isFalse();
    assertThat(issue.getFlow(0).getLocationCount()).isOne();
    assertThat(issue.getFlow(0).getLocation(0).getTextRange().getStartLine()).isOne();
    assertThat(issue.getFlow(1).getLocationCount()).isOne();
    assertThat(issue.getFlow(1).getLocation(0).getTextRange().getStartLine()).isEqualTo(3);
  }

  private void assertIssueLocationLine(ExternalIssue issue) {
    assertThat(issue.getFlowCount()).isZero();
    assertThat(issue.getMsg()).isEqualTo("fix the bug here");
    assertThat(issue.getEngineId()).isEqualTo("externalXoo");
    assertThat(issue.getRuleId()).isEqualTo("rule2");
    assertThat(issue.getSeverity()).isEqualTo(Severity.CRITICAL);
    assertThat(issue.getType()).isEqualTo(IssueType.BUG);
    assertThat(issue.getEffort()).isZero();
    assertThat(issue.getTextRange().getStartLine()).isEqualTo(3);
    assertThat(issue.getTextRange().getEndLine()).isEqualTo(3);
    assertThat(issue.getTextRange().getStartOffset()).isZero();
    assertThat(issue.getTextRange().getEndOffset()).isEqualTo(24);
  }

  private void assertPreciseIssueLocation(ExternalIssue issue) {
    assertThat(issue.getFlowCount()).isZero();
    assertThat(issue.getMsg()).isEqualTo("fix the issue here");
    assertThat(issue.getEngineId()).isEqualTo("externalXoo");
    assertThat(issue.getRuleId()).isEqualTo("rule1");
    assertThat(issue.getSeverity()).isEqualTo(Severity.MAJOR);
    assertThat(issue.getEffort()).isEqualTo(50L);
    assertThat(issue.getType()).isEqualTo(IssueType.CODE_SMELL);
    assertThat(issue.getTextRange().getStartLine()).isEqualTo(5);
    assertThat(issue.getTextRange().getEndLine()).isEqualTo(5);
    assertThat(issue.getTextRange().getStartOffset()).isEqualTo(3);
    assertThat(issue.getTextRange().getEndOffset()).isEqualTo(41);
  }
}