From bc4d972f94e694922be2b46671bbd49c44b0307f Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Fri, 17 Jan 2025 15:10:37 +0100 Subject: [PATCH] test(model): Factor out `fromJson()` Add an analog function to `fromYaml()` for consistency, and to reduce repetition. Signed-off-by: Frank Viernau --- model/src/main/kotlin/Mappers.kt | 2 ++ model/src/test/kotlin/ProvenanceTest.kt | 18 ++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/model/src/main/kotlin/Mappers.kt b/model/src/main/kotlin/Mappers.kt index 70ff73d73c407..f18dc2dac9763 100644 --- a/model/src/main/kotlin/Mappers.kt +++ b/model/src/main/kotlin/Mappers.kt @@ -72,6 +72,8 @@ inline fun String.fromYaml(): T = yamlMapper.readValue(this) fun Any?.toYaml(): String = yamlMapper.writeValueAsString(this) +inline fun String.fromJson(): T = jsonMapper.readValue(this) + fun Any?.toJson(prettyPrint: Boolean = true): String { val writer = if (prettyPrint) { jsonMapper.writerWithDefaultPrettyPrinter() diff --git a/model/src/test/kotlin/ProvenanceTest.kt b/model/src/test/kotlin/ProvenanceTest.kt index 28dd5a702c1ea..7a69aa2c64923 100644 --- a/model/src/test/kotlin/ProvenanceTest.kt +++ b/model/src/test/kotlin/ProvenanceTest.kt @@ -19,8 +19,6 @@ package org.ossreviewtoolkit.model -import com.fasterxml.jackson.module.kotlin.readValue - import io.kotest.core.spec.style.WordSpec import io.kotest.matchers.shouldBe @@ -36,11 +34,11 @@ class ProvenanceTest : WordSpec({ } "be deserializable as Provenance" { - jsonMapper.readValue(json) shouldBe UnknownProvenance + json.fromJson() shouldBe UnknownProvenance } "be deserializable as UnknownProvenance" { - jsonMapper.readValue(json) shouldBe UnknownProvenance + json.fromJson() shouldBe UnknownProvenance } } @@ -69,15 +67,15 @@ class ProvenanceTest : WordSpec({ } "be deserializable as Provenance" { - jsonMapper.readValue(json) shouldBe provenance + json.fromJson() shouldBe provenance } "be deserializable as KnownProvenance" { - jsonMapper.readValue(json) shouldBe provenance + json.fromJson() shouldBe provenance } "be deserializable as ArtifactProvenance" { - jsonMapper.readValue(json) shouldBe provenance + json.fromJson() shouldBe provenance } } @@ -109,15 +107,15 @@ class ProvenanceTest : WordSpec({ } "be serializable and deserializable as Provenance" { - jsonMapper.readValue(json) shouldBe provenance + json.fromJson() shouldBe provenance } "be serializable and deserializable as KnownProvenance" { - jsonMapper.readValue(json) shouldBe provenance + json.fromJson() shouldBe provenance } "be serializable and deserializable as ArtifactProvenance" { - jsonMapper.readValue(json) shouldBe provenance + json.fromJson() shouldBe provenance } } })