Skip to content

Commit 0c43a88

Browse files
committed
importing raw report
1 parent 93147dd commit 0c43a88

File tree

9 files changed

+191
-95
lines changed

9 files changed

+191
-95
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<jdk.min.version>1.8</jdk.min.version>
1717
<sslr.version>1.23</sslr.version>
1818
<mockito.version>2.21.0</mockito.version>
19-
<assertj-core.version>3.6.1</assertj-core.version>
19+
<assertj-core.version>3.15.0</assertj-core.version>
2020
</properties>
2121

2222
<dependencies>

src/main/java/org/elegoff/plugins/rust/externalreport/clippy/ClippyJsonReportReader.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.elegoff.plugins.rust.externalreport.clippy;
22

33
import javax.annotation.Nullable;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.InputStreamReader;
4+
import java.io.*;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.List;
8+
import java.util.ArrayList;
79
import java.util.stream.Stream;
810
import java.util.function.Consumer;
911
import org.sonarsource.analyzer.commons.internal.json.simple.JSONArray;
@@ -42,9 +44,9 @@ static void read(InputStream in, Consumer<Issue> consumer) throws IOException, P
4244

4345
private void read(InputStream in) throws IOException, ParseException {
4446
JSONObject rootObject = (JSONObject) jsonParser.parse(new InputStreamReader(in, UTF_8));
45-
JSONArray files = (JSONArray) rootObject.get("results");
46-
if (files != null) {
47-
((Stream<JSONObject>) files.stream()).forEach(this::onResult);
47+
JSONArray results = (JSONArray) rootObject.get("results");
48+
if (results != null) {
49+
((Stream<JSONObject>) results.stream()).forEach(this::onResult);
4850
}
4951
}
5052

@@ -79,4 +81,41 @@ private static Integer toInteger(Object value) {
7981
return null;
8082
}
8183

84+
public static InputStream toJSON(File rawReport) throws IOException{
85+
String BEGIN="{\"results\": [";
86+
String END="]}";
87+
88+
if (rawReport == null) {
89+
throw new FileNotFoundException();
90+
}
91+
92+
StringBuffer sb = new StringBuffer(BEGIN);
93+
94+
//read text report line by line
95+
String reportPath = rawReport.getAbsolutePath();
96+
BufferedReader reader;
97+
98+
reader = new BufferedReader(new FileReader(
99+
reportPath));
100+
String line = reader.readLine();
101+
String separator="";
102+
while (line != null) {
103+
104+
// read next line
105+
106+
//a valid Clippy result needs to be a valid json String
107+
if (line.startsWith("{") && line.endsWith("}")){
108+
sb.append(separator).append(line);
109+
separator= ",";
110+
}
111+
line = reader.readLine();
112+
}
113+
reader.close();
114+
115+
sb.append(END);
116+
117+
InputStream in = new ByteArrayInputStream(sb.toString().getBytes());
118+
return in;
119+
}
120+
82121
}

src/main/java/org/elegoff/plugins/rust/externalreport/clippy/ClippySensor.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.sonar.api.utils.log.Loggers;
2828
import org.sonarsource.analyzer.commons.ExternalReportProvider;
2929
import org.sonarsource.analyzer.commons.ExternalRuleLoader;
30+
import org.sonarsource.analyzer.commons.internal.json.simple.JSONObject;
3031
import org.sonarsource.analyzer.commons.internal.json.simple.parser.ParseException;
3132

3233
import static org.apache.commons.lang.StringUtils.isEmpty;
@@ -52,7 +53,24 @@ public void execute(SensorContext context) {
5253
}
5354

5455

