Skip to content

Commit 3ddb025

Browse files
committed
docu
1 parent c91e91c commit 3ddb025

File tree

1 file changed

+195
-87
lines changed

1 file changed

+195
-87
lines changed

src/site/markdown/testing.md

Lines changed: 195 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,139 @@
11
# Imixs-Workflow - Simulation and Testing
22

3-
The Imixs-Workflow project provides a services and test environments to simulate and test the processing life cycle of a specific model or process instance.
3+
The Imixs-Workflow project provides services and test environments to simulate and test the processing life cycle of a specific model or process instance. You can use these services to implement JUnit test classes to test without the need of a full deployment.
44

5-
## Simulate a Processing Live Cycle
5+
For a Maven project just add the following dependencies into your projects `pom.xml` file:
6+
7+
```xml
8+
....
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<jakarta.version>10.0.0</jakarta.version>
15+
<org.imixs.workflow.version>6.1.0</org.imixs.workflow.version>
16+
<microprofile.version>6.0</microprofile.version>
17+
<microprofile-metrics.version>4.0</microprofile-metrics.version>
18+
<!-- test dependencies -->
19+
<junit.jupiter.version>5.9.2</junit.jupiter.version>
20+
<mockito.version>5.8.0</mockito.version>
21+
<org.imixs.mock.version>6.1.0</org.imixs.mock.version>
22+
</properties>
23+
....
24+
<build>
25+
<plugins>
26+
..........
27+
<!-- use JDK settings for compiling -->
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>3.8.1</version>
32+
<configuration>
33+
<source>11</source>
34+
<target>11</target>
35+
</configuration>
36+
</plugin>
37+
<!-- Testing JUnit 5 -->
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-surefire-plugin</artifactId>
41+
<version>2.22.2</version>
42+
</plugin>
43+
</plugins>
44+
...........
45+
<testResources>
46+
<testResource>
47+
<directory>${basedir}/src/test/resources</directory>
48+
</testResource>
49+
</testResources>
50+
<dependencies>
51+
<!-- Imixs Workflow -->
52+
<dependency>
53+
<groupId>org.imixs.workflow</groupId>
54+
<artifactId>imixs-workflow-core</artifactId>
55+
<version>${org.imixs.workflow.version}</version>
56+
</dependency>
57+
<!-- Jakarta EE -->
58+
<dependency>
59+
<groupId>jakarta.platform</groupId>
60+
<artifactId>jakarta.jakartaee-api</artifactId>
61+
<version>${jakarta.version}</version>
62+
<scope>provided</scope>
63+
</dependency>
64+
<!-- JUnit 5 Dependencies -->
65+
<dependency>
66+
<groupId>org.junit.jupiter</groupId>
67+
<artifactId>junit-jupiter-api</artifactId>
68+
<version>${junit.jupiter.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.junit.jupiter</groupId>
73+
<artifactId>junit-jupiter-engine</artifactId>
74+
<version>${junit.jupiter.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.mockito</groupId>
79+
<artifactId>mockito-core</artifactId>
80+
<version>${mockito.version}</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.mockito</groupId>
85+
<artifactId>mockito-junit-jupiter</artifactId>
86+
<version>${mockito.version}</version>
87+
<scope>test</scope>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.eclipse.parsson</groupId>
91+
<artifactId>jakarta.json</artifactId>
92+
<version>1.1.1</version>
93+
<scope>test</scope>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.glassfish.jaxb</groupId>
97+
<artifactId>jaxb-runtime</artifactId>
98+
<version>3.0.0</version>
99+
<scope>test</scope>
100+
</dependency>
101+
<dependency>
102+
<groupId>jakarta.xml.bind</groupId>
103+
<artifactId>jakarta.xml.bind-api</artifactId>
104+
<version>3.0.0</version>
105+
<scope>test</scope>
106+
</dependency>
107+
<dependency>
108+
<groupId>org.imixs.workflow</groupId>
109+
<artifactId>imixs-mock</artifactId>
110+
<version>${org.imixs.mock.version}</version>
111+
<scope>test</scope>
112+
</dependency>
113+
</dependencies>
114+
......
6115

7-
The `WorkflowService` provides a method called `evalNextTask`. This method evaluates the next task for a process instance (workitem) based on the current model definition. A Workitem must at least provide the properties `$TASKID` and `$EVENTID`. The method call can be helpful in many cases when a business logic just need to compute the next logical BPMN Task Element which will be assigned with a given process instance.
116+
```
117+
118+
## Simulate a Processing Live Cycle
8119

120+
The `WorkflowService` provides a method called `evalNextTask`. This method evaluates the next task for a process instance (workitem) based on the current model definition. A Workitem must at least provide the properties `$TASKID` and `$EVENTID`. The method call can be helpful in many cases when a business logic just need to compute the next logical BPMN Task Element which will be assigned with a given process instance.
9121

