@@ -141,41 +141,105 @@ void sendEmail(NewsletterDraft draft) {
141141 }
142142
143143 @ Test
144- void testJacksonAutomagicalConversion () throws Exception {
145- try (WorkflowApplication app = WorkflowApplication .builder ().build ()) {
144+ void testAutomaticConversion () throws Exception {
145+ testConversionWorkflow (
146+ FuncWorkflowBuilder .workflow ("intelligent-newsletter" )
147+ .tasks (
148+ function ("draftAgent" , this ::writeDraft ).exportAsTaskOutput (),
149+ emitJson ("draftReady" , "org.acme.email.review.required" , NewsletterDraft .class ),
150+ listen (
151+ "waitHumanReview" ,
152+ to ().one (
153+ consumed ("org.acme.newsletter.review.done" )
154+ .extensionByInstanceId ("instanceid" ))),
155+ switchWhenOrElse (
156+ h -> HumanReview .NEEDS_REVISION .equals (h .status ()),
157+ "humanEditorAgent" ,
158+ "sendNewsletter" ,
159+ HumanReview .class ),
160+ function ("humanEditorAgent" , this ::editDraft )
161+ .exportAsTaskOutput ()
162+ .then ("draftReady" ),
163+ consume ("sendNewsletter" , this ::sendEmail )
164+ // Because we are in Jackson, the payload at this evaluation stage can be a
165+ // Map.
166+ // We simply check for the "status" field to know if it's the review payload.
167+ .inputFrom (
168+ (Map <String , Object > payload ,
169+ WorkflowContextData wfc ,
170+ TaskContextData tfc ) ->
171+ payload .containsKey ("status" ) ? wfc .context () : payload ))
172+ .build ());
173+ }
146174
147- Workflow workflow =
148- FuncWorkflowBuilder .workflow ("intelligent-newsletter" )
149- .tasks (
150- function ("draftAgent" , this ::writeDraft ).exportAsTaskOutput (),
151- emitJson ("draftReady" , "org.acme.email.review.required" , NewsletterDraft .class ),
152- listen (
153- "waitHumanReview" ,
154- to ().one (
155- consumed ("org.acme.newsletter.review.done" )
156- .extensionByInstanceId ("instanceid" )))
157- .outputAs ((Collection <?> events ) -> events .iterator ().next ()),
158- // The engine sees the incoming JsonNode, sees this task expects
159- // HumanReview.class,
160- // and natively deserializes it for you before executing the lambda!
161- switchWhenOrElse (
162- h -> HumanReview .NEEDS_REVISION .equals (h .status ()),
163- "humanEditorAgent" ,
164- "sendNewsletter" ,
165- HumanReview .class ),
166- function ("humanEditorAgent" , this ::editDraft )
167- .exportAsTaskOutput ()
168- .then ("draftReady" ),
169- consume ("sendNewsletter" , this ::sendEmail )
170- // Because we are in Jackson, the payload at this evaluation stage can be a
171- // Map.
172- // We simply check for the "status" field to know if it's the review payload.
173- .inputFrom (
174- (Map <String , Object > payload ,
175- WorkflowContextData wfc ,
176- TaskContextData tfc ) ->
177- payload .containsKey ("status" ) ? wfc .context () : payload ))
178- .build ();
175+ @ Test
176+ void testCollectionConversion () throws Exception {
177+ testConversionWorkflow (
178+ FuncWorkflowBuilder .workflow ("intelligent-newsletter" )
179+ .tasks (
180+ function ("draftAgent" , this ::writeDraft ).exportAsTaskOutput (),
181+ emitJson ("draftReady" , "org.acme.email.review.required" , NewsletterDraft .class ),
182+ listen (
183+ "waitHumanReview" ,
184+ to ().one (
185+ consumed ("org.acme.newsletter.review.done" )
186+ .extensionByInstanceId ("instanceid" )))
187+ .outputAs ((Collection col ) -> col .iterator ().next ()),
188+ switchWhenOrElse (
189+ h -> HumanReview .NEEDS_REVISION .equals (h .status ()),
190+ "humanEditorAgent" ,
191+ "sendNewsletter" ,
192+ HumanReview .class ),
193+ function ("humanEditorAgent" , this ::editDraft )
194+ .exportAsTaskOutput ()
195+ .then ("draftReady" ),
196+ consume ("sendNewsletter" , this ::sendEmail )
197+ // Because we are in Jackson, the payload at this evaluation stage can be a
198+ // Map.
199+ // We simply check for the "status" field to know if it's the review payload.
200+ .inputFrom (
201+ (Map <String , Object > payload ,
202+ WorkflowContextData wfc ,
203+ TaskContextData tfc ) ->
204+ payload .containsKey ("status" ) ? wfc .context () : payload ))
205+ .build ());
206+ }
207+
208+ @ Test
209+ void testNodeConversion () throws Exception {
210+ testConversionWorkflow (
211+ FuncWorkflowBuilder .workflow ("intelligent-newsletter" )
212+ .tasks (
213+ function ("draftAgent" , this ::writeDraft ).exportAsTaskOutput (),
214+ emitJson ("draftReady" , "org.acme.email.review.required" , NewsletterDraft .class ),
215+ listen (
216+ "waitHumanReview" ,
217+ to ().one (
218+ consumed ("org.acme.newsletter.review.done" )
219+ .extensionByInstanceId ("instanceid" )))
220+ .outputAs ((ArrayNode col ) -> col .get (0 )),
221+ switchWhenOrElse (
222+ h -> HumanReview .NEEDS_REVISION .equals (h .status ()),
223+ "humanEditorAgent" ,
224+ "sendNewsletter" ,
225+ HumanReview .class ),
226+ function ("humanEditorAgent" , this ::editDraft )
227+ .exportAsTaskOutput ()
228+ .then ("draftReady" ),
229+ consume ("sendNewsletter" , this ::sendEmail )
230+ // Because we are in Jackson, the payload at this evaluation stage can be a
231+ // Map.
232+ // We simply check for the "status" field to know if it's the review payload.
233+ .inputFrom (
234+ (Map <String , Object > payload ,
235+ WorkflowContextData wfc ,
236+ TaskContextData tfc ) ->
237+ payload .containsKey ("status" ) ? wfc .context () : payload ))
238+ .build ());
239+ }
240+
241+ private void testConversionWorkflow (Workflow workflow ) throws Exception {
242+ try (WorkflowApplication app = WorkflowApplication .builder ().build ()) {
179243
180244 WorkflowDefinition definition = app .workflowDefinition (workflow );
181245 WorkflowInstance instance = definition .instance (new NewsletterRequest ("Tech Stocks" ));
0 commit comments