diff --git a/src/test/kotlin/eventDemo/Helpers.kt b/src/test/kotlin/eventDemo/Helpers.kt index 391b268..15a725d 100644 --- a/src/test/kotlin/eventDemo/Helpers.kt +++ b/src/test/kotlin/eventDemo/Helpers.kt @@ -5,7 +5,6 @@ import eventDemo.business.entity.Deck import eventDemo.configuration.business.configureGameListener import eventDemo.configuration.injection.appKoinModule import eventDemo.configuration.ktor.configuration -import io.kotest.engine.runBlocking import io.ktor.server.config.ApplicationConfig import io.ktor.server.testing.ApplicationTestBuilder import io.ktor.server.testing.testApplication @@ -13,6 +12,7 @@ import io.ktor.utils.io.KtorDsl import org.koin.core.Koin import org.koin.core.module.KoinApplicationDslMarker import org.koin.dsl.koinApplication +import org.koin.ktor.ext.getKoin import redis.clients.jedis.UnifiedJedis import javax.sql.DataSource @@ -24,22 +24,31 @@ fun Deck.allCards(): Set = @KoinApplicationDslMarker suspend fun testKoinApplicationWithConfig(block: suspend Koin.() -> T): T = - koinApplication { modules(appKoinModule(ApplicationConfig("application.conf").configuration())) }.koin.block() + koinApplication { modules(appKoinModule(ApplicationConfig("application.conf").configuration())) } + .koin + .apply { + cleanDataTest() + configureGameListener() + }.block() @KtorDsl -suspend fun testApplicationWithConfig(block: suspend ApplicationTestBuilder.(koin: Koin) -> Unit) { +fun testApplicationWithConfig( + configBuilder: Koin.() -> Unit = {}, + block: suspend ApplicationTestBuilder.() -> Unit, +) { testApplication { val conf = ApplicationConfig("application.conf") environment { config = conf } - val koin = koinApplication { modules(appKoinModule(conf.configuration())) }.koin - koin.cleanDataTest() - koin.configureGameListener() - runBlocking { - block(koin) + application { + val koin = getKoin() + koin.cleanDataTest() + koin.configureGameListener() + configBuilder(koin) } + block() } } diff --git a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameListRouteTest.kt b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameListRouteTest.kt index 40fc564..88568ff 100644 --- a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameListRouteTest.kt +++ b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameListRouteTest.kt @@ -44,15 +44,18 @@ class GameListRouteTest : } test("/games return a game with status OPENING") { - testApplicationWithConfig { koin -> - val gameId = GameId() - val player1 = Player(name = "Nikola") - - val eventHandler = koin.get() - runBlocking { - eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) } - } - + val gameId = GameId() + val player1 = Player(name = "Nikola") + testApplicationWithConfig( + { + runBlocking { + get() + .handle(gameId) { + NewPlayerEvent(gameId, player1, it) + } + } + }, + ) { // Wait until the projection is created eventually(3.seconds) { httpClient() @@ -73,11 +76,11 @@ class GameListRouteTest : } test("/games return a game with status IS_STARTED") { - testApplicationWithConfig { koin -> - val gameId = GameId() - val player1 = Player(name = "Nikola") - val player2 = Player(name = "Einstein") - val eventHandler = koin.get() + val gameId = GameId() + val player1 = Player(name = "Nikola") + val player2 = Player(name = "Einstein") + testApplicationWithConfig({ + val eventHandler = get() runBlocking { eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) } eventHandler.handle(gameId) { NewPlayerEvent(gameId, player2, it) } @@ -92,7 +95,7 @@ class GameListRouteTest : ) } } - + }) { httpClient() .get("/games") { withAuth(player1) diff --git a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt index ef60c77..09d8523 100644 --- a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt +++ b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt @@ -21,7 +21,6 @@ import eventDemo.business.notification.PlayerAsPlayACardNotification import eventDemo.business.notification.PlayerWasReadyNotification import eventDemo.business.notification.TheGameWasStartedNotification import eventDemo.business.notification.WelcomeToTheGameNotification -import eventDemo.configuration.business.configureGameListener import eventDemo.libs.event.projection.ProjectionSnapshotRepositoryInMemory import eventDemo.testKoinApplicationWithConfig import io.kotest.assertions.nondeterministic.until @@ -177,7 +176,6 @@ class GameSimulationTest : val commandHandler by inject() val eventStore by inject() val playerNotificationListener by inject() - configureGameListener() playerNotificationListener.startListening(player1, gameId) { channelNotification1.trySendBlocking(it) } playerNotificationListener.startListening(player2, gameId) { channelNotification2.trySendBlocking(it) } diff --git a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameStateRouteTest.kt b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameStateRouteTest.kt index a93e438..6323239 100644 --- a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameStateRouteTest.kt +++ b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameStateRouteTest.kt @@ -48,14 +48,13 @@ class GameStateRouteTest : } test("/games/{id}/card/last") { - testApplicationWithConfig { koin -> - val gameId = GameId() - val player1 = Player(name = "Nikola") - val player2 = Player(name = "Einstein") - var lastPlayedCard: Card? = null - - val eventHandler = koin.get() - val stateRepo = koin.get() + val gameId = GameId() + val player1 = Player(name = "Nikola") + val player2 = Player(name = "Einstein") + var lastPlayedCard: Card? = null + testApplicationWithConfig({ + val eventHandler = get() + val stateRepo = get() runBlocking { eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) } @@ -89,7 +88,7 @@ class GameStateRouteTest : } delay(100) } - + }) { httpClient() .get("/games/$gameId/card/last") { withAuth(player1) diff --git a/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt b/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt index b67a362..c27141e 100644 --- a/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt +++ b/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt @@ -8,7 +8,6 @@ import eventDemo.business.event.projection.projectionListener.PlayerNotification import eventDemo.business.notification.CommandSuccessNotification import eventDemo.business.notification.Notification import eventDemo.business.notification.WelcomeToTheGameNotification -import eventDemo.configuration.business.configureGameListener import eventDemo.testKoinApplicationWithConfig import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldContain @@ -26,11 +25,10 @@ import kotlin.time.Duration.Companion.seconds class GameCommandHandlerTest : FunSpec({ test("handle a command should execute the command") { - withTimeout(1.seconds) { + withTimeout(5.seconds) { testKoinApplicationWithConfig { - configureGameListener() - val commandHandler by inject() - val notificationListener by inject() + val commandHandler = get() + val notificationListener = get() val gameId = GameId() val player = Player("Tesla") val channelCommand = Channel(Channel.BUFFERED) diff --git a/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt b/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt index 90ca6de..938dc8d 100644 --- a/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt +++ b/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt @@ -6,7 +6,7 @@ import eventDemo.business.event.GameEventHandler import eventDemo.business.event.event.NewPlayerEvent import eventDemo.business.event.projection.gameState.GameState import eventDemo.business.event.projection.gameState.GameStateRepository -import eventDemo.testApplicationWithConfig +import eventDemo.testKoinApplicationWithConfig import io.kotest.assertions.nondeterministic.eventually import io.kotest.assertions.nondeterministic.eventuallyConfig import io.kotest.core.spec.style.FunSpec @@ -29,9 +29,9 @@ class GameStateRepositoryTest : test("GameStateRepository should build the projection when a new event occurs") { val aggregateId = GameId() - testApplicationWithConfig { koin -> - val repo = koin.get() - val eventHandler = koin.get() + testKoinApplicationWithConfig { + val repo = get() + val eventHandler = get() eventHandler .handle(aggregateId) { NewPlayerEvent(aggregateId = aggregateId, player = player1, version = it) } .also { event -> @@ -50,10 +50,10 @@ class GameStateRepositoryTest : test("get should build the last version of the state") { val aggregateId = GameId() - testApplicationWithConfig { koin -> - val repo = koin.get() - val eventHandler = koin.get() - val projectionBus = koin.get() + testKoinApplicationWithConfig { + val repo = get() + val eventHandler = get() + val projectionBus = get() var state: GameState? = null projectionBus.subscribe { @@ -86,9 +86,9 @@ class GameStateRepositoryTest : test("getUntil should build the state until the event") { repeat(10) { val aggregateId = GameId() - testApplicationWithConfig { koin -> - val repo = koin.get() - val eventHandler = koin.get() + testKoinApplicationWithConfig { + val repo = get() + val eventHandler = get() val event1 = eventHandler @@ -115,9 +115,9 @@ class GameStateRepositoryTest : test("getUntil should be concurrently secure") { val aggregateId = GameId() - testApplicationWithConfig { koin -> - val repo = koin.get() - val eventHandler = koin.get() + testKoinApplicationWithConfig { + val repo = get() + val eventHandler = get() (1..10) .map { r -> diff --git a/src/test/kotlin/eventDemo/libs/event/EventStreamTest.kt b/src/test/kotlin/eventDemo/libs/event/EventStreamTest.kt index 844c76f..2cbccfb 100644 --- a/src/test/kotlin/eventDemo/libs/event/EventStreamTest.kt +++ b/src/test/kotlin/eventDemo/libs/event/EventStreamTest.kt @@ -1,6 +1,5 @@ package eventDemo.libs.event -import eventDemo.cleanEventSource import eventDemo.testKoinApplicationWithConfig import io.kotest.core.spec.style.FunSpec import io.kotest.datatest.withData @@ -13,7 +12,6 @@ import kotlinx.coroutines.launch import kotlinx.serialization.json.Json import org.junit.jupiter.api.assertNull import org.junit.jupiter.api.assertThrows -import javax.sql.DataSource import kotlin.test.assertNotNull @DelicateCoroutinesApi @@ -29,7 +27,6 @@ class EventStreamTest : suspend fun eventStreams(): List> = testKoinApplicationWithConfig { - get().cleanEventSource() listOf( EventStreamInMemory(IdTest()), EventStreamInPostgresql(