10122
```java
11123
try {
12-
// simulate the processing life cycle of a given workitem
13-
ItemCollection nextTaskEnity = workflowService.evalNextTask(workitem);
124+
// simulate the processing life cycle of a given workitem
125+
ItemCollection nextTaskEnity = workflowService.evalNextTask(workitem);
14126
} catch (ModelException e) {
15-
throw new PluginException(DocumentComposerPlugin.class.getSimpleName(), e.getErrorCode(), e.getMessage());
127+
throw new PluginException(DocumentComposerPlugin.class.getSimpleName(), e.getErrorCode(), e.getMessage());
16128
}
17129
```
18130

19131
**Note:** During the evaluation life-cycle more than one events can be evaluated. This depends on the model definition which can define follow-up-events, split-events and conditional events. The `evalNextTask` method did not persist the process instance or execute any plugin or adapter classes.
20132

21-
22133
## Testing with the WorkflowMockEnvironment
23134

24-
The Test class `WorkflowMockEnvironment` mocks a full workflow environment including a in-memory-database. The `WorkflowMockEnvironment` can be used for more complex integration tests using JUnit 5 or higher.
135+
The Test class `WorkflowMockEnvironment` mocks a full workflow environment including a in-memory-database. The `WorkflowMockEnvironment` can be used for more complex integration tests using JUnit 5 or higher.
25136

26-
27137
```java
28138
import static org.junit.jupiter.api.Assertions.assertEquals;
29139
import org.imixs.workflow.ItemCollection;
@@ -34,76 +144,75 @@ import org.junit.jupiter.api.Test;
34144

35145
public class TestTemplate {
36146

37-
protected WorkflowMockEnvironment workflowEnvironment;
38-
39-
/**
40-
* Setup the Mock environment
41-
*/
42-
@BeforeEach
43-
public void setUp() throws PluginException, ModelException {
44-
workflowEnvironment = new WorkflowMockEnvironment();
45-
workflowEnvironment.setUp();
46-
workflowEnvironment.loadBPMNModel("/bpmn/TestWorkflowService.bpmn");
47-
}
48-
49-
/**
50-
* This test simulates a workflowService process call.
51-
*/
52-
@Test
53-
public void testProcessSimple() {
54-
try {
55-
// load a test workitem
56-
ItemCollection workitem = workflowEnvironment.getDocumentService().load("W0000-00001");
57-
workitem.model("1.0.0").task(100).event(10);
58-
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
59-
// expected new task is 200
60-
assertEquals(200, workitem.getTaskID());
61-
} catch (AccessDeniedException | ProcessingErrorException | PluginException | ModelException e) {
62-
Assert.fail(e.getMessage());
63-
}
64-
}
65-
66-
/**
67-
* Test a complex workflow process with conditional events
68-
*/
69-
@Test
70-
public void testConditionalEvent1()
71-
throws AccessDeniedException, ProcessingErrorException, PluginException, ModelException {
72-
73-
workflowEnvironment.loadBPMNModel("/bpmn/conditional_event1.bpmn");
74-
// load test workitem
75-
ItemCollection workitem = workflowEnvironment.getDocumentService().load("W0000-00001");
76-
workitem.replaceItemValue("_budget", 99);
77-
workitem.model("1.0.0").task(1000).event(10);
78-
// test _budget<100 => 1200
79-
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
80-
Assert.assertEquals(1200, workitem.getTaskID());
81-
// test _budget>100 => 1100
82-
workitem.replaceItemValue("_budget", 9999);
83-
workitem.model("1.0.0").task(1000).event(10);
84-
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
85-
Assert.assertEquals(1100, workitem.getTaskID());
86-
}
147+
protected WorkflowMockEnvironment workflowEnvironment;
148+
149+
/**
150+
* Setup the Mock environment
151+
*/
152+
@BeforeEach
153+
public void setUp() throws PluginException, ModelException {
154+
workflowEnvironment = new WorkflowMockEnvironment();
155+
workflowEnvironment.setUp();
156+
workflowEnvironment.loadBPMNModel("/bpmn/TestWorkflowService.bpmn");
157+
}
158+
159+
/**
160+
* This test simulates a workflowService process call.
161+
*/
162+
@Test
163+
public void testProcessSimple() {
164+
try {
165+
// load a test workitem
166+
ItemCollection workitem = new ItemCollection().model("1.0.0").task(100).event(10);
167+
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
168+
// expected new task is 200
169+
assertEquals(200, workitem.getTaskID());
170+
} catch (AccessDeniedException | ProcessingErrorException | PluginException | ModelException e) {
171+
Assert.fail(e.getMessage());
172+
}
173+
}
174+
175+
/**
176+
* Test a complex workflow process with conditional events
177+
*/
178+
@Test
179+
public void testConditionalEvent()
180+
throws AccessDeniedException, ProcessingErrorException, PluginException, ModelException {
181+
182+
// load test workitem
183+
ItemCollection workitem = new ItemCollection().model("1.0.0").task(200).event(30);
184+
// add some business data...
185+
workitem.replaceItemValue("name","Imixs Software Solutions");
186+
workitem.replaceItemValue("budget", 1000.00);
187+
// test _budget<=1000 => 1200
188+
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
189+
Assert.assertEquals(1200, workitem.getTaskID());
190+
// test _budget>1000 => 1100
191+
workitem = new ItemCollection().model("1.0.0").task(200).event(30);
192+
workitem.replaceItemValue("budget", 9999);
193+
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
194+
Assert.assertEquals(1100, workitem.getTaskID());
195+
}
87196
}
88197

89198
```
90199

