-
Notifications
You must be signed in to change notification settings - Fork 60
/
jobs_test.go
341 lines (278 loc) · 11.8 KB
/
jobs_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package bpmn_engine
import (
"testing"
"github.com/corbym/gocrest/has"
"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
)
const (
varCounter = "counter"
varEngineValidationAttempts = "engineValidationAttempts"
varHasReachedMaxAttempts = "hasReachedMaxAttempts"
)
func increaseCounterHandler(job ActivatedJob) {
counter := job.GetVariable(varCounter).(int)
counter++
job.SetVariable(varCounter, counter)
job.Complete()
}
func jobFailHandler(job ActivatedJob) {
job.Fail("just because I can")
}
func jobCompleteHandler(job ActivatedJob) {
job.Complete()
}
func Test_job_implements_Activity(t *testing.T) {
var _ Activity = &job{}
}
func Test_a_job_can_fail_and_keeps_the_instance_in_active_state(t *testing.T) {
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple_task.bpmn")
bpmnEngine.NewTaskHandler().Id("id").Handler(jobFailHandler)
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, instance.State, is.EqualTo(Active))
}
// Test_simple_count_loop requires correct Task-Output-Mapping in the BPMN file
func Test_simple_count_loop(t *testing.T) {
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple-count-loop.bpmn")
bpmnEngine.NewTaskHandler().Id("id-increaseCounter").Handler(increaseCounterHandler)
vars := map[string]interface{}{}
vars[varCounter] = 0
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, vars)
then.AssertThat(t, instance.GetVariable(varCounter), is.EqualTo(4))
then.AssertThat(t, instance.State, is.EqualTo(Completed))
}
func Test_simple_count_loop_with_message(t *testing.T) {
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple-count-loop-with-message.bpmn")
vars := map[string]interface{}{}
vars[varEngineValidationAttempts] = 0
bpmnEngine.NewTaskHandler().Id("do-nothing").Handler(jobCompleteHandler)
bpmnEngine.NewTaskHandler().Id("validate").Handler(func(job ActivatedJob) {
attempts := job.GetVariable(varEngineValidationAttempts).(int)
foobar := attempts >= 1
attempts++
job.SetVariable(varEngineValidationAttempts, attempts)
job.SetVariable(varHasReachedMaxAttempts, foobar)
job.Complete()
})
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, vars) // should stop at the intermediate message catch event
_ = bpmnEngine.PublishEventForInstance(instance.GetInstanceKey(), "msg", nil)
_, _ = bpmnEngine.RunOrContinueInstance(instance.GetInstanceKey()) // again, should stop at the intermediate message catch event
// validation happened
_ = bpmnEngine.PublishEventForInstance(instance.GetInstanceKey(), "msg", nil)
_, _ = bpmnEngine.RunOrContinueInstance(instance.GetInstanceKey()) // should finish
// validation happened
then.AssertThat(t, instance.GetVariable(varHasReachedMaxAttempts), is.True())
then.AssertThat(t, instance.GetVariable(varEngineValidationAttempts), is.EqualTo(2))
then.AssertThat(t, instance.State, is.EqualTo(Completed))
// internal State expected
then.AssertThat(t, bpmnEngine.GetMessageSubscriptions(), has.Length(2))
then.AssertThat(t, bpmnEngine.GetMessageSubscriptions()[0].State, is.EqualTo(Completed))
then.AssertThat(t, bpmnEngine.GetMessageSubscriptions()[1].State, is.EqualTo(Completed))
}
func Test_activated_job_data(t *testing.T) {
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple_task.bpmn")
bpmnEngine.NewTaskHandler().Id("id").Handler(func(aj ActivatedJob) {
then.AssertThat(t, aj.GetElementId(), is.Not(is.Empty()))
then.AssertThat(t, aj.GetCreatedAt(), is.Not(is.Nil()))
then.AssertThat(t, aj.GetKey(), is.Not(is.EqualTo(int64(0))))
then.AssertThat(t, aj.GetBpmnProcessId(), is.Not(is.Empty()))
then.AssertThat(t, aj.GetProcessDefinitionKey(), is.Not(is.EqualTo(int64(0))))
then.AssertThat(t, aj.GetProcessDefinitionVersion(), is.Not(is.EqualTo(int32(0))))
then.AssertThat(t, aj.GetProcessInstanceKey(), is.Not(is.EqualTo(int64(0))))
})
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, instance.State, is.EqualTo(Active))
}
func Test_task_InputOutput_mapping_happy_path(t *testing.T) {
// setup
bpmnEngine := New()
cp := CallPath{}
// give
process, _ := bpmnEngine.LoadFromFile("../../test-cases/service-task-input-output.bpmn")
bpmnEngine.NewTaskHandler().Id("service-task-1").Handler(cp.TaskHandler)
bpmnEngine.NewTaskHandler().Id("user-task-2").Handler(cp.TaskHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
for _, job := range bpmnEngine.jobs {
then.AssertThat(t, job.JobState, is.EqualTo(Completed))
}
then.AssertThat(t, cp.CallPath, is.EqualTo("service-task-1,user-task-2"))
// id from input should not exist in instance scope
then.AssertThat(t, pi.GetVariable("id"), is.Nil())
// output should exist in instance scope
then.AssertThat(t, pi.GetVariable("dstcity"), is.EqualTo("beijing"))
then.AssertThat(t, pi.GetVariable("order"), is.EqualTo(map[string]interface{}{
"name": "order1",
"id": "1234",
}))
then.AssertThat(t, pi.GetVariable("orderId"), is.EqualTo(1234))
then.AssertThat(t, pi.GetVariable("orderName"), is.EqualTo("order1"))
}
func Test_instance_fails_on_Invalid_Input_mapping(t *testing.T) {
// setup
bpmnEngine := New()
cp := CallPath{}
// give
process, _ := bpmnEngine.LoadFromFile("../../test-cases/service-task-invalid-input.bpmn")
bpmnEngine.NewTaskHandler().Id("invalid-input").Handler(cp.TaskHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
then.AssertThat(t, cp.CallPath, is.EqualTo(""))
then.AssertThat(t, pi.GetVariable("id"), is.Nil())
then.AssertThat(t, bpmnEngine.jobs[0].JobState, is.EqualTo(Failed))
then.AssertThat(t, pi.GetState(), is.EqualTo(Failed))
}
func Test_job_fails_on_Invalid_Output_mapping(t *testing.T) {
// setup
bpmnEngine := New()
cp := CallPath{}
// give
process, _ := bpmnEngine.LoadFromFile("../../test-cases/service-task-invalid-output.bpmn")
bpmnEngine.NewTaskHandler().Id("invalid-output").Handler(cp.TaskHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
then.AssertThat(t, cp.CallPath, is.EqualTo("invalid-output"))
then.AssertThat(t, pi.GetVariable("order"), is.Nil())
then.AssertThat(t, bpmnEngine.jobs[0].JobState, is.EqualTo(Failed))
then.AssertThat(t, pi.GetState(), is.EqualTo(Failed))
}
func Test_task_type_handler(t *testing.T) {
// setup
bpmnEngine := New()
cp := CallPath{}
// give
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple-task-with-type.bpmn")
bpmnEngine.NewTaskHandler().Type("foobar").Handler(cp.TaskHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
then.AssertThat(t, cp.CallPath, is.EqualTo("id"))
then.AssertThat(t, pi.GetState(), is.EqualTo(Completed))
}
func Test_task_type_handler_ID_handler_has_precedence(t *testing.T) {
// setup
bpmnEngine := New()
calledHandler := "none"
idHandler := func(job ActivatedJob) {
calledHandler = "ID"
job.Complete()
}
typeHandler := func(job ActivatedJob) {
calledHandler = "TYPE"
job.Complete()
}
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple-task-with-type.bpmn")
// given reverse order of definition, means 'type:foobar' before 'id'
bpmnEngine.NewTaskHandler().Type("foobar").Handler(typeHandler)
bpmnEngine.NewTaskHandler().Id("id").Handler(idHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
then.AssertThat(t, calledHandler, is.EqualTo("ID"))
then.AssertThat(t, pi.GetState(), is.EqualTo(Completed))
}
func Test_just_one_handler_called(t *testing.T) {
// setup
bpmnEngine := New()
cp := CallPath{}
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple-task-with-type.bpmn")
// given multiple matching handlers executed
bpmnEngine.NewTaskHandler().Id("id").Handler(cp.TaskHandler)
bpmnEngine.NewTaskHandler().Id("id").Handler(cp.TaskHandler)
bpmnEngine.NewTaskHandler().Type("foobar").Handler(cp.TaskHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
then.AssertThat(t, cp.CallPath, is.EqualTo("id").Reason("just one execution"))
then.AssertThat(t, pi.GetState(), is.EqualTo(Completed))
}
func Test_assignee_and_candidate_groups_are_assigned_to_handler(t *testing.T) {
// setup
bpmnEngine := New()
cp := CallPath{}
process, _ := bpmnEngine.LoadFromFile("../../test-cases/user-tasks-with-assignments.bpmn")
// given multiple matching handlers executed
bpmnEngine.NewTaskHandler().Assignee("john.doe").Handler(cp.TaskHandler)
bpmnEngine.NewTaskHandler().CandidateGroups("marketing", "support").Handler(cp.TaskHandler)
// when
pi, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
// then
then.AssertThat(t, cp.CallPath, is.EqualTo("assignee-task,group-task"))
then.AssertThat(t, pi.GetState(), is.EqualTo(Completed))
}
func Test_task_default_all_output_variables_map_to_process_instance(t *testing.T) {
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple_task-no_output_mapping.bpmn")
bpmnEngine.NewTaskHandler().Id("id").Handler(func(job ActivatedJob) {
job.SetVariable("aVariable", true)
job.Complete()
})
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, instance.State, is.EqualTo(Completed))
then.AssertThat(t, instance.GetVariable("aVariable"), is.True())
}
func Test_task_no_output_variables_mapping_on_failure(t *testing.T) {
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple_task-no_output_mapping.bpmn")
bpmnEngine.NewTaskHandler().Id("id").Handler(func(job ActivatedJob) {
job.SetVariable("aVariable", true)
job.Fail("because I can")
})
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, instance.State, is.EqualTo(Active))
then.AssertThat(t, instance.GetVariable("aVariable"), is.Nil())
}
func Test_task_just_declared_output_variables_map_to_process_instance(t *testing.T) {
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/simple_task-with_output_mapping.bpmn")
bpmnEngine.NewTaskHandler().Id("id").Handler(func(job ActivatedJob) {
job.SetVariable("valueFromHandler", true)
job.SetVariable("otherVariable", "value")
job.Complete()
})
instance, _ := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, instance.State, is.EqualTo(Completed))
then.AssertThat(t, instance.GetVariable("valueFromHandler"), is.True())
then.AssertThat(t, instance.GetVariable("otherVariable"), is.Nil())
}
func Test_missing_task_handlers_break_execution_and_can_be_continued_later(t *testing.T) {
cp := CallPath{}
// setup
bpmnEngine := New()
process, _ := bpmnEngine.LoadFromFile("../../test-cases/parallel-gateway-flow.bpmn")
// given
bpmnEngine.NewTaskHandler().Id("id-a-1").Handler(cp.TaskHandler)
instance, err := bpmnEngine.CreateAndRunInstance(process.ProcessKey, nil)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, instance.State, is.EqualTo(Active))
then.AssertThat(t, cp.CallPath, is.EqualTo("id-a-1"))
// when
bpmnEngine.NewTaskHandler().Id("id-b-1").Handler(cp.TaskHandler)
bpmnEngine.NewTaskHandler().Id("id-b-2").Handler(cp.TaskHandler)
instance, err = bpmnEngine.RunOrContinueInstance(instance.GetInstanceKey())
then.AssertThat(t, instance, is.Not(is.Nil()))
then.AssertThat(t, instance.State, is.EqualTo(Completed))
// then
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, cp.CallPath, is.EqualTo("id-a-1,id-b-1,id-b-2"))
}