Skip to content

Commit

Permalink
test(plugins): Simplify creating plugin instances
Browse files Browse the repository at this point in the history
Use the convenience function introduced in the previous commit to
simplify the creation of plugin instances in tests.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher committed Nov 25, 2024
1 parent 8061a66 commit 1f76243
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 99 deletions.
3 changes: 1 addition & 2 deletions plugins/advisors/osv/src/funTest/kotlin/OsvFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.toPurl
import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.utils.test.getAssetFile

class OsvFunTest : StringSpec({
Expand Down Expand Up @@ -89,7 +88,7 @@ class OsvFunTest : StringSpec({
private fun identifierToPackage(id: String): Package =
Identifier(id).let { Package.EMPTY.copy(id = it, purl = it.toPurl()) }

private fun createOsv(): Osv = OsvFactory().create(PluginConfig())
private fun createOsv(): Osv = OsvFactory.create()

private fun Map<Identifier, AdvisorResult>.patchTimes(): Map<Identifier, AdvisorResult> =
mapValues { (_, advisorResult) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import io.kotest.matchers.shouldBe
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.utils.toPurl
import org.ossreviewtoolkit.plugins.api.PluginConfig

class VulnerableCodeFunTest : WordSpec({
"Vulnerable Go packages" should {
"return findings for QUIC" {
val vc = VulnerableCodeFactory().create(PluginConfig())
val vc = VulnerableCodeFactory.create()
val id = Identifier("Go::github.com/quic-go/quic-go:0.40.0")
val pkg = Package.EMPTY.copy(id, purl = id.toPurl())

Expand All @@ -60,7 +59,7 @@ class VulnerableCodeFunTest : WordSpec({

"Vulnerable Maven packages" should {
"return findings for Guava" {
val vc = VulnerableCodeFactory().create(PluginConfig())
val vc = VulnerableCodeFactory.create()
val id = Identifier("Maven:com.google.guava:guava:19.0")
val pkg = Package.EMPTY.copy(id, purl = id.toPurl())

Expand All @@ -86,7 +85,7 @@ class VulnerableCodeFunTest : WordSpec({
}

"return findings for Commons-Compress" {
val vc = VulnerableCodeFactory().create(PluginConfig())
val vc = VulnerableCodeFactory.create()
val id = Identifier("Maven:org.apache.commons:commons-compress:1.23.0")
val pkg = Package.EMPTY.copy(id, purl = id.toPurl())

Expand All @@ -112,7 +111,7 @@ class VulnerableCodeFunTest : WordSpec({

"Vulnerable NPM packages" should {
"return findings for Elliptic" {
val vc = VulnerableCodeFactory().create(PluginConfig())
val vc = VulnerableCodeFactory.create()
val id = Identifier("NPM::elliptic:6.5.7")
val pkg = Package.EMPTY.copy(id, purl = id.toPurl())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import kotlin.time.Duration.Companion.seconds
import org.ossreviewtoolkit.clients.clearlydefined.ClearlyDefinedService.Server
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.utils.spdx.toSpdx

class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({
"The production server" should {
val provider = ClearlyDefinedPackageCurationProviderFactory().create(PluginConfig())
val provider = ClearlyDefinedPackageCurationProviderFactory.create()

"return an existing curation for the javax.servlet-api Maven package" {
val packages = createPackagesFromIds("Maven:javax.servlet:javax.servlet-api:3.1.0")
Expand Down Expand Up @@ -93,7 +92,7 @@ class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({
}

"be retrieved for packages without a namespace" {
val provider = ClearlyDefinedPackageCurationProviderFactory().create(PluginConfig())
val provider = ClearlyDefinedPackageCurationProviderFactory.create()
val packages = createPackagesFromIds("NPM::acorn:0.6.0")

withRetry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import io.kotest.matchers.shouldNot

import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.plugins.api.PluginConfig

class OrtConfigPackageCurationProviderFunTest : StringSpec({
fun createProvider() = OrtConfigPackageCurationProviderFactory().create(PluginConfig())
fun createProvider() = OrtConfigPackageCurationProviderFactory.create()

"The provider succeeds to return known curations for packages" {
val azureCore = Identifier("NuGet:Azure:Core:1.22.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import io.kotest.matchers.should

import java.time.LocalDate

import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.test.getAssetFile
Expand All @@ -35,7 +34,7 @@ class DocBookTemplateReporterFunTest : StringSpec({
"DocBook report is created from default template" {
val expectedResultFile = getAssetFile("docbook-template-reporter-expected-result.xml")

val reportContent = DocBookTemplateReporterFactory().create(PluginConfig())
val reportContent = DocBookTemplateReporterFactory.create()
.generateReport(ReporterInput(ORT_RESULT), tempdir())
.single().getOrThrow().readText()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import io.kotest.core.spec.style.StringSpec
import io.kotest.engine.spec.tempdir
import io.kotest.matchers.shouldBe

import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.test.getAssetFile
Expand All @@ -33,7 +32,7 @@ class HtmlTemplateReporterFunTest : StringSpec({
"HTML report is created from default template" {
val expectedResultFile = getAssetFile("html-template-reporter-expected-result.html")

val reporter = HtmlTemplateReporterFactory().create(PluginConfig())
val reporter = HtmlTemplateReporterFactory.create()
val reportContent = reporter.generateReport(ReporterInput(ORT_RESULT), tempdir())
.single().getOrThrow().readText()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import io.kotest.matchers.should

import java.time.LocalDate

import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.test.getAssetFile
Expand All @@ -34,7 +33,7 @@ import org.ossreviewtoolkit.utils.test.matchExpectedResult
class ManPageTemplateReporterFunTest : StringSpec({
"ManPage report is created from default template" {
val expectedResultFile = getAssetFile("manpage-template-reporter-expected-result.1")
val reporter = ManPageTemplateReporterFactory().create(PluginConfig())
val reporter = ManPageTemplateReporterFactory.create()

val reportContent = reporter.generateReport(ReporterInput(ORT_RESULT), tempdir())
.single().getOrThrow().readText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ import io.kotest.matchers.result.shouldBeSuccess
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe

import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ORT_RESULT_WITH_VULNERABILITIES
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.common.Options

class PdfTemplateReporterFunTest : StringSpec({
"The report is created successfully from an existing result and default template" {
val reportFileResults = createReporter().generateReport(ReporterInput(ORT_RESULT), tempdir())
val reportFileResults = PdfTemplateReporterFactory.create().generateReport(ReporterInput(ORT_RESULT), tempdir())

reportFileResults.shouldBeSingleton {
it shouldBeSuccess { reportFile ->
Expand All @@ -54,20 +52,20 @@ class PdfTemplateReporterFunTest : StringSpec({

"Report generation is aborted when path to non-existing PDF theme file is given" {
shouldThrow<IllegalArgumentException> {
createReporter(mapOf("pdf.theme.file" to "dummy.file"))
PdfTemplateReporterFactory.create(pdfThemeFile = "dummy.file")
.generateReport(ReporterInput(ORT_RESULT), tempdir())
}
}

"Report generation is aborted when a non-existent PDF fonts directory is given" {
shouldThrow<IllegalArgumentException> {
createReporter(mapOf("pdf.fonts.dir" to "fake.path"))
PdfTemplateReporterFactory.create(pdfFontsDir = "fake.path")
.generateReport(ReporterInput(ORT_RESULT), tempdir())
}
}

"Advisor reports are generated if the result contains an advisor section" {
val reportFileResults = createReporter().generateReport(
val reportFileResults = PdfTemplateReporterFactory.create().generateReport(
ReporterInput(ORT_RESULT_WITH_VULNERABILITIES),
tempdir()
)
Expand All @@ -76,6 +74,3 @@ class PdfTemplateReporterFunTest : StringSpec({
reportFileNames.shouldContainAll("AsciiDoc_vulnerability_report.pdf", "AsciiDoc_defect_report.pdf")
}
})

private fun createReporter(options: Options = emptyMap()) =
PdfTemplateReporterFactory().create(PluginConfig(options = options))
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,38 @@ import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe

import java.io.File

import org.cyclonedx.parsers.JsonParser
import org.cyclonedx.parsers.XmlParser

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.plugins.reporters.cyclonedx.CycloneDxReporter.Companion.REPORT_BASE_FILENAME
import org.ossreviewtoolkit.reporter.ORT_RESULT
import org.ossreviewtoolkit.reporter.ORT_RESULT_WITH_ILLEGAL_COPYRIGHTS
import org.ossreviewtoolkit.reporter.ORT_RESULT_WITH_VULNERABILITIES
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.common.Options
import org.ossreviewtoolkit.utils.common.normalizeLineBreaks
import org.ossreviewtoolkit.utils.test.getAssetAsString

class CycloneDxReporterFunTest : WordSpec({
val defaultSchemaVersion = DEFAULT_SCHEMA_VERSION.versionString
val outputDir = tempdir()

fun generateReport(result: OrtResult, options: Options): List<Result<File>> =
CycloneDxReporterFactory().create(PluginConfig(options)).generateReport(ReporterInput(result), outputDir)

"BOM generation with single option" should {
val optionSingle = mapOf("single.bom" to "true")

"create just one file" {
val jsonOptions = optionSingle + mapOf("output.file.formats" to "json")

val bomFileResults = generateReport(ORT_RESULT, jsonOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("json")
).generateReport(ReporterInput(ORT_RESULT), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess outputDir.resolve("$REPORT_BASE_FILENAME.json")
}
}

"be valid XML according to schema version $defaultSchemaVersion" {
val xmlOptions = optionSingle + mapOf("output.file.formats" to "xml")

val bomFileResults = generateReport(ORT_RESULT, xmlOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("xml")
).generateReport(ReporterInput(ORT_RESULT), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess { bomFile ->
Expand All @@ -85,9 +77,11 @@ class CycloneDxReporterFunTest : WordSpec({

"create the expected XML file" {
val expectedBom = getAssetAsString("cyclonedx-reporter-expected-result.xml")
val xmlOptions = optionSingle + mapOf("output.file.formats" to "xml")

val bomFileResults = generateReport(ORT_RESULT_WITH_VULNERABILITIES, xmlOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("xml")
).generateReport(ReporterInput(ORT_RESULT_WITH_VULNERABILITIES), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess { bomFile ->
Expand All @@ -101,9 +95,10 @@ class CycloneDxReporterFunTest : WordSpec({
}

"the expected XML file even if some copyrights contain non printable characters" {
val xmlOptions = optionSingle + mapOf("output.file.formats" to "xml")

val bomFileResults = generateReport(ORT_RESULT_WITH_ILLEGAL_COPYRIGHTS, xmlOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("xml")
).generateReport(ReporterInput(ORT_RESULT_WITH_ILLEGAL_COPYRIGHTS), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess { bomFile ->
Expand All @@ -114,9 +109,10 @@ class CycloneDxReporterFunTest : WordSpec({
}

"be valid JSON according to schema version $defaultSchemaVersion" {
val jsonOptions = optionSingle + mapOf("output.file.formats" to "json")

val bomFileResults = generateReport(ORT_RESULT_WITH_VULNERABILITIES, jsonOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("json")
).generateReport(ReporterInput(ORT_RESULT_WITH_VULNERABILITIES), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess { bomFile ->
Expand All @@ -129,9 +125,11 @@ class CycloneDxReporterFunTest : WordSpec({

"create the expected JSON file" {
val expectedBom = getAssetAsString("cyclonedx-reporter-expected-result.json")
val jsonOptions = optionSingle + mapOf("output.file.formats" to "json")

val bomFileResults = generateReport(ORT_RESULT_WITH_VULNERABILITIES, jsonOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = true,
outputFileFormats = listOf("json")
).generateReport(ReporterInput(ORT_RESULT_WITH_VULNERABILITIES), outputDir)

bomFileResults.shouldBeSingleton {
it shouldBeSuccess { bomFile ->
Expand All @@ -146,22 +144,22 @@ class CycloneDxReporterFunTest : WordSpec({
}

"BOM generation with multi option" should {
val optionMulti = mapOf("single.bom" to "false")

"create one file per project" {
val jsonOptions = optionMulti + mapOf("output.file.formats" to "json")

val bomFileResults = generateReport(ORT_RESULT_WITH_VULNERABILITIES, jsonOptions)
val bomFileResults = CycloneDxReporterFactory.create(
singleBom = false,
outputFileFormats = listOf("json")
).generateReport(ReporterInput(ORT_RESULT_WITH_VULNERABILITIES), outputDir)

bomFileResults shouldHaveSize 2
bomFileResults.forAll { it.shouldBeSuccess() }
}

"generate valid XML files according to schema version $defaultSchemaVersion" {
val xmlOptions = optionMulti + mapOf("output.file.formats" to "xml")

val (bomFileResultWithFindings, bomFileResultWithoutFindings) =
generateReport(ORT_RESULT_WITH_VULNERABILITIES, xmlOptions).also {
CycloneDxReporterFactory.create(
singleBom = false,
outputFileFormats = listOf("xml")
).generateReport(ReporterInput(ORT_RESULT_WITH_VULNERABILITIES), outputDir).also {
it shouldHaveSize 2
}

Expand All @@ -179,10 +177,11 @@ class CycloneDxReporterFunTest : WordSpec({
}

"generate valid JSON files according to schema version $defaultSchemaVersion" {
val jsonOptions = optionMulti + mapOf("output.file.formats" to "json")

val (bomFileResultWithFindings, bomFileResultWithoutFindings) =
generateReport(ORT_RESULT_WITH_VULNERABILITIES, jsonOptions).also {
CycloneDxReporterFactory.create(
singleBom = false,
outputFileFormats = listOf("json")
).generateReport(ReporterInput(ORT_RESULT_WITH_VULNERABILITIES), outputDir).also {
it shouldHaveSize 2
}

Expand All @@ -200,10 +199,11 @@ class CycloneDxReporterFunTest : WordSpec({
}

"generate expected JSON files" {
val jsonOptions = optionMulti + mapOf("output.file.formats" to "json")

val (bomFileResultWithFindings, bomFileResultWithoutFindings) =
generateReport(ORT_RESULT, jsonOptions).also {
CycloneDxReporterFactory.create(
singleBom = false,
outputFileFormats = listOf("json")
).generateReport(ReporterInput(ORT_RESULT), outputDir).also {
it shouldHaveSize 2
}

Expand Down
Loading

0 comments on commit 1f76243

Please sign in to comment.