Skip to content

Commit

Permalink
La til funksjoner for bedre asserts mot test topics
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsmsa committed Oct 9, 2024
1 parent 86f51ea commit 325fa79
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions apps/bekreftelse-tjeneste/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reified T: BekreftelseHendelse, A> TestOutputTopic<Long, BekreftelseHendelse>.assertEvent(function: (T) -> A): A =
assertEvent<T>().let(function)

inline fun <reified T: BekreftelseHendelse> TestOutputTopic<Long, BekreftelseHendelse>.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<T>()
}
}

val Int.seconds: Duration get() = Duration.ofSeconds(this.toLong())

class BekreftelseStreamTest : FreeSpec({

val identitetsnummer = "12345678901"
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions test/kafka-streams-test-functions/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
kotlin("jvm")
}

dependencies {
implementation(libs.kafka.streams.test)
implementation(libs.test.kotest.assertionsCore)
}
Original file line number Diff line number Diff line change
@@ -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 <reified T : Any, A> TestOutputTopic<*, in T>.assertEvent(function: (T) -> A): A =
assertEvent<T>().let(function)

inline fun <reified T : Any> 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<T>()
}
}

val Int.seconds: Duration get() = Duration.ofSeconds(this.toLong())

0 comments on commit 325fa79

Please sign in to comment.