2525import java .io .IOException ;
2626import java .util .Map ;
2727import java .util .concurrent .CompletionException ;
28- import java .util .stream .Stream ;
2928import mockwebserver3 .MockResponse ;
3029import mockwebserver3 .MockWebServer ;
3130import okhttp3 .Headers ;
3534import org .junit .jupiter .api .BeforeAll ;
3635import org .junit .jupiter .api .BeforeEach ;
3736import org .junit .jupiter .api .Test ;
38- import org .junit .jupiter .params .ParameterizedTest ;
39- import org .junit .jupiter .params .provider .Arguments ;
40- import org .junit .jupiter .params .provider .MethodSource ;
41- import org .junit .jupiter .params .provider .ValueSource ;
4237
4338public class HTTPWorkflowDefinitionTest {
4439
4540 private static WorkflowApplication appl ;
46-
4741 private static MockWebServer mockServer ;
4842
4943 @ BeforeAll
@@ -67,139 +61,138 @@ void shutdownServer() {
6761 mockServer .close ();
6862 }
6963
70- @ ParameterizedTest
71- @ MethodSource ("provideParameters" )
72- void testWorkflowExecution (
73- String fileName , Object input , Runnable setup , Condition <Object > condition )
74- throws IOException {
75- setup .run ();
76- assertThat (
77- appl .workflowDefinition (readWorkflowFromClasspath (fileName ))
78- .instance (input )
79- .start ()
80- .join ())
81- .is (condition );
82- }
83-
84- @ ParameterizedTest
85- @ ValueSource (
86- strings = {
87- "workflows-samples/call-http-query-parameters.yaml" ,
88- "workflows-samples/call-http-query-parameters-external-schema.yaml"
89- })
90- void testWrongSchema (String fileName ) {
91- IllegalArgumentException exception =
92- catchThrowableOfType (
93- IllegalArgumentException .class ,
94- () -> appl .workflowDefinition (readWorkflowFromClasspath (fileName )).instance (Map .of ()));
95- assertThat (exception )
96- .isNotNull ()
97- .hasMessageContaining ("There are JsonSchema validation errors" );
98- }
99-
10064 private static boolean httpCondition (WorkflowModel obj ) {
10165 Map <String , Object > map = obj .asMap ().orElseThrow ();
10266 return map .containsKey ("photoUrls" ) || map .containsKey ("petId" );
10367 }
10468
105- private static Stream <Arguments > provideParameters () {
106- Map <String , Object > petInput = Map .of ("petId" , 10 );
107- Map <String , Object > starTrekInput = Map .of ("uid" , "MOMA0000092393" );
108- Condition <WorkflowModel > petCondition =
109- new Condition <>(HTTPWorkflowDefinitionTest ::httpCondition , "callHttpCondition" );
110- Condition <WorkflowModel > starTrekCondition =
111- new Condition <>(
112- o ->
113- ((Map <String , Object >) o .asMap ().orElseThrow ().get ("movie" ))
114- .get ("title" )
115- .equals ("Star Trek" ),
116- "StartTrek" );
117- Condition <WorkflowModel > postCondition =
118- new Condition <WorkflowModel >(
119- o -> o .asText ().orElseThrow ().equals ("Javierito" ), "CallHttpPostCondition" );
120-
121- Condition <WorkflowModel > putCondition =
122- new Condition <>(o -> o .asText ().get ().contains ("John" ), "CallHttpPutCondition" );
69+ @ Test
70+ void callHttpGet_should_return_pet_data () throws Exception {
71+ WorkflowModel result =
72+ appl .workflowDefinition (readWorkflowFromClasspath ("workflows-samples/call-http-get.yaml" ))
73+ .instance (Map .of ("petId" , 10 ))
74+ .start ()
75+ .join ();
76+ assertThat (result )
77+ .has (new Condition <>(HTTPWorkflowDefinitionTest ::httpCondition , "callHttpCondition" ));
78+ }
12379
124- Condition <WorkflowModel > patchCondition =
125- new Condition <>(o -> o .asText ().get ().contains ("John" ), "CallHttpPatchCondition" );
80+ @ Test
81+ void callHttpGet_with_not_found_petId_should_keep_input_petId () throws Exception {
82+ WorkflowModel result =
83+ appl .workflowDefinition (readWorkflowFromClasspath ("workflows-samples/call-http-get.yaml" ))
84+ .instance (Map .of ("petId" , "-1" ))
85+ .start ()
86+ .join ();
87+ assertThat (result .asMap ().orElseThrow ()).containsKey ("petId" );
88+ }
12689
127- Map <String , String > postMap = Map .of ("name" , "Javierito" , "surname" , "Unknown" );
128- Map <String , Object > putMap = Map .of ("firstName" , "John" );
90+ @ Test
91+ void callHttpEndpointInterpolation_should_work () throws Exception {
92+ WorkflowModel result =
93+ appl .workflowDefinition (
94+ readWorkflowFromClasspath (
95+ "workflows-samples/call-http-endpoint-interpolation.yaml" ))
96+ .instance (Map .of ("petId" , 10 ))
97+ .start ()
98+ .join ();
99+ assertThat (result )
100+ .has (new Condition <>(HTTPWorkflowDefinitionTest ::httpCondition , "callHttpCondition" ));
101+ }
129102
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- """ ));
103+ @ Test
104+ void callHttpQueryParameters_should_find_star_trek_movie () throws Exception {
105+ WorkflowModel result =
106+ appl . workflowDefinition (
107+ readWorkflowFromClasspath ( "workflows-samples/call-http-query-parameters.yaml" ))
108+ . instance ( Map . of ("uid " , "MOMA0000092393" ))
109+ . start ()
110+ . join ();
111+ assertThat ((( Map < String , Object >) result . asMap (). orElseThrow (). get ( "movie" )). get ( "title" ))
112+ . isEqualTo ( "Star Trek" );
113+ }
141114
142- return Stream .of (
143- Arguments .of ("workflows-samples/call-http-get.yaml" , petInput , doNothing , petCondition ),
144- Arguments .of (
145- "workflows-samples/call-http-get.yaml" ,
146- Map .of ("petId" , "-1" ),
147- doNothing ,
148- new Condition <WorkflowModel >(
149- o -> o .asMap ().orElseThrow ().containsKey ("petId" ), "notFoundCondition" )),
150- Arguments .of (
151- "workflows-samples/call-http-endpoint-interpolation.yaml" ,
152- petInput ,
153- doNothing ,
154- petCondition ),
155- Arguments .of (
156- "workflows-samples/call-http-query-parameters.yaml" ,
157- starTrekInput ,
158- doNothing ,
159- starTrekCondition ),
160- Arguments .of (
161- "workflows-samples/call-http-find-by-status.yaml" ,
162- Map .of (),
163- doNothing ,
164- new Condition <WorkflowModel >(o -> !o .asCollection ().isEmpty (), "HasElementCondition" )),
165- Arguments .of (
166- "workflows-samples/call-http-query-parameters-external-schema.yaml" ,
167- starTrekInput ,
168- doNothing ,
169- starTrekCondition ),
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 ));
115+ @ Test
116+ void callHttpFindByStatus_should_return_non_empty_collection () throws Exception {
117+
118+ WorkflowModel result =
119+ appl .workflowDefinition (
120+ readWorkflowFromClasspath ("workflows-samples/call-http-find-by-status.yaml" ))
121+ .instance (Map .of ())
122+ .start ()
123+ .join ();
124+ assertThat (result .asCollection ()).isNotEmpty ();
177125 }
178126
179- private static final Runnable doNothing = () -> {};
127+ @ Test
128+ void callHttpQueryParameters_external_schema_should_find_star_trek () throws Exception {
129+ WorkflowModel result =
130+ appl .workflowDefinition (
131+ readWorkflowFromClasspath (
132+ "workflows-samples/call-http-query-parameters-external-schema.yaml" ))
133+ .instance (Map .of ("uid" , "MOMA0000092393" ))
134+ .start ()
135+ .join ();
136+ assertThat (((Map <String , Object >) result .asMap ().orElseThrow ().get ("movie" )).get ("title" ))
137+ .isEqualTo ("Star Trek" );
138+ }
180139
181140 @ Test
182- void post_should_run_call_http_with_expression_body () {
141+ void callHttpPost_should_return_created_firstName () throws Exception {
183142 mockServer .enqueue (
184143 new MockResponse (
185144 200 ,
186145 Headers .of ("Content-Type" , "application/json" ),
187146 """
188- {
189- "firstName": "Javierito"
190- }
191- """ ));
147+ {
148+ "firstName": "Javierito"
149+ }
150+ """ ));
151+ WorkflowModel result =
152+ appl .workflowDefinition (readWorkflowFromClasspath ("workflows-samples/call-http-post.yaml" ))
153+ .instance (Map .of ("name" , "Javierito" , "surname" , "Unknown" ))
154+ .start ()
155+ .join ();
156+ assertThat (result .asText ().orElseThrow ()).isEqualTo ("Javierito" );
157+ }
192158
159+ @ Test
160+ void testCallHttpDelete () {
193161 assertDoesNotThrow (
194162 () -> {
195163 appl .workflowDefinition (
196- readWorkflowFromClasspath ("workflows-samples/call-http-post-expr .yaml" ))
197- .instance (Map .of ("name" , "Javierito" , "surname" , "Unknown" ))
164+ readWorkflowFromClasspath ("workflows-samples/call-http-delete .yaml" ))
165+ .instance (Map .of ())
198166 .start ()
199167 .join ();
200168 });
201169 }
202170
171+ @ Test
172+ void callHttpPut_should_contain_firstName_with_john () throws Exception {
173+ WorkflowModel result =
174+ appl .workflowDefinition (readWorkflowFromClasspath ("workflows-samples/call-http-put.yaml" ))
175+ .instance (Map .of ("firstName" , "John" ))
176+ .start ()
177+ .join ();
178+ assertThat (result .asText ().orElseThrow ()).contains ("John" );
179+ }
180+
181+ @ Test
182+ void testWrongSchema_should_throw_illegal_argument () {
183+ IllegalArgumentException exception =
184+ catchThrowableOfType (
185+ IllegalArgumentException .class ,
186+ () ->
187+ appl .workflowDefinition (
188+ readWorkflowFromClasspath (
189+ "workflows-samples/call-http-query-parameters.yaml" ))
190+ .instance (Map .of ()));
191+ assertThat (exception )
192+ .isNotNull ()
193+ .hasMessageContaining ("There are JsonSchema validation errors" );
194+ }
195+
203196 @ Test
204197 void testHeadCall () {
205198 mockServer .enqueue (
@@ -258,24 +251,4 @@ void testRedirectAsFalse() {
258251 .contains (
259252 "The property 'redirect' is set to false but received status 301 (Redirection); expected status in the 200-299 range" );
260253 }
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- // }
281254}
0 commit comments