-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateFixtureGiven.kt
121 lines (104 loc) · 3.27 KB
/
AggregateFixtureGiven.kt
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
@file:Suppress("unused")
package io.holixon.axon.testing.jgiven.aggregate
import com.tngtech.jgiven.Stage
import com.tngtech.jgiven.annotation.*
import io.holixon.axon.testing.jgiven.AxonJGivenStage
import io.holixon.axon.testing.jgiven.step
import org.axonframework.test.aggregate.AggregateTestFixture
import org.axonframework.test.aggregate.TestExecutor
import java.time.Instant
import java.util.function.Supplier
/**
* Given stage for aggregate fixture.
* @param T aggregate type.
*/
@AxonJGivenStage
class AggregateFixtureGiven<T> : Stage<AggregateFixtureGiven<T>>() {
@ExpectedScenarioState(required = true)
private lateinit var fixture: AggregateTestFixture<T>
@ProvidedScenarioState
private lateinit var context: AggregateTestFixtureContext<T>
@BeforeStage
internal fun initStage() {
context = AggregateTestFixtureContext(fixture)
}
/**
* Nothing happens before.
*/
@As("no prior activity")
fun noPriorActivity(): AggregateFixtureGiven<T> = execute { fixture.givenNoPriorActivity() }
/**
* A command has been dispatched.
* @param command dispatched command.
*/
@As("command:")
fun command(@Quoted command: Any): AggregateFixtureGiven<T> = this.commands(command)
/**
* One or several commands has been dispatched.
* @param commands dispatched commands.
*/
@As("commands:")
fun commands(@Quoted vararg commands: Any): AggregateFixtureGiven<T> = this.commands(commands.toList())
/**
* One or several commands has been dispatched.
* @param commands dispatched commands.
*/
@As("commands:")
fun commands(@Quoted commands: List<Any>): AggregateFixtureGiven<T> = execute {
if (context.isFirstGiven) {
context.isFirstGiven = false
context.fixture!!.givenCommands(commands)
} else {
context.testExecutor!!.andGivenCommands(commands)
}
}
/**
* An event has been published.
* @param event published event.
*/
@As("event:")
fun event(@Quoted event: Any): AggregateFixtureGiven<T> = this.events(event)
/**
* One or several events has been published.
* @param events published events.
*/
@As("events:")
fun events(@Quoted vararg events: Any): AggregateFixtureGiven<T> = this.events(events.toList())
/**
* One or several events has been published.
* @param events published events.
*/
@As("events:")
fun events(@Quoted events: List<Any>): AggregateFixtureGiven<T> = execute {
if (context.isFirstGiven) {
context.isFirstGiven = false
context.fixture!!.given(events)
} else {
context.testExecutor!!.andGiven(events)
}
}
/**
* Sets the time.
* @param instant new time.
*/
fun currentTime(instant: Instant): AggregateFixtureGiven<T> = execute {
context.testExecutor!!.andGivenCurrentTime(instant)
}
/**
* Sets the state of the aggregate.
* @param aggregate aggregate state supplier.
*/
fun state(aggregate: Supplier<T>): AggregateFixtureGiven<T> = execute {
fixture.givenState(aggregate)
}
/**
* Sets the state of the aggregate.
* @param aggregate aggregate state providing function..
*/
fun state(aggregate: () -> T): AggregateFixtureGiven<T> = execute {
fixture.givenState(aggregate)
}
private fun execute(block: () -> TestExecutor<T>) = step {
context.testExecutor = block.invoke()
}
}