-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetAllDwellingsSpringSliceTest.kt
More file actions
97 lines (88 loc) · 4.14 KB
/
GetAllDwellingsSpringSliceTest.kt
File metadata and controls
97 lines (88 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.dddheroes.heroesofddd.creaturerecruitment.read.getalldwellings
import com.dddheroes.heroesofddd.HeroesAxonSpringBootTest
import com.dddheroes.heroesofddd.creaturerecruitment.events.AvailableCreaturesChanged
import com.dddheroes.heroesofddd.creaturerecruitment.events.DwellingBuilt
import com.dddheroes.heroesofddd.shared.domain.identifiers.CreatureId
import com.dddheroes.heroesofddd.shared.domain.identifiers.DwellingId
import com.dddheroes.heroesofddd.shared.domain.identifiers.GameId
import com.dddheroes.heroesofddd.shared.domain.valueobjects.Quantity
import com.dddheroes.heroesofddd.shared.domain.valueobjects.ResourceType
import com.dddheroes.heroesofddd.shared.domain.valueobjects.Resources
import org.assertj.core.api.Assertions.assertThat
import org.axonframework.common.configuration.Configuration
import org.axonframework.extensions.kotlin.AxonMetadata
import org.axonframework.messaging.queryhandling.gateway.QueryGateway
import org.axonframework.test.fixture.*
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.TestPropertySource
import java.util.*
import java.util.concurrent.TimeUnit
@TestPropertySource(properties = ["slices.creaturerecruitment.read.getalldwellings.enabled=true"])
@HeroesAxonSpringBootTest
internal class GetAllDwellingsSpringSliceTest @Autowired constructor(
private val fixture: AxonTestFixture
) {
private val gameId = GameId.random()
private val playerId: String = UUID.randomUUID().toString()
private val gameMetadata = AxonMetadata.with("gameId", gameId.raw)
.and("playerId", playerId)
private val phoenixCost = Resources.of(ResourceType.GOLD to 2000, ResourceType.MERCURY to 1)
private val phoenixCostRaw = mapOf(
"GOLD" to 2000, "WOOD" to 0, "ORE" to 0,
"MERCURY" to 1, "SULFUR" to 0, "CRYSTAL" to 0, "GEMS" to 0
)
@Test
fun `given no events, when get all dwellings, then empty result`() {
fixture.When { nothing() } Then {
expect { cfg ->
val result = queryAllDwellings(cfg)
assertThat(result.items).isEmpty()
}
}
}
@Test
fun `given two dwellings built, when get all dwellings, then both returned`() {
val dwellingId1 = DwellingId.random()
val dwellingId2 = DwellingId.random()
val creatureId = CreatureId("phoenix")
val expectedCost = phoenixCostRaw
fixture.Given {
event(DwellingBuilt(dwellingId1, creatureId, phoenixCost), gameMetadata)
event(DwellingBuilt(dwellingId2, creatureId, phoenixCost), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryAllDwellings(cfg)
assertThat(result.items).containsExactlyInAnyOrder(
DwellingReadModel(gameId.raw, dwellingId1.raw, creatureId.raw, expectedCost, 0),
DwellingReadModel(gameId.raw, dwellingId2.raw, creatureId.raw, expectedCost, 0)
)
}
}
}
@Test
fun `given dwelling built and creatures changed, when get all dwellings, then dwelling has updated creatures`() {
val dwellingId = DwellingId.random()
val creatureId = CreatureId("phoenix")
fixture.Given {
event(DwellingBuilt(dwellingId, creatureId, phoenixCost), gameMetadata)
event(
AvailableCreaturesChanged(dwellingId, creatureId, changedBy = 5, changedTo = Quantity(5)),
gameMetadata
)
} Then {
awaitAndExpect { cfg ->
val result = queryAllDwellings(cfg)
val expectedCost = phoenixCostRaw
assertThat(result.items).containsExactlyInAnyOrder(
DwellingReadModel(gameId.raw, dwellingId.raw, creatureId.raw, expectedCost, 5)
)
}
}
}
private fun queryAllDwellings(cfg: Configuration): GetAllDwellings.Result =
cfg.getComponent(QueryGateway::class.java)
.query(GetAllDwellings(gameId), GetAllDwellings.Result::class.java)
.orTimeout(1, TimeUnit.SECONDS)
.join()
}