forked from andrewwebber/cqrs
/
cqrs_test.go
242 lines (202 loc) · 8.15 KB
/
cqrs_test.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package cqrs_test
import (
"log"
"testing"
"time"
"github.com/andrewwebber/cqrs"
"github.com/logrusorgru/glr"
)
var accountID = "5058e029-d329-4c4b-b111-b042e48b0c5f"
func TestScenario(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Type Registry
typeRegistry := cqrs.NewTypeRegistry()
// Event sourcing
persistance := cqrs.NewInMemoryEventStreamRepository()
bus := cqrs.NewInMemoryEventBus()
repository := cqrs.NewRepositoryWithPublisher(persistance, bus, typeRegistry)
typeRegistry.RegisterAggregate(&Account{}, AccountCreatedEvent{}, EmailAddressChangedEvent{}, AccountCreditedEvent{}, AccountDebitedEvent{}, PasswordChangedEvent{})
// Read Models
readModel := NewReadModelAccounts()
usersModel := NewUsersModel()
// Command Handlers
commandBus := cqrs.NewInMemoryCommandBus()
commandDispatcher := cqrs.NewCommandDispatchManager(commandBus, typeRegistry)
RegisterCommandHandlers(commandDispatcher, repository)
// Integration events
eventDispatcher := cqrs.NewVersionedEventDispatchManager(bus, typeRegistry)
integrationEventsLog := cqrs.NewInMemoryEventStreamRepository()
RegisterIntegrationEventHandlers(eventDispatcher, integrationEventsLog, readModel, usersModel)
commandDispatcherStopChannel := make(chan bool)
eventDispatcherStopChannel := make(chan bool)
go commandDispatcher.Listen(commandDispatcherStopChannel, false)
go eventDispatcher.Listen(eventDispatcherStopChannel, false)
log.Println("Dump models")
log.Println(readModel)
log.Println(usersModel)
log.Println("Find an account")
readModelAccount := readModel.Accounts[accountID]
log.Println(readModelAccount)
log.Println("Find a user")
user := usersModel.Users[accountID]
log.Println(user)
hashedPassword, err := GetHashForPassword("$ThisIsMyPassword1")
if err != nil {
t.Fatal("Error: ", err)
}
log.Println("Create new account...")
createAccountCommand := cqrs.CreateCommand(
CreateAccountCommand{"John", "Snow", "John.Snow@thewall.eu", hashedPassword, 0.0})
commandBus.PublishCommands([]cqrs.Command{createAccountCommand})
log.Println("Dump models")
log.Println(readModel)
log.Println(usersModel)
log.Println("Change Password")
changePasswordCommand := cqrs.CreateCommand(
ChangePasswordCommand{accountID, "$ThisIsANOTHERPassword"})
commandBus.PublishCommands([]cqrs.Command{changePasswordCommand})
log.Println("Change email address and credit the account")
changeEmailAddressCommand := cqrs.CreateCommand(
ChangeEmailAddressCommand{accountID, "john.snow@the.wall"})
creditAccountCommand := cqrs.CreateCommand(
CreditAccountCommand{accountID, 50})
creditAccountCommand2 := cqrs.CreateCommand(
CreditAccountCommand{accountID, 50})
commandBus.PublishCommands([]cqrs.Command{
changeEmailAddressCommand,
creditAccountCommand,
creditAccountCommand2})
log.Println("Dump models")
log.Println(readModel)
log.Println(usersModel)
log.Println("Change the email address, credit 150, debit 200")
lastEmailAddress := "john.snow@golang.org"
changeEmailAddressCommand = cqrs.CreateCommand(
ChangeEmailAddressCommand{accountID, lastEmailAddress})
creditAccountCommand = cqrs.CreateCommand(
CreditAccountCommand{accountID, 150})
debitAccountCommand := cqrs.CreateCommand(
DebitAccountCommand{accountID, 200})
commandBus.PublishCommands([]cqrs.Command{
changeEmailAddressCommand,
creditAccountCommand,
debitAccountCommand})
log.Println("Dump models")
log.Println(readModel)
log.Println(usersModel)
time.Sleep(300 * time.Millisecond)
log.Println("Dump history - integration events")
if history, err := repository.GetEventStreamRepository().AllIntegrationEventsEverPublished(); err != nil {
t.Fatal(err)
} else {
for _, event := range history {
log.Println(event)
}
}
log.Println("GetIntegrationEventsByCorrelationID")
correlationEvents, err := repository.GetEventStreamRepository().GetIntegrationEventsByCorrelationID(debitAccountCommand.CorrelationID)
if err != nil || len(correlationEvents) == 0 {
t.Fatal(err)
}
for correlationEvent := range correlationEvents {
log.Println(correlationEvent)
}
log.Println("Load the account from history")
account, error := NewAccountFromHistory(accountID, repository)
if error != nil {
t.Fatal(error)
}
// All events should have been replayed and the email address should be the latest
log.Println("Dump models")
log.Println(account)
log.Println(readModel)
log.Println(usersModel)
if account.EmailAddress != lastEmailAddress {
t.Fatal("Expected emailaddress to be ", lastEmailAddress)
}
if account.Balance != readModel.Accounts[accountID].Balance {
t.Fatal("Expected readmodel to be synced with write model")
}
eventDispatcherStopChannel <- true
commandDispatcherStopChannel <- true
}
func RegisterIntegrationEventHandlers(eventDispatcher *cqrs.VersionedEventDispatchManager, integrationEventsLog cqrs.VersionedEventPublicationLogger, readModel *ReadModelAccounts, usersModel *UsersModel) {
eventDispatcher.RegisterGlobalHandler(func(event cqrs.VersionedEvent) error {
integrationEventsLog.SaveIntegrationEvent(event)
readModel.UpdateViewModel([]cqrs.VersionedEvent{event})
usersModel.UpdateViewModel([]cqrs.VersionedEvent{event})
return nil
})
}
func RegisterCommandHandlers(commandDispatcher *cqrs.CommandDispatchManager, repository cqrs.EventSourcingRepository) {
commandDispatcher.RegisterCommandHandler(CreateAccountCommand{}, func(command cqrs.Command) error {
createAccountCommand := command.Body.(CreateAccountCommand)
log.Println(glr.Green("Processing command - Create account"))
account := NewAccount(createAccountCommand.FirstName,
createAccountCommand.LastName,
createAccountCommand.EmailAddress,
createAccountCommand.PasswordHash,
createAccountCommand.InitialBalance)
log.Println(glr.Green("Set ID..."))
account.SetID(accountID)
log.Println(account)
log.Println(glr.Green("Persist the account"))
repository.Save(account, command.CorrelationID)
log.Println(glr.Green(account.String()))
return nil
})
commandDispatcher.RegisterCommandHandler(ChangeEmailAddressCommand{}, func(command cqrs.Command) error {
changeEmailAddressCommand := command.Body.(ChangeEmailAddressCommand)
log.Println(glr.Green("Processing command - Change email address"))
account, err := NewAccountFromHistory(changeEmailAddressCommand.AccountID, repository)
if err != nil {
return err
}
account.ChangeEmailAddress(changeEmailAddressCommand.NewEmailAddress)
log.Println(glr.Green("Persist the account"))
repository.Save(account, command.CorrelationID)
log.Println(glr.Green(account.String()))
return nil
})
commandDispatcher.RegisterCommandHandler(ChangePasswordCommand{}, func(command cqrs.Command) error {
changePasswordCommand := command.Body.(ChangePasswordCommand)
log.Println(glr.Green("Processing command - Change password"))
account, err := NewAccountFromHistory(changePasswordCommand.AccountID, repository)
if err != nil {
return err
}
account.ChangePassword(changePasswordCommand.NewPassword)
log.Println(glr.Green("Persist the account"))
repository.Save(account, command.CorrelationID)
log.Println(glr.Green(account.String()))
return nil
})
commandDispatcher.RegisterCommandHandler(CreditAccountCommand{}, func(command cqrs.Command) error {
creditAccountCommand := command.Body.(CreditAccountCommand)
log.Println(glr.Green("Processing command - Credit account"))
account, err := NewAccountFromHistory(creditAccountCommand.AccountID, repository)
if err != nil {
return err
}
account.Credit(creditAccountCommand.Amount)
log.Println(glr.Green("Persist the account"))
repository.Save(account, command.CorrelationID)
log.Println(glr.Green(account.String()))
return nil
})
commandDispatcher.RegisterCommandHandler(DebitAccountCommand{}, func(command cqrs.Command) error {
debitAccountCommand := command.Body.(DebitAccountCommand)
log.Println(glr.Green("Processing command - Debit account"))
account, err := NewAccountFromHistory(debitAccountCommand.AccountID, repository)
if err != nil {
return err
}
if err := account.Debit(debitAccountCommand.Amount); err != nil {
return err
}
log.Println(glr.Green("Persist the account"))
repository.Save(account, command.CorrelationID)
log.Println(glr.Green(account.String()))
return nil
})
}