19
19
20
20
import com .fasterxml .jackson .databind .JsonNode ;
21
21
import com .fasterxml .jackson .databind .ObjectMapper ;
22
+ import com .fasterxml .jackson .databind .node .ArrayNode ;
22
23
import com .fasterxml .jackson .databind .node .ObjectNode ;
23
24
import io .serverlessworkflow .api .WorkflowReader ;
24
25
import io .serverlessworkflow .impl .json .JsonUtils ;
27
28
import java .util .concurrent .CompletableFuture ;
28
29
import java .util .stream .Stream ;
29
30
import org .junit .jupiter .api .BeforeAll ;
30
- import org .junit .jupiter .api .Test ;
31
31
import org .junit .jupiter .params .ParameterizedTest ;
32
32
import org .junit .jupiter .params .provider .Arguments ;
33
33
import org .junit .jupiter .params .provider .MethodSource ;
@@ -58,15 +58,16 @@ void testEventListened(String listen, String emit, JsonNode expectedResult, Obje
58
58
assertThat (waitingInstance .outputAsJsonNode ()).isEqualTo (expectedResult );
59
59
}
60
60
61
- @ Test
62
- void testUntilConsumed () throws IOException {
61
+ @ ParameterizedTest
62
+ @ MethodSource ("eventsListenerParameters" )
63
+ void testEventsListened (String listen , String emit1 , String emit2 , JsonNode expectedResult )
64
+ throws IOException {
63
65
WorkflowDefinition listenDefinition =
64
- appl .workflowDefinition (
65
- WorkflowReader .readWorkflowFromClasspath ("listen-to-any-until-consumed.yaml" ));
66
+ appl .workflowDefinition (WorkflowReader .readWorkflowFromClasspath (listen ));
66
67
WorkflowDefinition emitDoctorDefinition =
67
- appl .workflowDefinition (WorkflowReader .readWorkflowFromClasspath ("emit-doctor.yaml" ));
68
+ appl .workflowDefinition (WorkflowReader .readWorkflowFromClasspath (emit1 ));
68
69
WorkflowDefinition emitOutDefinition =
69
- appl .workflowDefinition (WorkflowReader .readWorkflowFromClasspath ("emit-out.yaml" ));
70
+ appl .workflowDefinition (WorkflowReader .readWorkflowFromClasspath (emit2 ));
70
71
WorkflowInstance waitingInstance = listenDefinition .instance (Map .of ());
71
72
CompletableFuture <JsonNode > future = waitingInstance .start ();
72
73
assertThat (waitingInstance .status ()).isEqualTo (WorkflowStatus .RUNNING );
@@ -77,16 +78,30 @@ void testUntilConsumed() throws IOException {
77
78
emitOutDefinition .instance (Map .of ()).start ().join ();
78
79
assertThat (future ).isCompleted ();
79
80
assertThat (waitingInstance .status ()).isEqualTo (WorkflowStatus .COMPLETED );
80
- assertThat (waitingInstance .outputAsJsonNode ()).isEqualTo (temperature () );
81
+ assertThat (waitingInstance .outputAsJsonNode ()).isEqualTo (expectedResult );
81
82
}
82
83
83
84
private static Stream <Arguments > eventListenerParameters () {
84
85
return Stream .of (
85
- Arguments .of ("listen-to-any.yaml" , "emit.yaml" , cruellaDeVil (), Map .of ()),
86
+ Arguments .of ("listen-to-any.yaml" , "emit.yaml" , array ( cruellaDeVil () ), Map .of ()),
86
87
Arguments .of (
87
88
"listen-to-any-filter.yaml" , "emit-doctor.yaml" , doctor (), Map .of ("temperature" , 39 )));
88
89
}
89
90
91
+ private static Stream <Arguments > eventsListenerParameters () {
92
+ return Stream .of (
93
+ Arguments .of (
94
+ "listen-to-all.yaml" ,
95
+ "emit-doctor.yaml" ,
96
+ "emit.yaml" ,
97
+ array (temperature (), cruellaDeVil ())),
98
+ Arguments .of (
99
+ "listen-to-any-until-consumed.yaml" ,
100
+ "emit-doctor.yaml" ,
101
+ "emit-out.yaml" ,
102
+ array (temperature ())));
103
+ }
104
+
90
105
private static JsonNode cruellaDeVil () {
91
106
ObjectMapper mapper = JsonUtils .mapper ();
92
107
ObjectNode node = mapper .createObjectNode ();
@@ -97,21 +112,24 @@ private static JsonNode cruellaDeVil() {
97
112
mapper
98
113
.createArrayNode ()
99
114
.add (mapper .createObjectNode ().put ("breed" , "dalmatian" ).put ("quantity" , 101 )));
100
- return mapper . createArrayNode (). add ( node ) ;
115
+ return node ;
101
116
}
102
117
103
118
private static JsonNode doctor () {
104
- ObjectMapper mapper = JsonUtils .mapper ();
105
- ObjectNode node = mapper .createObjectNode ();
106
- node .put ("temperature" , 39 );
119
+ ObjectNode node = temperature ();
107
120
node .put ("isSick" , true );
108
- return mapper . createArrayNode (). add (node );
121
+ return array (node );
109
122
}
110
123
111
- private static JsonNode temperature () {
112
- ObjectMapper mapper = JsonUtils .mapper ();
113
- ObjectNode node = mapper .createObjectNode ();
124
+ private static ObjectNode temperature () {
125
+ ObjectNode node = JsonUtils .mapper ().createObjectNode ();
114
126
node .put ("temperature" , 39 );
115
- return mapper .createArrayNode ().add (node );
127
+ return node ;
128
+ }
129
+
130
+ private static JsonNode array (JsonNode ... jsonNodes ) {
131
+ ArrayNode arrayNode = JsonUtils .mapper ().createArrayNode ();
132
+ for (JsonNode node : jsonNodes ) arrayNode .add (node );
133
+ return arrayNode ;
116
134
}
117
135
}
0 commit comments