1313import java .util .ArrayList ;
1414import java .util .List ;
1515import java .util .Map ;
16+ import java .util .Objects ;
1617import java .util .Optional ;
1718import java .util .function .Predicate ;
1819import java .util .stream .Collectors ;
1920
2021import org .camunda .bpm .engine .delegate .DelegateTask ;
2122import org .camunda .bpm .model .bpmn .Bpmn ;
2223import org .camunda .bpm .model .bpmn .BpmnModelInstance ;
24+ import org .camunda .bpm .model .bpmn .instance .ConditionExpression ;
2325import org .camunda .bpm .model .bpmn .instance .ExclusiveGateway ;
26+ import org .camunda .bpm .model .bpmn .instance .FlowNode ;
2427import org .camunda .bpm .model .bpmn .instance .Process ;
2528import org .camunda .bpm .model .bpmn .instance .SequenceFlow ;
2629import org .camunda .bpm .model .bpmn .instance .ServiceTask ;
4447import dev .dsf .process .tutorial .ConstantsTutorial ;
4548import dev .dsf .process .tutorial .TestProcessPluginGenerator ;
4649import dev .dsf .process .tutorial .TutorialProcessPluginDefinition ;
50+ import dev .dsf .process .tutorial .Utils ;
4751
4852@ RunWith (MockitoJUnitRunner .class )
4953public class BpmnAndUserTaskListenerTest
@@ -77,7 +81,7 @@ public void testVoteBpmnFile()
7781
7882 Process process = processes .get (0 );
7983
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" ;
8185 int userTaskCount = process .getChildElementsByType (UserTask .class ).size ();
8286 assertTrue (errorMissingUserTask , userTaskCount > 0 );
8387
@@ -87,21 +91,21 @@ public void testVoteBpmnFile()
8791 .filter (userTask -> userTask .getIncoming ().stream ()
8892 .anyMatch (isFlowConnectingUserTaskAndExclusiveGateway (userTask )))
8993 .filter (userTask -> userTask .getOutgoing ().stream ()
90- .anyMatch (isFlowConnectingUserTaskAndSaveUserVoteServer (userTask )))
94+ .anyMatch (isFlowConnectingUserTaskAndSaveUserVoteServiceTask (userTask )))
9195 .findFirst ();
9296 assertTrue (errorMissingCorrectUserTask , optUserTask .isPresent ());
9397 UserTask userTask = optUserTask .get ();
9498
9599 String errorUserTaskIncomingFlowMissingCondition = "User Task in process '" + processId + "' in file '"
96- + filename + " with name " + userTask .getOutgoing ()
100+ + filename + "' with name " + userTask .getName ()
97101 + " is missing condition expression '${userVote}' on incoming flow from exclusive gateway with name 'User Vote?'" ;
98102 assertTrue (errorUserTaskIncomingFlowMissingCondition ,
99103 userTask .getIncoming ().stream ().filter (isFlowConnectingUserTaskAndExclusiveGateway (userTask ))
100104 .allMatch (hasCorrectConditionExpression ()));
101105
102106 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 ());
105109
106110 String packageName = "dev.dsf.process.tutorial.listener" ;
107111 String errorNoUserTaskListenerFound = "No class extending DefaultUserTaskListener found in package '"
@@ -110,7 +114,7 @@ public void testVoteBpmnFile()
110114 assertTrue (errorNoUserTaskListenerFound , !userTaskListeners .isEmpty ());
111115
112116 String errorUserTaskIsMissingTaskListener = "User Task in process '" + processId + "' in file '" + filename
113- + " with name " + userTask .getOutgoing ()
117+ + "' with name " + userTask .getName ()
114118 + " is missing at least one Task Listener which extends DefaultUserTaskListener. Found classes to add which extend DefaultUserTaskListener: "
115119 + userTaskListeners .stream ().map (Class ::getSimpleName ).reduce ("" , (i , next ) -> i + next + " " );
116120 List <CamundaTaskListener > camundaTaskListeners = userTask
@@ -135,8 +139,8 @@ public void testVoteBpmnFile()
135139 .collect (Collectors .toMap (userTaskListener -> userTaskListener , this ::validateUserTaskListener ));
136140
137141 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 " ;
140144 errorNoTaskListenerInUserTaskIsValid += userTaskListenersWithErrors .keySet ().stream ()
141145 .map (key -> formatErrors (key , userTaskListenersWithErrors .get (key ))).collect (Collectors .joining ());
142146
@@ -146,27 +150,51 @@ public void testVoteBpmnFile()
146150
147151 private Predicate <SequenceFlow > isFlowConnectingUserTaskAndExclusiveGateway (UserTask userTask )
148152 {
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+ };
151164 }
152165
153166 private Predicate <SequenceFlow > hasCorrectConditionExpression ()
154167 {
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+ };
156175 }
157176
158- private Predicate <SequenceFlow > isFlowConnectingUserTaskAndSaveUserVoteServer (UserTask userTask )
177+ private Predicate <SequenceFlow > isFlowConnectingUserTaskAndSaveUserVoteServiceTask (UserTask userTask )
159178 {
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+ };
162190 }
163191
164192 private String formatErrors (Class <? extends DefaultUserTaskListener > userTaskListener , List <String > errors )
165193 {
166194 String formatted = "" ;
167195
168196 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 " );
170198
171199 return formatted ;
172200 }
@@ -230,11 +258,15 @@ private List<String> validateUserTaskListener(Class<? extends DefaultUserTaskLis
230258 errors .add (
231259 "Expected one call to QuestionnaireResponseItemComponent#setText for the QuestionnaireResponseItemComponent with linkId 'binary-question'" );
232260 }
233- catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e )
261+ catch (InvocationTargetException | IllegalAccessException | InstantiationException e )
234262 {
235263 throw new RuntimeException (e );
236264 }
237-
265+ catch (NoSuchMethodException e )
266+ {
267+ String errorUserTaskListenerDoesNotOverrideMethod = "Expected override of method 'beforeQuestionnaireResponseCreate'" ;
268+ errors .add (errorUserTaskListenerDoesNotOverrideMethod );
269+ }
238270 return errors ;
239271 }
240272}
0 commit comments