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.

ReportParserTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.externalissue;
  21. import java.nio.file.Path;
  22. import java.nio.file.Paths;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.sonar.scanner.externalissue.ReportParser.Report;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. public class ReportParserTest {
  29. @Rule
  30. public ExpectedException exception = ExpectedException.none();
  31. @Test
  32. public void parse_sample() {
  33. ReportParser parser = new ReportParser(Paths.get("src/test/resources/org/sonar/scanner/externalissue/report.json"));
  34. Report report = parser.parse();
  35. assertThat(report.issues).hasSize(4);
  36. assertThat(report.issues[0].engineId).isEqualTo("eslint");
  37. assertThat(report.issues[0].ruleId).isEqualTo("rule1");
  38. assertThat(report.issues[0].severity).isEqualTo("MAJOR");
  39. assertThat(report.issues[0].effortMinutes).isEqualTo(40);
  40. assertThat(report.issues[0].type).isEqualTo("CODE_SMELL");
  41. assertThat(report.issues[0].primaryLocation.filePath).isEqualTo("file1.js");
  42. assertThat(report.issues[0].primaryLocation.message).isEqualTo("fix the issue here");
  43. assertThat(report.issues[0].primaryLocation.textRange.startColumn).isEqualTo(2);
  44. assertThat(report.issues[0].primaryLocation.textRange.startLine).isEqualTo(1);
  45. assertThat(report.issues[0].primaryLocation.textRange.endColumn).isEqualTo(4);
  46. assertThat(report.issues[0].primaryLocation.textRange.endLine).isEqualTo(3);
  47. assertThat(report.issues[0].secondaryLocations).isNull();
  48. assertThat(report.issues[3].engineId).isEqualTo("eslint");
  49. assertThat(report.issues[3].ruleId).isEqualTo("rule3");
  50. assertThat(report.issues[3].severity).isEqualTo("MAJOR");
  51. assertThat(report.issues[3].effortMinutes).isNull();
  52. assertThat(report.issues[3].type).isEqualTo("BUG");
  53. assertThat(report.issues[3].secondaryLocations).hasSize(2);
  54. assertThat(report.issues[3].secondaryLocations[0].filePath).isEqualTo("file1.js");
  55. assertThat(report.issues[3].secondaryLocations[0].message).isEqualTo("fix the bug here");
  56. assertThat(report.issues[3].secondaryLocations[0].textRange.startLine).isEqualTo(1);
  57. assertThat(report.issues[3].secondaryLocations[1].filePath).isEqualTo("file2.js");
  58. assertThat(report.issues[3].secondaryLocations[1].message).isNull();
  59. assertThat(report.issues[3].secondaryLocations[1].textRange.startLine).isEqualTo(2);
  60. }
  61. private Path path(String reportName) {
  62. return Paths.get("src/test/resources/org/sonar/scanner/externalissue/" + reportName);
  63. }
  64. @Test
  65. public void fail_if_report_doesnt_exist() {
  66. ReportParser parser = new ReportParser(Paths.get("unknown.json"));
  67. exception.expect(IllegalStateException.class);
  68. exception.expectMessage("Failed to read external issues report 'unknown.json'");
  69. parser.parse();
  70. }
  71. @Test
  72. public void fail_if_report_is_not_valid_json() {
  73. ReportParser parser = new ReportParser(path("report_invalid_json.json"));
  74. exception.expect(IllegalStateException.class);
  75. exception.expectMessage("invalid JSON syntax");
  76. parser.parse();
  77. }
  78. @Test
  79. public void fail_if_primaryLocation_not_set() {
  80. ReportParser parser = new ReportParser(path("report_missing_primaryLocation.json"));
  81. exception.expect(IllegalStateException.class);
  82. exception.expectMessage("missing mandatory field 'primaryLocation'");
  83. parser.parse();
  84. }
  85. @Test
  86. public void fail_if_engineId_not_set() {
  87. ReportParser parser = new ReportParser(path("report_missing_engineId.json"));
  88. exception.expect(IllegalStateException.class);
  89. exception.expectMessage("missing mandatory field 'engineId'");
  90. parser.parse();
  91. }
  92. @Test
  93. public void fail_if_ruleId_not_set() {
  94. ReportParser parser = new ReportParser(path("report_missing_ruleId.json"));
  95. exception.expect(IllegalStateException.class);
  96. exception.expectMessage("missing mandatory field 'ruleId'");
  97. parser.parse();
  98. }
  99. @Test
  100. public void fail_if_severity_not_set() {
  101. ReportParser parser = new ReportParser(path("report_missing_severity.json"));
  102. exception.expect(IllegalStateException.class);
  103. exception.expectMessage("missing mandatory field 'severity'");
  104. parser.parse();
  105. }
  106. @Test
  107. public void fail_if_type_not_set() {
  108. ReportParser parser = new ReportParser(path("report_missing_type.json"));
  109. exception.expect(IllegalStateException.class);
  110. exception.expectMessage("missing mandatory field 'type'");
  111. parser.parse();
  112. }
  113. @Test
  114. public void fail_if_filePath_not_set_in_primaryLocation() {
  115. ReportParser parser = new ReportParser(path("report_missing_filePath.json"));
  116. exception.expect(IllegalStateException.class);
  117. exception.expectMessage("missing mandatory field 'filePath'");
  118. parser.parse();
  119. }
  120. @Test
  121. public void fail_if_message_not_set_in_primaryLocation() {
  122. ReportParser parser = new ReportParser(path("report_missing_message.json"));
  123. exception.expect(IllegalStateException.class);
  124. exception.expectMessage("missing mandatory field 'message'");
  125. parser.parse();
  126. }
  127. }