1818import static io .serverlessworkflow .api .WorkflowReader .readWorkflowFromClasspath ;
1919import static org .assertj .core .api .Assertions .assertThat ;
2020import static org .assertj .core .api .Assertions .catchThrowableOfType ;
21+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
2122
2223import io .serverlessworkflow .impl .WorkflowApplication ;
2324import io .serverlessworkflow .impl .WorkflowModel ;
2425import java .io .IOException ;
2526import java .util .Map ;
27+ import java .util .concurrent .CompletionException ;
2628import java .util .stream .Stream ;
29+ import mockwebserver3 .MockResponse ;
30+ import mockwebserver3 .MockWebServer ;
31+ import okhttp3 .Headers ;
2732import org .assertj .core .api .Condition ;
2833import org .junit .jupiter .api .AfterAll ;
34+ import org .junit .jupiter .api .AfterEach ;
2935import org .junit .jupiter .api .BeforeAll ;
36+ import org .junit .jupiter .api .BeforeEach ;
37+ import org .junit .jupiter .api .Test ;
3038import org .junit .jupiter .params .ParameterizedTest ;
3139import org .junit .jupiter .params .provider .Arguments ;
3240import org .junit .jupiter .params .provider .MethodSource ;
@@ -36,6 +44,8 @@ public class HTTPWorkflowDefinitionTest {
3644
3745 private static WorkflowApplication appl ;
3846
47+ private static MockWebServer mockServer ;
48+
3949 @ BeforeAll
4050 static void init () {
4151 appl = WorkflowApplication .builder ().build ();
@@ -46,10 +56,23 @@ static void cleanup() {
4656 appl .close ();
4757 }
4858
59+ @ BeforeEach
60+ void setup () throws IOException {
61+ mockServer = new MockWebServer ();
62+ mockServer .start (9876 );
63+ }
64+
65+ @ AfterEach
66+ void shutdownServer () {
67+ mockServer .close ();
68+ }
69+
4970 @ ParameterizedTest
5071 @ MethodSource ("provideParameters" )
51- void testWorkflowExecution (String fileName , Object input , Condition <Object > condition )
72+ void testWorkflowExecution (
73+ String fileName , Object input , Runnable setup , Condition <Object > condition )
5274 throws IOException {
75+ setup .run ();
5376 assertThat (
5477 appl .workflowDefinition (readWorkflowFromClasspath (fileName ))
5578 .instance (input )
@@ -94,27 +117,165 @@ private static Stream<Arguments> provideParameters() {
94117 Condition <WorkflowModel > postCondition =
95118 new Condition <WorkflowModel >(
96119 o -> o .asText ().orElseThrow ().equals ("Javierito" ), "CallHttpPostCondition" );
120+
121+ Condition <WorkflowModel > putCondition =
122+ new Condition <>(o -> o .asText ().get ().contains ("John" ), "CallHttpPutCondition" );
123+
124+ Condition <WorkflowModel > patchCondition =
125+ new Condition <>(o -> o .asText ().get ().contains ("John" ), "CallHttpPatchCondition" );
126+
97127 Map <String , String > postMap = Map .of ("name" , "Javierito" , "surname" , "Unknown" );
128+ Map <String , Object > putMap = Map .of ("firstName" , "John" );
129+
130+ Runnable setupPost =
131+ () ->
132+ mockServer .enqueue (
133+ new MockResponse (
134+ 200 ,
135+ Headers .of ("Content-Type" , "application/json" ),
136+ """
137+ {
138+ "firstName": "Javierito"
139+ }
140+ """ ));
141+
98142 return Stream .of (
99- Arguments .of ("workflows-samples/callGetHttp .yaml" , petInput , petCondition ),
143+ Arguments .of ("workflows-samples/call-http-get .yaml" , petInput , doNothing , petCondition ),
100144 Arguments .of (
101- "workflows-samples/callGetHttp .yaml" ,
145+ "workflows-samples/call-http-get .yaml" ,
102146 Map .of ("petId" , "-1" ),
147+ doNothing ,
103148 new Condition <WorkflowModel >(
104149 o -> o .asMap ().orElseThrow ().containsKey ("petId" ), "notFoundCondition" )),
105150 Arguments .of (
106- "workflows-samples/call-http-endpoint-interpolation.yaml" , petInput , petCondition ),
151+ "workflows-samples/call-http-endpoint-interpolation.yaml" ,
152+ petInput ,
153+ doNothing ,
154+ petCondition ),
107155 Arguments .of (
108- "workflows-samples/call-http-query-parameters.yaml" , starTrekInput , starTrekCondition ),
156+ "workflows-samples/call-http-query-parameters.yaml" ,
157+ starTrekInput ,
158+ doNothing ,
159+ starTrekCondition ),
109160 Arguments .of (
110- "workflows-samples/callFindByStatusHttp .yaml" ,
161+ "workflows-samples/call-http-find-by-status .yaml" ,
111162 Map .of (),
163+ doNothing ,
112164 new Condition <WorkflowModel >(o -> !o .asCollection ().isEmpty (), "HasElementCondition" )),
113165 Arguments .of (
114166 "workflows-samples/call-http-query-parameters-external-schema.yaml" ,
115167 starTrekInput ,
168+ doNothing ,
116169 starTrekCondition ),
117- Arguments .of ("workflows-samples/callPostHttp.yaml" , postMap , postCondition ),
118- Arguments .of ("workflows-samples/callPostHttpAsExpr.yaml" , postMap , postCondition ));
170+ Arguments .of ("workflows-samples/call-http-post.yaml" , postMap , setupPost , postCondition ),
171+ Arguments .of (
172+ "workflows-samples/call-http-delete.yaml" ,
173+ Map .of (),
174+ doNothing ,
175+ new Condition <WorkflowModel >(o -> o .asMap ().isEmpty (), "HTTP delete" )),
176+ Arguments .of ("workflows-samples/call-http-put.yaml" , putMap , doNothing , putCondition ));
119177 }
178+
179+ private static final Runnable doNothing = () -> {};
180+
181+ @ Test
182+ void post_should_run_call_http_with_expression_body () {
183+ mockServer .enqueue (
184+ new MockResponse (
185+ 200 ,
186+ Headers .of ("Content-Type" , "application/json" ),
187+ """
188+ {
189+ "firstName": "Javierito"
190+ }
191+ """ ));
192+
193+ assertDoesNotThrow (
194+ () -> {
195+ appl .workflowDefinition (
196+ readWorkflowFromClasspath ("workflows-samples/call-http-post-expr.yaml" ))
197+ .instance (Map .of ("name" , "Javierito" , "surname" , "Unknown" ))
198+ .start ()
199+ .join ();
200+ });
201+ }
202+
203+ @ Test
204+ void testHeadCall () {
205+ mockServer .enqueue (
206+ new MockResponse (
207+ 200 ,
208+ Headers .of (
209+ Map .of (
210+ "Content-Length" ,
211+ "123" ,
212+ "Content-Type" ,
213+ "application/json" ,
214+ "X-Custom-Header" ,
215+ "CustomValue" )),
216+ "" ));
217+ assertDoesNotThrow (
218+ () -> {
219+ appl .workflowDefinition (
220+ readWorkflowFromClasspath ("workflows-samples/call-http-head.yaml" ))
221+ .instance (Map .of ())
222+ .start ()
223+ .join ();
224+ });
225+ }
226+
227+ @ Test
228+ void testOptionsCall () {
229+ mockServer .enqueue (new MockResponse (200 , Headers .of ("Allow" , "GET, POST, OPTIONS" ), "" ));
230+
231+ assertDoesNotThrow (
232+ () -> {
233+ appl .workflowDefinition (
234+ readWorkflowFromClasspath ("workflows-samples/call-http-options.yaml" ))
235+ .instance (Map .of ())
236+ .start ()
237+ .join ();
238+ });
239+ }
240+
241+ @ Test
242+ void testRedirectAsFalse () {
243+ mockServer .enqueue (
244+ new MockResponse (301 , Headers .of ("Location" , "http://localhost:9876/redirected" ), "" ));
245+
246+ CompletionException exception =
247+ catchThrowableOfType (
248+ CompletionException .class ,
249+ () ->
250+ appl .workflowDefinition (
251+ readWorkflowFromClasspath (
252+ "workflows-samples/call-http-redirect-false.yaml" ))
253+ .instance (Map .of ())
254+ .start ()
255+ .join ());
256+
257+ assertThat (exception .getCause ().getMessage ())
258+ .contains (
259+ "The property 'redirect' is set to false but received status 301 (Redirection); expected status in the 200-299 range" );
260+ }
261+
262+ // @Test
263+ // void testRedirectAsTrueWhenReceivingRedirection() {
264+ // mockServer.enqueue(
265+ // new MockResponse(301, Headers.of("Location", "http://localhost:9876/redirected"), ""));
266+ //
267+ // mockServer.enqueue(
268+ // new MockResponse(
269+ // 200, Headers.of("Content-Type", "application/json"), "{\"status\":\"OK\"}"));
270+ //
271+ // assertDoesNotThrow(
272+ // () -> {
273+ // appl.workflowDefinition(
274+ //
275+ // readWorkflowFromClasspath("workflows-samples/call-with-response-output-expr.yaml"))
276+ // .instance(Map.of())
277+ // .start()
278+ // .join();
279+ // });
280+ // }
120281}
0 commit comments