This commit is contained in:
2024-05-30 21:41:02 +02:00
parent 03ba14d918
commit ae5c229e4b
32 changed files with 537 additions and 344 deletions

View File

@@ -1,11 +1,10 @@
package eventDemo.app.actions
import eventDemo.app.Card
import eventDemo.app.EventStream
import eventDemo.app.GameId
import eventDemo.app.PlayCardEvent
import eventDemo.app.read
import eventDemo.module
import eventDemo.shared.GameId
import eventDemo.shared.entity.Card
import eventDemo.shared.event.CardIsPlayedEvent
import eventDemo.shared.event.GameEventStream
import io.kotest.core.spec.style.FunSpec
import io.ktor.client.call.body
import io.ktor.client.request.accept
@@ -30,8 +29,9 @@ class CardTest :
stopKoin()
module()
}
val id = GameId()
val card: Card = Card.Simple(1, Card.Color.Blue)
val card: Card = Card.NumericCard(1, Card.Color.Blue)
httpClient()
.post("/game/$id/card") {
contentType(Json)
@@ -40,8 +40,8 @@ class CardTest :
}.apply {
assertEquals(HttpStatusCode.OK, status, message = bodyAsText())
val eventStream = getKoin().get<EventStream<GameId>>()
assertEquals(PlayCardEvent(id, card), eventStream.read<PlayCardEvent, GameId>(id))
val eventStream = getKoin().get<GameEventStream>()
assertEquals(CardIsPlayedEvent(id, card), eventStream.readLast(id))
}
}
}
@@ -49,20 +49,22 @@ class CardTest :
test("/game/{id}/card/last") {
testApplication {
val id = GameId()
val card: Card = Card.Simple(1, Card.Color.Blue)
val card: Card = Card.NumericCard(1, Card.Color.Blue)
application {
stopKoin()
module()
val eventStream by inject<EventStream<GameId>>()
val eventStream by inject<GameEventStream>()
eventStream.publish(
PlayCardEvent(GameId(), Card.Simple(2, Card.Color.Yellow)),
PlayCardEvent(id, card),
CardIsPlayedEvent(id, Card.NumericCard(2, Card.Color.Yellow)),
CardIsPlayedEvent(id, card),
// Other game
CardIsPlayedEvent(GameId(), Card.NumericCard(2, Card.Color.Yellow)),
)
}
httpClient().get("/game/$id/card/last").apply {
assertEquals(HttpStatusCode.OK, status, message = bodyAsText())
assertEquals(card, this.call.body<Card>())
assertEquals(card, call.body<Card>())
}
}
}

View File

@@ -1,70 +0,0 @@
package eventDemo.app.actions
import eventDemo.app.Card
import eventDemo.app.Command
import eventDemo.app.CommandStream
import eventDemo.app.Game
import eventDemo.app.PlayCardCommand
import eventDemo.module
import io.kotest.core.spec.style.FunSpec
import io.ktor.client.call.body
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType.Application.Json
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import io.ktor.server.testing.testApplication
import org.koin.core.context.stopKoin
import org.koin.java.KoinJavaComponent.getKoin
import org.koin.ktor.ext.inject
import kotlin.test.assertEquals
class CommandTest :
FunSpec({
test("/command/send") {
testApplication {
val client = httpClient()
application {
stopKoin()
module()
}
val command = PlayCardCommand(Game.new(), Card.Simple(1, Card.Color.Blue))
client
.post("/command/send") {
contentType(Json)
accept(Json)
setBody(command)
}.apply {
assertEquals(HttpStatusCode.OK, status, message = bodyAsText())
val commandStream = getKoin().get<CommandStream>()
assertEquals(command, commandStream.readNext())
}
}
}
test("/command/next") {
testApplication {
val command =
PlayCardCommand(
Game.new(),
Card.Simple(1, Card.Color.Blue),
)
application {
stopKoin()
module()
val commandStream by inject<CommandStream>()
commandStream.sendRequest(command)
}
httpClient().get("/command/next").apply {
assertEquals(HttpStatusCode.OK, status, message = bodyAsText())
assertEquals(command, this.call.body<Command>())
}
}
}
})