From 3f0a34901c55eb1e204fe4028d2773f8484b513f Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Wed, 9 Oct 2024 15:09:39 +0200 Subject: SONAR-23327 Implement addTelemetryProperty in the scanner engine --- .../scanner/protocol/output/FileStructureTest.java | 21 ++++++++++++++----- .../protocol/output/ScannerReportWriterTest.java | 24 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'sonar-scanner-protocol/src/test/java/org/sonar/scanner') diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java index b2787934275..9c7ada548b3 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java @@ -20,6 +20,7 @@ package org.sonar.scanner.protocol.output; import java.io.File; +import java.nio.charset.Charset; import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; @@ -60,10 +61,10 @@ public class FileStructureTest { @Test public void locate_files() throws Exception { File dir = temp.newFolder(); - FileUtils.write(new File(dir, "metadata.pb"), "metadata content"); - FileUtils.write(new File(dir, "issues-3.pb"), "external issues of component 3"); - FileUtils.write(new File(dir, "external-issues-3.pb"), "issues of component 3"); - FileUtils.write(new File(dir, "component-42.pb"), "details of component 42"); + FileUtils.write(new File(dir, "metadata.pb"), "metadata content", Charset.defaultCharset()); + FileUtils.write(new File(dir, "issues-3.pb"), "external issues of component 3", Charset.defaultCharset()); + FileUtils.write(new File(dir, "external-issues-3.pb"), "issues of component 3", Charset.defaultCharset()); + FileUtils.write(new File(dir, "component-42.pb"), "details of component 42", Charset.defaultCharset()); FileStructure structure = new FileStructure(dir); assertThat(structure.metadataFile()).exists().isFile(); @@ -78,9 +79,19 @@ public class FileStructureTest { public void contextProperties_file() throws Exception { File dir = temp.newFolder(); File file = new File(dir, "context-props.pb"); - FileUtils.write(file, "content"); + FileUtils.write(file, "content", Charset.defaultCharset()); FileStructure structure = new FileStructure(dir); assertThat(structure.contextProperties()).exists().isFile().isEqualTo(file); } + + @Test + public void telemetryFile_hasTheCorrectName() throws Exception { + File dir = temp.newFolder(); + File file = new File(dir, "telemetry-entries.pb"); + FileUtils.write(file, "content", Charset.defaultCharset()); + + FileStructure structure = new FileStructure(dir); + assertThat(structure.telemetryEntries()).exists().isFile().isEqualTo(file); + } } diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java index 368886e2265..4b89c9a3db5 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java @@ -154,7 +154,8 @@ public class ScannerReportWriterTest { // write data ScannerReport.Cve cve = ScannerReport.Cve.newBuilder() .setCveId("CVE-2023-20863") - .setDescription("In spring framework versions prior to 5.2.24 release+ ,5.3.27+ and 6.0.8+ , it is possible for a user to provide a specially crafted SpEL expression that may cause a denial-of-service (DoS) condition.") + .setDescription("In spring framework versions prior to 5.2.24 release+ ,5.3.27+ and 6.0.8+ , it is possible for a user to provide a" + + " specially crafted SpEL expression that may cause a denial-of-service (DoS) condition.") .setCvssScore(6.5f) .setEpssScore(0.00306f) .setEpssPercentile(0.70277f) @@ -366,4 +367,25 @@ public class ScannerReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGES, 1)).isTrue(); } + @Test + public void write_telemetry() { + + List input = List.of( + ScannerReport.TelemetryEntry.newBuilder() + .setKey("key") + .setValue("value").build(), + ScannerReport.TelemetryEntry.newBuilder() + .setKey("key2") + .setValue("value2").build()); + + underTest.writeTelemetry(input); + + try (CloseableIterator telemetryIterator = + Protobuf.readStream(underTest.getFileStructure().telemetryEntries(), ScannerReport.TelemetryEntry.parser())) { + + assertThat(telemetryIterator).toIterable() + .containsExactlyElementsOf(input) + .hasSize(input.size()); + } + } } -- cgit v1.2.3