55-
private void importReport(File reportPath, SensorContext context, Set<String> unresolvedInputFiles) {
56+
private void importReport(File rawReport, SensorContext context, Set<String> unresolvedInputFiles) {
57+
LOG.info("Importing {}", rawReport);
58+
59+
60+
61+
try {
62+
InputStream in = ClippyJsonReportReader.toJSON(rawReport);
63+
ClippyJsonReportReader.read(in, issue -> saveIssue(context, issue, unresolvedInputFiles));
64+
} catch (IOException | ParseException e) {
65+
LOG.error("No issues information will be saved as the report file '{}' can't be read. " +
66+
e.getClass().getSimpleName() + ": " + e.getMessage(), rawReport, e);
67+
}
68+
69+
}
70+
71+
72+
73+
private void importReportObsolete(File reportPath, SensorContext context, Set<String> unresolvedInputFiles) {
5674
try (InputStream in = new FileInputStream(reportPath)) {
5775
LOG.info("Importing {}", reportPath);
5876

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package org.elegoff.plugins.rust.externalreport.clippy;
2+
3+
import org.junit.Test;
4+
import org.sonarsource.analyzer.commons.internal.json.simple.JSONArray;
5+
import org.sonarsource.analyzer.commons.internal.json.simple.JSONObject;
6+
import org.sonarsource.analyzer.commons.internal.json.simple.parser.JSONParser;
7+
import org.sonarsource.analyzer.commons.internal.json.simple.parser.ParseException;
8+
9+
import java.io.*;
10+
import java.net.URL;
11+
import java.util.List;
12+
13+
import static java.nio.charset.StandardCharsets.UTF_8;
14+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
15+
import static org.fest.assertions.Assertions.assertThat;
16+
17+
public class ClippyJsonReportReaderTest {
18+
private final JSONParser jsonParser = new JSONParser();
19+
20+
@Test
21+
public void noReportProvided(){
22+
assertThatThrownBy(() -> {
23+
InputStream in = ClippyJsonReportReader.toJSON(null);;
24+
}).isInstanceOf(IOException.class);
25+
}
26+
27+
@Test
28+
public void invalidReportPathProvided(){
29+
assertThatThrownBy(() -> {
30+
InputStream in = ClippyJsonReportReader.toJSON(new File("invalid.txt"));;
31+
}).isInstanceOf(IOException.class);
32+
}
33+
34+
@Test
35+
public void emptyReport() {
36+
File empty = this.getFileFromResources("org/sonar/plugins/rust/clippy/empty-report.txt");
37+
InputStream in = null;
38+
try {
39+
in = ClippyJsonReportReader.toJSON(empty);
40+
assertThat(in).isNotNull();
41+
assertThat(isValidJsonReport(in)).isTrue();
42+
in = ClippyJsonReportReader.toJSON(empty);
43+
assertThat(issueCount(in)).isZero();
44+
45+
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
@Test
52+
public void validReport() {
53+
File report = this.getFileFromResources("org/sonar/plugins/rust/clippy/myreport.txt");
54+
InputStream in = null;
55+
try {
56+
in = ClippyJsonReportReader.toJSON(report);
57+
assertThat(in).isNotNull();
58+
assertThat(isValidJsonReport(in)).isTrue();
59+
in = ClippyJsonReportReader.toJSON(report);
60+
assertThat(issueCount(in)).isEqualTo(2);
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
private File getFileFromResources(String fileName) {
67+
68+
ClassLoader classLoader = getClass().getClassLoader();
69+
70+
URL resource = classLoader.getResource(fileName);
71+
if (resource == null) {
72+
throw new IllegalArgumentException("file is not found!");
73+
} else {
74+
return new File(resource.getFile());
75+
}
76+
77+
}
78+
79+
private boolean isValidJsonReport(InputStream in){
80+
JSONObject rootObject = null;
81+
boolean res = false;
82+
try {
83+
rootObject = (JSONObject) jsonParser.parse(new InputStreamReader(in, UTF_8));
84+
85+
JSONArray results = (JSONArray) rootObject.get("results");
86+
if (results != null) return true;
87+
88+
} catch (ParseException |IOException e) {
89+
e.printStackTrace();
90+
}
91+
return res;
92+
}
93+
94+
private int issueCount(InputStream in){
95+
JSONObject rootObject = null;
96+
int NbIssue = 0;
97+
try {
98+
rootObject = (JSONObject) jsonParser.parse(new InputStreamReader(in, UTF_8));
99+
100+
JSONArray results = (JSONArray) rootObject.get("results");
101+
for (Object o: results){
102+
JSONObject jo = (JSONObject)o;
103+
JSONObject message = (JSONObject)jo.get("message");
104+
JSONArray spans = (JSONArray)message.get("spans");
105+
if ((spans != null)&& spans.size()>0){
106+
NbIssue++;
107+
}
108+
109+
}
110+
111+
//JSONObject message = (JSONObject) results.get("message");
112+
//if (results != null) return results.size();
113+
114+
115+
} catch (ParseException |IOException e) {
116+
e.printStackTrace();
117+
}
118+
return NbIssue;
119+
}
120+
121+
}

src/test/java/org/elegoff/plugins/rust/externalreport/clippy/ClippySensorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class ClippySensorTest{
3333
private static final String CLIPPY_FILE = "rust-project:clippy/main.rs";
3434
private static final String CLIPPY_AEC="external_clippy:clippy::absurd_extreme_comparisons";
35-
private static final String CLIPPY_REPORT_JSON = "clippy-report.json";
35+
private static final String CLIPPY_REPORT_TXT = "myreport.txt";
3636

3737
private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "org", "sonar", "plugins", "rust", "clippy");
3838

@@ -53,7 +53,7 @@ public void test_descriptor() {
5353

5454
@Test
5555
public void issues_with_sonarqube() throws IOException {
56-
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, CLIPPY_REPORT_JSON);
56+
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, CLIPPY_REPORT_TXT);
5757
assertThat(externalIssues).hasSize(2);
5858

5959
ExternalIssue first = externalIssues.get(0);
@@ -101,7 +101,7 @@ public void no_issues_with_invalid_report_path() throws IOException {
101101

102102
@Test
103103
public void no_issues_with_empty_clippy_report() throws IOException {
104-
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, "clippy-report-empty.json");
104+
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, "empty-report.txt");
105105
assertThat(externalIssues).isEmpty();
106106
assertNoErrorWarnDebugLogs(logTester);
107107
}

src/test/resources/org/sonar/plugins/rust/clippy/clippy-report-empty.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/test/resources/org/sonar/plugins/rust/clippy/clippy-report.json

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/test/resources/org/sonar/plugins/rust/clippy/empty-report.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)