13
13
import java .util .ArrayList ;
14
14
import java .util .List ;
15
15
import java .util .Map ;
16
+ import java .util .Objects ;
16
17
import java .util .Optional ;
17
18
import java .util .function .Predicate ;
18
19
import java .util .stream .Collectors ;
19
20
20
21
import org .camunda .bpm .engine .delegate .DelegateTask ;
21
22
import org .camunda .bpm .model .bpmn .Bpmn ;
22
23
import org .camunda .bpm .model .bpmn .BpmnModelInstance ;
24
+ import org .camunda .bpm .model .bpmn .instance .ConditionExpression ;
23
25
import org .camunda .bpm .model .bpmn .instance .ExclusiveGateway ;
26
+ import org .camunda .bpm .model .bpmn .instance .FlowNode ;
24
27
import org .camunda .bpm .model .bpmn .instance .Process ;
25
28
import org .camunda .bpm .model .bpmn .instance .SequenceFlow ;
26
29
import org .camunda .bpm .model .bpmn .instance .ServiceTask ;
44
47
import dev .dsf .process .tutorial .ConstantsTutorial ;
45
48
import dev .dsf .process .tutorial .TestProcessPluginGenerator ;
46
49
import dev .dsf .process .tutorial .TutorialProcessPluginDefinition ;
50
+ import dev .dsf .process .tutorial .Utils ;
47
51
48
52
@ RunWith (MockitoJUnitRunner .class )
49
53
public class BpmnAndUserTaskListenerTest
@@ -77,7 +81,7 @@ public void testVoteBpmnFile()
77
81
78
82
Process process = processes .get (0 );
79
83
80
- String errorMissingUserTask = "Process '" + processId + "' in file '" + filename + "is missing a User Task" ;
84
+ String errorMissingUserTask = "Process '" + processId + "' in file '" + filename + "' is missing a User Task" ;
81
85
int userTaskCount = process .getChildElementsByType (UserTask .class ).size ();
82
86
assertTrue (errorMissingUserTask , userTaskCount > 0 );
83
87
@@ -87,21 +91,21 @@ public void testVoteBpmnFile()
87
91
.filter (userTask -> userTask .getIncoming ().stream ()
88
92
.anyMatch (isFlowConnectingUserTaskAndExclusiveGateway (userTask )))
89
93
.filter (userTask -> userTask .getOutgoing ().stream ()
90
- .anyMatch (isFlowConnectingUserTaskAndSaveUserVoteServer (userTask )))
94
+ .anyMatch (isFlowConnectingUserTaskAndSaveUserVoteServiceTask (userTask )))
91
95
.findFirst ();
92
96
assertTrue (errorMissingCorrectUserTask , optUserTask .isPresent ());
93
97
UserTask userTask = optUserTask .get ();
94
98
95
99
String errorUserTaskIncomingFlowMissingCondition = "User Task in process '" + processId + "' in file '"
96
- + filename + " with name " + userTask .getOutgoing ()
100
+ + filename + "' with name " + userTask .getName ()
97
101
+ " is missing condition expression '${userVote}' on incoming flow from exclusive gateway with name 'User Vote?'" ;
98
102
assertTrue (errorUserTaskIncomingFlowMissingCondition ,
99
103
userTask .getIncoming ().stream ().filter (isFlowConnectingUserTaskAndExclusiveGateway (userTask ))
100
104
.allMatch (hasCorrectConditionExpression ()));
101
105
102
106
String errorUserTaskIsMissingCorrectFormKey = "User Task in process '" + processId + "' in file '" + filename
103
- + " with name " + userTask .getOutgoing () + " is missing Form Key with value " + questionnaireUrl ;
104
- assertEquals (errorUserTaskIsMissingCorrectFormKey , userTask .getCamundaFormKey (), questionnaireUrl );
107
+ + "' with name " + userTask .getName () + " is missing Form Key with value " + questionnaireUrl ;
108
+ assertEquals (errorUserTaskIsMissingCorrectFormKey , questionnaireUrl , userTask .getCamundaFormKey ());
105
109
106
110
String packageName = "dev.dsf.process.tutorial.listener" ;
107
111
String errorNoUserTaskListenerFound = "No class extending DefaultUserTaskListener found in package '"
@@ -110,7 +114,7 @@ public void testVoteBpmnFile()
110
114
assertTrue (errorNoUserTaskListenerFound , !userTaskListeners .isEmpty ());
111
115
112
116
String errorUserTaskIsMissingTaskListener = "User Task in process '" + processId + "' in file '" + filename
113
- + " with name " + userTask .getOutgoing ()
117
+ + "' with name " + userTask .getName ()
114
118
+ " is missing at least one Task Listener which extends DefaultUserTaskListener. Found classes to add which extend DefaultUserTaskListener: "
115
119
+ userTaskListeners .stream ().map (Class ::getSimpleName ).reduce ("" , (i , next ) -> i + next + " " );
116
120
List <CamundaTaskListener > camundaTaskListeners = userTask
@@ -135,8 +139,8 @@ public void testVoteBpmnFile()
135
139
.collect (Collectors .toMap (userTaskListener -> userTaskListener , this ::validateUserTaskListener ));
136
140
137
141
String errorNoTaskListenerInUserTaskIsValid = "User Task in process '" + processId + "' in file '" + filename
138
- + " with name " + userTask .getOutgoing ()
139
- + " is missing at least one valid UserTaskListener. Errors are: \n " ;
142
+ + " with name " + userTask .getName ()
143
+ + "' is missing at least one valid UserTaskListener. Errors are: \n " ;
140
144
errorNoTaskListenerInUserTaskIsValid += userTaskListenersWithErrors .keySet ().stream ()
141
145
.map (key -> formatErrors (key , userTaskListenersWithErrors .get (key ))).collect (Collectors .joining ());
142
146
@@ -146,27 +150,51 @@ public void testVoteBpmnFile()
146
150
147
151
private Predicate <SequenceFlow > isFlowConnectingUserTaskAndExclusiveGateway (UserTask userTask )
148
152
{
149
- return flow -> flow .getTarget ().equals (userTask ) && flow .getSource () instanceof ExclusiveGateway
150
- && flow .getSource ().getName ().equals ("User Vote?" );
153
+ return flow ->
154
+ {
155
+ FlowNode target = flow .getTarget ();
156
+ FlowNode source = flow .getSource ();
157
+ if (Objects .nonNull (target ) && Objects .nonNull (source ))
158
+ {
159
+ return target .equals (userTask ) && source instanceof ExclusiveGateway
160
+ && "User Vote?" .equals (source .getName ());
161
+ }
162
+ return false ;
163
+ };
151
164
}
152
165
153
166
private Predicate <SequenceFlow > hasCorrectConditionExpression ()
154
167
{
155
- return flow -> flow .getConditionExpression ().getTextContent ().equals ("${userVote}" );
168
+ return flow ->
169
+ {
170
+ ConditionExpression conditionExpression = flow .getConditionExpression ();
171
+ if (Objects .nonNull (conditionExpression ))
172
+ return "${userVote}" .equals (conditionExpression .getTextContent ());
173
+ return false ;
174
+ };
156
175
}
157
176
158
- private Predicate <SequenceFlow > isFlowConnectingUserTaskAndSaveUserVoteServer (UserTask userTask )
177
+ private Predicate <SequenceFlow > isFlowConnectingUserTaskAndSaveUserVoteServiceTask (UserTask userTask )
159
178
{
160
- return flow -> flow .getSource ().equals (userTask ) && flow .getTarget () instanceof ServiceTask
161
- && flow .getTarget ().getName ().equals ("Save User Vote" );
179
+ return flow ->
180
+ {
181
+ FlowNode target = flow .getTarget ();
182
+ FlowNode source = flow .getSource ();
183
+ if (Objects .nonNull (target ) && Objects .nonNull (source ))
184
+ {
185
+ return source .equals (userTask ) && target instanceof ServiceTask
186
+ && "Save User Vote" .equals (target .getName ());
187
+ }
188
+ return false ;
189
+ };
162
190
}
163
191
164
192
private String formatErrors (Class <? extends DefaultUserTaskListener > userTaskListener , List <String > errors )
165
193
{
166
194
String formatted = "" ;
167
195
168
196
formatted += "Class: " + userTaskListener .getSimpleName () + "\n " ;
169
- formatted += " Errors: \n " + errors .stream ().reduce ("" , (i , next ) -> i + " " + next + "\n " );
197
+ formatted += errors .stream ().reduce ("" , (i , next ) -> i + " " + next + "\n " );
170
198
171
199
return formatted ;
172
200
}
@@ -230,11 +258,15 @@ private List<String> validateUserTaskListener(Class<? extends DefaultUserTaskLis
230
258
errors .add (
231
259
"Expected one call to QuestionnaireResponseItemComponent#setText for the QuestionnaireResponseItemComponent with linkId 'binary-question'" );
232
260
}
233
- catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e )
261
+ catch (InvocationTargetException | IllegalAccessException | InstantiationException e )
234
262
{
235
263
throw new RuntimeException (e );
236
264
}
237
-
265
+ catch (NoSuchMethodException e )
266
+ {
267
+ String errorUserTaskListenerDoesNotOverrideMethod = "Expected override of method 'beforeQuestionnaireResponseCreate'" ;
268
+ errors .add (errorUserTaskListenerDoesNotOverrideMethod );
269
+ }
238
270
return errors ;
239
271
}
240
272
}
0 commit comments