JsonReporter implements Reporter { @Override public void execute(String projectName, Date date, Collection<Trackable> trackables, AnalysisResults result, Function<String, RuleDetails> ruleDescriptionProducer) { try (Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) { JsonWriter jsonWriter = new Gson().newJsonWriter(writer); jsonWriter.beginObject(); jsonWriter.name("issues"); jsonWriter.beginArray(); trackables.stream().map(Trackable::getIssue).forEach(issue -> { try { issueToJSON(issue, ruleDescriptionProducer, jsonWriter); } catch (IOException e) { LOGGER.error("Failed to convert to json " + issue.toString(), e); } }); jsonWriter.endArray(); jsonWriter.endObject(); } catch (IOException e) { LOGGER.error("Error while printing to stream", e); } } JsonReporter(OutputStream stream); @Override void execute(String projectName, Date date, Collection<Trackable> trackables, AnalysisResults result, Function<String, RuleDetails> ruleDescriptionProducer);  }
@Test public void should_print_to_output_stream() throws Exception { reporter.execute("someproject", new Date(), new ArrayList<>(), result, key -> RULE_DESCRIPTION); String output = new String(stream.toByteArray()); assertThat(output).contains("{\"issues\":[]"); }
@Test public void should_print_one_issue() throws Exception { List<Issue> issues = new ArrayList<>(); issues.add(TestUtils.createTestIssue("comp1", "S1000", "some message", "BLOCKER", 10, 2)); reporter.execute("someproject", new Date(), toTrackables(issues), result, key -> RULE_DESCRIPTION); String output = new String(stream.toByteArray()); assertThat(output).isNotEmpty(); assertThat(output).contains("{\"issues\":[{" + "\"file\":\"comp1\"" + ",\"key\":\"S1000\"" + ",\"severity\":\"BLOCKER\"" + ",\"title\":\"some rule description\"" + ",\"message\":\"some message\"" + ",\"pos\":{\"line\":10,\"column\":2}" + ",\"end_pos\":{\"line\":0,\"column\":0}" + "}]"); }