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.

SarifSerializerImplTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.core.sarif;
  21. import java.net.URISyntaxException;
  22. import java.net.URL;
  23. import java.nio.file.NoSuchFileException;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.util.Set;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.mockito.junit.MockitoJUnitRunner;
  30. import static java.lang.String.format;
  31. import static java.util.Objects.requireNonNull;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  34. import static org.assertj.core.api.Assertions.fail;
  35. import static org.sonar.core.sarif.SarifVersionValidator.UNSUPPORTED_VERSION_MESSAGE_TEMPLATE;
  36. @RunWith(MockitoJUnitRunner.class)
  37. public class SarifSerializerImplTest {
  38. private static final String SARIF_JSON = "{\"version\":\"2.1.0\",\"$schema\":\"http://json.schemastore.org/sarif-2.1.0-rtm.4\",\"runs\":[{\"results\":[]}]}";
  39. private final SarifSerializerImpl serializer = new SarifSerializerImpl();
  40. @Test
  41. public void serialize() {
  42. Run.builder().results(Set.of()).build();
  43. Sarif210 sarif210 = new Sarif210("http://json.schemastore.org/sarif-2.1.0-rtm.4", Run.builder().results(Set.of()).build());
  44. String result = serializer.serialize(sarif210);
  45. assertThat(result).isEqualTo(SARIF_JSON);
  46. }
  47. @Test
  48. public void deserialize() throws URISyntaxException, NoSuchFileException {
  49. URL sarifResource = requireNonNull(getClass().getResource("eslint-sarif210.json"));
  50. Path sarif = Paths.get(sarifResource.toURI());
  51. Sarif210 deserializationResult = serializer.deserialize(sarif);
  52. verifySarif(deserializationResult);
  53. }
  54. @Test
  55. public void deserialize_shouldFail_whenFileCantBeFound() {
  56. String file = "wrongPathToFile";
  57. Path sarif = Paths.get(file);
  58. assertThatThrownBy(() -> serializer.deserialize(sarif))
  59. .isInstanceOf(NoSuchFileException.class);
  60. }
  61. @Test
  62. public void deserialize_shouldFail_whenJsonSyntaxIsIncorrect() throws URISyntaxException {
  63. URL sarifResource = requireNonNull(getClass().getResource("invalid-json-syntax.json"));
  64. Path sarif = Paths.get(sarifResource.toURI());
  65. assertThatThrownBy(() -> serializer.deserialize(sarif))
  66. .isInstanceOf(IllegalStateException.class)
  67. .hasMessage(format("Failed to read SARIF report at '%s': invalid JSON syntax or file is not UTF-8 encoded", sarif));
  68. }
  69. @Test
  70. public void deserialize_whenFileIsNotUtf8encoded_shouldFail() throws URISyntaxException {
  71. URL sarifResource = requireNonNull(getClass().getResource("sarif210-nonUtf8.json"));
  72. Path sarif = Paths.get(sarifResource.toURI());
  73. assertThatThrownBy(() -> serializer.deserialize(sarif))
  74. .isInstanceOf(IllegalStateException.class)
  75. .hasMessage(format("Failed to read SARIF report at '%s': invalid JSON syntax or file is not UTF-8 encoded", sarif));
  76. }
  77. @Test
  78. public void deserialize_shouldFail_whenSarifVersionIsNotSupported() throws URISyntaxException {
  79. URL sarifResource = requireNonNull(getClass().getResource("unsupported-sarif-version-abc.json"));
  80. Path sarif = Paths.get(sarifResource.toURI());
  81. assertThatThrownBy(() -> serializer.deserialize(sarif))
  82. .isInstanceOf(IllegalStateException.class)
  83. .hasMessage(format(UNSUPPORTED_VERSION_MESSAGE_TEMPLATE, "A.B.C"));
  84. }
  85. private void verifySarif(Sarif210 deserializationResult) {
  86. Sarif210 expected = buildExpectedSarif210();
  87. assertThat(deserializationResult).isNotNull();
  88. assertThat(deserializationResult).usingRecursiveComparison().ignoringFields("runs").isEqualTo(expected);
  89. Run run = getRun(deserializationResult);
  90. Run expectedRun = getRun(expected);
  91. assertThat(run).usingRecursiveComparison().ignoringFields("results", "tool.driver.rules").isEqualTo(expectedRun);
  92. Result result = getResult(run);
  93. Result expectedResult = getResult(expectedRun);
  94. assertThat(result).usingRecursiveComparison().isEqualTo(expectedResult);
  95. Rule rule = getRule(run);
  96. Rule expectedRule = getRule(expectedRun);
  97. assertThat(rule).usingRecursiveComparison().ignoringFields("properties").isEqualTo(expectedRule);
  98. }
  99. private static Sarif210 buildExpectedSarif210() {
  100. return new Sarif210("http://json.schemastore.org/sarif-2.1.0-rtm.4", buildExpectedRun());
  101. }
  102. private static Run buildExpectedRun() {
  103. Tool tool = new Tool(buildExpectedDriver());
  104. return Run.builder()
  105. .tool(tool)
  106. .results(Set.of(buildExpectedResult())).build();
  107. }
  108. private static Driver buildExpectedDriver() {
  109. return Driver.builder()
  110. .name("ESLint")
  111. .rules(Set.of(buildExpectedRule()))
  112. .build();
  113. }
  114. private static Rule buildExpectedRule() {
  115. return Rule.builder()
  116. .id("no-unused-vars")
  117. .shortDescription("disallow unused variables")
  118. .build();
  119. }
  120. private static Result buildExpectedResult() {
  121. return Result.builder()
  122. .ruleId("no-unused-vars")
  123. .locations(Set.of(buildExpectedLocation()))
  124. .message("'x' is assigned a value but never used.")
  125. .level("error")
  126. .build();
  127. }
  128. private static Location buildExpectedLocation() {
  129. ArtifactLocation artifactLocation = new ArtifactLocation(null, "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js");
  130. PhysicalLocation physicalLocation = PhysicalLocation.of(artifactLocation, buildExpectedRegion());
  131. return Location.of(physicalLocation);
  132. }
  133. private static Region buildExpectedRegion() {
  134. return Region.builder()
  135. .startLine(1)
  136. .startColumn(5)
  137. .build();
  138. }
  139. private static Run getRun(Sarif210 sarif210) {
  140. return sarif210.getRuns().stream().findFirst().orElseGet(() -> fail("runs property is missing"));
  141. }
  142. private static Result getResult(Run run) {
  143. return run.getResults().stream().findFirst().orElseGet(() -> fail("results property is missing"));
  144. }
  145. private static Rule getRule(Run run) {
  146. return run.getTool().getDriver().getRules().stream().findFirst().orElseGet(() -> fail("rules property is missing"));
  147. }
  148. }