zeebe_bpmn_rspec 0.1.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/README.md +172 -4
- data/docker-compose.yml +4 -4
- data/lib/zeebe_bpmn_rspec.rb +6 -1
- data/lib/zeebe_bpmn_rspec/activated_job.rb +41 -8
- data/lib/zeebe_bpmn_rspec/helpers.rb +36 -18
- data/lib/zeebe_bpmn_rspec/matchers/have_activated.rb +99 -0
- data/lib/zeebe_bpmn_rspec/matchers/have_headers.rb +15 -0
- data/lib/zeebe_bpmn_rspec/matchers/have_variables.rb +15 -0
- data/lib/zeebe_bpmn_rspec/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8ab5c676ef52ca21100add177726c7443298348603c89e8a53e1692b2876e21
|
4
|
+
data.tar.gz: a9a7acca330b1377991b524cb9e31d91ccd7599ef0391ca58aef6700dc7da168
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aae2aa7d081b9dd5cc342f955f060b0a605f23c680b3a7b9368857fc08ada31a58fb047cdd41e419d8394127786b29b76486c8feb0ce5aef1925e9ee3f88237d
|
7
|
+
data.tar.gz: 5639b3ee712ea0af3b961e3402d5c606d3b26f55279785d844909030557fa8054c2ab344c62d10afcf020408055c58491eba9dcfc288eef673d37fd5d3eca799
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,26 @@
|
|
1
1
|
# zeebe_bpmn_rspec
|
2
2
|
|
3
|
+
## v0.4.1
|
4
|
+
- Allow `with_workflow_instance` to be called without a block.
|
5
|
+
- Allow `worker` to be specified when activating a job.
|
6
|
+
- Expose `workflow_instance_key` for activated jobs.
|
7
|
+
|
8
|
+
## v0.4.0
|
9
|
+
- Add `ttl_ms` option for `publish_message`.
|
10
|
+
- Add `update_retries` method to `ActivatedJob` class.
|
11
|
+
- Add `set_variables` helper.
|
12
|
+
- Add support for `:fetch_variables` option when activating jobs.
|
13
|
+
|
14
|
+
## v0.3.1
|
15
|
+
- Use consistent activate request timeout.
|
16
|
+
- Provide a better error when a job is not activated.
|
17
|
+
|
18
|
+
## v0.3.0
|
19
|
+
- Add custom matchers, `have_variables`, `have_headers`, and `have_activated`.
|
20
|
+
|
21
|
+
## v0.2.0
|
22
|
+
- Add `retries` option to `ActivatedJob#and_fail`.
|
23
|
+
- Add method aliases: `and_complete` (`complete`), `and_fail` (`fail`), `and_throw_error` (`throw_error`).
|
24
|
+
|
3
25
|
## v0.1.0
|
4
26
|
- Initial version
|
data/README.md
CHANGED
@@ -39,6 +39,8 @@ end
|
|
39
39
|
|
40
40
|
The gem adds the following helper methods to RSpec.
|
41
41
|
|
42
|
+
The gem also defines [Custom Matchers](#custom-matchers).
|
43
|
+
|
42
44
|
### Deploy Workflow
|
43
45
|
|
44
46
|
The `deploy_workflow` method requires a path to a BPMN file and deploys it to Zeebe. There is no support for
|
@@ -118,7 +120,7 @@ activate_job("my_job").
|
|
118
120
|
|
119
121
|
#### Complete Job
|
120
122
|
|
121
|
-
Jobs can be completed by calling `and_complete
|
123
|
+
Jobs can be completed by calling `and_complete` (also aliased as `complete`). Variables can optionally be returned with the
|
122
124
|
completed job.
|
123
125
|
|
124
126
|
```ruby
|
@@ -134,7 +136,7 @@ project_job("my_job").
|
|
134
136
|
|
135
137
|
#### Fail Job
|
136
138
|
|
137
|
-
Jobs can be failed by calling `and_fail
|
139
|
+
Jobs can be failed by calling `and_fail` (also aliased as `fail`). An optional message can be specified when failing a job.
|
138
140
|
|
139
141
|
```ruby
|
140
142
|
# Failing a job can be chanined with expectations
|
@@ -147,9 +149,27 @@ activate_job("my_job").
|
|
147
149
|
and_fail("something didn't go right")
|
148
150
|
```
|
149
151
|
|
152
|
+
By default retries are set to zero when a job is failed but the remaining retries can optionally be specified:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
job = activate_job("my_job")
|
156
|
+
|
157
|
+
job.fail(retries: 1)
|
158
|
+
```
|
159
|
+
|
160
|
+
#### Update Retries
|
161
|
+
|
162
|
+
The retries for a job can also be modified using the `update_retries` method:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
job = activate_job("my_job")
|
166
|
+
|
167
|
+
job.update_retries(3)
|
168
|
+
```
|
169
|
+
|
150
170
|
#### Throw Error
|
151
171
|
|
152
|
-
The `and_throw_error` method can be used to throw an error for a job. The error code is required and an
|
172
|
+
The `and_throw_error` (also aliased as `throw_error`) method can be used to throw an error for a job. The error code is required and an
|
153
173
|
optional message may be specified:
|
154
174
|
|
155
175
|
```ruby
|
@@ -165,7 +185,18 @@ activate_job("my_job").
|
|
165
185
|
|
166
186
|
#### Activating Multiple Jobs
|
167
187
|
|
168
|
-
|
188
|
+
Multiple jobs can be activated using the `activate_jobs` method.
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
activate_jobs("my_job")
|
192
|
+
```
|
193
|
+
|
194
|
+
The call to `activate_jobs` returns an Enumerator that returns `ActivatedJob` instance.
|
195
|
+
The maximum number of jobs to return can be specified:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
jobs = activate_jobs("my_job", max_jobs: 2).to_a
|
199
|
+
```
|
169
200
|
|
170
201
|
### Workflow Complete
|
171
202
|
|
@@ -198,6 +229,143 @@ publish_message("message_name", correlation_key: expected_value,
|
|
198
229
|
variables: { foo: "bar" })
|
199
230
|
```
|
200
231
|
|
232
|
+
The time-to-live (in milliseconds) cna also be specified for a message.
|
233
|
+
It defaults to 5000 milliseconds if unspecified.
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
publish_message("message_name", correlation_key: expected_value, ttl_ms: 1000)
|
237
|
+
```
|
238
|
+
|
239
|
+
### Set Variables
|
240
|
+
|
241
|
+
The `set_variables` method can be used to set variables for a specified
|
242
|
+
scope in Zeebe:
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
# workflow_instance_key is a method that returns the key for the current workflow instance
|
246
|
+
set_variables(workflow_instance_key, { foo: "bar" })
|
247
|
+
```
|
248
|
+
|
249
|
+
An activated job can be used to determine the key for the task that it is associated with:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
job = job_with_type("my_type")
|
253
|
+
set_variables(job.task_key, { foo: "baz"})
|
254
|
+
```
|
255
|
+
|
256
|
+
Variables default to being local to the scope on which they are set. This
|
257
|
+
can be overridden by specifying the `:local` option:
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
set_variables(job.task_key, { foo: "baz"}, local: false)
|
261
|
+
```
|
262
|
+
|
263
|
+
### Custom Matchers
|
264
|
+
|
265
|
+
In addition to the helpers documented above, this gem defines custom RSpec matchers to provide a more typical
|
266
|
+
experience of expectations and matchers.
|
267
|
+
|
268
|
+
#### expect_job_of_type
|
269
|
+
|
270
|
+
The `expect_job_of_type` helper is a convenient wrapper to activate a job and set an expectation target.
|
271
|
+
|
272
|
+
```ruby
|
273
|
+
expect_job_of_type("my_type")
|
274
|
+
```
|
275
|
+
|
276
|
+
Similar to the `activate_job` helper, it activates a job and wraps the result in an `ActivatedJob` object.
|
277
|
+
That object is then passed to `expect()`. Unlike `activate_job`, this helper does not raise if there is no job activated.
|
278
|
+
|
279
|
+
This is equivalent to `expect(job_with_type("my_type")` or `expect(activate_job("my_type", validate: false))`.
|
280
|
+
|
281
|
+
`expect_job_of_type` is expected to be used with the matchers below.
|
282
|
+
|
283
|
+
#### have_activated
|
284
|
+
|
285
|
+
The `have_activated` matcher checks that the target represents an activated job. It will raise an error if no job
|
286
|
+
was activated.
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
expect_job_of_type("my_type").to have_activated
|
290
|
+
```
|
291
|
+
|
292
|
+
Various additional methods can be chained on the `have_activated` matcher.
|
293
|
+
|
294
|
+
The `with_variables` method can be used to check the input variables that the job was activated with:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
expect_job_of_type("my_type").to have_activated.with_variables(user_id: 123)
|
298
|
+
```
|
299
|
+
|
300
|
+
The `with_headers` method can be used to check the headers that the job was activated with:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
expect_job_of_type("my_type").to have_activated.with_headers(id_type: "user")
|
304
|
+
```
|
305
|
+
|
306
|
+
The `with_variables` and `with_headers` methods can be chained on the same expectation:
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
expect_job_of_type("my_type").to have_activated.
|
310
|
+
with_variables(user_id: 123).
|
311
|
+
with_headers(id_type: "user")
|
312
|
+
```
|
313
|
+
|
314
|
+
The matcher also supports methods to complete, fail, or throw an error for a job:
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
# Complete
|
318
|
+
expect_job_of_type("my_type").to have_activated.and_complete
|
319
|
+
|
320
|
+
# Complete with new variables
|
321
|
+
expect_job_of_type("my_type").to have_activated.and_complete(result_code: 456)
|
322
|
+
|
323
|
+
# Fail (sets retries to 0 by default)
|
324
|
+
expect_job_of_type("my_type").to have_activated.and_fail
|
325
|
+
|
326
|
+
# Fail and specify retries
|
327
|
+
expect_job_of_type("my_type").to have_activated.and_fail(retries: 1)
|
328
|
+
|
329
|
+
# Fail with an error message
|
330
|
+
expect_job_of_type("my_type").to have_activated.and_fail("boom!")
|
331
|
+
|
332
|
+
# Fail with an error message and specify retries
|
333
|
+
expect_job_of_type("my_type").to have_activated.and_fail("boom!", retries: 2)
|
334
|
+
|
335
|
+
# Throw an error (error code is required)
|
336
|
+
expect_job_of_type("my_type").to have_activated.and_throw_error("MY_ERROR")
|
337
|
+
|
338
|
+
# Throw an error with an error message
|
339
|
+
expect_job_of_type("my_type").to have_activated.and_throw_error("MY_ERROR", "went horribly wrong")
|
340
|
+
```
|
341
|
+
|
342
|
+
Only one of `and_complete`, `and_fail`, or `and_throw_error` can be specified for a single expectation.
|
343
|
+
|
344
|
+
#### have_variables and have_headers
|
345
|
+
|
346
|
+
In addition to the `with_variables` and `with_headers` methods that can be chained onto the `have_activated`
|
347
|
+
matcher, there are matchers that can be used directly to set expectations on the variables or
|
348
|
+
headers for an `ActivatedJob`.
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
job = activate_job("my_type")
|
352
|
+
|
353
|
+
expect(job).to have_variables(user: 123)
|
354
|
+
expect(job).to have_headers(id_type: "user")
|
355
|
+
```
|
356
|
+
|
357
|
+
## Tips & Tricks
|
358
|
+
|
359
|
+
### Enumerator for Multiple Jobs
|
360
|
+
|
361
|
+
When activating multiple jobs, call `to_a` on the result of `activate_jobs` to get
|
362
|
+
an array of activated jobs objects.
|
363
|
+
|
364
|
+
### Timer Duration
|
365
|
+
|
366
|
+
Specify timer durations using a variable so that tests can easily set the variable
|
367
|
+
to specify a short duration.
|
368
|
+
|
201
369
|
## Limitations
|
202
370
|
|
203
371
|
The current gem and approach have some limitations:
|
data/docker-compose.yml
CHANGED
@@ -6,7 +6,7 @@ x-environment: &default-environment
|
|
6
6
|
PRYRC: /usr/src/app/.docker-pryrc
|
7
7
|
BUNDLE_IGNORE_CONFIG: 1
|
8
8
|
BUNDLE_DISABLE_SHARED_GEMS: "true"
|
9
|
-
ZEEBE_ADDRESS: zeebe
|
9
|
+
ZEEBE_ADDRESS: zeebe:26500
|
10
10
|
x-service: &default-service
|
11
11
|
image: ruby:2.6.6
|
12
12
|
volumes:
|
@@ -18,7 +18,7 @@ x-service: &default-service
|
|
18
18
|
stdin_open: true
|
19
19
|
services:
|
20
20
|
zeebe:
|
21
|
-
image: camunda/zeebe
|
21
|
+
image: camunda/zeebe:${ZEEBE_VERSION:-0.24.1}
|
22
22
|
environment:
|
23
23
|
ZEEBE_LOG_LEVEL: debug
|
24
24
|
volumes:
|
@@ -28,8 +28,8 @@ services:
|
|
28
28
|
monitor:
|
29
29
|
image: camunda/zeebe-simple-monitor:0.19.0
|
30
30
|
environment:
|
31
|
-
- zeebe.client.broker.contactPoint=zeebe
|
32
|
-
- zeebe.client.worker.hazelcast.connection=zeebe
|
31
|
+
- zeebe.client.broker.contactPoint=zeebe:26500
|
32
|
+
- zeebe.client.worker.hazelcast.connection=zeebe:5701
|
33
33
|
ports:
|
34
34
|
- "8082:8082"
|
35
35
|
depends_on:
|
data/lib/zeebe_bpmn_rspec.rb
CHANGED
@@ -4,11 +4,12 @@ require "rspec"
|
|
4
4
|
require "zeebe/client"
|
5
5
|
require "zeebe_bpmn_rspec/helpers"
|
6
6
|
require "zeebe_bpmn_rspec/version"
|
7
|
+
require "zeebe_bpmn_rspec/matchers/have_activated"
|
7
8
|
|
8
9
|
# Top-level gem module
|
9
10
|
module ZeebeBpmnRspec
|
10
11
|
class << self
|
11
|
-
attr_writer :client, :zeebe_address
|
12
|
+
attr_writer :client, :zeebe_address, :activate_request_timeout
|
12
13
|
|
13
14
|
def configure
|
14
15
|
yield(self)
|
@@ -23,6 +24,10 @@ module ZeebeBpmnRspec
|
|
23
24
|
def zeebe_address
|
24
25
|
@zeebe_address || ENV["ZEEBE_ADDRESS"] || (raise "zeebe_address must be set")
|
25
26
|
end
|
27
|
+
|
28
|
+
def activate_request_timeout
|
29
|
+
@activate_request_timeout || 1000
|
30
|
+
end
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
@@ -8,27 +8,50 @@ module ZeebeBpmnRspec
|
|
8
8
|
class ActivatedJob
|
9
9
|
include ::Zeebe::Client::GatewayProtocol # for direct reference of request classes
|
10
10
|
|
11
|
-
attr_reader :job, :type
|
11
|
+
attr_reader :job, :type
|
12
12
|
|
13
|
-
def initialize(job, type:, workflow_instance_key:, client:, context:)
|
13
|
+
def initialize(job, type:, workflow_instance_key:, client:, context:, validate:) # rubocop:disable Metrics/ParameterLists
|
14
14
|
@job = job
|
15
15
|
@type = type
|
16
16
|
@workflow_instance_key = workflow_instance_key
|
17
17
|
@client = client
|
18
18
|
@context = context
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
expect(job
|
23
|
-
|
20
|
+
if validate
|
21
|
+
context.instance_eval do
|
22
|
+
expect(job).not_to be_nil, "expected to receive job of type '#{type}' but received none"
|
23
|
+
aggregate_failures do
|
24
|
+
expect(job.workflowInstanceKey).to eq(workflow_instance_key)
|
25
|
+
expect(job.type).to eq(type)
|
26
|
+
end
|
24
27
|
end
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
31
|
+
def raw
|
32
|
+
job
|
33
|
+
end
|
34
|
+
|
28
35
|
def key
|
29
36
|
job.key
|
30
37
|
end
|
31
38
|
|
39
|
+
def workflow_instance_key
|
40
|
+
job.workflowInstanceKey
|
41
|
+
end
|
42
|
+
|
43
|
+
def retries
|
44
|
+
job.retries
|
45
|
+
end
|
46
|
+
|
47
|
+
def task_key
|
48
|
+
job.elementInstanceKey
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
raw.to_s
|
53
|
+
end
|
54
|
+
|
32
55
|
def variables
|
33
56
|
@_variables ||= JSON.parse(job.variables)
|
34
57
|
end
|
@@ -66,16 +89,18 @@ module ZeebeBpmnRspec
|
|
66
89
|
}.compact
|
67
90
|
))
|
68
91
|
end
|
92
|
+
alias throw_error and_throw_error
|
69
93
|
|
70
|
-
def and_fail(message = nil)
|
94
|
+
def and_fail(message = nil, retries: nil)
|
71
95
|
client.fail_job(FailJobRequest.new(
|
72
96
|
{
|
73
97
|
jobKey: job.key,
|
74
|
-
retries: 0,
|
98
|
+
retries: retries || 0,
|
75
99
|
errorMessage: message,
|
76
100
|
}.compact
|
77
101
|
))
|
78
102
|
end
|
103
|
+
alias fail and_fail
|
79
104
|
|
80
105
|
def and_complete(variables = {})
|
81
106
|
client.complete_job(CompleteJobRequest.new(
|
@@ -83,6 +108,14 @@ module ZeebeBpmnRspec
|
|
83
108
|
variables: variables.to_json
|
84
109
|
))
|
85
110
|
end
|
111
|
+
alias complete and_complete
|
112
|
+
|
113
|
+
def update_retries(retries = 1)
|
114
|
+
client.update_job_retries(UpdateJobRetriesRequest.new(
|
115
|
+
jobKey: job.key,
|
116
|
+
retries: retries
|
117
|
+
))
|
118
|
+
end
|
86
119
|
|
87
120
|
private
|
88
121
|
|
@@ -26,7 +26,7 @@ module ZeebeBpmnRspec
|
|
26
26
|
variables: variables.to_json
|
27
27
|
))
|
28
28
|
@__workflow_instance_key = workflow.workflowInstanceKey
|
29
|
-
yield(workflow.workflowInstanceKey)
|
29
|
+
yield(workflow.workflowInstanceKey) if block_given?
|
30
30
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
31
31
|
# exceptions are rescued to ensure that instances are cancelled
|
32
32
|
# any error is re-raised below
|
@@ -64,37 +64,46 @@ module ZeebeBpmnRspec
|
|
64
64
|
@__workflow_instance_key
|
65
65
|
end
|
66
66
|
|
67
|
-
def activate_job(type)
|
68
|
-
stream = _zeebe_client.activate_jobs(ActivateJobsRequest.new(
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
67
|
+
def activate_job(type, fetch_variables: nil, validate: true, worker: "#{type}-#{SecureRandom.hex}")
|
68
|
+
stream = _zeebe_client.activate_jobs(ActivateJobsRequest.new({
|
69
|
+
type: type,
|
70
|
+
worker: worker,
|
71
|
+
maxJobsToActivate: 1,
|
72
|
+
timeout: 1000,
|
73
|
+
fetchVariable: fetch_variables&.then { |v| Array(v) },
|
74
|
+
requestTimeout: ZeebeBpmnRspec.activate_request_timeout,
|
75
|
+
}.compact))
|
75
76
|
|
76
77
|
job = nil
|
77
78
|
stream.find { |response| job = response.jobs.first }
|
78
|
-
raise "No job with type #{type.inspect} found" if job.nil?
|
79
|
-
|
80
79
|
# puts job.inspect # support debug logging?
|
81
80
|
|
82
81
|
ActivatedJob.new(job,
|
83
82
|
type: type,
|
84
83
|
workflow_instance_key: workflow_instance_key,
|
85
84
|
client: _zeebe_client,
|
86
|
-
context: self
|
85
|
+
context: self,
|
86
|
+
validate: validate)
|
87
87
|
end
|
88
88
|
alias process_job activate_job
|
89
89
|
# TODO: deprecate process_job
|
90
90
|
|
91
|
-
def
|
91
|
+
def job_with_type(type, fetch_variables: nil)
|
92
|
+
activate_job(type, fetch_variables: fetch_variables, validate: false)
|
93
|
+
end
|
94
|
+
|
95
|
+
def expect_job_of_type(type, fetch_variables: nil)
|
96
|
+
expect(job_with_type(type, fetch_variables: fetch_variables))
|
97
|
+
end
|
98
|
+
|
99
|
+
def activate_jobs(type, max_jobs: nil, fetch_variables: nil)
|
92
100
|
stream = _zeebe_client.activate_jobs(ActivateJobsRequest.new({
|
93
101
|
type: type,
|
94
102
|
worker: "#{type}-#{SecureRandom.hex}",
|
95
103
|
maxJobsToActivate: max_jobs,
|
96
|
-
timeout:
|
97
|
-
|
104
|
+
timeout: 1000,
|
105
|
+
fetchVariable: fetch_variables&.then { |v| Array(v) },
|
106
|
+
requestTimeout: ZeebeBpmnRspec.activate_request_timeout,
|
98
107
|
}.compact))
|
99
108
|
|
100
109
|
Enumerator.new do |yielder|
|
@@ -104,23 +113,32 @@ module ZeebeBpmnRspec
|
|
104
113
|
type: type,
|
105
114
|
workflow_instance_key: workflow_instance_key,
|
106
115
|
client: _zeebe_client,
|
107
|
-
context: self
|
116
|
+
context: self,
|
117
|
+
validate: true)
|
108
118
|
end
|
109
119
|
end
|
110
120
|
end
|
111
121
|
end
|
112
122
|
|
113
|
-
def publish_message(name, correlation_key:, variables: nil)
|
123
|
+
def publish_message(name, correlation_key:, variables: nil, ttl_ms: 5000)
|
114
124
|
_zeebe_client.publish_message(PublishMessageRequest.new(
|
115
125
|
{
|
116
126
|
name: name,
|
117
127
|
correlationKey: correlation_key,
|
118
|
-
timeToLive:
|
128
|
+
timeToLive: ttl_ms,
|
119
129
|
variables: variables&.to_json,
|
120
130
|
}.compact
|
121
131
|
))
|
122
132
|
end
|
123
133
|
|
134
|
+
def set_variables(key, variables, local: true)
|
135
|
+
_zeebe_client.set_variables(SetVariablesRequest.new(
|
136
|
+
elementInstanceKey: key,
|
137
|
+
variables: variables.to_json,
|
138
|
+
local: local
|
139
|
+
))
|
140
|
+
end
|
141
|
+
|
124
142
|
def reset_zeebe!
|
125
143
|
@__workflow_instance_key = nil
|
126
144
|
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "zeebe_bpmn_rspec/matchers/have_variables"
|
4
|
+
require "zeebe_bpmn_rspec/matchers/have_headers"
|
5
|
+
|
6
|
+
module ZeebeBpmnRspec
|
7
|
+
class HaveActivatedMatcherError < StandardError
|
8
|
+
def initialize
|
9
|
+
super("Only one of complete, fail, and throw error can be specified")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# rubocop:disable Metrics/BlockLength
|
15
|
+
RSpec::Matchers.define :have_activated do
|
16
|
+
match do |job|
|
17
|
+
@job = job
|
18
|
+
|
19
|
+
@matcher_error = nil
|
20
|
+
begin
|
21
|
+
aggregate_failures "activated job#{of_type(job)}" do
|
22
|
+
unless job.is_a?(ZeebeBpmnRspec::ActivatedJob)
|
23
|
+
raise ArgumentError.new("expectation target must be a "\
|
24
|
+
"#{ZeebeBpmnRspec::ActivatedJob.name}, got #{job.inspect}")
|
25
|
+
end
|
26
|
+
|
27
|
+
if job.raw.nil?
|
28
|
+
raise RSpec::Expectations::ExpectationNotMetError.new("expected activated job#{of_type(job)}, got nil")
|
29
|
+
end
|
30
|
+
|
31
|
+
expect(job).to have_variables(@variables) if @variables
|
32
|
+
expect(job).to have_headers(@headers) if @headers
|
33
|
+
end
|
34
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
35
|
+
@matcher_error = e
|
36
|
+
end
|
37
|
+
return false if @matcher_error
|
38
|
+
|
39
|
+
if @complete
|
40
|
+
job.complete(@output || {})
|
41
|
+
elsif @fail
|
42
|
+
job.fail(@fail_message, retries: @retries)
|
43
|
+
elsif @throw
|
44
|
+
job.throw_error(@error_code, @throw_message)
|
45
|
+
end
|
46
|
+
|
47
|
+
true
|
48
|
+
end
|
49
|
+
|
50
|
+
def of_type(job)
|
51
|
+
job.respond_to?(:type) ? " of type #{job.type}" : nil
|
52
|
+
end
|
53
|
+
|
54
|
+
failure_message do |_job|
|
55
|
+
raise matcher_error
|
56
|
+
end
|
57
|
+
|
58
|
+
attr_reader :job, :matcher_error
|
59
|
+
|
60
|
+
def predestined?
|
61
|
+
@complete || @fail || @throw
|
62
|
+
end
|
63
|
+
|
64
|
+
def check_predestined!
|
65
|
+
raise ZeebeBpmnRspec::HaveActivatedMatcherError.new if predestined?
|
66
|
+
end
|
67
|
+
|
68
|
+
chain :with_variables do |variables|
|
69
|
+
@variables = variables.is_a?(Hash) ? variables.stringify_keys : variables
|
70
|
+
end
|
71
|
+
|
72
|
+
chain :with_headers do |headers|
|
73
|
+
@headers = headers.is_a?(Hash) ? headers.stringify_keys : headers
|
74
|
+
end
|
75
|
+
|
76
|
+
chain :and_complete do |output = nil|
|
77
|
+
check_predestined!
|
78
|
+
|
79
|
+
@output = output
|
80
|
+
@complete = true
|
81
|
+
end
|
82
|
+
|
83
|
+
chain :and_fail do |message = nil, retries: 0|
|
84
|
+
check_predestined!
|
85
|
+
|
86
|
+
@fail_message = message
|
87
|
+
@retries = retries
|
88
|
+
@fail = true
|
89
|
+
end
|
90
|
+
|
91
|
+
chain :and_throw_error do |error_code, message = nil|
|
92
|
+
check_predestined!
|
93
|
+
|
94
|
+
@error_code = error_code
|
95
|
+
@throw_message = message
|
96
|
+
@throw = true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
# rubocop:enable Metrics/BlockLength
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec::Matchers.define :have_headers do
|
4
|
+
match do |actual|
|
5
|
+
@job = actual
|
6
|
+
@actual = @job.headers
|
7
|
+
values_match?(expected, @actual)
|
8
|
+
end
|
9
|
+
|
10
|
+
failure_message do |_actual|
|
11
|
+
"expected that job:\n #{@job}\n\nwould have headers #{expected}"
|
12
|
+
end
|
13
|
+
|
14
|
+
diffable
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec::Matchers.define :have_variables do |expected|
|
4
|
+
match do |actual|
|
5
|
+
@job = actual
|
6
|
+
@actual = @job.variables
|
7
|
+
values_match?(expected, @actual)
|
8
|
+
end
|
9
|
+
|
10
|
+
failure_message do |_actual|
|
11
|
+
"expected that job:\n #{@job}\n\nwould have variables #{expected}"
|
12
|
+
end
|
13
|
+
|
14
|
+
diffable
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zeebe_bpmn_rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ezCater, Inc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -152,6 +152,9 @@ files:
|
|
152
152
|
- lib/zeebe_bpmn_rspec.rb
|
153
153
|
- lib/zeebe_bpmn_rspec/activated_job.rb
|
154
154
|
- lib/zeebe_bpmn_rspec/helpers.rb
|
155
|
+
- lib/zeebe_bpmn_rspec/matchers/have_activated.rb
|
156
|
+
- lib/zeebe_bpmn_rspec/matchers/have_headers.rb
|
157
|
+
- lib/zeebe_bpmn_rspec/matchers/have_variables.rb
|
155
158
|
- lib/zeebe_bpmn_rspec/version.rb
|
156
159
|
- zeebe_bpmn_rspec.gemspec
|
157
160
|
homepage: https://github.com/ezcater/zeebe_bpmn_rspec
|
@@ -174,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
177
|
- !ruby/object:Gem::Version
|
175
178
|
version: '0'
|
176
179
|
requirements: []
|
177
|
-
rubygems_version: 3.
|
180
|
+
rubygems_version: 3.0.3
|
178
181
|
signing_key:
|
179
182
|
specification_version: 4
|
180
183
|
summary: Zeebe BPMN testing using RSpec
|