2 Commits
Author SHA1 Message Date
flecomte 9c9d057f0a refactoring: Masive refactor to build the V2
Tests / lint (push) Has been skipped
Tests / test (push) Has been skipped
Tests / build (push) Failing after 14m50s
2026-07-28 23:46:52 +02:00
flecomte f3b848ea93 refactoring: Masive refactor to build the V2 2026-07-26 17:51:58 +02:00
63 changed files with 873 additions and 1888 deletions
-26
View File
@@ -1,26 +0,0 @@
# Version control
.git
.github
# Gradle build outputs / caches (must always be rebuilt fresh inside the image)
.gradle
build/
.kotlin
!gradle/wrapper/gradle-wrapper.jar
# IDE
.idea
.vscode
.run
*.iml
*.iws
*.ipr
# Docker-only local files (secrets/env must never be baked into the image)
docker/.env
docker/*.env.docker
docker/*.secret
# Misc
*.hprof
.gradle-docker-cache/
-7
View File
@@ -1,7 +0,0 @@
* text=auto
* eol=lf
*.sh text eol=lf
*.png binary
*.jar binary
gradlew.bat eol=crlf
gradlew text eol=lf
+30 -42
View File
@@ -18,10 +18,10 @@ jobs:
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v6 uses: actions/checkout@v4
- name: Set up JDK 21 - name: Set up JDK 21
uses: actions/setup-java@v5 uses: actions/setup-java@v4
with: with:
distribution: 'temurin' distribution: 'temurin'
java-version: '21' java-version: '21'
@@ -31,7 +31,7 @@ jobs:
run: echo "key=gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}" >> $GITHUB_OUTPUT run: echo "key=gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}" >> $GITHUB_OUTPUT
- name: Cache Gradle dependencies - name: Cache Gradle dependencies
uses: actions/cache@v6 uses: actions/cache@v3
with: with:
path: | path: |
~/.gradle/caches ~/.gradle/caches
@@ -48,16 +48,16 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v6 uses: actions/checkout@v4
- name: Set up JDK 21 - name: Set up JDK 21
uses: actions/setup-java@v5 uses: actions/setup-java@v4
with: with:
distribution: 'temurin' distribution: 'temurin'
java-version: '21' java-version: '21'
- name: Restore Gradle cache - name: Restore Gradle cache
uses: actions/cache@v6 uses: actions/cache@v3
with: with:
path: | path: |
~/.gradle/caches ~/.gradle/caches
@@ -73,65 +73,53 @@ jobs:
run: ./gradlew ktlintCheck run: ./gradlew ktlintCheck
- name: Publish ktlint report - name: Publish ktlint report
uses: yutailang0119/action-ktlint@v5 uses: yutailang0119/action-ktlint@v4
if: always() if: always()
with: with:
report-path: build/reports/ktlint/**/*.xml report-path: build/reports/ktlint/**/*.xml
continue-on-error: false continue-on-error: false
test: test:
needs: build
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
GRADLE_CACHE_DIR: ${{ github.workspace }}/.gradle-docker-cache
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v6 uses: actions/checkout@v4
- name: Install a pinned Docker Compose version - name: Set up JDK 21
run: | uses: actions/setup-java@v4
mkdir -p ~/.docker/cli-plugins
curl -fSL https://github.com/docker/compose/releases/download/v5.1.4/docker-compose-linux-x86_64 \
-o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
docker compose version
- name: Prepare docker secrets
run: |
[ -f docker/postgresql.secret ] || echo -n "changeit" > docker/postgresql.secret
- name: Generate cache key
id: cache-key-generator
run: echo "key=gradle-docker-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}" >> $GITHUB_OUTPUT
- name: Restore Gradle cache (Docker)
uses: actions/cache@v6
with: with:
path: ${{ env.GRADLE_CACHE_DIR }} distribution: 'temurin'
key: ${{ steps.cache-key-generator.outputs.key }} java-version: '21'
- name: Restore Gradle cache
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ needs.build.outputs.cache-key }}
restore-keys: | restore-keys: |
gradle-docker-${{ runner.os }}- gradle-${{ runner.os }}-
- name: Prepare cache directory permissions - name: Grant execute permission to Gradle wrapper
run: | run: chmod +x gradlew
mkdir -p "$GRADLE_CACHE_DIR"
chmod -R 777 "$GRADLE_CACHE_DIR"
- name: Run tests in Docker - name: Start CI Docker Compose services
run: docker compose -f docker/docker-compose-test.yaml run tests run: ./gradlew composeUp -Pci
- name: Shut down Docker services - name: Run tests
if: always() run: ./gradlew test -x composeUp --no-daemon
run: docker compose -f docker/docker-compose-test.yaml down -v
- name: Upload test reports - name: Upload test reports
if: always() if: always()
uses: actions/upload-artifact@v7 uses: actions/upload-artifact@v4
with: with:
name: test-results name: test-results
path: build/reports/tests/test path: build/reports/tests/test
- name: Publish Test Report - name: Publish Test Report
uses: dorny/test-reporter@v3 uses: dorny/test-reporter@v1
if: always() if: always()
with: with:
name: JUnit Tests name: JUnit Tests
-1
View File
@@ -37,4 +37,3 @@ out/
/docker/.env /docker/.env
/docker/*.secret /docker/*.secret
*.hprof *.hprof
/.gradle-docker-cache/
-27
View File
@@ -1,27 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="docker composeUp" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="composeUp" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<ExternalSystemDebugDisabled>false</ExternalSystemDebugDisabled>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<GradleProfilingDisabled>false</GradleProfilingDisabled>
<GradleCoverageDisabled>false</GradleCoverageDisabled>
<method v="2" />
</configuration>
</component>
-133
View File
@@ -1,133 +0,0 @@
# CLAUDE.md — event-demo
Ce fichier donne le contexte du projet pour toute session Claude Code future sur ce dépôt.
## Vue d'ensemble
`event-demo` est un projet démo personnel (Fabrice Lecomte) qui illustre plusieurs patterns
d'architecture backend :
- Event Sourcing
- Event-Driven (bus d'événements asynchrone)
- CQRS (séparation commandes / projections en lecture)
- Architecture Hexagonale (ports & adapters), un dossier par *bounded context*
Le cas d'usage servant de support est un jeu de cartes façon UNO (créer une partie, rejoindre,
jouer une carte, piocher, etc.), avec authentification des joueurs.
Dépôts distants configurés : `gitea` (auto-hébergé, git.gogn.synology.me — remote historique)
et `github` (`flecomte/event-demo`, miroir). Vérifier vers lequel pousser selon le contexte.
## Stack technique
- **Langage** : Kotlin 2.1.21, JDK 21 (toolchain Gradle)
- **Framework serveur** : Ktor 3.5.1 (Netty), DI via Koin 4.2.1
- **Sérialisation** : kotlinx.serialization (JSON)
- **Persistance** :
- PostgreSQL (event store, via HikariCP) + migrations Flyway (`migrations/events/`)
- RabbitMQ (bus d'événements / bus de commandes, via amqp-client)
- **Auth** : JWT (ktor-server-auth-jwt), hash de mot de passe via password4j
- **Infra dev/prod** : Docker Compose (fichiers `docker/docker-compose-{dev,test,prod}.yaml`
incluant des « parts » réutilisables dans `docker/parts/`), reverse proxy Træfik
- **Tests** : Kotest (runner JUnit5), MockK, kotest-extensions-koin, ArchUnit (test d'architecture)
- **Qualité** : ktlint (`ktlint_official`, standard + experimental activés), reporting checkstyle
- **CI** : GitHub Actions (`.github/workflows/tests.yml`) — build/cache Gradle, `ktlintCheck`,
puis tests exécutés **dans Docker** (`docker compose -f docker/docker-compose-test.yaml run tests`)
- **API** : documentée en OpenAPI (`resources/openapi/documentation.yaml`)
## Architecture
Un dossier par *bounded context* sous `src/main/kotlin/eventDemo/contexts/<context>/`, chacun
strictement découpé en 3 couches :
- `domain/` — aucune dépendance vers les autres couches
- `application/` — ne dépend que de `domain`
- `infrastructure/` — dépend de `domain` et `application`
Contexts actuels :
- **`auth`** : `User`, création de compte, login JWT, event store dédié (Postgresql),
projection utilisateur.
- **`game`** : cœur du jeu — `Card`, `DrawPile`/`DiscardPile`, `Player`, `GameId`, commandes
(`JoinTheGameCommand`, `PlayCardCommand`, `ReadyToPlayCommand`, `TakeCartFromDrawPileCommand`),
state machine du jeu via `sealed interface Game` (`GameInit``GameCreated``GameStarted`
`GameEnded`), notifications, projections (liste de parties), listeners/réactions.
Libs transverses dans `libs/` (indépendantes de tout contexte) :
- `bus/` — abstraction `Bus<E>` avec implémentations in-memory et RabbitMQ (fanout exchange)
- `command/``Command`, `CommandUnicityChecker` (empêche la double exécution d'une commande,
cache glissant de 10 min par défaut)
- `eventSource/``Event`, `EventStream` (append/lecture par version, gestion de
`VersionConflictException`), `EventStore` in-memory / Postgresql
- `helpers/`, `serializer/` — utilitaires (conversion de frames WebSocket, sérialiseurs UUID, etc.)
## Patterns notables dans le code
- **Event sourcing** : `Game.loadFromHistory(events)` reconstruit l'état en repliant
(`fold`) les événements sur une state machine scellée, en utilisant la syntaxe Kotlin 2.1
`when` avec garde `if` (ex. `is GameCreatedEvent if this is GameInit -> applyEvent(event)`).
- **CQRS** : écriture via les command handlers (`application/command/handlers`), lecture via des
projections dédiées (`application/projections`), propagées via le bus RabbitMQ, pas de couplage
direct avec l'écriture.
- **Event-driven** : réactions asynchrones (`ReactionListener`, `EventToNotificationSubscriber`)
déclenchées par le bus RabbitMQ (exchange fanout, une queue par abonné).
- **Exceptions métier** : hiérarchie `GameException` / `IllegalActionException` dans
`domain/game/errors`, une exception par règle métier violée (ex.
`NeedMorePlayersToStartGameException`, `ItsNotTheTurnException`).
## Commandes utiles
```shell
./gradlew build # build complet
./gradlew test # tests (JUnit5 via Kotest)
./gradlew ktlintCheck # lint
./gradlew ktlintFormat # auto-format
./gradlew buildFatJar # jar exécutable "all-in-one" (utilisé par le Dockerfile prod)
# Dépendances seules (Postgres, RabbitMQ, Træfik, pgAdmin...) pour lancer l'app en local hors docker
docker compose -f docker/docker-compose-dev.yaml up -d
# Stack de test façon CI
docker compose -f docker/docker-compose-test.yaml up -d
# ou directement (comme en CI) :
docker compose -f docker/docker-compose-test.yaml run tests
# Stack complète en prod
docker compose -f docker/docker-compose-prod.yaml -p event-demo up -d
```
URLs en dev (voir `doc/installation.md`, nécessite Træfik + résolution des `*.traefik.me`) :
API sur `http://api.traefik.me/`, dashboard
Træfik, pgAdmin et RabbitMQ management exposés via des sous-domaines `traefik.me`.
## Conventions de code
- ktlint en mode `ktlint_official` + règles `standard` et `experimental` activées
(voir `.editorconfig`), indentation **2 espaces**, virgules finales (*trailing commas*)
systématiques, wrapping forcé des expressions/signatures multi-lignes.
- Fins de ligne forcées en **LF** (`.gitattributes`), sauf `gradlew.bat` en CRLF.
- Code et identifiants en anglais.
- Style Kotlin idiomatique/fonctionnel : `fold`, `let`, `apply`, `when` exhaustifs, classes/interfaces
scellées (`sealed class`/`sealed interface`) pour modéliser états et événements plutôt que des enums
avec des champs optionnels.
## Pièges connus / choses à savoir avant de toucher au build ou à la CI
- **MockK/ByteBuddy en Docker** : l'auto-attach dynamique de MockK échoue dans les conteneurs
(le handshake SIGQUIT de l'AttachListener JVM time-out). Le `build.gradle.kts` charge donc
l'agent `byte-buddy-agent` de façon statique via `-javaagent` pour les tâches `Test`, afin
que MockK détecte l'instrumentation déjà présente et saute l'attach dynamique. Ne pas retirer
ce bloc sans repenser l'exécution des tests en Docker.
- **Secret Postgres en CI** : `docker/postgresql.secret` est généré à la volée par le workflow
GitHub Actions s'il n'existe pas (`echo -n "changeit" > docker/postgresql.secret`) — normal,
pas un fichier à committer.
- Les tests « officiels » de la CI tournent **dans Docker**, pas directement via `./gradlew test`
sur l'hôte — en cas de comportement différent entre local et CI, vérifier d'abord les
variables d'environnement/versions du `docker-compose-test.yaml`.
## Historique récent (pour contexte)
Le projet a connu un « Massive refactor to build the V2 » (commit `e2d7942`) : passage d'une
architecture par couches techniques plates (`adapter/presenter/domain`) à l'organisation actuelle
par bounded context (`auth`/`game`) avec 3 couches hexagonales chacune.
+27 -1
View File
@@ -2,6 +2,7 @@ Event Demo
========== ==========
- [Installation](./doc/installation.md) - [Installation](./doc/installation.md)
- [What's the demo for ?](#whats-the-demo-for-) - [What's the demo for ?](#whats-the-demo-for-)
- [What's in this demo](#whats-in-this-demo)
- [The stack](#the-stack) - [The stack](#the-stack)
- [Architecture](./doc/architecture.md) - [Architecture](./doc/architecture.md)
@@ -17,6 +18,31 @@ of different patterns and architectures.
- The CQRS pattern. - The CQRS pattern.
- With the Hexagonal architecture. - With the Hexagonal architecture.
What's in this demo
-------------------
- The **event sourcing** pattern.
- The **event driven** pattern.
- The **CQRS** pattern with **command** and **query**.
- A fully **asynchronous** architecture.Concurently process.
- A **pure Kotlin** implementation of **readmodel**/**projection**.
- A **Redis** implementation of **readmodel**/**projection**.
- A **pure Kotlin** implementation of **Event Store**.
- A **Postgresql** implementation of **Event Store**.
- A **pure Kotlin** implementation of **Event Bus**.
- A **RabbitMQ** implementation of **Event Bus**.
- A **Hexagonal** architecture.
- Use of **Web Sockets**.
- Use of the classic **Rest** route.
- Simple usage of the **JWT**.
- The **Ktor** framework.
- The **Koin** Dependency Injection framework
- Concurrently process.
- Use of coroutines.
- Using **docker compose** for the stack with **traefik**.
- Use of **flyway** to migrate the postgresql schema.
The stack The stack
--------- ---------
@@ -25,11 +51,11 @@ Language
Framework Framework
- Ktor - Ktor
- with Koin for Dependency Injection
Database Database
- Postgresql - Postgresql
- with Flyway - with Flyway
- Redis
- RabbitMQ - RabbitMQ
Infra Infra
+59 -11
View File
@@ -14,6 +14,7 @@ plugins {
id("io.ktor.plugin") version "3.5.1" id("io.ktor.plugin") version "3.5.1"
id("org.jetbrains.kotlin.plugin.serialization") version "2.4.10" id("org.jetbrains.kotlin.plugin.serialization") version "2.4.10"
id("org.jlleitschuh.gradle.ktlint") version "14.2.0" id("org.jlleitschuh.gradle.ktlint") version "14.2.0"
id("com.avast.gradle.docker-compose") version "0.17.12"
} }
group = "io.github.flecomte" group = "io.github.flecomte"
@@ -46,17 +47,63 @@ java {
tasks.withType<Test>().configureEach { tasks.withType<Test>().configureEach {
useJUnitPlatform() useJUnitPlatform()
jvmArgs("-Djdk.attach.allowAttachSelf=true", "-XX:+EnableDynamicAgentLoading") }
// Dynamic self-attach (used by MockK/ByteBuddy) times out in Docker containers because the
// SIGQUIT-triggered AttachListener handshake never completes there. Loading the byte-buddy dockerCompose {
// agent jar statically via -javaagent avoids the attach handshake entirely: MockK detects the val composeFile =
// pre-installed Instrumentation instance and skips dynamic attach. if (project.hasProperty("ci")) {
doFirst { // Use docker-compose-ci.yaml for the CI
val agentJar = "docker/docker-compose-ci.yaml"
classpath.files.firstOrNull { it.name.startsWith("byte-buddy-agent") } } else {
?: error("byte-buddy-agent jar not found on test classpath") // Use docker-compose-test.yaml for local tests
jvmArgs("-javaagent:$agentJar") "docker/docker-compose-test.yaml"
} }
useComposeFiles.set(listOf(composeFile))
setProjectName("event-demo-test")
}
tasks.test {
dependsOn("composeUp")
dockerCompose.useComposeFiles.set(listOf("docker/docker-compose-test.yaml"))
dockerCompose.setProjectName("event-demo-test")
}
tasks.named("run") {
dependsOn("composeUp")
dockerCompose.useComposeFiles.set(listOf("docker/docker-compose-test.yaml"))
dockerCompose.setProjectName("event-demo-dev")
}
tasks.register<Copy>("copyEnv") {
group = "docker"
description = "copy the default dotenv file"
from("docker")
into("docker")
rename {
it.removeSuffix(".template")
}
include(".env.template")
eachFile {
if (File("docker/$name").exists()) {
exclude()
}
}
doLast {
val files =
listOf(
File("docker/pgadmin.secret"),
File("docker/postgresql.secret"),
)
files.forEach {
if (!it.exists()) {
it.writeText("changeit")
}
}
}
}
tasks.composeUp {
dependsOn("copyEnv")
} }
dependencies { dependencies {
@@ -81,6 +128,7 @@ dependencies {
implementation("io.github.oshai:kotlin-logging-jvm:${kotlinLoggingVersion.get()}") implementation("io.github.oshai:kotlin-logging-jvm:${kotlinLoggingVersion.get()}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:${kotlinSerializationVersion.get()}") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:${kotlinSerializationVersion.get()}")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.2") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.2")
implementation("redis.clients:jedis:5.2.0")
implementation("org.postgresql:postgresql:42.7.13") implementation("org.postgresql:postgresql:42.7.13")
implementation("com.zaxxer:HikariCP:6.3.0") implementation("com.zaxxer:HikariCP:6.3.0")
implementation("com.rabbitmq:amqp-client:5.25.0") implementation("com.rabbitmq:amqp-client:5.25.0")
@@ -93,6 +141,6 @@ dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:${kotlinVersion.get()}") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:${kotlinVersion.get()}")
testImplementation("io.ktor:ktor-server-test-host-jvm:${ktorVersion.get()}") testImplementation("io.ktor:ktor-server-test-host-jvm:${ktorVersion.get()}")
testImplementation("io.kotest:kotest-runner-junit5:${kotestVersion.get()}") testImplementation("io.kotest:kotest-runner-junit5:${kotestVersion.get()}")
testImplementation("io.mockk:mockk:1.14.11") testImplementation("io.mockk:mockk:1.13.17")
testImplementation("com.tngtech.archunit:archunit-junit5:1.3.0") testImplementation("com.tngtech.archunit:archunit-junit5:1.3.0")
} }
+11
View File
@@ -0,0 +1,11 @@
Architecture
============
The Workflow
------------
![Workflow Diagram](./schemas/Workflow.svg)
The business Entities
---------------------
![Entities and Projections Diagram](./schemas/Entities.svg)
+67
View File
@@ -0,0 +1,67 @@
# Exemple de structure
Les couches, du plus interne au plus externe
```
Domain (le cœur, ne dépend de RIEN d'externe)
Application (orchestre le Domain, ne connaît pas l'infra concrète)
Infrastructure (WebSocket, DB, event store — dépend de tout le reste)
```
```
src/
└── contexts/
├── auth/
└── ...
└── game/
├── domain/ ← Le cœur métier, zéro dépendance externe
│ ├── game/
│ │ ├── Game.ts ← Aggregate Root
│ │ ├── Player.ts ← Entity interne
│ │ ├── Card.ts ← Entity
│ │ ├── Color.ts ← Value Object
│ │ ├── Deck.ts ← VO ou petite structure
│ │ └── errors/
│ │ ├── InvalidMoveError.ts
│ │ └── ColorChoiceRequiredError.ts
│ └── events/ ← Events de DOMAINE (internes)
│ ├── CardPlayed.ts
│ ├── CardDrawn.ts
│ ├── TurnPassed.ts
│ └── DomainEvent.ts ← interface/type de base
├── application/ ← Orchestration, cas d'usage
│ ├── commands/ ← Les Commandes (intentions)
│ │ ├── PlayCardCommand.ts
│ │ └── DrawCardCommand.ts
│ ├── handlers/ ← Un handler par commande
│ │ ├── PlayCardHandler.ts ← charge l'aggregate, appelle game.playCard(), save
│ │ └── DrawCardHandler.ts
│ ├── projections/ ← LA LOGIQUE de construction des projections
│ │ ├── GameSummaryProjector.kt ← écoute les events, met à jour la vue
│ │ └── PlayerStatsProjector.kt
│ └── ports/ ← INTERFACES seulement (le "hexagone")
│ ├── GameRepository.ts ← interface, pas d'implémentation
│ ├── EventPublisher.ts ← interface, pas d'implémentation
│ └── ProjectionStore.kt ← interface, où lire/écrire la projection
├── infrastructure/ ← Tout ce qui est technique/externe
│ ├── persistence/
│ │ ├── EventStoreGameRepository.ts ← implémente GameRepository
│ │ ├── EventStore.ts
│ │ ├── projections/
│ │ │ ├── GameSummaryProjectionStore.kt ← implémentation concrète (DB, table dédiée)
│ │ │ └── models/
│ │ │ └── GameSummaryView.kt ← structure de la vue elle-même
│ ├── websocket/
│ │ ├── WebSocketServer.ts
│ │ ├── connectionManager.ts ← Map<gameId, Map<playerId, WebSocket>>
│ │ └── commandRouter.ts ← reçoit le message brut, dispatch vers le bon handler
│ └── eventPublisher/
│ └── WebSocketEventPublisher.ts ← implémente EventPublisher, fait le broadcast
└── presentation/ ← Traduction vers/depuis le client (le fameux DTO layer)
├── clientEvents/
│ ├── ClientEvent.ts ← types des events envoyés au front
│ └── toClientEvent.ts ← fonction de traduction domain event → client event
└── clientCommands/
└── parseIncomingCommand.ts ← valide/parse le message brut du client → Command
```
+5 -15
View File
@@ -1,33 +1,23 @@
Installation Installation
============ ============
To run the stack in production: To run the stack:
```shell ```shell
docker compose -f docker\docker-compose-prod.yaml -p event-demo up -d docker compose -f docker\docker-compose-prod.yaml -p event-demo up -d
``` ```
To run only the app dependencies in development mode and run the app localy (not in docker):
```shell
docker compose -f docker\docker-compose-dev.yaml up -d
```
To run the tests in docker (it's designed for the CI):
```shell
docker compose -f docker\docker-compose-test.yaml up -d
```
Api url: Api url:
- [Backend API](http://api.traefik.me/) - [Backend API](http://api.traefik.me/)
- [Frontend web site](http://app.traefik.me/) - [Frontend web site](http://app.traefik.me/) (WIP)
Exposed url on dev env: Exposed url on test env:
- [PostgreSql](http://localhost:5432/) - [PostgreSql](http://localhost:5432/)
- [Redis](http://localhost:6379/)
- [RabbitMQ](http://localhost:15672/) - [RabbitMQ](http://localhost:15672/)
Admin service URL: Admin service URL:
- [Træfik dashboard](http://traefik.traefik.me/) - [Træfik dashboard](http://traefik.traefik.me/)
- [Redis insight](http://insight.redis.traefik.me/)
- [pgAdmin](http://pgadmin.postgresql.traefik.me/) - [pgAdmin](http://pgadmin.postgresql.traefik.me/)
- [RabbitMQ management](http://management.rabbitmq.traefik.me/) - [RabbitMQ management](http://management.rabbitmq.traefik.me/)
+93
View File
@@ -0,0 +1,93 @@
@startuml
'https://plantuml.com/class-diagram
left to right direction
class GameList <<Projection>> {
+ status: Status
}
class GameState <<Projection>> {
+ players: List<Player>
+ currentPlayerTurn: Player
+ lastCardPlayer: Player
+ colorOnCurrentStack: Color
+ direction: Direction
+ readyPlayers: List<Player>
+ deck: Deck
+ isStarted: Boolean
+ playerWins: List<Player>
}
interface Card {
+ id: UUID
}
enum Color {
+ Blue
+ Red
+ Yellow
+ Green
}
class GameId {
+ id: UUID
}
class Player {
+ id: PlayerId
+ name: String
}
class Deck {
+ stack: Stack
+ discard: Discard
+ playersHands: PlayersHands
}
class Stack {
+ cards: List<Card>
+ shuffle()
}
class Discard {
+ cards: List<Card>
}
class PlayerHands {
+ map: Map<PlayerId, List<Card>>
}
class NumericCard {
+ number: Int
+ color: Color
}
class ReverseCard {
+ color: Color
}
class PassCard {
+ color: Color
}
class Plus2Card {
+ color: Color
}
class Plus4Card
class ChangeColorCard
GameState *-- Deck
GameState o-- "many" Player
Deck *-- PlayerHands
PlayerHands *-- "many" Card
PlayerHands o-- "many" Player
Stack *-- "many" Card
Discard *-- "many" Card
Deck *-- Stack
Deck *-- Discard
GameState *-- GameId
Card <|--- NumericCard
Card <|--- ReverseCard
Card <|--- PassCard
Card <|--- ChangeColorCard
Card <|--- Plus2Card
Card <|--- Plus4Card
ReverseCard o-- Color
NumericCard o-- Color
PassCard o-- Color
Plus2Card o-- Color
GameList *-- GameId
GameList o-- "many" Player
@enduml
File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 48 KiB

+84
View File
@@ -0,0 +1,84 @@
@startuml
'https://plantuml.com/use-case-diagram
package Legend {
usecase (Queries) #7693C4
usecase (Projections) #AB64C9
usecase (Events) #5FAD56
}
actor User
entity Query #7693C4
entity Command #5FAD56
entity Event #5FAD56
entity Projection #AB64C9
database Postgresql
database Redis
queue RabbitMQ
usecase (Web socket adapter) #5FAD56
usecase (Command handler) #5FAD56
usecase/ (Action) #5FAD56
usecase (Event handler) #5FAD56
usecase (Version builder) #5FAD56
usecase (Event store) #5FAD56
usecase (Event stream) #5FAD56
usecase (Event bus) #5FAD56
usecase/ (Reaction listener) #5FAD56
usecase/ (Projection builder) #AB64C9
usecase (Projection repository) #AB64C9
usecase (Projection bus) #AB64C9
usecase (Controller) #7693C4
User -> Query : <<create>>
Command <- User : <<create>>
User ---> (Controller) : Get \nprojection
User <-- (Controller) : Returns \nprojection
(Controller) --------> (Projection repository) : Get \nprojection
User -> (Web socket adapter) : Send \ncommand
(Web socket adapter) --> (Command handler) : Send \ncommand
(Web socket adapter) ...> User : Send notification \n(error or success)
(Command handler) ..> (Web socket adapter) : Send \nnotification
(Command handler) -> (Action) : Execute action
(Command handler) <- (Action) : Returns \nevent builder
(Command handler) ---> (Event handler) : Dispatch \nevent \n(send an event builder)
(Command handler) --> Event : <<Create>>
(Event handler) --> (Event store) : Publish \nevent
(Event handler) <-- (Reaction listener) : Dispatch \n new event
(Version builder) <- (Event handler) : build next version
note "Acquire a lock, \nget the next event version, \nand then, build the event " as EventHandlerNote
EventHandlerNote <-- (Event handler)
(Event store) -left-> (Event stream)
(Event store) ---> (Event bus) : Publish \nevent
(Event stream) --> Postgresql : Persist \nevent
(Event bus) -> RabbitMQ : Publish \nevent
(Event bus) -> RabbitMQ : Subscribe \nto event
(Event bus) <. RabbitMQ : Emit event
(Reaction listener) ---> (Projection bus) : Subscribe
(Reaction listener) <.. (Projection bus) : Emit projection
(Projection bus) <- (Projection repository) : Publish \nprojection
RabbitMQ <- (Projection bus) : Publish \nprojection
RabbitMQ <- (Projection bus) : Subscribe \nto projection
RabbitMQ .> (Projection bus) : Emit projection
(Event bus) <---- (Projection repository) : Subscribe
(Event bus) ..> (Projection repository) : Emit event
(Projection repository) --> Redis : Persist \nprojection
(Projection repository) <- Redis : Get \nprojection
(Projection repository) -> (Projection builder) : Build \nprojection
(Projection builder) --> Projection : <<create projection>>
@enduml
File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 35 KiB

-2
View File
@@ -1,2 +0,0 @@
POSTGRESQL_URL=jdbc:postgresql://postgresql/event-demo
RABBITMQ_URL=rabbitmq
+1
View File
@@ -0,0 +1 @@
PGADMIN_DEFAULT_EMAIL=
+4 -4
View File
@@ -1,5 +1,5 @@
# Stage 1: Cache Gradle dependencies # Stage 1: Cache Gradle dependencies
FROM gradle:9.6.1-jdk21-alpine AS cache FROM gradle:latest AS cache
RUN mkdir -p /home/gradle/cache_home RUN mkdir -p /home/gradle/cache_home
ENV GRADLE_USER_HOME=/home/gradle/cache_home ENV GRADLE_USER_HOME=/home/gradle/cache_home
COPY build.gradle.* gradle.properties /home/gradle/app/ COPY build.gradle.* gradle.properties /home/gradle/app/
@@ -7,7 +7,7 @@ WORKDIR /home/gradle/app
RUN gradle build -i -x check RUN gradle build -i -x check
# Stage 2: Build Application # Stage 2: Build Application
FROM gradle:9.6.1-jdk21-alpine AS build FROM gradle:latest AS build
COPY --from=cache /home/gradle/cache_home /home/gradle/.gradle COPY --from=cache /home/gradle/cache_home /home/gradle/.gradle
COPY --chown=gradle:gradle . /home/gradle/src COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src WORKDIR /home/gradle/src
@@ -16,8 +16,8 @@ WORKDIR /home/gradle/src
RUN gradle buildFatJar --no-daemon RUN gradle buildFatJar --no-daemon
# Stage 3: Create the Runtime Image # Stage 3: Create the Runtime Image
FROM eclipse-temurin:21-jre-alpine AS runtime FROM amazoncorretto:21 AS runtime
EXPOSE 8080 EXPOSE 8080
RUN mkdir /app RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*-all.jar /app/event-demo-all.jar COPY --from=build /home/gradle/src/build/libs/*.jar /app/event-demo-all.jar
ENTRYPOINT ["java","-jar","/app/event-demo-all.jar"] ENTRYPOINT ["java","-jar","/app/event-demo-all.jar"]
-10
View File
@@ -1,10 +0,0 @@
# Image officielle Gradle avec JDK 21 déjà installé
FROM gradle:9.6.1-jdk21
WORKDIR /app
# Copie du wrapper et des fichiers de config en premier pour profiter du cache Docker
COPY build.gradle.kts settings.gradle.kts ./
# Lance les tests Kotlin
CMD ["gradle", "test", "--no-daemon"]
+6
View File
@@ -0,0 +1,6 @@
name: event-demo-test
include:
- path:
- parts/docker-compose-databases.yaml
- parts/docker-compose-databases-expose.yaml
- parts/docker-compose-traefik.yaml
-17
View File
@@ -1,17 +0,0 @@
name: event-demo-dev
include:
- path:
- parts/docker-compose-databases.yaml
- parts/docker-compose-databases-expose.yaml
- parts/docker-compose-tools.yaml
- parts/docker-compose-tools-local.yaml
- parts/docker-compose-traefik.yaml
services:
postgresql:
environment:
POSTGRES_PASSWORD: "changeit"
pgadmin:
environment:
PGADMIN_DEFAULT_PASSWORD: "changeit"
-13
View File
@@ -5,16 +5,3 @@ include:
- parts/docker-compose-app.yaml - parts/docker-compose-app.yaml
- parts/docker-compose-tools.yaml - parts/docker-compose-tools.yaml
- parts/docker-compose-traefik.yaml - parts/docker-compose-traefik.yaml
services:
postgresql:
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/postgresql_password
volumes:
- ./postgresql.secret:/run/secrets/postgresql_password:ro
pgadmin:
environment:
PGADMIN_DEFAULT_PASSWORD_FILE: /run/secrets/pgadmin_password
volumes:
- ./pgadmin.secret:/run/secrets/pgadmin_password:ro
+2 -6
View File
@@ -2,10 +2,6 @@ name: event-demo-test
include: include:
- path: - path:
- parts/docker-compose-databases.yaml - parts/docker-compose-databases.yaml
- parts/docker-compose-test.yaml - parts/docker-compose-databases-expose.yaml
- parts/docker-compose-tools.yaml
- parts/docker-compose-traefik.yaml - parts/docker-compose-traefik.yaml
services:
postgresql:
environment:
POSTGRES_PASSWORD: "changeit"
+2 -2
View File
@@ -10,8 +10,8 @@ services:
condition: service_healthy condition: service_healthy
rabbitmq: rabbitmq:
condition: service_healthy condition: service_healthy
env_file: redis:
- ../.env.docker condition: service_healthy
labels: labels:
- "traefik.http.routers.api.rule=Host(`api.traefik.me`)" - "traefik.http.routers.api.rule=Host(`api.traefik.me`)"
- "traefik.http.services.api.loadbalancer.server.port=8080" - "traefik.http.services.api.loadbalancer.server.port=8080"
@@ -1,4 +1,8 @@
services: services:
redis:
ports:
- "6379:6379"
postgresql: postgresql:
ports: ports:
- "5432:5432" - "5432:5432"
@@ -1,4 +1,12 @@
services: services:
redis:
image: redis/redis-stack:7.4.0-v8
healthcheck:
test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
volumes:
- redis_data:/data
- redisinsight_data:/redisinsight
flyway: flyway:
image: flyway/flyway image: flyway/flyway
command: migrate command: migrate
@@ -14,7 +22,10 @@ services:
image: postgres:18.4 image: postgres:18.4
command: postgres -c 'max_connections=500' command: postgres -c 'max_connections=500'
environment: environment:
POSTGRES_PASSWORD_FILE: /run/secrets/postgresql_password
POSTGRES_USER: event-demo POSTGRES_USER: event-demo
secrets:
- postgresql_password
healthcheck: healthcheck:
test: ["CMD-SHELL", "sh -c 'pg_isready -U event-demo'"] test: ["CMD-SHELL", "sh -c 'pg_isready -U event-demo'"]
interval: 1s interval: 1s
@@ -36,6 +47,12 @@ services:
volumes: volumes:
- rabbitmq_data:/var/lib/rabbitmq/ - rabbitmq_data:/var/lib/rabbitmq/
secrets:
postgresql_password:
file: ../postgresql.secret
volumes: volumes:
redis_data:
redisinsight_data:
postgresql_data: postgresql_data:
rabbitmq_data: rabbitmq_data:
-20
View File
@@ -1,20 +0,0 @@
services:
tests:
build:
context: ../..
dockerfile: docker/DockerfileTest
volumes:
- ${GRADLE_CACHE_DIR:-gradle-cache}:/home/gradle/.gradle
- ../..:/app
depends_on:
flyway:
condition: service_completed_successfully
postgresql:
condition: service_healthy
rabbitmq:
condition: service_healthy
env_file:
- ../.env.docker
volumes:
gradle-cache:
@@ -1,18 +0,0 @@
services:
pgadmin:
environment:
PGADMIN_CONFIG_SERVER_MODE: 'False'
PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
configs:
- source: pgpass
target: /pgpass
mode: 0600
uid: "5050"
gid: "5050"
- source: servers_json
target: /pgadmin4/servers.json
configs:
pgpass:
content: |
*:*:*:event-demo:changeit
+13 -21
View File
@@ -2,39 +2,31 @@ services:
pgadmin: pgadmin:
image: dpage/pgadmin4 image: dpage/pgadmin4
environment: environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin@event-demo.dev} PGADMIN_DEFAULT_EMAIL: $PGADMIN_DEFAULT_EMAIL
PGADMIN_DEFAULT_PASSWORD_FILE: /run/secrets/pgadmin_password
secrets:
- pgadmin_password
volumes: volumes:
- pgadmin_data:/var/lib/pgadmin - pgadmin_data:/var/lib/pgadmin
configs:
- source: servers_json
target: /pgadmin4/servers.json
labels: labels:
- "traefik.http.routers.pgadmin.rule=Host(`pgadmin.postgresql.traefik.me`)" - "traefik.http.routers.pgadmin.rule=Host(`pgadmin.postgresql.traefik.me`)"
- "traefik.http.services.pgadmin.loadbalancer.server.port=80" - "traefik.http.services.pgadmin.loadbalancer.server.port=80"
redis:
labels:
- "traefik.http.routers.redisinsight.rule=Host(`insight.redis.traefik.me`)"
- "traefik.http.routers.redisinsight.service=redisinsight"
- "traefik.http.services.redisinsight.loadbalancer.server.port=8001"
rabbitmq: rabbitmq:
labels: labels:
- "traefik.http.routers.rabbitmq-management.rule=Host(`management.rabbitmq.traefik.me`)" - "traefik.http.routers.rabbitmq-management.rule=Host(`management.rabbitmq.traefik.me`)"
- "traefik.http.routers.rabbitmq-management.service=rabbitmq-management" - "traefik.http.routers.rabbitmq-management.service=rabbitmq-management"
- "traefik.http.services.rabbitmq-management.loadbalancer.server.port=15672" - "traefik.http.services.rabbitmq-management.loadbalancer.server.port=15672"
configs: secrets:
servers_json: pgadmin_password:
content: | file: ../pgadmin.secret
{
"Servers": {
"1": {
"Name": "Event demo",
"Group": "Servers",
"Host": "postgresql",
"Port": 5432,
"MaintenanceDB": "event-demo",
"Username": "event-demo",
"PassFile": "/pgpass",
"SSLMode": "prefer"
}
}
}
volumes: volumes:
pgadmin_data: pgadmin_data:
+40 -644
View File
@@ -1,264 +1,58 @@
openapi: "3.0.3" openapi: "3.0.3"
info: info:
title: "event_demo API" title: "event_demo API"
description: | description: "event_demo API"
API for the event_demo project: a small event-sourced Uno-like card game. version: "1.0.0"
The API is split in two parts:
- a classic REST API used to authenticate, create users, list games and
replay a game's history.
- a WebSocket endpoint (`/games/{id}`) used to play a game in real time:
the client sends `GameCommand` messages and receives `Notification`
messages back. See the `x-websocket-channels` section below for details,
since WebSockets are not natively described by OpenAPI 3.0.
Authentication is done with a JWT bearer token obtained from `/login/{username}`.
Note that most polymorphic messages (`GameCommand`, `Notification`) are
discriminated by a `type` field whose value is the fully-qualified Kotlin
class name of the payload (since no custom serial name is declared for
these types), while `Card` uses short discriminator values
(`Simple`, `Reverse`, `Pass`, `Plus2`, `Plus4`, `ChangeColor`).
version: "2.0.0"
servers: servers:
- url: "https://event_demo" - url: "https://event_demo"
tags:
- name: Auth
description: User registration and authentication
- name: Games
description: Listing games and reading/playing their state
security:
- bearerAuth: []
paths: paths:
"/login/{username}": "/game/{id}/card/last":
post:
tags: [Auth]
summary: Log in and obtain a JWT
security: []
parameters:
- name: username
in: path
required: true
schema:
type: string
- name: password
in: query
description: The user's plain-text password.
required: true
schema:
type: string
responses:
200:
description: Successful login
content:
application/json:
schema:
$ref: "#/components/schemas/LoginResponse"
400:
description: Unknown username or invalid password
"/users/create":
post:
tags: [Auth]
summary: Create a new user account
description: Requires a valid JWT (any authenticated user can create new users).
parameters:
- name: username
in: query
required: true
schema:
type: string
- name: password
in: query
description: The plain-text password, hashed server-side before being stored.
required: true
schema:
type: string
responses:
200:
description: The newly created user
content:
application/json:
schema:
$ref: "#/components/schemas/CreateUserResponse"
401:
$ref: "#/components/responses/Unauthorized"
"/games":
get: get:
tags: [Games] description: get the last card played
summary: List all known games
description: Returns up to the 100 most recent games (pagination is not yet exposed on this route).
responses: responses:
200: 200:
description: The list of games description: The last card
content: content:
application/json: application/json:
schema: schema:
type: array
items:
$ref: "#/components/schemas/GameList"
401:
$ref: "#/components/responses/Unauthorized"
"/games/{id}":
get:
tags: [Games]
summary: Replay a game's full notification history
description: |
Rebuilds every notification that would have been sent to the calling
player since the beginning of the game (from its event stream), so a
client reconnecting can catch up on the current game state.
This same path also accepts a WebSocket upgrade to play the game live,
see `x-websocket-channels` at the root of this document.
parameters:
- name: id
in: path
required: true
schema:
type: string
format: uuid
responses:
200:
description: The full list of notifications for this game, from this player's point of view
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Notification"
401:
$ref: "#/components/responses/Unauthorized"
404:
description: No game found for this id
x-websocket-channels:
"/games/{id}":
description: |
WebSocket endpoint to join and play a game in real time. Requires the
same JWT bearer authentication as the REST routes (sent the same way,
e.g. via the `Authorization` header during the WebSocket handshake).
parameters:
- name: id
in: path
required: true
schema:
type: string
format: uuid
send:
description: Commands sent by the client to act on the game.
schema:
$ref: "#/components/schemas/GameCommand"
receive:
description: Notifications sent by the server as the game progresses.
schema:
$ref: "#/components/schemas/Notification"
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
description: >
JWT obtained from `POST /login/{username}`. It carries the `username`
and `userid` claims and currently expires 60 seconds after issuance.
responses:
Unauthorized:
description: Missing, invalid or expired JWT
content:
text/plain:
schema:
type: string
example: "Token is not valid or has expired"
schemas:
LoginResponse:
type: object
required: [token]
properties:
token:
type: string
description: JWT bearer token to use on subsequent requests.
CreateUserResponse:
type: object
required: [id]
properties:
id:
type: string
format: uuid
HttpErrorBadRequest:
type: object
description: Generic problem-details style error body used by some validation failures.
properties:
title:
type: string
statusCode:
type: integer
invalidParams:
type: array
items:
$ref: "#/components/schemas/InvalidParam"
InvalidParam:
type: object
required: [name, reason]
properties:
name:
type: string
reason:
type: string
PlayerId:
type: string
format: uuid
Player:
type: object
required: [name, userId, hand, id]
properties:
name:
type: string
userId:
type: string
format: uuid
id:
$ref: "#/components/schemas/PlayerId"
hand:
description: The set of cards currently held by the player.
type: array
items:
$ref: "#/components/schemas/Card" $ref: "#/components/schemas/Card"
GameList: components:
type: object schemas:
required: [aggregateId, status, players, winners] Card:
oneOf:
- $ref: "#/components/schemas/SimpleCard"
- $ref: "#/components/schemas/ReverseCard"
- $ref: "#/components/schemas/PassCard"
- $ref: "#/components/schemas/Plus2Card"
- $ref: "#/components/schemas/Plus4Card"
- $ref: "#/components/schemas/ChangeColorCard"
SimpleCard:
properties: properties:
aggregateId: number:
type: string type: integer
format: uuid color:
status: $ref: "#/components/schemas/CardColor"
$ref: "#/components/schemas/GameStatus" ReverseCard:
players: properties:
type: array color:
items: $ref: "#/components/schemas/CardColor"
$ref: "#/components/schemas/Player" PassCard:
winners: properties:
type: array color:
items: $ref: "#/components/schemas/CardColor"
$ref: "#/components/schemas/PlayerId" Plus2Card:
properties:
GameStatus: color:
type: string $ref: "#/components/schemas/CardColor"
enum: Plus4Card:
- OPENING properties:
- IS_STARTED nextColor:
- FINISH $ref: "#/components/schemas/CardColor"
- CANCELED ChangeColorCard:
properties:
nextColor:
$ref: "#/components/schemas/CardColor"
CardColor: CardColor:
type: string type: string
enum: enum:
@@ -266,401 +60,3 @@ components:
- Red - Red
- Yellow - Yellow
- Green - Green
Card:
description: >
A playing card. Discriminated by the "type" field using the short
names declared on each Kotlin subtype (@SerialName), unlike
GameCommand/Notification below.
oneOf:
- $ref: "#/components/schemas/NumericCard"
- $ref: "#/components/schemas/ReverseCard"
- $ref: "#/components/schemas/PassCard"
- $ref: "#/components/schemas/Plus2Card"
- $ref: "#/components/schemas/Plus4Card"
- $ref: "#/components/schemas/ChangeColorCard"
discriminator:
propertyName: type
mapping:
Simple: "#/components/schemas/NumericCard"
Reverse: "#/components/schemas/ReverseCard"
Pass: "#/components/schemas/PassCard"
Plus2: "#/components/schemas/Plus2Card"
Plus4: "#/components/schemas/Plus4Card"
ChangeColor: "#/components/schemas/ChangeColorCard"
NumericCard:
description: A numbered card (0-9) of a given color.
type: object
required: [type, id, number, color]
properties:
type:
type: string
enum: [Simple]
id:
type: string
format: uuid
number:
type: integer
minimum: 0
maximum: 9
color:
$ref: "#/components/schemas/CardColor"
ReverseCard:
description: Reverses the turn order.
type: object
required: [type, id, color]
properties:
type:
type: string
enum: [Reverse]
id:
type: string
format: uuid
color:
$ref: "#/components/schemas/CardColor"
PassCard:
description: Skips the next player's turn.
type: object
required: [type, id, color]
properties:
type:
type: string
enum: [Pass]
id:
type: string
format: uuid
color:
$ref: "#/components/schemas/CardColor"
Plus2Card:
description: Forces the next player to draw 2 cards and skips their turn.
type: object
required: [type, id, color]
properties:
type:
type: string
enum: [Plus2]
id:
type: string
format: uuid
color:
$ref: "#/components/schemas/CardColor"
Plus4Card:
description: >
Forces the next player to draw 4 cards and skips their turn. The new
color is chosen separately, via the `chosenColor` field of
PlayCardCommand, and is not part of the card itself.
type: object
required: [type, id]
properties:
type:
type: string
enum: [Plus4]
id:
type: string
format: uuid
ChangeColorCard:
description: >
Changes the current color. The new color is chosen separately, via
the `chosenColor` field of PlayCardCommand, and is not part of the
card itself.
type: object
required: [type, id]
properties:
type:
type: string
enum: [ChangeColor]
id:
type: string
format: uuid
GameCommand:
description: >
A command sent by the client over the game WebSocket to act on a game.
Discriminated by "type", whose value is the fully-qualified Kotlin
class name of the command (no `@SerialName` is declared on these
types).
oneOf:
- $ref: "#/components/schemas/JoinTheGameCommand"
- $ref: "#/components/schemas/PlayCardCommand"
- $ref: "#/components/schemas/ReadyToPlayCommand"
- $ref: "#/components/schemas/TakeCartFromDrawPileCommand"
discriminator:
propertyName: type
mapping:
eventDemo.contexts.game.application.command.models.JoinTheGameCommand: "#/components/schemas/JoinTheGameCommand"
eventDemo.contexts.game.application.command.models.PlayCardCommand: "#/components/schemas/PlayCardCommand"
eventDemo.contexts.game.application.command.models.ReadyToPlayCommand: "#/components/schemas/ReadyToPlayCommand"
eventDemo.contexts.game.application.command.models.TakeCartFromDrawPileCommand: "#/components/schemas/TakeCartFromDrawPileCommand"
JoinTheGameCommand:
description: Join an existing (not yet started) game.
type: object
required: [type, userId, payload]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.command.models.JoinTheGameCommand]
userId:
type: string
format: uuid
payload:
type: object
required: [aggregateId]
properties:
aggregateId:
type: string
format: uuid
description: The id of the game to join.
ReadyToPlayCommand:
description: Mark the calling player as ready, so the game can start once everyone is ready.
type: object
required: [type, userId, payload]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.command.models.ReadyToPlayCommand]
userId:
type: string
format: uuid
payload:
type: object
required: [aggregateId, playerId]
properties:
aggregateId:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
TakeCartFromDrawPileCommand:
description: Draw a card from the draw pile.
type: object
required: [type, userId, payload]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.command.models.TakeCartFromDrawPileCommand]
userId:
type: string
format: uuid
payload:
type: object
required: [aggregateId, playerId]
properties:
aggregateId:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
PlayCardCommand:
description: Play a card from the calling player's hand.
type: object
required: [type, userId, payload]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.command.models.PlayCardCommand]
userId:
type: string
format: uuid
payload:
type: object
required: [aggregateId, playerId, card]
properties:
aggregateId:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
card:
$ref: "#/components/schemas/Card"
chosenColor:
description: The color to switch to, only required when playing a Plus4Card or ChangeColorCard.
nullable: true
allOf:
- $ref: "#/components/schemas/CardColor"
Notification:
description: >
A notification sent by the server, either replayed by `GET /games/{id}`
or streamed live over the game WebSocket. Discriminated by "type",
whose value is the fully-qualified Kotlin class name of the
notification (no `@SerialName` is declared on these types).
oneOf:
- $ref: "#/components/schemas/WelcomeToTheGameNotification"
- $ref: "#/components/schemas/PlayerAsJoinTheGameNotification"
- $ref: "#/components/schemas/PlayerWasReadyNotification"
- $ref: "#/components/schemas/TheGameWasStartedNotification"
- $ref: "#/components/schemas/ItsTheTurnOfNotification"
- $ref: "#/components/schemas/PlayerAsPlayACardNotification"
- $ref: "#/components/schemas/YourNewCardNotification"
- $ref: "#/components/schemas/PlayerHavePassNotification"
- $ref: "#/components/schemas/PilesShuffledNotification"
- $ref: "#/components/schemas/PlayerWinNotification"
discriminator:
propertyName: type
mapping:
eventDemo.contexts.game.application.notification.models.WelcomeToTheGameNotification: "#/components/schemas/WelcomeToTheGameNotification"
eventDemo.contexts.game.application.notification.models.PlayerAsJoinTheGameNotification: "#/components/schemas/PlayerAsJoinTheGameNotification"
eventDemo.contexts.game.application.notification.models.PlayerWasReadyNotification: "#/components/schemas/PlayerWasReadyNotification"
eventDemo.contexts.game.application.notification.models.TheGameWasStartedNotification: "#/components/schemas/TheGameWasStartedNotification"
eventDemo.contexts.game.application.notification.models.ItsTheTurnOfNotification: "#/components/schemas/ItsTheTurnOfNotification"
eventDemo.contexts.game.application.notification.models.PlayerAsPlayACardNotification: "#/components/schemas/PlayerAsPlayACardNotification"
eventDemo.contexts.game.application.notification.models.YourNewCardNotification: "#/components/schemas/YourNewCardNotification"
eventDemo.contexts.game.application.notification.models.PlayerHavePassNotification: "#/components/schemas/PlayerHavePassNotification"
eventDemo.contexts.game.application.notification.models.PilesShuffledNotification: "#/components/schemas/PilesShuffledNotification"
eventDemo.contexts.game.application.notification.models.PlayerWinNotification: "#/components/schemas/PlayerWinNotification"
WelcomeToTheGameNotification:
description: Sent to a player right after they join a game, listing all players currently in it.
type: object
required: [type, id, players]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.WelcomeToTheGameNotification]
id:
type: string
format: uuid
players:
type: array
items:
$ref: "#/components/schemas/Player"
PlayerAsJoinTheGameNotification:
description: Sent to the other players when a new player joins the game.
type: object
required: [type, id, player]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.PlayerAsJoinTheGameNotification]
id:
type: string
format: uuid
player:
$ref: "#/components/schemas/Player"
PlayerWasReadyNotification:
description: Sent to all players when a player marks themselves as ready.
type: object
required: [type, id, playerId]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.PlayerWasReadyNotification]
id:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
TheGameWasStartedNotification:
description: Sent to each player when the game starts, with their initial hand.
type: object
required: [type, id, hand]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.TheGameWasStartedNotification]
id:
type: string
format: uuid
hand:
type: array
items:
$ref: "#/components/schemas/Card"
ItsTheTurnOfNotification:
description: Sent to all players to indicate whose turn it now is.
type: object
required: [type, id, player]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.ItsTheTurnOfNotification]
id:
type: string
format: uuid
player:
$ref: "#/components/schemas/Player"
PlayerAsPlayACardNotification:
description: Sent to all players when a player plays a card.
type: object
required: [type, id, playerId, card]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.PlayerAsPlayACardNotification]
id:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
card:
$ref: "#/components/schemas/Card"
YourNewCardNotification:
description: Sent to a player with the cards they just drew from the draw pile.
type: object
required: [type, id, cards]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.YourNewCardNotification]
id:
type: string
format: uuid
cards:
type: array
items:
$ref: "#/components/schemas/Card"
PlayerHavePassNotification:
description: Sent to the other players when a player draws a card and passes their turn.
type: object
required: [type, id, playerId]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.PlayerHavePassNotification]
id:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
PilesShuffledNotification:
description: Sent to all players when the discard pile is reshuffled into the draw pile.
type: object
required: [type, id]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.PilesShuffledNotification]
id:
type: string
format: uuid
PlayerWinNotification:
description: Sent to all players when a player wins the game.
type: object
required: [type, id, playerId]
properties:
type:
type: string
enum: [eventDemo.contexts.game.application.notification.models.PlayerWinNotification]
id:
type: string
format: uuid
playerId:
$ref: "#/components/schemas/PlayerId"
@@ -3,6 +3,7 @@ package eventDemo.configuration
import io.ktor.server.config.ApplicationConfig import io.ktor.server.config.ApplicationConfig
data class Configuration( data class Configuration(
val redisUrl: String,
val jwtSecret: String, val jwtSecret: String,
val postgresql: Postgresql, val postgresql: Postgresql,
val rabbitmq: RabbitMQ, val rabbitmq: RabbitMQ,
@@ -24,6 +25,7 @@ data class Configuration(
val ApplicationConfig.configuration val ApplicationConfig.configuration
get() = get() =
Configuration( Configuration(
redisUrl = getProperty("redis.url"),
jwtSecret = getProperty("jwt.secret"), jwtSecret = getProperty("jwt.secret"),
postgresql = postgresql =
Configuration.Postgresql( Configuration.Postgresql(
@@ -7,6 +7,8 @@ import org.koin.core.module.Module
import org.koin.core.scope.Scope import org.koin.core.scope.Scope
import org.koin.core.scope.ScopeCallback import org.koin.core.scope.ScopeCallback
import org.koin.dsl.bind import org.koin.dsl.bind
import redis.clients.jedis.JedisPooled
import redis.clients.jedis.UnifiedJedis
import javax.sql.DataSource import javax.sql.DataSource
fun Module.configureDIDataSource(config: Configuration) { fun Module.configureDIDataSource(config: Configuration) {
@@ -24,6 +26,11 @@ fun Module.configureDIDataSource(config: Configuration) {
} }
} bind DataSource::class } bind DataSource::class
// Redis (for Projections)
single {
JedisPooled(config.redisUrl)
} bind UnifiedJedis::class
// RabbitMQ (for EventBus) // RabbitMQ (for EventBus)
factory { factory {
ConnectionFactory().apply { ConnectionFactory().apply {
@@ -37,8 +37,16 @@ abstract class GameEventManager(
return this return this
} }
protected inline fun <reified G : Game> Game.isStatusOrFail(message: String): G = protected fun <G : Game> Game.isStatusOrFail(
this as? G ?: throw CommandException(message) kClass: KClass<G>,
message: String,
): G {
if (kClass.isInstance(this)) {
return this as G
} else {
throw CommandException(message)
}
}
protected fun <T> retry( protected fun <T> retry(
mapAttempts: Int = 5, mapAttempts: Int = 5,
@@ -20,7 +20,7 @@ class JoinTheGameHandler(
retry { retry {
command command
.getGame() .getGame()
.isStatusOrFail<GameCreated>("The game is started") .isStatusOrFail(GameCreated::class, "The game is started")
.userJoinTheGame( .userJoinTheGame(
userId = command.userId, userId = command.userId,
name = user.username, name = user.username,
@@ -16,7 +16,7 @@ class PlayCardHandler(
override fun handle(command: PlayCardCommand) { override fun handle(command: PlayCardCommand) {
command command
.getGame() .getGame()
.isStatusOrFail<GameStarted>("The game is not started") .isStatusOrFail(GameStarted::class, "The game is not started")
.playTheCard( .playTheCard(
card = command.payload.card, card = command.payload.card,
playerId = command.payload.playerId, playerId = command.payload.playerId,
@@ -16,7 +16,7 @@ class ReadyToPlayHandler(
override fun handle(command: ReadyToPlayCommand) { override fun handle(command: ReadyToPlayCommand) {
command command
.getGame() .getGame()
.isStatusOrFail<GameCreated>("The game is started") .isStatusOrFail(GameCreated::class, "The game is started")
.setReadyPlayer(command.payload.playerId) .setReadyPlayer(command.payload.playerId)
.saveEvents() .saveEvents()
.publishEvents() .publishEvents()
@@ -18,7 +18,7 @@ class TakeCartFromDrawPileHandler(
override fun handle(command: TakeCartFromDrawPileCommand) { override fun handle(command: TakeCartFromDrawPileCommand) {
command command
.getGame() .getGame()
.isStatusOrFail<GameStarted>("The game is not started") .isStatusOrFail(GameStarted::class, "The game is not started")
.playerTakeCartFromDrawPile(command.payload.playerId, 1) .playerTakeCartFromDrawPile(command.payload.playerId, 1)
.saveEvents() .saveEvents()
.publishEvents() .publishEvents()
@@ -24,10 +24,6 @@ import eventDemo.contexts.game.domain.events.PlayerWinEvent
import eventDemo.contexts.game.domain.game.gameState.Game import eventDemo.contexts.game.domain.game.gameState.Game
import eventDemo.contexts.game.domain.game.gameState.GameStarted import eventDemo.contexts.game.domain.game.gameState.GameStarted
import eventDemo.sharedKernel.UserId import eventDemo.sharedKernel.UserId
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.withLoggingContext
private val logger = KotlinLogging.logger {}
fun GameEvent.toNotification( fun GameEvent.toNotification(
game: Game, game: Game,
@@ -35,16 +31,15 @@ fun GameEvent.toNotification(
): Iterable<Notification> = ): Iterable<Notification> =
Iterable { Iterable {
iterator { iterator {
val currentPlayerId = game.players.get(currentUserId).id
context(iterator: SequenceScope<Notification>) context(iterator: SequenceScope<Notification>)
suspend fun Notification.send() { suspend fun Notification.send() {
withLoggingContext("notification" to (this).toString()) {
logger.info { "Notification sent" }
iterator.yield(this) iterator.yield(this)
} }
}
fun PlayerActionEvent.isFromCurrentUser(): Boolean = fun PlayerActionEvent.isFromCurrentUser(): Boolean =
game.players.get(currentUserId).id == playerId currentPlayerId != playerId
when (this@toNotification) { when (this@toNotification) {
is GameCreatedEvent -> { is GameCreatedEvent -> {
@@ -56,7 +51,7 @@ fun GameEvent.toNotification(
} }
is NewPlayerEvent -> { is NewPlayerEvent -> {
if (this@toNotification.player.userId != currentUserId) { if (this@toNotification.isFromCurrentUser()) {
PlayerAsJoinTheGameNotification( PlayerAsJoinTheGameNotification(
player = this@toNotification.player, player = this@toNotification.player,
).send() ).send()
@@ -90,7 +85,6 @@ fun GameEvent.toNotification(
if (game is GameStarted) { if (game is GameStarted) {
ItsTheTurnOfNotification(player = game.nextPlayer) ItsTheTurnOfNotification(player = game.nextPlayer)
.send()
} }
} }
@@ -107,20 +101,21 @@ fun GameEvent.toNotification(
if (game is GameStarted) { if (game is GameStarted) {
ItsTheTurnOfNotification(player = game.nextPlayer) ItsTheTurnOfNotification(player = game.nextPlayer)
.send()
} }
} }
is PlayerReadyEvent -> { is PlayerReadyEvent -> {
if (this@toNotification.isFromCurrentUser()) {
PlayerWasReadyNotification( PlayerWasReadyNotification(
playerId = this@toNotification.playerId, playerId = this@toNotification.playerId,
).send() )
}
} }
is PlayerWinEvent -> { is PlayerWinEvent -> {
PlayerWinNotification( PlayerWinNotification(
playerId = this@toNotification.playerId, playerId = this@toNotification.playerId,
).send() )
} }
} }
} }
@@ -48,7 +48,7 @@ class ReactionListener(
} }
private fun sendWinnerEvent(game: Game) { private fun sendWinnerEvent(game: Game) {
if (game is GameStarted && game.lastPlayerId != null) { if (game is GameStarted) {
val lastPlayerWin = val lastPlayerWin =
game game
.players .players
@@ -4,7 +4,6 @@ import eventDemo.contexts.game.domain.game.DiscardPile
import eventDemo.contexts.game.domain.game.DrawPile import eventDemo.contexts.game.domain.game.DrawPile
import eventDemo.contexts.game.domain.game.GameId import eventDemo.contexts.game.domain.game.GameId
import eventDemo.contexts.game.domain.game.Player import eventDemo.contexts.game.domain.game.Player
import eventDemo.contexts.game.domain.game.PlayerHand
import eventDemo.contexts.game.infrastructure.persistence.serializers.EventIdSerializer import eventDemo.contexts.game.infrastructure.persistence.serializers.EventIdSerializer
import eventDemo.contexts.game.infrastructure.persistence.serializers.PlayerIdSerializer import eventDemo.contexts.game.infrastructure.persistence.serializers.PlayerIdSerializer
import eventDemo.libs.eventSource.EventId import eventDemo.libs.eventSource.EventId
@@ -21,7 +20,6 @@ data class GameStartedEvent(
override val aggregateId: GameId, override val aggregateId: GameId,
@Serializable(with = PlayerIdSerializer::class) @Serializable(with = PlayerIdSerializer::class)
val firstPlayer: Player.PlayerId, val firstPlayer: Player.PlayerId,
val playersHans: Map<Player.PlayerId, PlayerHand>,
val drawPile: DrawPile, val drawPile: DrawPile,
val discardPile: DiscardPile, val discardPile: DiscardPile,
override val version: Int, override val version: Int,
@@ -30,3 +28,9 @@ data class GameStartedEvent(
override val eventId: EventId = EventId(UUID.randomUUID()) override val eventId: EventId = EventId(UUID.randomUUID())
override val createdAt: Instant = Clock.System.now() override val createdAt: Instant = Clock.System.now()
} }
private var isDisabled = false
internal fun disableShuffleDeck() {
isDisabled = true
}
@@ -7,8 +7,6 @@ import kotlinx.serialization.Serializable
value class DrawPile( value class DrawPile(
val cards: Set<Card> = emptySet(), val cards: Set<Card> = emptySet(),
) { ) {
val size: Int get() = cards.size
fun take(number: Int): Pair<DrawPile, Set<Card>> = fun take(number: Int): Pair<DrawPile, Set<Card>> =
cards.drop(number).toDrawPile() to cards.take(number).toSet() cards.drop(number).toDrawPile() to cards.take(number).toSet()
@@ -25,7 +25,6 @@ data class Player(
} }
} }
@Serializable
class PlayerList( class PlayerList(
val players: Set<Player> = emptySet(), val players: Set<Player> = emptySet(),
) : Set<Player> by players { ) : Set<Player> by players {
@@ -39,8 +39,7 @@ sealed interface Game {
companion object { companion object {
fun loadFromHistory(events: Set<GameEvent>): Game = fun loadFromHistory(events: Set<GameEvent>): Game =
events events.fold(GameInit(events.first().aggregateId)) { game: Game, event ->
.fold(GameInit(events.first().aggregateId)) { game: Game, event ->
game.run { game.run {
when (event) { when (event) {
is GameCreatedEvent if this is GameInit -> applyEvent(event) is GameCreatedEvent if this is GameInit -> applyEvent(event)
@@ -61,13 +60,6 @@ sealed interface Game {
is DrawFilledWithDiscardEvent -> error("Game is end") is DrawFilledWithDiscardEvent -> error("Game is end")
} }
} }
}.let {
when (it) {
is GameInit -> it
is GameCreated -> it.copy(recordedEvents = emptySet())
is GameEnded -> it.copy(recordedEvents = emptySet())
is GameStarted -> it.copy(recordedEvents = emptySet())
}
} }
} }
} }
@@ -79,3 +71,18 @@ internal fun <T : GameEvent> T.checkState(
if (!block(this)) throw exception(this) if (!block(this)) throw exception(this)
return this return this
} }
/**
* recordedEvents versions must be ordered and incremental.
*/
internal fun Game.checkRecorderEventsConsistency() {
recordedEvents
.also { if (it.size != (it.lastOrNull()?.version ?: 0)) throw InconsistentEventVersionException(recordedEvents) }
.mapIndexed { index, event ->
(index + 1) == event.version
}.run {
if (any { !it }) {
throw InconsistentEventVersionException(recordedEvents)
}
}
}
@@ -9,7 +9,6 @@ import eventDemo.contexts.game.domain.game.DiscardPile
import eventDemo.contexts.game.domain.game.DrawPile import eventDemo.contexts.game.domain.game.DrawPile
import eventDemo.contexts.game.domain.game.GameId import eventDemo.contexts.game.domain.game.GameId
import eventDemo.contexts.game.domain.game.Player import eventDemo.contexts.game.domain.game.Player
import eventDemo.contexts.game.domain.game.PlayerHand
import eventDemo.contexts.game.domain.game.PlayerList import eventDemo.contexts.game.domain.game.PlayerList
import eventDemo.contexts.game.domain.game.errors.AllPlayerNotReadyException import eventDemo.contexts.game.domain.game.errors.AllPlayerNotReadyException
import eventDemo.contexts.game.domain.game.errors.DeckMissingCardsException import eventDemo.contexts.game.domain.game.errors.DeckMissingCardsException
@@ -24,28 +23,24 @@ data class GameCreated(
override val recordedEvents: Set<GameEvent>, override val recordedEvents: Set<GameEvent>,
override val version: Int, override val version: Int,
) : Game { ) : Game {
init {
checkRecorderEventsConsistency()
}
val allPlayerIsReady: Boolean val allPlayerIsReady: Boolean
get() { get() {
return playersStatus.isNotEmpty() && playersStatus.values.all { it == PlayerStatus.Ready } return playersStatus.values.all { it == PlayerStatus.Ready }
} }
fun startGame(deck: Deck = newDeck().shuffleDeck()): GameStarted { fun startGame(deck: Deck = newDeck().shuffleDeck()): GameStarted {
val (drawPile, discardPile, playersHands) = val (drawPile, discardPile) = initPiles(deck)
initPiles(deck)
.let { (drawPile, discardPile) ->
createHandsFromDrawPile(drawPile)
.let { (drawPile, playersHands) ->
Triple(drawPile, discardPile, playersHands)
}
}
return GameStartedEvent( return GameStartedEvent(
aggregateId = aggregateId, aggregateId = aggregateId,
firstPlayer = players.randomPlayer().id, firstPlayer = players.random().id,
version = version + 1, version = version + 1,
drawPile = drawPile, drawPile = drawPile,
discardPile = discardPile, discardPile = discardPile,
playersHans = playersHands,
).checkState( ).checkState(
{ players.size > 1 }, { players.size > 1 },
{ NeedMorePlayersToStartGameException(players) }, { NeedMorePlayersToStartGameException(players) },
@@ -55,8 +50,7 @@ data class GameCreated(
).checkState( ).checkState(
{ deck.size == 108 }, { deck.size == 108 },
{ DeckMissingCardsException(players, deck) }, { DeckMissingCardsException(players, deck) },
).also { if (it.drawPile.size + it.discardPile.size + playersHands.values.sumOf { it.size } != 108) error("missing cards!") } ).run(::applyEvent)
.run(::applyEvent)
} }
private fun initPiles(deck: Set<Card>): Pair<DrawPile, DiscardPile> = private fun initPiles(deck: Set<Card>): Pair<DrawPile, DiscardPile> =
@@ -67,20 +61,6 @@ data class GameCreated(
draw to DiscardPile(cards) draw to DiscardPile(cards)
} }
private fun createHandsFromDrawPile(drawPile: DrawPile) =
players
.map { it.id }
.fold(Pair(drawPile, emptyMap<Player.PlayerId, PlayerHand>())) { (drawAcc, handsAcc), playerId ->
drawAcc
.take(7)
.let { (draw, hand) ->
Pair(
draw,
handsAcc + (playerId to PlayerHand(hand)),
)
}
}
fun userJoinTheGame( fun userJoinTheGame(
userId: UserId, userId: UserId,
name: String, name: String,
@@ -127,13 +107,8 @@ data class GameCreated(
internal fun applyEvent(event: GameStartedEvent): GameStarted = internal fun applyEvent(event: GameStartedEvent): GameStarted =
GameStarted( GameStarted(
aggregateId = event.aggregateId, aggregateId = event.aggregateId,
players = players = players,
players lastPlayerId = event.firstPlayer,
.map {
it.copy(hand = event.playersHans[it.id] ?: error("Player ${it.id} not found"))
}.let { PlayerList(it.toSet()) },
lastPlayerId = null,
nextPlayerId = event.firstPlayer,
drawPile = event.drawPile, drawPile = event.drawPile,
discardPile = event.discardPile, discardPile = event.discardPile,
version = event.version + 1, version = event.version + 1,
@@ -158,22 +133,7 @@ fun newDeck(): Deck =
(1..2).map { Card.PassCard(color) } (1..2).map { Card.PassCard(color) }
}.let { }.let {
it + (1..4).map { Card.Plus4Card() } it + (1..4).map { Card.Plus4Card() }
}.let {
it + (1..4).map { Card.ChangeColorCard() }
}.toSet() }.toSet()
fun Set<Card>.shuffleDeck(): Set<Card> { fun Set<Card>.shuffleDeck() =
if (isDisabled) return this shuffled().toSet()
return shuffled().toSet()
}
private fun PlayerList.randomPlayer(): Player {
if (isDisabled) return first()
return random()
}
private var isDisabled = false
fun disableRandomForTest() {
isDisabled = true
}
@@ -16,5 +16,6 @@ data class GameEnded(
if (!players.map { it.id }.containsAll(playerWins)) { if (!players.map { it.id }.containsAll(playerWins)) {
throw IllegalArgumentException("Player ${players.map { it.id }} were not in players") throw IllegalArgumentException("Player ${players.map { it.id }} were not in players")
} }
checkRecorderEventsConsistency()
} }
} }
@@ -23,14 +23,41 @@ import eventDemo.contexts.game.domain.game.errors.ThePlayerIsNotInTheGameExcepti
import eventDemo.contexts.game.domain.game.errors.ThePlayerMustPlayACardException import eventDemo.contexts.game.domain.game.errors.ThePlayerMustPlayACardException
import eventDemo.contexts.game.domain.game.gameState.Game.Direction import eventDemo.contexts.game.domain.game.gameState.Game.Direction
fun PlayerList.nextPlayerTurn( data class GameStarted(
lastPlayerId: Player.PlayerId, override val aggregateId: GameId,
direction: Direction, override val players: PlayerList,
): Player.PlayerId { val drawPile: DrawPile,
val lastPlayer = get(lastPlayerId) val discardPile: DiscardPile,
val playersLastTurn = filter { it.hand.cards.isNotEmpty() || it == lastPlayer } val lastPlayerId: Player.PlayerId,
val currentColor: Color,
val playedTurnHistory: List<History> = emptyList(),
val direction: Direction = Direction.CLOCKWISE,
val playerWins: Set<Player.PlayerId> = emptySet(),
override val version: Int,
override val recordedEvents: Set<GameEvent>,
) : Game {
val playersInGame by lazy { players.filter { it.hand.cards.isNotEmpty() } }
return playersLastTurn val lastPlayedCard: Card? by lazy { discardPile.topCard }
val lastPlayed: Player by lazy { players.get(lastPlayerId) }
init {
checkRecorderEventsConsistency()
}
data class History(
val playerId: Player.PlayerId,
val event: GameEvent,
val direction: Direction,
)
val lastPlayer by lazy { players.get(lastPlayerId) }
val nextPlayer: Player by lazy {
val playersLastTurn = players.filter { it.hand.cards.isNotEmpty() || it == lastPlayer }
playersLastTurn
.indexOf(lastPlayer) .indexOf(lastPlayer)
.let { lastPlayerIndex -> .let { lastPlayerIndex ->
if (direction == Direction.CLOCKWISE) { if (direction == Direction.CLOCKWISE) {
@@ -46,36 +73,8 @@ fun PlayerList.nextPlayerTurn(
lastPlayerIndex - 1 lastPlayerIndex - 1
} }
} }
}.let { nextPlayerIndex -> elementAt(nextPlayerIndex).id } }.let { nextPlayerIndex -> players.elementAt(nextPlayerIndex) }
} }
data class GameStarted(
override val aggregateId: GameId,
override val players: PlayerList,
val drawPile: DrawPile,
val discardPile: DiscardPile,
val lastPlayerId: Player.PlayerId?,
val nextPlayerId: Player.PlayerId,
val currentColor: Color,
val playedTurnHistory: List<History> = emptyList(),
val direction: Direction = Direction.CLOCKWISE,
val playerWins: Set<Player.PlayerId> = emptySet(),
override val version: Int,
override val recordedEvents: Set<GameEvent>,
) : Game {
val playersInGame by lazy { players.filter { it.hand.cards.isNotEmpty() } }
val lastPlayedCard: Card? by lazy { discardPile.topCard }
data class History(
val playerId: Player.PlayerId,
val event: GameEvent,
val direction: Direction,
)
val lastPlayer: Player? by lazy { lastPlayerId?.let { players.get(it) } }
val nextPlayer: Player by lazy { players.get(nextPlayerId) }
fun canBePlayThisCard(card: Card): Boolean { fun canBePlayThisCard(card: Card): Boolean {
val cardOnBoard = discardPile.topCard ?: return false val cardOnBoard = discardPile.topCard ?: return false
@@ -145,12 +144,8 @@ data class GameStarted(
): GameStarted = ): GameStarted =
CardIsPlayedEvent(aggregateId, card, playerId, chosenColor, version + 1) CardIsPlayedEvent(aggregateId, card, playerId, chosenColor, version + 1)
.checkPlayerTurn() .checkPlayerTurn()
.checkState({ .checkState({ card is Card.CardWithColor && chosenColor != null }, { TheCardIsAColorCardException(playerId) })
(card is Card.CardWithColor && chosenColor == null) || card is Card.CardWith4Color .checkState({ card is Card.CardWith4Color && chosenColor == null }, { TheCardHasNoColorException(playerId) })
}, { TheCardIsAColorCardException(playerId) })
.checkState({
(card is Card.CardWith4Color && chosenColor != null) || card is Card.CardWithColor
}, { TheCardHasNoColorException(playerId) })
.run(::applyEvent) .run(::applyEvent)
internal fun applyEvent(event: CardIsPlayedEvent): GameStarted = internal fun applyEvent(event: CardIsPlayedEvent): GameStarted =
@@ -172,7 +167,6 @@ data class GameStarted(
discardPile = discardPile.withNewCard(card = event.card), discardPile = discardPile.withNewCard(card = event.card),
currentColor = color, currentColor = color,
lastPlayerId = event.playerId, lastPlayerId = event.playerId,
nextPlayerId = players.nextPlayerTurn(event.playerId, nextDirectionAfterPlay),
playedTurnHistory = playedTurnHistory - History(event.playerId, event, direction), playedTurnHistory = playedTurnHistory - History(event.playerId, event, direction),
direction = nextDirectionAfterPlay, direction = nextDirectionAfterPlay,
version = event.version, version = event.version,
@@ -208,8 +202,6 @@ data class GameStarted(
copy( copy(
players = players.withNewCardOnPlayerHand(event.playerId, event.takenCards), players = players.withNewCardOnPlayerHand(event.playerId, event.takenCards),
drawPile = drawPile.take(event.takenCards.size).first, drawPile = drawPile.take(event.takenCards.size).first,
lastPlayerId = event.playerId,
nextPlayerId = players.nextPlayerTurn(event.playerId, direction),
version = event.version, version = event.version,
recordedEvents = recordedEvents + event, recordedEvents = recordedEvents + event,
) )
@@ -16,7 +16,7 @@ fun Route.gameWebSocket(channelSubscriber: GameChannelsSubscriber) {
authenticate { authenticate {
webSocket("/games/{id}") { webSocket("/games/{id}") {
channelSubscriber.subscribePlayerToGameChannels( channelSubscriber.subscribePlayerToGameChannels(
gameId = GameId(UUID.fromString(call.parameters["id"]!!)), gameId = GameId(UUID.nameUUIDFromBytes(call.parameters["id"]?.encodeToByteArray()!!)),
userId = call.currentUserId, userId = call.currentUserId,
incomingCommandChannel = toObjectChannel(incoming), incomingCommandChannel = toObjectChannel(incoming),
sendNotificationChannel = fromFrameChannel(outgoing), sendNotificationChannel = fromFrameChannel(outgoing),
@@ -78,11 +78,7 @@ class BusInRabbitMQ<E>(
body: ByteArray, body: ByteArray,
) { ) {
runBlocking { runBlocking {
val obj = stringToObject(body.toString(Charsets.UTF_8)) block(stringToObject(body.toString(Charsets.UTF_8)))
withLoggingContext("item" to obj.toString()) {
logger.info { "Received delivery of $exchangeName" }
}
block(obj)
} }
channel.basicAck(envelope.deliveryTag, false) channel.basicAck(envelope.deliveryTag, false)
} }
+5
View File
@@ -12,6 +12,11 @@ jwt {
secret = ${?JWT_SECRET} secret = ${?JWT_SECRET}
} }
redis {
url = "redis://localhost:6379"
url = ${?REDIS_URL}
}
postgresql { postgresql {
url = "jdbc:postgresql://localhost:5432/event-demo" url = "jdbc:postgresql://localhost:5432/event-demo"
url = ${?POSTGRESQL_URL} url = ${?POSTGRESQL_URL}
+2
View File
@@ -7,5 +7,7 @@ object Tag {
object RabbitMQ : Tag() object RabbitMQ : Tag()
object Redis : Tag()
object Concurrence : Tag() object Concurrence : Tag()
} }
@@ -6,18 +6,12 @@ import com.tngtech.archunit.library.Architectures.layeredArchitecture
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
/** /**
* Vérifie le respect des frontières de l'architecture hexagonale (ports & adapters), * Vérifie le respect des frontières de l'architecture hexagonale (ports & adapters).
* pour chaque bounded context sous `eventDemo.contexts`.
* *
* Les contexts ne sont pas listés en dur : ils sont déduits des classes réellement * Convention attendue :
* présentes sous `eventDemo.contexts.*`, de sorte que l'ajout d'un nouveau context * eventDemo.contexts.uno.domain
* (nouveau dossier `eventDemo.contexts.<xxx>`) soit automatiquement couvert par ce test, * eventDemo.contexts.uno.application
* sans modification de ce fichier. * eventDemo.contexts.uno.infrastructure
*
* Convention attendue, pour un contexte donné :
* eventDemo.contexts.<context>.domain
* eventDemo.contexts.<context>.application
* eventDemo.contexts.<context>.infrastructure
* *
* Règles imposées : * Règles imposées :
* domain → ne dépend d'aucune autre couche (ni application, ni infrastructure) * domain → ne dépend d'aucune autre couche (ni application, ni infrastructure)
@@ -25,32 +19,15 @@ import org.junit.jupiter.api.Test
* infrastructure → ne dépend que de domain et application * infrastructure → ne dépend que de domain et application
*/ */
class HexagonalArchitectureTest { class HexagonalArchitectureTest {
private val rootPackage = "eventDemo.contexts" private val basePackage = "eventDemo.contexts.uno"
private val classes = private val classes =
ClassFileImporter() ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages(rootPackage) .importPackages(basePackage)
// Premier segment de package après "eventDemo.contexts." (ex. "auth", "game", ...),
// recalculé à chaque exécution à partir des classes importées.
private val contexts: Set<String> =
classes
.map { it.packageName }
.filter { it.startsWith("$rootPackage.") }
.map { it.removePrefix("$rootPackage.").substringBefore('.') }
.toSet()
@Test @Test
fun `respecte les couches de l'architecture hexagonale`() { fun `respecte les couches de l'architecture hexagonale`() {
check(contexts.isNotEmpty()) {
"Aucun context trouvé sous `$rootPackage` : le test ne vérifie rien, " +
"vérifiez que le package racine est correct."
}
contexts.forEach { context ->
val basePackage = "$rootPackage.$context"
@Suppress("ktlint:standard:chain-method-continuation") @Suppress("ktlint:standard:chain-method-continuation")
layeredArchitecture() layeredArchitecture()
.consideringAllDependencies() .consideringAllDependencies()
@@ -62,5 +39,4 @@ class HexagonalArchitectureTest {
.whereLayer("Infrastructure").mayOnlyAccessLayers("Domain", "Application") .whereLayer("Infrastructure").mayOnlyAccessLayers("Domain", "Application")
.check(classes) .check(classes)
} }
}
} }
@@ -1,8 +1,6 @@
package eventDemo.contexts.game.application package eventDemo.contexts.game.application
import eventDemo.Tag import eventDemo.Tag
import eventDemo.contexts.auth.application.eventStores.UserRepository
import eventDemo.contexts.auth.domain.User
import eventDemo.contexts.game.application.channels.GameChannelsSubscriber import eventDemo.contexts.game.application.channels.GameChannelsSubscriber
import eventDemo.contexts.game.application.command.models.GameCommand import eventDemo.contexts.game.application.command.models.GameCommand
import eventDemo.contexts.game.application.eventStores.GameRepository import eventDemo.contexts.game.application.eventStores.GameRepository
@@ -13,11 +11,11 @@ import eventDemo.contexts.game.application.notification.models.PlayerAsPlayACard
import eventDemo.contexts.game.application.notification.models.PlayerWasReadyNotification import eventDemo.contexts.game.application.notification.models.PlayerWasReadyNotification
import eventDemo.contexts.game.application.notification.models.TheGameWasStartedNotification import eventDemo.contexts.game.application.notification.models.TheGameWasStartedNotification
import eventDemo.contexts.game.application.notification.models.WelcomeToTheGameNotification import eventDemo.contexts.game.application.notification.models.WelcomeToTheGameNotification
import eventDemo.contexts.game.domain.events.disableShuffleDeck
import eventDemo.contexts.game.domain.game.Card import eventDemo.contexts.game.domain.game.Card
import eventDemo.contexts.game.domain.game.GameId import eventDemo.contexts.game.domain.game.GameId
import eventDemo.contexts.game.domain.game.gameState.Game import eventDemo.contexts.game.domain.game.gameState.Game
import eventDemo.contexts.game.domain.game.gameState.GameStarted import eventDemo.contexts.game.domain.game.gameState.GameStarted
import eventDemo.contexts.game.domain.game.gameState.disableRandomForTest
import eventDemo.testHelpers.CreateGameWithCommandsInChannelsHelpers.createGameWithCommandsInChannels import eventDemo.testHelpers.CreateGameWithCommandsInChannelsHelpers.createGameWithCommandsInChannels
import eventDemo.testHelpers.CreateGameWithCommandsInChannelsHelpers.joinTheGame import eventDemo.testHelpers.CreateGameWithCommandsInChannelsHelpers.joinTheGame
import eventDemo.testHelpers.CreateGameWithCommandsInChannelsHelpers.playCard import eventDemo.testHelpers.CreateGameWithCommandsInChannelsHelpers.playCard
@@ -26,14 +24,8 @@ import eventDemo.testHelpers.createNewUser
import eventDemo.testHelpers.testKoinApplicationWithConfig import eventDemo.testHelpers.testKoinApplicationWithConfig
import io.kotest.assertions.nondeterministic.eventually import io.kotest.assertions.nondeterministic.eventually
import io.kotest.assertions.nondeterministic.until import io.kotest.assertions.nondeterministic.until
import io.kotest.assertions.retry
import io.kotest.core.spec.style.FunSpec import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.equals.shouldBeEqual import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.equals.shouldEqual
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.should
import io.kotest.matchers.types.shouldBeInstanceOf
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
@@ -51,9 +43,8 @@ class GameSimulationTest :
tags(Tag.Postgresql) tags(Tag.Postgresql)
test("Simulation of a game") { test("Simulation of a game") {
should { withTimeout(10.seconds) {
retry(maxRetry = 3, timeout = 20.seconds) { disableShuffleDeck()
disableRandomForTest()
val gameId = GameId() val gameId = GameId()
val user1 = createNewUser("user1") val user1 = createNewUser("user1")
val user2 = createNewUser("user2") val user2 = createNewUser("user2")
@@ -70,11 +61,6 @@ class GameSimulationTest :
testKoinApplicationWithConfig { testKoinApplicationWithConfig {
val gameRepository = get<GameRepository>() val gameRepository = get<GameRepository>()
val userRepository = get<UserRepository>()
userRepository.run {
save(user1)
save(user2)
}
gameRepository.create(gameId) gameRepository.create(gameId)
@@ -118,9 +104,8 @@ class GameSimulationTest :
// Player 1 actions // Player 1 actions
val player1Job = val player1Job =
launch { launch {
createGameWithCommandsInChannels(channelCommand1, gameId, user1) { createGameWithCommandsInChannels(channelCommand1) { getPlayer ->
user1.joinTheGame()
joinTheGame()
player1Notifications.waitNotification<WelcomeToTheGameNotification> { player1Notifications.waitNotification<WelcomeToTheGameNotification> {
players.map { it.userId }.contains(user1.id) players.map { it.userId }.contains(user1.id)
} }
@@ -131,38 +116,20 @@ class GameSimulationTest :
player.userId == user2.id player.userId == user2.id
} }
readyToPlay() getPlayer(user1).readyToPlay()
player1Notifications.waitNotification<PlayerWasReadyNotification> { player1Notifications.waitNotification<PlayerWasReadyNotification> {
playerId == getPlayer(user2).id playerId == getPlayer(user2).id
} }
playedCard1 = val player1Hand =
player1Notifications player1Notifications.waitNotification<TheGameWasStartedNotification> { hand.size == 7 }.hand
.waitNotification<TheGameWasStartedNotification> { hand.size == 7 }
.hand
.first()
.apply {
this.shouldBeInstanceOf<Card.NumericCard>()
number shouldEqual 1
color shouldEqual Card.Color.Red
}
playedCard1 = player1Hand.first()
player1Notifications.waitNotification<ItsTheTurnOfNotification> { player1Notifications.waitNotification<ItsTheTurnOfNotification> {
if (player.userId == user2.id) error("WRONG PLAYER TURN")
player.userId == user1.id player.userId == user1.id
} }
game getPlayer(user1).playCard(playedCard1!!, null)
.shouldBeInstanceOf<GameStarted>()
.discardPile
.topCard
.shouldNotBeNull()
.shouldBeInstanceOf<Card.NumericCard> {
it.number shouldEqual 0
it.color shouldEqual Card.Color.Red
}
playCard(playedCard1!!)
player1Notifications.waitNotification<ItsTheTurnOfNotification> { player1Notifications.waitNotification<ItsTheTurnOfNotification> {
player == getPlayer(user2) player == getPlayer(user2)
@@ -172,18 +139,8 @@ class GameSimulationTest :
playerId == getPlayer(user2).id && card == playedCard2 playerId == getPlayer(user2).id && card == playedCard2
} }
playedCard1 = playedCard1 = player1Hand.elementAt(1)
assertInstanceOf<GameStarted>(game) getPlayer(user1).playCard(playedCard1)
.playableCards(currentPlayer.id)
.first()
playedCard1.run {
this.shouldBeInstanceOf<Card.NumericCard>()
number shouldEqual 2
color shouldEqual Card.Color.Red
}
playCard(playedCard1)
player1Notifications.waitNotification<ItsTheTurnOfNotification> { player1Notifications.waitNotification<ItsTheTurnOfNotification> {
player == getPlayer(user2) player == getPlayer(user2)
@@ -194,11 +151,11 @@ class GameSimulationTest :
// Player 2 actions // Player 2 actions
val player2Job = val player2Job =
launch { launch {
createGameWithCommandsInChannels(channelCommand2, gameId, user2) { createGameWithCommandsInChannels(channelCommand2) { getPlayer ->
// wait player 1 has joined the game // wait player 1 has joined the game
until(3.seconds) { player1HasJoin } until(3.seconds) { player1HasJoin }
joinTheGame() user2.joinTheGame()
player2Notifications.waitNotification<WelcomeToTheGameNotification> { player2Notifications.waitNotification<WelcomeToTheGameNotification> {
players.map { it.userId }.contains(user1.id) && players.map { it.userId }.contains(user1.id) &&
@@ -206,48 +163,30 @@ class GameSimulationTest :
} }
player2Notifications.waitNotification<PlayerWasReadyNotification> { playerId == getPlayer(user1).id } player2Notifications.waitNotification<PlayerWasReadyNotification> { playerId == getPlayer(user1).id }
readyToPlay() getPlayer(user2).readyToPlay()
playedCard2 = val player2Hand =
player2Notifications player2Notifications.waitNotification<TheGameWasStartedNotification> { hand.size == 7 }.hand
.waitNotification<TheGameWasStartedNotification> { hand.size == 7 }
.hand
.first()
.apply {
this.shouldBeInstanceOf<Card.NumericCard>()
number shouldEqual 8
color shouldEqual Card.Color.Red
}
player2Notifications.waitNotification<ItsTheTurnOfNotification> { player2Notifications.waitNotification<ItsTheTurnOfNotification> {
if (player.userId == user2.id) error("WRONG PLAYER TURN")
player.userId == user1.id player.userId == user1.id
} }
player2Notifications.waitNotification<PlayerAsPlayACardNotification> { player2Notifications.waitNotification<PlayerAsPlayACardNotification> {
playerId == getPlayer(user1).id && card == playedCard1 playerId == getPlayer(user1).id && card == playedCard1
} }
playedCard2 = player2Hand.first()
player2Notifications.waitNotification<ItsTheTurnOfNotification> { player2Notifications.waitNotification<ItsTheTurnOfNotification> {
player == currentPlayer player == getPlayer(user2)
} }
game getPlayer(user2).playCard(playedCard2)
.shouldBeInstanceOf<GameStarted>()
.discardPile
.topCard
.shouldNotBeNull()
.shouldBeInstanceOf<Card.NumericCard> {
it.number shouldEqual 1
it.color shouldEqual Card.Color.Red
}
playCard(playedCard2)
player2Notifications.waitNotification<ItsTheTurnOfNotification> { player2Notifications.waitNotification<ItsTheTurnOfNotification> {
player.userId == user1.id player.userId == user1.id
} }
player2Notifications.waitNotification<PlayerAsPlayACardNotification> { player2Notifications.waitNotification<PlayerAsPlayACardNotification> {
playerId == currentPlayer.id && card == playedCard2 playerId == getPlayer(user2).id && card == playedCard2
} }
} }
} }
@@ -261,26 +200,20 @@ class GameSimulationTest :
// Check if the state is correct // Check if the state is correct
game.aggregateId shouldBeEqual gameId game.aggregateId shouldBeEqual gameId
game.players.map { it.userId } shouldContainExactly setOf(user1.id, user2.id) game.players.map { it.id } shouldBeEqual setOf(user1.id, user2.id)
assertNotNull(game.players.find { it.userId == user1.id }) assertNotNull(game.players.find { it.userId == user1.id })
.hand.size shouldBeEqual 5 .hand.size shouldBeEqual 5
assertNotNull(game.players.find { it.userId == user2.id }) assertNotNull(game.players.find { it.userId == user2.id })
.hand.size shouldBeEqual 6 .hand.size shouldBeEqual 6
game.direction shouldBeEqual Game.Direction.CLOCKWISE game.direction shouldBeEqual Game.Direction.CLOCKWISE
assertNotNull(game.lastPlayer?.userId) shouldBeEqual user1.id assertNotNull(game.lastPlayed.userId) shouldBeEqual user1
assertNotNull(game.discardPile.topCard) shouldBeEqual assertNotNull(playedCard1) assertNotNull(game.discardPile.topCard) shouldBeEqual assertNotNull(playedCard1)
} }
} }
} }
}
}) })
context(user: User) private suspend inline fun <reified T : Notification> MutableList<Notification>.waitNotification(crossinline block: T.() -> Boolean): T =
private suspend inline fun <reified T : Notification> MutableList<Notification>.waitNotification(crossinline block: T.() -> Boolean): T { eventually(3.seconds) {
println("NOTIFICATION WAITING: ${T::class.simpleName} for user: ${user.username}") filterIsInstance<T>().first { block(it) }
return eventually(5.seconds) { }
filterIsInstance<T>()
.first { block(it) }
.also { remove(it) }
}.also { println("NOTIFICATION RECEIVED: ${T::class.simpleName} for user: ${user.username}") }
}
@@ -1,7 +1,6 @@
package eventDemo.contexts.game.application.notification package eventDemo.contexts.game.application.notification
import eventDemo.contexts.auth.application.eventStores.UserEventStoreRepository import eventDemo.contexts.auth.application.eventStores.UserEventStoreRepository
import eventDemo.contexts.auth.application.eventStores.UserRepository
import eventDemo.contexts.auth.infrastructure.persistence.eventStore.UserEventStoreInMemory import eventDemo.contexts.auth.infrastructure.persistence.eventStore.UserEventStoreInMemory
import eventDemo.contexts.game.application.command.handlers.GameCommandHandlerDispatcher import eventDemo.contexts.game.application.command.handlers.GameCommandHandlerDispatcher
import eventDemo.contexts.game.application.command.handlers.JoinTheGameHandler import eventDemo.contexts.game.application.command.handlers.JoinTheGameHandler
@@ -15,8 +14,6 @@ import eventDemo.contexts.game.application.notification.models.WelcomeToTheGameN
import eventDemo.contexts.game.infrastructure.persistence.eventBus.GameEventBusInMemory import eventDemo.contexts.game.infrastructure.persistence.eventBus.GameEventBusInMemory
import eventDemo.contexts.game.infrastructure.persistence.eventStore.GameEventStoreInMemory import eventDemo.contexts.game.infrastructure.persistence.eventStore.GameEventStoreInMemory
import eventDemo.sharedKernel.UserId import eventDemo.sharedKernel.UserId
import eventDemo.testHelpers.createNewUser
import io.kotest.assertions.nondeterministic.eventually
import io.kotest.core.spec.style.FunSpec import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.equals.shouldEqual import io.kotest.matchers.equals.shouldEqual
@@ -24,7 +21,6 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.junit.jupiter.api.assertInstanceOf import org.junit.jupiter.api.assertInstanceOf
import kotlin.time.Duration.Companion.seconds
class EventToNotificationSubscriberTest : class EventToNotificationSubscriberTest :
FunSpec({ FunSpec({
@@ -50,12 +46,8 @@ class EventToNotificationSubscriberTest :
val game = gameRepository.create() val game = gameRepository.create()
val user1 = createNewUser("user1") val user1 = UserId()
val user2 = createNewUser("user2") val user2 = UserId()
userRepository.run {
save(user1)
save(user2)
}
val player1Notifications = mutableListOf<Notification>() val player1Notifications = mutableListOf<Notification>()
GlobalScope.launch { GlobalScope.launch {
@@ -64,22 +56,20 @@ class EventToNotificationSubscriberTest :
} }
} }
commentDispatcher.dispatch(JoinTheGameCommand(user1.id, JoinTheGameCommand.Payload(game.aggregateId))) commentDispatcher.dispatch(JoinTheGameCommand(user1, JoinTheGameCommand.Payload(game.aggregateId)))
subscriber subscriber
.subscribeToEventsAndSendNotification( .subscribeToEventsAndSendNotification(
game.aggregateId, game.aggregateId,
user2.id, user1,
notificationChannel, notificationChannel,
).use { ).use {
commentDispatcher.dispatch(JoinTheGameCommand(user2.id, JoinTheGameCommand.Payload(game.aggregateId))) commentDispatcher.dispatch(JoinTheGameCommand(user2, JoinTheGameCommand.Payload(game.aggregateId)))
} }
eventually(duration = 1.seconds) { player1Notifications.size shouldEqual 2
player1Notifications.size shouldEqual 1
}
player1Notifications.first().let { notification -> player1Notifications.first().let { notification ->
assertInstanceOf<WelcomeToTheGameNotification>(notification) assertInstanceOf<WelcomeToTheGameNotification>(notification)
notification.players.map { it.userId } shouldContain user2.id notification.players.map { it.userId } shouldContain user1
} }
} }
}) })
@@ -1,301 +0,0 @@
package eventDemo.contexts.game.application.notification
import eventDemo.contexts.game.application.notification.models.ItsTheTurnOfNotification
import eventDemo.contexts.game.application.notification.models.PilesShuffledNotification
import eventDemo.contexts.game.application.notification.models.PlayerAsPlayACardNotification
import eventDemo.contexts.game.application.notification.models.PlayerHavePassNotification
import eventDemo.contexts.game.application.notification.models.PlayerWasReadyNotification
import eventDemo.contexts.game.application.notification.models.TheGameWasStartedNotification
import eventDemo.contexts.game.application.notification.models.WelcomeToTheGameNotification
import eventDemo.contexts.game.application.notification.models.YourNewCardNotification
import eventDemo.contexts.game.domain.events.CardIsPlayedEvent
import eventDemo.contexts.game.domain.events.DrawFilledWithDiscardEvent
import eventDemo.contexts.game.domain.events.GameStartedEvent
import eventDemo.contexts.game.domain.events.NewPlayerEvent
import eventDemo.contexts.game.domain.events.PlayerHaveDrawCardEvent
import eventDemo.contexts.game.domain.events.PlayerReadyEvent
import eventDemo.contexts.game.domain.game.Card
import eventDemo.contexts.game.domain.game.DiscardPile
import eventDemo.contexts.game.domain.game.DrawPile
import eventDemo.contexts.game.domain.game.GameId
import eventDemo.contexts.game.domain.game.Player
import eventDemo.contexts.game.domain.game.PlayerHand
import eventDemo.contexts.game.domain.game.PlayerList
import eventDemo.contexts.game.domain.game.gameState.GameCreated
import eventDemo.contexts.game.domain.game.gameState.GameStarted
import eventDemo.sharedKernel.UserId
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.assertInstanceOf
class ToNotificationTest :
FunSpec({
val player1 =
Player(
name = "Bob",
userId = UserId(),
hand = PlayerHand(setOf(Card.NumericCard(1, Card.Color.Red))),
id = Player.PlayerId(),
)
val player2 =
Player(
name = "John",
userId = UserId(),
hand = PlayerHand(setOf(Card.NumericCard(1, Card.Color.Red))),
id = Player.PlayerId(),
)
test("NewPlayerEvent") {
val game =
GameCreated(
aggregateId = GameId(),
version = 1,
players = PlayerList(setOf(player1)),
recordedEvents = setOf(),
)
NewPlayerEvent(
game.aggregateId,
version = 2,
player = player1,
).toNotification(
game = game,
currentUserId = player1.userId,
).let {
it.toList().size shouldBe 1
// Check if the user is
assertInstanceOf<WelcomeToTheGameNotification>(it.first()).run {
players.size shouldBe 1
players.first().name shouldBe "Bob"
}
}
}
test("PlayerReadyEvent") {
val game =
GameCreated(
aggregateId = GameId(),
version = 1,
players = PlayerList(setOf(player1)),
recordedEvents = setOf(),
)
PlayerReadyEvent(
game.aggregateId,
version = 2,
playerId = player1.id,
).toNotification(
game = game,
currentUserId = player1.userId,
).let {
it.toList().size shouldBe 1
assertInstanceOf<PlayerWasReadyNotification>(it.first()).let {
it.playerId shouldBe player1.id
}
}
}
test("PlayerHaveDrawCardEvent on current player") {
val game =
GameStarted(
aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)),
drawPile = DrawPile(),
discardPile = DiscardPile(),
lastPlayerId = player1.id,
nextPlayerId = player2.id,
currentColor = Card.Color.Red,
version = 1,
recordedEvents = setOf(),
)
val card = Card.NumericCard(1, Card.Color.Blue)
PlayerHaveDrawCardEvent(
game.aggregateId,
version = 2,
playerId = player1.id,
takenCards = setOf(card),
).toNotification(
game = game,
currentUserId = player1.userId,
).let {
it.toList().size shouldBe 2
it.toList().let { notifications ->
assertInstanceOf<YourNewCardNotification>(notifications.first()).let {
it.cards.first() shouldBe card
}
assertInstanceOf<ItsTheTurnOfNotification>(notifications[1]).let {
it.player.id shouldBe player2.id
}
}
}
}
test("PlayerHaveDrawCardEvent on other player") {
val game =
GameStarted(
aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)),
drawPile = DrawPile(),
discardPile = DiscardPile(),
lastPlayerId = player1.id,
nextPlayerId = player2.id,
currentColor = Card.Color.Red,
version = 1,
recordedEvents = setOf(),
)
val card = Card.NumericCard(1, Card.Color.Blue)
PlayerHaveDrawCardEvent(
game.aggregateId,
version = 2,
playerId = player1.id,
takenCards = setOf(card),
).toNotification(
game = game,
currentUserId = player2.userId,
).let {
it.toList().size shouldBe 2
it.toList().let { notifications ->
assertInstanceOf<PlayerHavePassNotification>(notifications.first()).let {
it.playerId shouldBe player1.id
}
assertInstanceOf<ItsTheTurnOfNotification>(notifications[1]).let {
it.player.id shouldBe player2.id
}
}
}
}
test("CardIsPlayedEvent on current player") {
val game =
GameStarted(
aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)),
drawPile = DrawPile(),
discardPile = DiscardPile(),
lastPlayerId = player2.id,
nextPlayerId = player1.id,
currentColor = Card.Color.Red,
version = 1,
recordedEvents = setOf(),
)
val card = Card.NumericCard(1, Card.Color.Blue)
CardIsPlayedEvent(
game.aggregateId,
version = 2,
playerId = player1.id,
card = card,
).toNotification(
game = game,
currentUserId = player1.userId,
).toList()
.let { notifications ->
notifications.size shouldBe 2
assertInstanceOf<PlayerAsPlayACardNotification>(notifications.first()).let {
it.playerId shouldBe player1.id
it.card shouldBe card
}
assertInstanceOf<ItsTheTurnOfNotification>(notifications[1]).let {
it.player.id shouldBe player1.id
}
}
}
test("CardIsPlayedEvent on other player") {
val game =
GameStarted(
aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)),
drawPile = DrawPile(),
discardPile = DiscardPile(),
lastPlayerId = player1.id,
nextPlayerId = player2.id,
currentColor = Card.Color.Red,
version = 1,
recordedEvents = setOf(),
)
val card = Card.NumericCard(1, Card.Color.Blue)
CardIsPlayedEvent(
game.aggregateId,
version = 2,
playerId = player2.id,
card = card,
).toNotification(
game = game,
currentUserId = player1.userId,
).let {
it.toList().size shouldBe 2
it.toList().let { notifications ->
assertInstanceOf<PlayerAsPlayACardNotification>(notifications.first()).let {
it.playerId shouldBe player2.id
it.card shouldBe card
}
assertInstanceOf<ItsTheTurnOfNotification>(notifications[1]).let {
it.player.id shouldBe player2.id
}
}
}
}
test("DrawFilledWithDiscardEvent") {
val game =
GameStarted(
aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)),
drawPile = DrawPile(),
discardPile = DiscardPile(),
lastPlayerId = player1.id,
nextPlayerId = player2.id,
currentColor = Card.Color.Red,
version = 1,
recordedEvents = setOf(),
)
DrawFilledWithDiscardEvent(
game.aggregateId,
version = 2,
newDrawPile = DrawPile(),
newDiscardPile = DiscardPile(),
).toNotification(
game = game,
currentUserId = player1.userId,
).let {
it.toList().size shouldBe 1
assertInstanceOf<PilesShuffledNotification>(it.first())
}
}
test("GameStartedEvent") {
val game =
GameStarted(
aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)),
drawPile = DrawPile(),
discardPile = DiscardPile(),
lastPlayerId = player1.id,
nextPlayerId = player2.id,
currentColor = Card.Color.Red,
version = 1,
recordedEvents = setOf(),
)
GameStartedEvent(
game.aggregateId,
version = 2,
firstPlayer = player1.id,
playersHans =
mapOf(
player1.id to player1.hand,
player2.id to player2.hand,
),
drawPile = DrawPile(),
discardPile = DiscardPile(),
).toNotification(
game = game,
currentUserId = player1.userId,
).toList()
.let { notifications ->
notifications.size shouldBe 2
assertInstanceOf<TheGameWasStartedNotification>(notifications.first()).let {
it.hand.size shouldBe 1
it.hand.first() shouldBe player1.hand.cards.first()
}
assertInstanceOf<ItsTheTurnOfNotification>(notifications[1]).let {
it.player.id shouldBe player2.id
}
}
}
})
@@ -11,7 +11,6 @@ import eventDemo.contexts.game.domain.game.GameId
import eventDemo.contexts.game.domain.game.Player import eventDemo.contexts.game.domain.game.Player
import eventDemo.contexts.game.domain.game.PlayerHand import eventDemo.contexts.game.domain.game.PlayerHand
import eventDemo.contexts.game.domain.game.PlayerList import eventDemo.contexts.game.domain.game.PlayerList
import eventDemo.contexts.game.domain.game.gameState.Game.Direction
import eventDemo.sharedKernel.UserId import eventDemo.sharedKernel.UserId
import eventDemo.testHelpers.act import eventDemo.testHelpers.act
import eventDemo.testHelpers.assert import eventDemo.testHelpers.assert
@@ -196,12 +195,10 @@ class GameStartedTest :
val card3 = Card.NumericCard(3, Card.Color.Red) val card3 = Card.NumericCard(3, Card.Color.Red)
assert { assert {
val player1 = Player("Player 1", UserId()) val player1 = Player("Player 1", UserId())
val player2 = Player("Player 2", UserId())
GameStarted( GameStarted(
aggregateId = GameId(), aggregateId = GameId(),
players = PlayerList(setOf(player1)), players = PlayerList(setOf(player1)),
lastPlayerId = player1.id, lastPlayerId = player1.id,
nextPlayerId = player2.id,
drawPile = DrawPile(), drawPile = DrawPile(),
discardPile = discardPile =
DiscardPile( DiscardPile(
@@ -247,14 +244,17 @@ class GameStartedTest :
val card3 = Card.NumericCard(3, Card.Color.Red) val card3 = Card.NumericCard(3, Card.Color.Red)
val card4 = Card.NumericCard(4, Card.Color.Red) val card4 = Card.NumericCard(4, Card.Color.Red)
val player1 = Player("Jo", UserId()) val player1 =
val player2 = Player("Bob", UserId()) Player(
"Jo",
UserId(),
hand = PlayerHand(cards = setOf()),
)
assert { assert {
GameStarted( GameStarted(
aggregateId = GameId(), aggregateId = GameId(),
players = PlayerList(setOf(player1)), players = PlayerList(setOf(player1)),
lastPlayerId = player1.id, lastPlayerId = player1.id,
nextPlayerId = player2.id,
drawPile = drawPile =
DrawPile( DrawPile(
setOf( setOf(
@@ -303,7 +303,6 @@ class GameStartedTest :
aggregateId = GameId(), aggregateId = GameId(),
players = PlayerList(setOf(player1, player2)), players = PlayerList(setOf(player1, player2)),
lastPlayerId = player1.id, lastPlayerId = player1.id,
nextPlayerId = player2.id,
drawPile = DrawPile(), drawPile = DrawPile(),
discardPile = DiscardPile(), discardPile = DiscardPile(),
currentColor = Card.Color.Yellow, currentColor = Card.Color.Yellow,
@@ -374,12 +373,10 @@ private fun gameWithCard(
val player1 = Player("Tesla", UserId(), hand = played1Hand) val player1 = Player("Tesla", UserId(), hand = played1Hand)
val player2 = Player("Einstein", UserId(), hand = played2Hand) val player2 = Player("Einstein", UserId(), hand = played2Hand)
val player3 = Player("Curie", UserId(), hand = PlayerHand(setOf(Card.NumericCard(8, Card.Color.Yellow)))) val player3 = Player("Curie", UserId(), hand = PlayerHand(setOf(Card.NumericCard(8, Card.Color.Yellow))))
val players = PlayerList(setOf(player1, player2, player3))
return GameStarted( return GameStarted(
aggregateId = GameId(), aggregateId = GameId(),
players = players, players = PlayerList(setOf(player1, player2, player3)),
lastPlayerId = player3.id, lastPlayerId = player3.id,
nextPlayerId = players.nextPlayerTurn(player3.id, Direction.CLOCKWISE),
discardPile = DiscardPile(setOf(onTheDiscardPile)), discardPile = DiscardPile(setOf(onTheDiscardPile)),
drawPile = DrawPile(), drawPile = DrawPile(),
currentColor = (onTheDiscardPile as? Card.CardWithColor)?.color ?: chosenColor ?: error("no color"), currentColor = (onTheDiscardPile as? Card.CardWithColor)?.color ?: chosenColor ?: error("no color"),
@@ -1,41 +0,0 @@
package eventDemo.contexts.game.domain.game.gameState
import eventDemo.contexts.game.domain.game.Card
import io.kotest.assertions.retry
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlin.time.Duration.Companion.seconds
class NewDeckTest :
FunSpec({
test("newDeck") {
newDeck().let {
it shouldNotBe null
it.filterIsInstance<Card.NumericCard>() shouldHaveSize 76
it.filterIsInstance<Card.Plus2Card>() shouldHaveSize 8
it.filterIsInstance<Card.ReverseCard>() shouldHaveSize 8
it.filterIsInstance<Card.PassCard>() shouldHaveSize 8
it.filterIsInstance<Card.Plus4Card>() shouldHaveSize 4
it.filterIsInstance<Card.ChangeColorCard>() shouldHaveSize 4
it shouldHaveSize 108
}
}
test("shuffleDeck") {
val deck = (0..9).map { Card.NumericCard(it, Card.Color.Red) }
deck.run {
this[3].number shouldBe 3
}
should {
retry(maxRetry = 4, timeout = 1.seconds) {
deck.shuffled().run {
this[3].number shouldNotBe 3
}
}
}
}
})
@@ -0,0 +1,21 @@
package eventDemo.contexts.game.intrastructure.persistence.connectors
import eventDemo.Tag
import eventDemo.testHelpers.testKoinApplicationWithConfig
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.equals.shouldBeEqual
import redis.clients.jedis.UnifiedJedis
class RedisTest :
FunSpec({
tags(Tag.Redis)
test("test connection with jedis") {
testKoinApplicationWithConfig {
get<UnifiedJedis>().also {
it.set("test", "test")
it.get("test") shouldBeEqual "test"
}
}
}
})
@@ -117,8 +117,8 @@ class GameListRouteTest :
call.body<List<GameList>>().first().let { call.body<List<GameList>>().first().let {
it.status shouldBeEqual GameList.Status.IS_STARTED it.status shouldBeEqual GameList.Status.IS_STARTED
it.players shouldHaveSize 2 it.players shouldHaveSize 2
it.players.map { it.userId } shouldContain user1.id it.players.map { it.userId } shouldContain user1
it.players.map { it.userId } shouldContain user2.id it.players.map { it.userId } shouldContain user2
it.winners shouldHaveSize 0 it.winners shouldHaveSize 0
} }
} }
@@ -2,7 +2,6 @@ package eventDemo.libs.bus
import com.rabbitmq.client.ConnectionFactory import com.rabbitmq.client.ConnectionFactory
import eventDemo.testHelpers.spyPing import eventDemo.testHelpers.spyPing
import eventDemo.testHelpers.testKoinApplicationWithConfig
import io.kotest.core.spec.style.FunSpec import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData import io.kotest.datatest.withData
import io.kotest.matchers.string.shouldStartWith import io.kotest.matchers.string.shouldStartWith
@@ -16,13 +15,19 @@ private data class ObjTest(
class BusTest : class BusTest :
FunSpec({ FunSpec({
context("Pub/sub") { context("Pub/sub") {
testKoinApplicationWithConfig { val factory =
ConnectionFactory().apply {
host = "localhost"
port = 5672
username = "event-demo"
password = "changeit"
}
val list: Map<String, Bus<ObjTest>> = val list: Map<String, Bus<ObjTest>> =
mapOf( mapOf(
BusInMemory::class.java.simpleName to BusInMemory(), BusInMemory::class.java.simpleName to BusInMemory(),
BusInRabbitMQ::class.java.simpleName to BusInRabbitMQ::class.java.simpleName to
BusInRabbitMQ( BusInRabbitMQ(
get<ConnectionFactory>(), factory,
"testExchange", "testExchange",
{ it.value }, { it.value },
{ ObjTest(it) }, { ObjTest(it) },
@@ -40,5 +45,4 @@ class BusTest :
} }
} }
} }
}
}) })
@@ -33,10 +33,9 @@ class EventStreamTest :
block(aggregateId) block(aggregateId)
} }
fun Koin.eventStreams(): Map<String, EventStream<EventXTest, IdTest>> = fun Koin.eventStreams(): List<EventStream<EventXTest, IdTest>> =
mapOf( listOf(
EventStreamInMemory::class.simpleName.toString() to EventStreamInMemory(IdTest()), EventStreamInMemory(IdTest()),
EventStreamInPostgresql::class.simpleName.toString() to
EventStreamInPostgresql( EventStreamInPostgresql(
IdTest(), IdTest(),
dataSource = get(), dataSource = get(),
@@ -9,70 +9,51 @@ import eventDemo.contexts.game.application.eventStores.GameRepository
import eventDemo.contexts.game.domain.game.Card import eventDemo.contexts.game.domain.game.Card
import eventDemo.contexts.game.domain.game.GameId import eventDemo.contexts.game.domain.game.GameId
import eventDemo.contexts.game.domain.game.Player import eventDemo.contexts.game.domain.game.Player
import eventDemo.contexts.game.domain.game.gameState.Game
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import org.koin.core.Koin import org.koin.core.Koin
import java.util.UUID
object CreateGameWithCommandsInChannelsHelpers { object CreateGameWithCommandsInChannelsHelpers {
class Data(
private val repo: GameRepository,
val gameId: GameId,
val currentUser: User,
) {
fun getPlayer(user: User): Player =
game.players.get(user.id)
val currentPlayer: Player get() = getPlayer(currentUser)
val game: Game
get() = repo.get(gameId)!!
}
context(koin: Koin) context(koin: Koin)
suspend fun <T> createGameWithCommandsInChannels( suspend fun <T> createGameWithCommandsInChannels(
channelCommand: Channel<GameCommand>, channelCommand: Channel<GameCommand>,
gameId: GameId, gameName: String = "testGame${UUID.randomUUID()}",
user: User, block: suspend context(Channel<GameCommand>, GameId) ((User) -> Player) -> T,
block:
suspend context(
CreateGameWithCommandsInChannelsHelpers,
Channel<GameCommand>,
User,
) Data.() -> T,
): T { ): T {
val gameId = GameId(UUID.nameUUIDFromBytes(gameName.encodeToByteArray()))
val repo = koin.get<GameRepository>() val repo = koin.get<GameRepository>()
repo.getOrCreate(gameId) repo.getOrCreate(gameId)
return with(channelCommand) { return with(gameId) {
with(user) { with(channelCommand) {
with(CreateGameWithCommandsInChannelsHelpers) { with(CreateGameWithCommandsInChannelsHelpers) {
Data(repo, gameId, user).block() block { user -> repo.get(gameId)!!.players.get(user.id) }
} }
} }
} }
} }
context(channelCommand: Channel<GameCommand>, data: Data) context(channelCommand: Channel<GameCommand>, gameId: GameId)
suspend fun joinTheGame(): JoinTheGameCommand = suspend fun User.joinTheGame(): JoinTheGameCommand =
JoinTheGameCommand( JoinTheGameCommand(
data.currentUser.id, id,
JoinTheGameCommand.Payload(data.gameId), JoinTheGameCommand.Payload(gameId),
).also { channelCommand.send(it) } ).also { channelCommand.send(it) }
context(channelCommand: Channel<GameCommand>, data: Data) context(channelCommand: Channel<GameCommand>, gameId: GameId)
suspend fun readyToPlay(): ReadyToPlayCommand = suspend fun Player.readyToPlay(): ReadyToPlayCommand =
ReadyToPlayCommand( ReadyToPlayCommand(
data.currentUser.id, userId,
ReadyToPlayCommand.Payload(data.gameId, data.currentPlayer.id), ReadyToPlayCommand.Payload(gameId, id),
).also { channelCommand.send(it) } ).also { channelCommand.send(it) }
context(channelCommand: Channel<GameCommand>, data: Data) context(channelCommand: Channel<GameCommand>, gameId: GameId)
suspend fun playCard( suspend fun Player.playCard(
card: Card, card: Card,
chosenColor: Card.Color? = null, chosenColor: Card.Color? = null,
): PlayCardCommand = ): PlayCardCommand =
PlayCardCommand( PlayCardCommand(
data.currentUser.id, userId,
PlayCardCommand.Payload(data.gameId, data.currentPlayer.id, card, chosenColor), PlayCardCommand.Payload(gameId, id, card, chosenColor),
).also { channelCommand.send(it) } ).also { channelCommand.send(it) }
} }
@@ -15,13 +15,10 @@ import org.koin.core.module.KoinApplicationDslMarker
import org.koin.dsl.koinApplication import org.koin.dsl.koinApplication
import org.koin.ktor.ext.getKoin import org.koin.ktor.ext.getKoin
const val CONFIG_FILE_NAME = "application.conf"
@KoinApplicationDslMarker @KoinApplicationDslMarker
suspend fun <T> testKoinApplicationWithConfig(block: suspend Koin.() -> T): T = suspend fun <T> testKoinApplicationWithConfig(block: suspend Koin.() -> T): T =
koinApplication { koinApplication { modules(appKoinModule(ApplicationConfig("application.conf").configuration)) }
modules(appKoinModule(ApplicationConfig(CONFIG_FILE_NAME).configuration)) .koin
}.koin
.run { .run {
cleanDataTest() cleanDataTest()
configureProjectionListener() configureProjectionListener()
@@ -37,7 +34,7 @@ fun testApplicationWithConfig(
) { ) {
val logger = KotlinLogging.logger {} val logger = KotlinLogging.logger {}
testApplication { testApplication {
val conf = ApplicationConfig(CONFIG_FILE_NAME) val conf = ApplicationConfig("application.conf")
environment { environment {
config = conf config = conf
} }
@@ -1,6 +1,7 @@
package eventDemo.testHelpers package eventDemo.testHelpers
import org.koin.core.Koin import org.koin.core.Koin
import redis.clients.jedis.UnifiedJedis
import javax.sql.DataSource import javax.sql.DataSource
fun DataSource.cleanEventSource() { fun DataSource.cleanEventSource() {
@@ -16,6 +17,11 @@ fun DataSource.cleanEventSource() {
} }
} }
fun UnifiedJedis.cleanProjections() {
flushAll()
}
fun Koin.cleanDataTest() { fun Koin.cleanDataTest() {
get<DataSource>().cleanEventSource() get<DataSource>().cleanEventSource()
get<UnifiedJedis>().cleanProjections()
} }