-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetArmyCreaturesSpringSliceTest.kt
More file actions
137 lines (125 loc) · 5.59 KB
/
GetArmyCreaturesSpringSliceTest.kt
File metadata and controls
137 lines (125 loc) · 5.59 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.dddheroes.heroesofddd.armies.read.getarmycreatures
import com.dddheroes.heroesofddd.HeroesAxonSpringBootTest
import com.dddheroes.heroesofddd.armies.events.CreatureAddedToArmy
import com.dddheroes.heroesofddd.armies.events.CreatureRemovedFromArmy
import com.dddheroes.heroesofddd.shared.domain.identifiers.ArmyId
import com.dddheroes.heroesofddd.shared.domain.identifiers.CreatureId
import com.dddheroes.heroesofddd.shared.domain.identifiers.GameId
import com.dddheroes.heroesofddd.shared.domain.valueobjects.Quantity
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.concurrent.TimeUnit
@TestPropertySource(properties = ["slices.armies.read.getarmycreatures.enabled=true"])
@HeroesAxonSpringBootTest
internal class GetArmyCreaturesSpringSliceTest @Autowired constructor(
private val fixture: AxonTestFixture
) {
private val gameId = GameId.random()
private val armyId = ArmyId("hero-catherine-army")
private val gameMetadata = AxonMetadata.with("gameId", gameId.raw)
@Test
fun `given no events, when get army creatures, then empty stacks`() {
fixture.When { nothing() } Then {
expect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.armyId).isEqualTo(armyId.raw)
assertThat(result.stacks).isEmpty()
}
}
}
@Test
fun `given creature added, when get army creatures, then one stack returned`() {
fixture.Given {
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.armyId).isEqualTo(armyId.raw)
assertThat(result.stacks).containsExactlyInAnyOrder(
GetArmyCreatures.CreatureStack("angel", 5)
)
}
}
}
@Test
fun `given two different creatures added, when get army creatures, then two stacks returned`() {
fixture.Given {
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
event(CreatureAddedToArmy(armyId, CreatureId("bowman"), Quantity(3)), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.stacks).containsExactlyInAnyOrder(
GetArmyCreatures.CreatureStack("angel", 5),
GetArmyCreatures.CreatureStack("bowman", 3)
)
}
}
}
@Test
fun `given same creature added twice, when get army creatures, then quantities aggregated`() {
fixture.Given {
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(3)), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.stacks).containsExactlyInAnyOrder(
GetArmyCreatures.CreatureStack("angel", 8)
)
}
}
}
@Test
fun `given creature added then partially removed, when get army creatures, then reduced quantity`() {
fixture.Given {
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
event(CreatureRemovedFromArmy(armyId, CreatureId("angel"), Quantity(2)), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.stacks).containsExactlyInAnyOrder(
GetArmyCreatures.CreatureStack("angel", 3)
)
}
}
}
@Test
fun `given creature added then fully removed, when get army creatures, then stack removed`() {
fixture.Given {
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
event(CreatureRemovedFromArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.stacks).isEmpty()
}
}
}
@Test
fun `given creatures from different armies, when get army creatures, then only requested army returned`() {
val otherArmyId = ArmyId("hero-roland-army")
fixture.Given {
event(CreatureAddedToArmy(armyId, CreatureId("angel"), Quantity(5)), gameMetadata)
event(CreatureAddedToArmy(otherArmyId, CreatureId("black-dragon"), Quantity(2)), gameMetadata)
} Then {
awaitAndExpect { cfg ->
val result = queryArmyCreatures(cfg)
assertThat(result.stacks).containsExactlyInAnyOrder(
GetArmyCreatures.CreatureStack("angel", 5)
)
}
}
}
private fun queryArmyCreatures(cfg: Configuration): GetArmyCreatures.Result =
cfg.getComponent(QueryGateway::class.java)
.query(GetArmyCreatures(gameId, armyId), GetArmyCreatures.Result::class.java)
.orTimeout(1, TimeUnit.SECONDS)
.join()
}