diff options
author | Antoine Vinot <antoine.vinot@sonarsource.com> | 2022-11-08 12:11:52 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-11-15 20:02:59 +0000 |
commit | 7b6049377bbeb887442a038efbc119656ebb3a12 (patch) | |
tree | dad87ff49b0f771fc82154e0180c4804ce2936ba /sonar-core/src/test | |
parent | e71079a32e8938780c040d4c08aedd5591352b7e (diff) | |
download | sonarqube-7b6049377bbeb887442a038efbc119656ebb3a12.tar.gz sonarqube-7b6049377bbeb887442a038efbc119656ebb3a12.zip |
SONAR-17560 Move existing sarif object to sonar-core
Diffstat (limited to 'sonar-core/src/test')
5 files changed, 237 insertions, 0 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/sarif/RuleTest.java b/sonar-core/src/test/java/org/sonar/core/sarif/RuleTest.java new file mode 100644 index 00000000000..e579f8c3571 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/sarif/RuleTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2017-2022 SonarSource SA + * All rights reserved + * mailto:info AT sonarsource DOT com + */ +package org.sonar.core.sarif; + +import java.util.Set; +import org.apache.commons.lang.RandomStringUtils; +import org.junit.Test; +import org.sonar.api.rule.RuleKey; +import org.sonar.core.sarif.PropertiesBag; +import org.sonar.core.sarif.Rule; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class RuleTest { + + @Test + public void equals_matchOnlyOnId() { + Rule rule1 = createRule("rep1", "rule1"); + Rule rule1Bis = createRule("rep1", "rule1"); + Rule rule2 = withRuleId(rule1, "rep1", "rule2"); + + assertThat(rule1).isEqualTo(rule1Bis).isNotEqualTo(rule2); + } + + @Test + public void equals_notMatchWithNull(){ + Rule rule1 = createRule("rep1", "rule2"); + + assertThat(rule1).isNotEqualTo(null); + } + + @Test + public void equals_matchWithSameObject(){ + Rule rule1 = createRule("rep5", "rule2"); + + assertThat(rule1).isEqualTo(rule1); + } + + private static Rule withRuleId(Rule rule, String repoName, String ruleName) { + return new Rule(RuleKey.of(repoName, ruleName), rule.getName(), rule.getFullDescription().getText(), rule.getProperties()); + } + + private static Rule createRule(String repoName, String ruleName) { + return new Rule(RuleKey.of(repoName, ruleName), RandomStringUtils.randomAlphanumeric(5), RandomStringUtils.randomAlphanumeric(5), + PropertiesBag.of(RandomStringUtils.randomAlphanumeric(3), Set.of(RandomStringUtils.randomAlphanumeric(4)))); + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/sarif/Sarif210SerializationDeserializationTest.java b/sonar-core/src/test/java/org/sonar/core/sarif/Sarif210SerializationDeserializationTest.java new file mode 100644 index 00000000000..ef40fe8dcd5 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/sarif/Sarif210SerializationDeserializationTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017-2022 SonarSource SA + * All rights reserved + * mailto:info AT sonarsource DOT com + */ +package org.sonar.core.sarif; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.sonar.core.sarif.Sarif210; +import org.sonar.test.JsonAssert; + +import static java.util.Objects.requireNonNull; + +public class Sarif210SerializationDeserializationTest { + + private static final String VALID_SARIF_210_FILE_JSON = "valid-sarif210.json"; + + @Test + public void verify_json_serialization_of_sarif210() throws IOException { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + String expectedJson = IOUtils.toString(requireNonNull(getClass().getResource(VALID_SARIF_210_FILE_JSON)), StandardCharsets.UTF_8); + Sarif210 deserializedJson = gson.fromJson(expectedJson, Sarif210.class); + String reserializedJson = gson.toJson(deserializedJson); + + JsonAssert.assertJson(reserializedJson).isSimilarTo(expectedJson); + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/sarif/SarifSerializerTest.java b/sonar-core/src/test/java/org/sonar/core/sarif/SarifSerializerTest.java new file mode 100644 index 00000000000..e239a5e859f --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/sarif/SarifSerializerTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2017-2022 SonarSource SA + * All rights reserved + * mailto:info AT sonarsource DOT com + */ +package org.sonar.core.sarif; + +import com.google.gson.Gson; +import org.sonar.core.sarif.Sarif210; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class SarifSerializerTest { + + private static final String SARIF_JSON = "{\"message\" : \"A sarif in json format as String.\"}"; + private static final String SARIF_JSON_ENCODED = "H4sIAAAAAAAAAKtWyk0tLk5MT1VSsFJQclQoTizKTFPIzFPIKs7PU0jLL8pNLFFILFYILinKzEvXU6oFACgK7/YxAAAA"; + + @Mock + private Gson gson; + + @InjectMocks + private SarifSerializer serializer; + + @Test + public void serializeAndEncode_should_compressInGZipAndEncodeBase64() { + when(gson.toJson(any(Sarif210.class))).thenReturn(SARIF_JSON); + Sarif210 sarif210 = mock(Sarif210.class); + + String encoded = serializer.serializeAndEncode(sarif210); + + assertThat(encoded).isEqualTo(SARIF_JSON_ENCODED); + } + +} diff --git a/sonar-core/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/sonar-core/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 00000000000..1f0955d450f --- /dev/null +++ b/sonar-core/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline diff --git a/sonar-core/src/test/resources/org/sonar/core/sarif/valid-sarif210.json b/sonar-core/src/test/resources/org/sonar/core/sarif/valid-sarif210.json new file mode 100644 index 00000000000..7e0c2ef9340 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/sarif/valid-sarif210.json @@ -0,0 +1,107 @@ +{ + "version": "2.1.0", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", + "runs": [ + { + "tool": { + "driver": { + "name": "SonarQube", + "organization": "SonarSource", + "semanticVersion": "9.6", + "rules": [ + { + "id": "java:S5132", + "name": "java:S5132", + "shortDescription": { + "text": "Make this final static field too." + }, + "fullDescription": { + "text": "Make this final static field too." + }, + "help": { + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam hendrerit nisi sed sollicitudin pellentesque. Nunc posuere purus rhoncus pulvinar aliquam. Ut aliquet tristique nisl vitae volutpat. Nulla aliquet porttitor venenatis. Donec a dui et dui fringilla consectetur id nec massa. Aliquam erat volutpat. Sed ut dui ut lacus dictum fermentum vel tincidunt neque. Sed sed lacinia lectus. Duis sit amet sodales felis. Duis nunc eros, mattis at dui ac, convallis semper risus. In adipiscing ultrices tellus, in suscipit massa vehicula eu." + }, + "properties": { + "tags": [ + "tag1", + "tag2" + ] + } + } + ] + } + }, + "results": [ + { + "ruleId": "java:S5132", + "message": { + "text": "this is the message" + }, + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "www.google.com", + "uriBaseId": "%SRCROOT" + }, + "region": { + "startLine": 11, + "endLine": 222, + "startColumn": 54, + "endColumn": 4 + } + } + } + ], + "partialFingerprints": { + "primaryLocationLineHash": "thisISTHEHAS" + }, + "codeFlows": [ + { + "threadFlows": [ + { + "locations": [ + { + "location": { + "physicalLocation": { + "artifactLocation": { + "uri": "www.google.com", + "uriBaseId": "%SRCROOT" + }, + "region": { + "startLine": 11, + "endLine": 222, + "startColumn": 54, + "endColumn": 4 + } + } + } + }, + { + "location": { + "physicalLocation": { + "artifactLocation": { + "uri": "www.google.com", + "uriBaseId": "%SRCROOT" + }, + "region": { + "startLine": 22, + "endLine": 4323, + "startColumn": 545, + "endColumn": 4324 + } + } + } + } + ] + } + ] + } + ] + } + ], + "language": "en-us", + "columnKind": "utf16CodeUnits" + } + ] +} |