Skip to content

Commit c233f15

Browse files
committed
test(aosd): Factor out JSON schema matching
Remove code redundancy and prepare for re-use in an upcoming change. Note: The analog function from kotest lacks support for a couple of features, such as enums, see [1]. The new matcher function can be used (temporarily) to make assertions stronger. [1]: https://github.com/kotest/kotest/blob/1c809c77390389478db10af0d4c73e774520664f/documentation/docs/assertions/json/schema.md Signed-off-by: Frank Viernau <[email protected]>
1 parent 3e69705 commit c233f15

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

plugins/reporters/aosd/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,5 @@ dependencies {
3939

4040
funTestImplementation(testFixtures(projects.reporter))
4141

42-
funTestImplementation(libs.jsonSchemaValidator)
4342
funTestImplementation(libs.kotest.assertions.json)
4443
}

plugins/reporters/aosd/src/funTest/kotlin/Aosd20ReporterFunTest.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,28 @@
1919

2020
package org.ossreviewtoolkit.plugins.reporters.aosd
2121

22-
import com.networknt.schema.InputFormat
23-
import com.networknt.schema.JsonSchemaFactory
24-
import com.networknt.schema.SpecVersion
25-
2622
import io.kotest.assertions.assertSoftly
2723
import io.kotest.assertions.json.shouldEqualJson
2824
import io.kotest.core.spec.style.WordSpec
2925
import io.kotest.engine.spec.tempdir
3026
import io.kotest.inspectors.forAll
31-
import io.kotest.matchers.collections.beEmpty
3227
import io.kotest.matchers.collections.shouldHaveSize
3328
import io.kotest.matchers.result.shouldBeSuccess
3429
import io.kotest.matchers.should
3530

3631
import org.ossreviewtoolkit.reporter.ORT_RESULT
3732
import org.ossreviewtoolkit.reporter.ReporterInput
3833
import org.ossreviewtoolkit.utils.test.getResource
34+
import org.ossreviewtoolkit.utils.test.matchJsonSchema
3935
import org.ossreviewtoolkit.utils.test.readResource
4036

