Skip to content

Commit

Permalink
test(model): Factor out fromJson()
Browse files Browse the repository at this point in the history
Add an analog function to `fromYaml()` for consistency, and to reduce
repetition.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Jan 17, 2025
1 parent 7d817f8 commit bc4d972
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 2 additions & 0 deletions model/src/main/kotlin/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ inline fun <reified T> String.fromYaml(): T = yamlMapper.readValue(this)

fun Any?.toYaml(): String = yamlMapper.writeValueAsString(this)

inline fun <reified T> String.fromJson(): T = jsonMapper.readValue(this)

fun Any?.toJson(prettyPrint: Boolean = true): String {
val writer = if (prettyPrint) {
jsonMapper.writerWithDefaultPrettyPrinter()
Expand Down
18 changes: 8 additions & 10 deletions model/src/test/kotlin/ProvenanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -36,11 +34,11 @@ class ProvenanceTest : WordSpec({
}

"be deserializable as Provenance" {
jsonMapper.readValue<Provenance>(json) shouldBe UnknownProvenance
json.fromJson<Provenance>() shouldBe UnknownProvenance
}

"be deserializable as UnknownProvenance" {
jsonMapper.readValue<UnknownProvenance>(json) shouldBe UnknownProvenance
json.fromJson<UnknownProvenance>() shouldBe UnknownProvenance
}
}

Expand Down Expand Up @@ -69,15 +67,15 @@ class ProvenanceTest : WordSpec({
}

"be deserializable as Provenance" {
jsonMapper.readValue<Provenance>(json) shouldBe provenance
json.fromJson<Provenance>() shouldBe provenance
}

"be deserializable as KnownProvenance" {
jsonMapper.readValue<KnownProvenance>(json) shouldBe provenance
json.fromJson<KnownProvenance>() shouldBe provenance
}

"be deserializable as ArtifactProvenance" {
jsonMapper.readValue<ArtifactProvenance>(json) shouldBe provenance
json.fromJson<ArtifactProvenance>() shouldBe provenance
}
}

Expand Down Expand Up @@ -109,15 +107,15 @@ class ProvenanceTest : WordSpec({
}

"be serializable and deserializable as Provenance" {
jsonMapper.readValue<Provenance>(json) shouldBe provenance
json.fromJson<Provenance>() shouldBe provenance
}

"be serializable and deserializable as KnownProvenance" {
jsonMapper.readValue<KnownProvenance>(json) shouldBe provenance
json.fromJson<UnknownProvenance>() shouldBe provenance
}

"be serializable and deserializable as ArtifactProvenance" {
jsonMapper.readValue<RepositoryProvenance>(json) shouldBe provenance
json.fromJson<RepositoryProvenance>() shouldBe provenance
}
}
})

0 comments on commit bc4d972

Please sign in to comment.