2121import java .util .Base64 ;
2222
2323import static com .flowci .TestUtils .newDummyInstance ;
24+ import static org .instancio .Select .field ;
2425import static org .junit .jupiter .api .Assertions .assertEquals ;
2526import static org .mockito .ArgumentMatchers .any ;
2627import static org .mockito .Mockito .*;
@@ -123,11 +124,14 @@ void givenCreateFlowParameter_whenCreateFlowWithUnexpectedError_thenReturnError(
123124 }
124125
125126 @ Test
126- void givenFlowId_whenFetching_thenReturnFlow () throws Exception {
127- var mockFlow = newDummyInstance (Flow .class ).create ();
128- when (fetchFlow .invoke (anyLong ())).thenReturn (mockFlow );
127+ void givenFlowName_whenFetching_thenReturnFlow () throws Exception {
128+ var mockFlow = newDummyInstance (Flow .class )
129+ .set (field (Flow ::getName ), "hello_world" )
130+ .create ();
129131
130- var r = mvc .perform (get ("/v2/flows/" + mockFlow .getId ()))
132+ when (fetchFlow .invoke (anyString ())).thenReturn (mockFlow );
133+
134+ var r = mvc .perform (get (String .format ("/v2/flows/%s" , mockFlow .getName ())))
131135 .andExpect (status ().is2xxSuccessful ())
132136 .andReturn ();
133137
@@ -136,22 +140,16 @@ void givenFlowId_whenFetching_thenReturnFlow() throws Exception {
136140 }
137141
138142 @ Test
139- void givenInvalidFlowId_whenFetching_thenReturnError () throws Exception {
140- var r = mvc . perform ( get ( "/v2/flows/-1" ) )
141- .andExpect ( status (). is4xxClientError () )
142- .andReturn ();
143+ void givenFlowName_whenFetchingYaml_thenReturnBase64EncodedYaml () throws Exception {
144+ var mockFlow = newDummyInstance ( Flow . class )
145+ .set ( field ( Flow :: getName ), "some_name" )
146+ .create ();
143147
144- var error = objectMapper .readValue (r .getResponse ().getContentAsString (), ErrorResponse .class );
145- assertEquals (400 , error .code ());
146- assertEquals ("invalid id" , error .message ());
147- }
148-
149- @ Test
150- void givenFlowId_whenFetchingYaml_thenReturnBase64EncodedYaml () throws Exception {
151148 var expectedYaml = Base64 .getEncoder ().encodeToString ("some yaml" .getBytes ());
149+ when (fetchFlow .invoke (anyString ())).thenReturn (mockFlow );
152150 when (fetchFlowYamlContent .invoke (any (), eq (true ))).thenReturn (expectedYaml );
153151
154- var r = mvc .perform (get ("/v2/flows/1001 /yaml" ))
152+ var r = mvc .perform (get (String . format ( "/v2/flows/%s /yaml" , mockFlow . getName ()) ))
155153 .andExpectAll (
156154 status ().is2xxSuccessful (),
157155 header ().string ("Content-Type" , "text/plain;charset=UTF-8" ))
@@ -161,13 +159,18 @@ void givenFlowId_whenFetchingYaml_thenReturnBase64EncodedYaml() throws Exception
161159 }
162160
163161 @ Test
164- void givenFlowIdAndYaml_whenUpdating_thenYamlIsUpdated () throws Exception {
162+ void givenFlowNameAndYaml_whenUpdating_thenYamlIsUpdated () throws Exception {
163+ var mockFlow = newDummyInstance (Flow .class )
164+ .set (field (Flow ::getName ), "some_name" )
165+ .create ();
166+ when (fetchFlow .invoke (anyString ())).thenReturn (mockFlow );
167+
165168 var expectedYaml = Base64 .getEncoder ().encodeToString ("some yaml" .getBytes ());
166169
167170 var yamlCaptor = ArgumentCaptor .forClass (String .class );
168171 doNothing ().when (updateFlowYamlContent ).invoke (any (), yamlCaptor .capture ());
169172
170- var r = mvc .perform (post ("/v2/flows/1001 /yaml" )
173+ mvc .perform (post (String . format ( "/v2/flows/%s /yaml" , mockFlow . getName ()) )
171174 .contentType (MediaType .TEXT_PLAIN )
172175 .content (expectedYaml ))
173176 .andExpect (status ().is2xxSuccessful ())
0 commit comments