4137
class Aosd20ReporterFunTest : WordSpec({
4238
"The example JSON report" should {
4339
"be valid according to the schema" {
44-
val schemaResource = getResource("/aosd20/aosd.schema.json")
45-
val schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaResource.toURI())
46-
40+
val schemaJson = readResource("/aosd20/aosd.schema.json")
4741
val example = readResource("/aosd20/aosd.example.json")
48-
val errors = schema.validate(example, InputFormat.JSON)
4942

50-
errors should beEmpty()
43+
example should matchJsonSchema(schemaJson)
5144
}
5245

5346
"deserialize correctly" {
@@ -65,13 +58,11 @@ class Aosd20ReporterFunTest : WordSpec({
6558
val reportFiles = Aosd20Reporter().generateReport(ReporterInput(ORT_RESULT), outputDir)
6659

6760
"be valid according to the schema" {
68-
val schemaResource = getResource("/aosd20/aosd.schema.json")
69-
val schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaResource.toURI())
61+
val schemaJson = readResource("/aosd20/aosd.schema.json")
7062

7163
reportFiles.forAll {
7264
it shouldBeSuccess { reportFile ->
73-
val errors = schema.validate(reportFile.readText(), InputFormat.JSON)
74-
errors should beEmpty()
65+
reportFile.readText() should matchJsonSchema(schemaJson)
7566
}
7667
}
7768
}

plugins/reporters/aosd/src/funTest/kotlin/Aosd21ReporterFunTest.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@
1919

2020
package org.ossreviewtoolkit.plugins.reporters.aosd
2121

22-
import com.networknt.schema.InputFormat
23-
import com.networknt.schema.JsonSchemaFactory
24-
import com.networknt.schema.SpecVersion
25-
2622
import io.kotest.assertions.assertSoftly
2723
import io.kotest.assertions.json.shouldEqualJson
2824
import io.kotest.core.spec.style.WordSpec
2925
import io.kotest.engine.spec.tempdir
3026
import io.kotest.inspectors.forAll
31-
import io.kotest.matchers.collections.beEmpty
3227
import io.kotest.matchers.collections.shouldHaveSize
3328
import io.kotest.matchers.result.shouldBeSuccess
3429
import io.kotest.matchers.should
@@ -37,18 +32,16 @@ import io.kotest.matchers.shouldBe
3732
import org.ossreviewtoolkit.reporter.ORT_RESULT
3833
import org.ossreviewtoolkit.reporter.ReporterInput
3934
import org.ossreviewtoolkit.utils.test.getResource
35+
import org.ossreviewtoolkit.utils.test.matchJsonSchema
4036
import org.ossreviewtoolkit.utils.test.readResource
4137

4238
class Aosd21ReporterFunTest : WordSpec({
4339
"The example JSON report" should {
4440
"be valid according to the schema" {
45-
val schemaResource = getResource("/aosd21/AOSD2.1_Importscheme_V2.1.0.json")
46-
val schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaResource.toURI())
47-
41+
val schemaJson = readResource("/aosd21/AOSD2.1_Importscheme_V2.1.0.json")
4842
val example = readResource("/aosd21/AOSD2.1_Example_Json_Import_File_V2.1.0.json")
49-
val errors = schema.validate(example, InputFormat.JSON)
5043

51-
errors should beEmpty()
44+
example should matchJsonSchema(schemaJson)
5245
}
5346

5447
"deserialize correctly" {
@@ -69,13 +62,11 @@ class Aosd21ReporterFunTest : WordSpec({
6962
val reportFiles = Aosd21Reporter().generateReport(ReporterInput(ORT_RESULT), outputDir)
7063

7164
"be valid according to the schema" {
72-
val schemaResource = getResource("/aosd21/AOSD2.1_Importscheme_V2.1.0.json")
73-
val schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaResource.toURI())
65+
val schemaJson = readResource("/aosd21/AOSD2.1_Importscheme_V2.1.0.json")
7466

7567
reportFiles.forAll {
7668
it shouldBeSuccess { reportFile ->
77-
val errors = schema.validate(reportFile.readText(), InputFormat.JSON)
78-
errors should beEmpty()
69+
reportFile.readText() should matchJsonSchema(schemaJson)
7970
}
8071
}
8172
}

utils/test/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737

3838
implementation(libs.diffUtils)
3939
implementation(libs.jackson.module.kotlin)
40+
implementation(libs.jsonSchemaValidator)
4041
implementation(libs.kotest.extensions.junitXml)
4142
implementation(libs.kotest.framework.engine)
4243
implementation(libs.postgresEmbedded)

utils/test/src/main/kotlin/Matchers.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ package org.ossreviewtoolkit.utils.test
2222
import com.github.difflib.DiffUtils
2323
import com.github.difflib.UnifiedDiffUtils
2424

25+
import com.networknt.schema.InputFormat
26+
import com.networknt.schema.JsonSchemaFactory
27+
import com.networknt.schema.SpecVersion
28+
2529
import io.kotest.matchers.Matcher
2630
import io.kotest.matchers.MatcherResult
2731
import io.kotest.matchers.collections.beEmpty
@@ -119,3 +123,15 @@ fun matchExpectedResult(
119123
)
120124
}
121125
}
126+
127+
fun matchJsonSchema(schemaJson: String): Matcher<String> =
128+
Matcher { actual ->
129+
val schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaJson)
130+
val violations = schema.validate(actual, InputFormat.JSON)
131+
132+
MatcherResult(
133+
violations.isEmpty(),
134+
{ violations.joinToString(separator = "\n") { "${it.evaluationPath} => ${it.message}" } },
135+
{ "Expected some violation against JSON schema, but everything matched" }
136+
)
137+
}

0 commit comments

Comments
 (0)