From 325fa79445db8b156449f4442215a8e2dc4aea7d Mon Sep 17 00:00:00 2001 From: Nils Martin Sande Date: Wed, 9 Oct 2024 12:59:23 +0200 Subject: [PATCH] La til funksjoner for bedre asserts mot test topics --- apps/bekreftelse-tjeneste/build.gradle.kts | 1 + .../BekreftelseStreamTest.kt | 17 ++------------ settings.gradle.kts | 1 + .../build.gradle.kts | 8 +++++++ .../nav/paw/test/KafkaStreamsTestExtension.kt | 22 +++++++++++++++++++ 5 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 test/kafka-streams-test-functions/build.gradle.kts create mode 100644 test/kafka-streams-test-functions/src/main/kotlin/no/nav/paw/test/KafkaStreamsTestExtension.kt diff --git a/apps/bekreftelse-tjeneste/build.gradle.kts b/apps/bekreftelse-tjeneste/build.gradle.kts index 3b7a788a..46209f64 100644 --- a/apps/bekreftelse-tjeneste/build.gradle.kts +++ b/apps/bekreftelse-tjeneste/build.gradle.kts @@ -51,6 +51,7 @@ dependencies { testImplementation(libs.kafka.streams.test) testImplementation(libs.bundles.testLibsWithUnitTesting) testImplementation(project(":test:test-data-lib")) + testImplementation(project(":test:kafka-streams-test-functions")) } java { diff --git a/apps/bekreftelse-tjeneste/src/test/kotlin/no/nav/paw/bekreftelsetjeneste/BekreftelseStreamTest.kt b/apps/bekreftelse-tjeneste/src/test/kotlin/no/nav/paw/bekreftelsetjeneste/BekreftelseStreamTest.kt index ef6c022e..dc3956b6 100644 --- a/apps/bekreftelse-tjeneste/src/test/kotlin/no/nav/paw/bekreftelsetjeneste/BekreftelseStreamTest.kt +++ b/apps/bekreftelse-tjeneste/src/test/kotlin/no/nav/paw/bekreftelsetjeneste/BekreftelseStreamTest.kt @@ -13,26 +13,13 @@ import no.nav.paw.arbeidssoekerregisteret.testdata.mainavro.metadata import no.nav.paw.arbeidssoekerregisteret.testdata.mainavro.periode import no.nav.paw.bekreftelse.internehendelser.* import no.nav.paw.bekreftelsetjeneste.tilstand.* +import no.nav.paw.test.assertEvent +import no.nav.paw.test.seconds import org.apache.kafka.streams.TestOutputTopic import java.time.Duration import java.time.Instant import java.util.* -inline fun TestOutputTopic.assertEvent(function: (T) -> A): A = - assertEvent().let(function) - -inline fun TestOutputTopic.assertEvent(): T { - withClue("Expected event of type ${T::class.simpleName}, no event was found") { - isEmpty shouldBe false - } - withClue("Expected event of type ${T::class.simpleName}") { - val event = readValue() - return event.shouldBeInstanceOf() - } -} - -val Int.seconds: Duration get() = Duration.ofSeconds(this.toLong()) - class BekreftelseStreamTest : FreeSpec({ val identitetsnummer = "12345678901" diff --git a/settings.gradle.kts b/settings.gradle.kts index 612c1a49..af28988e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -15,6 +15,7 @@ include( "lib:kafka-streams", "lib:kafka-key-generator-client", "test:test-data-lib", + "test:kafka-streams-test-functions", "domain:bekreftelse-interne-hendelser", "domain:bekreftelsesansvar-avro-schema", "domain:bekreftelsesmelding-avro-schema", diff --git a/test/kafka-streams-test-functions/build.gradle.kts b/test/kafka-streams-test-functions/build.gradle.kts new file mode 100644 index 00000000..44011da2 --- /dev/null +++ b/test/kafka-streams-test-functions/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + kotlin("jvm") +} + +dependencies { + implementation(libs.kafka.streams.test) + implementation(libs.test.kotest.assertionsCore) +} diff --git a/test/kafka-streams-test-functions/src/main/kotlin/no/nav/paw/test/KafkaStreamsTestExtension.kt b/test/kafka-streams-test-functions/src/main/kotlin/no/nav/paw/test/KafkaStreamsTestExtension.kt new file mode 100644 index 00000000..c60515c2 --- /dev/null +++ b/test/kafka-streams-test-functions/src/main/kotlin/no/nav/paw/test/KafkaStreamsTestExtension.kt @@ -0,0 +1,22 @@ +package no.nav.paw.test + +import io.kotest.assertions.withClue +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeInstanceOf +import org.apache.kafka.streams.TestOutputTopic +import java.time.Duration + +inline fun TestOutputTopic<*, in T>.assertEvent(function: (T) -> A): A = + assertEvent().let(function) + +inline fun TestOutputTopic<*, in T>.assertEvent(): T { + withClue("Expected event of type ${T::class.simpleName}, no event was found") { + isEmpty shouldBe false + } + val event = readValue() + return withClue("Expected event of type ${T::class}, found ${event?.let { it::class }}") { + event.shouldBeInstanceOf() + } +} + +val Int.seconds: Duration get() = Duration.ofSeconds(this.toLong()) \ No newline at end of file