91-
92200
### How to setup a test case
93-
To setup a test case the Imixs `WorkflowMockEnvironment` provides a setup method to initialize the environment and a loadBPMNModel method to load a test model. To setup the environment it is recommended to call the setup() method in a `org.junit.jupiter.api.BeforeEach` annotated init method:
201+
202+
To setup a test case the Imixs `WorkflowMockEnvironment` provides a setup method to initialize the environment and the method `loadBPMNModel` to load a test model. To setup the environment it is recommended to call the setup() method in a `org.junit.jupiter.api.BeforeEach` annotated init method:
94203

95204
```java
96205
@BeforeEach
97206
public void setUp() throws PluginException, ModelException {
98-
workflowEnvironment = new WorkflowMockEnvironment();
99-
workflowEnvironment.setUp();
100-
workflowEnvironment.loadBPMNModel("/bpmn/TestWorkflowService.bpmn");
207+
workflowEnvironment = new WorkflowMockEnvironment();
208+
workflowEnvironment.setUp();
209+
workflowEnvironment.loadBPMNModel("/bpmn/TestWorkflowService.bpmn");
101210
}
102211
```
103-
104-
212+
105213
### How to test business cases
106-
The main goal of the `WorkflowMockEnvironment` is to test business logic of a specific workflow model. A workflow instance can be created form a empty ItemCollection and tested with any kind of data.
214+
215+
The main goal of the `WorkflowMockEnvironment` is to test business logic of a specific workflow model. A workflow instance can be created form a empty ItemCollection and tested with any kind of data.
107216

108217
```java
109218
// Load a test model
@@ -116,36 +225,35 @@ workitem.model("1.0.0").task(1000).event(10);
116225
workitem = workflowEnvironment.workflowService.processWorkItem(workitem);
117226
// evaluate the results....
118227
Assert.assertEquals(1200, workitem.getTaskID());
228+
String uniqueID=workitem.getUniqueID();
229+
workitem=workflowEnvironment.getDocumentService().load(uniqueID);
230+
Assert.notNull(workitem);
119231
```
120232

121-
122-
123233
### Mock Imixs Adapter Classes
124234

125-
The `WorkflowMockEnvironment` also allows you to test custom Imixs Workflow `GenericAdapter` or `SignalAdapter` classes. You just need to call the method `registerAdapter(...)` method before you call `setup()`. See the following example:
235+
The `WorkflowMockEnvironment` also allows you to test custom Imixs Workflow `GenericAdapter` or `SignalAdapter` classes. You just need to call the method `registerAdapter(...)` method before you call `setup()`. See the following example:
126236

127237
```java
128238

129239
public class MyAdapterTest {
130240

131-
@InjectMocks
132-
protected MyImixsAdapter myAdapter;
133-
134-
protected WorkflowMockEnvironment workflowEnvironment;
241+
@InjectMocks
242+
protected MyImixsAdapter myAdapter;
135243

136-
@BeforeEach
137-
public void setUp() throws PluginException, ModelException {
138-
// Ensures that @Mock and @InjectMocks annotations are processed
139-
MockitoAnnotations.openMocks(this);
140-
workflowEnvironment = new WorkflowMockEnvironment();
244+
protected WorkflowMockEnvironment workflowEnvironment;
141245

142-
// register AccessAdapter Mock
143-
workflowEnvironment.registerAdapter(accessAdapter);
144-
145-
// Setup Environment
146-
workflowEnvironment.setUp();
147-
}
148-
.....
246+
@BeforeEach
247+
public void setUp() throws PluginException, ModelException {
248+
// Ensures that @Mock and @InjectMocks annotations are processed
249+
MockitoAnnotations.openMocks(this);
250+
workflowEnvironment = new WorkflowMockEnvironment();
251+
// register AccessAdapter Mock
252+
workflowEnvironment.registerAdapter(accessAdapter);
253+
// Setup Environment
254+
workflowEnvironment.setUp();
255+
}
256+
.....
149257
}
150258

151-
```
259+
```

0 commit comments

Comments
 (0)