Skip to content

Commit 44103f4

Browse files
test: Add test for CycloneDX XML with authors
This test is currently expected to fail due to a bug in the cyclonedx-core-java [1]. [1]: CycloneDX/cyclonedx-core-java#638 Signed-off-by: Marcel Bochtler <marcel.bochtler@bosch.com>
1 parent d4c6d1b commit 44103f4

File tree

2 files changed

+435
-0
lines changed

2 files changed

+435
-0
lines changed

plugins/reporters/cyclonedx/src/funTest/kotlin/CycloneDxReporterFunTest.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import io.kotest.matchers.shouldNotBe
3636
import org.cyclonedx.parsers.JsonParser
3737
import org.cyclonedx.parsers.XmlParser
3838

39+
import org.ossreviewtoolkit.model.Identifier
40+
import org.ossreviewtoolkit.model.OrtResult
3941
import org.ossreviewtoolkit.plugins.reporters.cyclonedx.CycloneDxReporter.Companion.REPORT_BASE_FILENAME
4042
import org.ossreviewtoolkit.reporter.ORT_RESULT
4143
import org.ossreviewtoolkit.reporter.ORT_RESULT_WITH_ILLEGAL_COPYRIGHTS
@@ -94,6 +96,30 @@ class CycloneDxReporterFunTest : WordSpec({
9496
}
9597
}
9698

99+
"create the expected XML file with authors" {
100+
val expectedBom = readResource("/cyclonedx-reporter-expected-result-with-authors.xml")
101+
102+
val ortResult = ORT_RESULT.withAuthors(
103+
packageId = Identifier("NPM:@ort:license-file:1.0"),
104+
authors = setOf("Author One", "Author Two")
105+
)
106+
107+
val bomFileResults = CycloneDxReporterFactory.create(
108+
singleBom = true,
109+
outputFileFormats = listOf("xml")
110+
).generateReport(ReporterInput(ortResult), outputDir)
111+
112+
bomFileResults.shouldBeSingleton {
113+
it shouldBeSuccess { bomFile ->
114+
bomFile shouldBe aFile()
115+
bomFile shouldNotBe emptyFile()
116+
117+
val actualBom = bomFile.readText().patchCycloneDxResult().normalizeLineBreaks()
118+
actualBom shouldBe expectedBom
119+
}
120+
}
121+
}
122+
97123
"the expected XML file even if some copyrights contain non printable characters" {
98124
val bomFileResults = CycloneDxReporterFactory.create(
99125
singleBom = true,
@@ -224,6 +250,26 @@ class CycloneDxReporterFunTest : WordSpec({
224250
}
225251
})
226252

253+
/**
254+
* Add [authors] to the [package][packageId].
255+
*/
256+
private fun OrtResult.withAuthors(packageId: Identifier, authors: Set<String>): OrtResult =
257+
analyzer?.let { analyzer ->
258+
copy(
259+
analyzer = analyzer.copy(
260+
result = analyzer.result.copy(
261+
packages = analyzer.result.packages.map { pkg ->
262+
if (pkg.id == packageId) {
263+
pkg.copy(authors = authors)
264+
} else {
265+
pkg
266+
}
267+
}.toSet()
268+
)
269+
)
270+
)
271+
} ?: this
272+
227273
private fun String.patchCycloneDxResult(): String =
228274
replaceFirst(
229275
"""urn:uuid:[a-f0-9]{8}(?:-[a-f0-9]{4}){4}[a-f0-9]{8}""".toRegex(),

0 commit comments

Comments
 (0)