You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSONReportTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan.report;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.StringWriter;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Collections;
  26. import java.util.TimeZone;
  27. import org.apache.commons.io.IOUtils;
  28. import org.junit.Before;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  33. import org.sonar.api.batch.fs.InputFile;
  34. import org.sonar.api.batch.fs.internal.DefaultFileSystem;
  35. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  36. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  37. import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
  38. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  39. import org.sonar.api.batch.rule.Rules;
  40. import org.sonar.api.batch.rule.internal.RulesBuilder;
  41. import org.sonar.api.config.internal.MapSettings;
  42. import org.sonar.api.issue.Issue;
  43. import org.sonar.api.platform.Server;
  44. import org.sonar.api.rule.RuleKey;
  45. import org.sonar.scanner.issue.IssueCache;
  46. import org.sonar.scanner.issue.tracking.TrackedIssue;
  47. import org.sonar.scanner.scan.branch.BranchConfiguration;
  48. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  49. import static net.javacrumbs.jsonunit.assertj.JsonAssert.assertThatJson;
  50. import static org.assertj.core.api.Assertions.assertThat;
  51. import static org.mockito.Mockito.mock;
  52. import static org.mockito.Mockito.verifyZeroInteractions;
  53. import static org.mockito.Mockito.when;
  54. public class JSONReportTest {
  55. private SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
  56. @Rule
  57. public TemporaryFolder temp = new TemporaryFolder();
  58. private JSONReport jsonReport;
  59. private DefaultFileSystem fs;
  60. private Server server = mock(Server.class);
  61. private Rules rules = mock(Rules.class);
  62. private MapSettings settings = new MapSettings();
  63. private IssueCache issueCache = mock(IssueCache.class);
  64. private InputModuleHierarchy moduleHierarchy;
  65. @Before
  66. public void before() throws Exception {
  67. moduleHierarchy = mock(InputModuleHierarchy.class);
  68. File projectBaseDir = temp.newFolder();
  69. fs = new DefaultFileSystem(projectBaseDir.toPath());
  70. SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00"));
  71. when(server.getVersion()).thenReturn("3.6");
  72. ProjectDefinition def = ProjectDefinition.create().setBaseDir(projectBaseDir).setWorkDir(temp.newFolder()).setKey("struts");
  73. DefaultInputProject project = new DefaultInputProject(def, 1);
  74. InputComponentStore inputComponentStore = new InputComponentStore(mock(BranchConfiguration.class));
  75. DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java")
  76. .setModuleBaseDir(projectBaseDir.toPath()).build();
  77. inputFile.setStatus(InputFile.Status.CHANGED);
  78. inputFile.setPublished(true);
  79. inputComponentStore.put("struts", inputFile);
  80. RulesBuilder builder = new RulesBuilder();
  81. builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles");
  82. rules = builder.build();
  83. jsonReport = new JSONReport(settings.asConfig(), fs, server, rules, issueCache, project, inputComponentStore);
  84. }
  85. @Test
  86. public void should_write_json() throws Exception {
  87. TrackedIssue issue = new TrackedIssue();
  88. issue.setKey("200");
  89. issue.setComponentKey("struts:src/main/java/org/apache/struts/Action.java");
  90. issue.setRuleKey(RuleKey.of("squid", "AvoidCycles"));
  91. issue.setMessage("There are 2 cycles");
  92. issue.setSeverity("MINOR");
  93. issue.setStatus(Issue.STATUS_OPEN);
  94. issue.setResolution(null);
  95. issue.setStartLine(1);
  96. issue.setEndLine(2);
  97. issue.setStartLineOffset(3);
  98. issue.setEndLineOffset(4);
  99. issue.setGap(3.14);
  100. issue.setAssignee("simon");
  101. issue.setCreationDate(SIMPLE_DATE_FORMAT.parse("2013-04-24"));
  102. issue.setNew(false);
  103. when(issueCache.all()).thenReturn(Collections.singleton(issue));
  104. StringWriter writer = new StringWriter();
  105. jsonReport.writeJson(writer);
  106. assertThatJson(writer.toString()).isEqualTo(IOUtils.toString(this.getClass().getResource(this.getClass().getSimpleName() + "/report.json")));
  107. }
  108. @Test
  109. public void should_exclude_resolved_issues() throws Exception {
  110. RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
  111. TrackedIssue issue = new TrackedIssue();
  112. issue.setKey("200");
  113. issue.setComponentKey("struts:src/main/java/org/apache/struts/Action.java");
  114. issue.setRuleKey(ruleKey);
  115. issue.setStatus(Issue.STATUS_CLOSED);
  116. issue.setResolution(Issue.RESOLUTION_FIXED);
  117. issue.setCreationDate(SIMPLE_DATE_FORMAT.parse("2013-04-24"));
  118. issue.setNew(false);
  119. when(issueCache.all()).thenReturn(Collections.singleton(issue));
  120. StringWriter writer = new StringWriter();
  121. jsonReport.writeJson(writer);
  122. assertThatJson(writer.toString()).isEqualTo(IOUtils.toString(this.getClass().getResource(this.getClass().getSimpleName() + "/report-without-resolved-issues.json")));
  123. }
  124. @Test
  125. public void should_not_export_by_default() throws IOException {
  126. File workDir = temp.newFolder("sonar");
  127. fs.setWorkDir(workDir.toPath());
  128. jsonReport.execute();
  129. verifyZeroInteractions(issueCache);
  130. }
  131. @Test
  132. public void should_export_issues_to_file() throws IOException {
  133. File workDir = temp.newFolder("sonar");
  134. fs.setWorkDir(workDir.toPath());
  135. when(issueCache.all()).thenReturn(Collections.<TrackedIssue>emptyList());
  136. settings.setProperty("sonar.report.export.path", "output.json");
  137. jsonReport.execute();
  138. assertThat(new File(workDir, "output.json")).exists();
  139. }
  140. }