-
Notifications
You must be signed in to change notification settings - Fork 22
/
CallActivityMockExampleTest.java
356 lines (287 loc) · 13.2 KB
/
CallActivityMockExampleTest.java
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package org.camunda.community.mockito;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessInstanceWithVariablesImpl;
import org.camunda.bpm.engine.impl.persistence.entity.TimerEntity;
import org.camunda.bpm.engine.runtime.EventSubscription;
import org.camunda.bpm.engine.runtime.Job;
import org.camunda.bpm.engine.runtime.JobQuery;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.community.mockito.function.DeployProcess;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.camunda.bpm.engine.variable.Variables.createVariables;
import static org.camunda.bpm.model.xml.test.assertions.ModelAssertions.assertThat;
import static org.camunda.community.mockito.MostUsefulProcessEngineConfiguration.mostUsefulProcessEngineConfiguration;
import static org.camunda.community.mockito.ProcessExpressions.registerCallActivityMock;
import static org.junit.Assert.assertEquals;
public class CallActivityMockExampleTest {
private static final String PROCESS_ID = "myProcess";
private static final String SUB_PROCESS_ID = "mySubProcess";
private static final String SUB_PROCESS2_ID = "mySubProcess2";
private static final String MESSAGE_DOIT = "DOIT";
private static final String SIGNAL_ALLDOIT = "ALLDOIT";
private static final String TASK_USERTASK = "user_task";
@Rule
public final ProcessEngineRule camunda = new ProcessEngineRule(mostUsefulProcessEngineConfiguration().buildProcessEngine());
@Before
public void setUp() {
prepareProcessWithOneSubprocess();
}
@Test
public void register_subprocess_mock_addVar() {
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionAddVariable("foo", "bar")
.deploy(camunda));
final ProcessInstance processInstance = startProcess(PROCESS_ID);
isWaitingAt(processInstance, TASK_USERTASK);
//TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*)
//assertThat(processInstance).hasVariables("foo", "bar");
final Map<String, Object> variables = camunda.getRuntimeService().getVariables(processInstance.getId());
assertThat(variables).hasSize(1);
assertThat(variables).containsEntry("foo", "bar");
}
@Test
public void register_subprocess_mock_withOwnConsumer() {
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionDo(execution -> {
execution.setVariable("foo", "barbar");
})
.deploy(camunda));
final ProcessInstance processInstance = startProcess(PROCESS_ID);
isWaitingAt(processInstance, TASK_USERTASK);
//TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*)
//assertThat(processInstance).hasVariables("foo", "bar");
final Map<String, Object> variables = camunda.getRuntimeService().getVariables(processInstance.getId());
assertThat(variables).hasSize(1);
assertThat(variables).containsEntry("foo", "barbar");
}
@Test
public void register_subprocess_mock_withReceiveMessage() {
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionWaitForMessage(MESSAGE_DOIT)
.deploy(camunda));
final ProcessInstance processInstance = startProcess(PROCESS_ID);
assertThatProcessIsWaitingForMessage(MESSAGE_DOIT);
camunda.getRuntimeService().correlateMessage(MESSAGE_DOIT);
isWaitingAt(processInstance, TASK_USERTASK);
}
@Test
public void register_subprocess_mock_withReceiveSignal() {
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionWaitForSignal(SIGNAL_ALLDOIT)
.deploy(camunda));
final ProcessInstance processInstance = startProcess(PROCESS_ID);
assertThatProcessIsWaitingForSignal(SIGNAL_ALLDOIT);
camunda.getRuntimeService().createSignalEvent(SIGNAL_ALLDOIT).send();
isWaitingAt(processInstance, TASK_USERTASK);
}
@Test
public void register_subprocess_mock_withSendMessage() {
camunda.manageDeployment(
registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionSendMessage(MESSAGE_DOIT)
.deploy(camunda)
);
final String waitForMessageId = "waitForMessage";
final BpmnModelInstance waitForMessage = Bpmn.createExecutableProcess(waitForMessageId)
.camundaHistoryTimeToLive(1)
.startEvent("start")
.intermediateCatchEvent("waitForMessageCatchEvent")
.message(MESSAGE_DOIT)
.endEvent("end")
.done();
camunda.manageDeployment(new DeployProcess(camunda).apply(waitForMessageId, waitForMessage));
//Start monitoring process for testing
final ProcessInstance waitingProcessInstance = startProcess(waitForMessageId);
assertThatProcessIsWaitingForMessage(MESSAGE_DOIT);
//Start our process with mocked subprocess
final ProcessInstance processInstance = startProcess(PROCESS_ID);
isWaitingAt(processInstance, TASK_USERTASK);
//Our monitoring process should be finished
isEnded(waitingProcessInstance);
}
@Test
public void register_subprocess_mock_withTimerDate() {
final Date date = Date.from(Instant.now().plusSeconds(60));
registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionWaitForTimerWithDate(date)
.deploy(camunda);
startProcess(PROCESS_ID);
assertThatTimerIsWaitingUntil(date);
}
@Test
public void register_subprocess_mock_withTimerDuration() {
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionWaitForTimerWithDuration("PT60S")
.deploy(camunda));
startProcess(PROCESS_ID);
assertThatTimerIsWaitingUntil(Date.from(Instant.now().plusSeconds(60)));
}
@Test(expected = RuntimeException.class)
public void register_subprocess_mock_withException() {
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionRunIntoError(new Exception("No"))
.deploy(camunda));
startProcess(PROCESS_ID);
}
@Test
public void register_subprocesses_mocks_withVariables() {
final BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID)
.camundaHistoryTimeToLive(1)
.startEvent("start")
.callActivity("call_subprocess")
.camundaOut("foo", "foo")
.calledElement(SUB_PROCESS_ID)
.callActivity("call_subprocess2")
.calledElement(SUB_PROCESS2_ID)
.camundaOut("bar", "bar")
.userTask(TASK_USERTASK)
.endEvent("end")
.done();
camunda.manageDeployment(new DeployProcess(camunda).apply(PROCESS_ID, processWithSubProcess));
registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionSetVariables(createVariables().putValue("foo", "bar"))
.deploy(camunda);
registerCallActivityMock(SUB_PROCESS2_ID)
.onExecutionSetVariables(createVariables().putValue("bar", "foo"))
.deploy(camunda);
final ProcessInstance processInstance = startProcess(PROCESS_ID);
isWaitingAt(processInstance, TASK_USERTASK);
//TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*)
//assertThat(processInstance).hasVariables("foo", "bar");
final Map<String, Object> variables = camunda.getRuntimeService().getVariables(processInstance.getId());
assertThat(variables).hasSize(2);
assertThat(variables).containsEntry("foo", "bar");
assertThat(variables).containsEntry("bar", "foo");
}
@Test
public void register_subprocesses_mocks_withWaitMessage_and_timer_and_setVariable() {
prepareProcessWithOneSubprocess();
final Date waitUntil = Date.from(Instant.now().plusSeconds(60));
registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionWaitForMessage(MESSAGE_DOIT)
.onExecutionWaitForTimerWithDate(waitUntil)
.onExecutionSetVariables(createVariables().putValue("foo", "bar"))
.deploy(camunda);
final ProcessInstance processInstance = startProcess(PROCESS_ID);
//Message should wait for message
assertThatProcessIsWaitingForMessage(MESSAGE_DOIT);
camunda.getRuntimeService().correlateMessage(MESSAGE_DOIT);
//Message should wait for date
Job job = assertThatTimerIsWaitingUntil(waitUntil);
execute(job);
//Process should wait at user task
isWaitingAt(processInstance, TASK_USERTASK);
//TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*)
//assertThat(processInstance).hasVariables("foo", "bar");
final Map<String, Object> variables = camunda.getRuntimeService().getVariables(processInstance.getId());
assertThat(variables).hasSize(1);
assertThat(variables).containsEntry("foo", "bar");
}
@Test
public void register_subprocess_mock_throwEscalation() {
String escalationCode = "SOME_ERROR";
String subprocessId = "call_subprocess";
String escalationEndId = "EscalationEnd";
BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID)
.camundaHistoryTimeToLive(1)
.startEvent("start")
.callActivity(subprocessId)
.calledElement(SUB_PROCESS_ID)
.boundaryEvent()
.escalation(escalationCode)
.endEvent(escalationEndId)
.moveToActivity(subprocessId)
.userTask(TASK_USERTASK)
.endEvent("end")
.done();
camunda.manageDeployment(new DeployProcess(camunda).apply(PROCESS_ID, processWithSubProcess));
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionThrowEscalation(escalationCode)
.deploy(camunda));
ProcessInstance processInstance = startProcess(PROCESS_ID);
isEnded(processInstance);
assertEquals(escalationEndId, ((ProcessInstanceWithVariablesImpl) processInstance).getExecutionEntity().getActivityId());
}
@Test
public void register_subprocess_mock_throwError() {
String errorCode = "SOME_ERROR";
String subprocessId = "call_subprocess";
String errorEndId = "ErrorEnd";
BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID)
.camundaHistoryTimeToLive(1)
.startEvent("start")
.callActivity(subprocessId)
.calledElement(SUB_PROCESS_ID)
.boundaryEvent()
.error(errorCode)
.endEvent(errorEndId)
.moveToActivity(subprocessId)
.userTask(TASK_USERTASK)
.endEvent("end")
.done();
camunda.manageDeployment(new DeployProcess(camunda).apply(PROCESS_ID, processWithSubProcess));
camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID)
.onExecutionThrowError(errorCode)
.deploy(camunda));
ProcessInstance processInstance = startProcess(PROCESS_ID);
isEnded(processInstance);
assertEquals(errorEndId, ((ProcessInstanceWithVariablesImpl) processInstance).getExecutionEntity().getActivityId());
}
private void prepareProcessWithOneSubprocess() {
final BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID)
.camundaHistoryTimeToLive(1)
.startEvent("start")
.callActivity("call_subprocess")
.camundaOut("foo", "foo")
.calledElement(SUB_PROCESS_ID)
.userTask(TASK_USERTASK)
.endEvent("end")
.done();
camunda.manageDeployment(new DeployProcess(camunda).apply(PROCESS_ID, processWithSubProcess));
}
private void assertThatProcessIsWaitingForMessage(String message) {
final EventSubscription eventSubscription = camunda.getRuntimeService().createEventSubscriptionQuery().singleResult();
assertThat(eventSubscription).isNotNull();
assertThat(eventSubscription.getEventName()).isEqualTo(message);
}
private void assertThatProcessIsWaitingForSignal(String signalName) {
final EventSubscription eventSubscription = camunda.getRuntimeService().createEventSubscriptionQuery().singleResult();
assertThat(eventSubscription).isNotNull();
assertThat(eventSubscription.getEventName()).isEqualTo(signalName);
}
private Job assertThatTimerIsWaitingUntil(Date date) {
final List<Job> list = jobQuery().list();
assertThat(list).hasSize(1);
final Job timer = list.get(0);
assertThat(timer).isInstanceOf(TimerEntity.class);
assertThat(timer.getDuedate()).isInSameSecondWindowAs(date); // Check the time distance (second boundary does not matter)
return timer;
}
private ProcessInstance startProcess(final String key) {
return camunda.getRuntimeService().startProcessInstanceByKey(key);
}
private void isWaitingAt(ProcessInstance processInstance, String activityId) {
assertThat(camunda.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstance.getId()).activityIdIn(activityId).active().singleResult()).isNotNull();
}
private void isEnded(ProcessInstance processInstance) {
Optional<HistoricProcessInstance> hi = Optional.ofNullable(camunda.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult())
.filter(h -> h.getEndTime() != null);
assertThat(hi).as("instance not ended").isNotEmpty();
}
private JobQuery jobQuery() {
return camunda.getManagementService().createJobQuery().active();
}
private void execute(Job job) {
camunda.getManagementService().executeJob(job.getId());
}
}