11package com .flowci .flow ;
22
33import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import com .flowci .SpringTest ;
45import com .flowci .common .model .ErrorResponse ;
56import com .flowci .flow .business .CreateFlow ;
6- import com .flowci .flow .business .FetchTemplates ;
7+ import com .flowci .flow .business .FetchFlow ;
78import com .flowci .flow .model .CreateFlowParam ;
89import com .flowci .flow .model .Flow ;
910import org .junit .jupiter .api .Test ;
1011import org .springframework .beans .factory .annotation .Autowired ;
11- import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
1212import org .springframework .boot .test .mock .mockito .MockBean ;
1313import org .springframework .dao .DataIntegrityViolationException ;
1414import org .springframework .http .MediaType ;
1515import org .springframework .test .web .servlet .MockMvc ;
1616
1717import java .sql .SQLException ;
1818
19+ import static com .flowci .TestUtils .newDummyInstance ;
1920import static org .junit .jupiter .api .Assertions .assertEquals ;
2021import static org .mockito .ArgumentMatchers .any ;
2122import static org .mockito .Mockito .mock ;
2223import static org .mockito .Mockito .when ;
24+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
2325import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
2426import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
2527
26- @ WebMvcTest (controllers = FlowController .class )
27- class FlowControllerTest {
28+ class FlowControllerTest extends SpringTest {
2829
2930 @ Autowired
3031 private MockMvc mvc ;
@@ -35,9 +36,8 @@ class FlowControllerTest {
3536 @ MockBean
3637 private CreateFlow createFlow ;
3738
38- // keep it !!
3939 @ MockBean
40- private FetchTemplates fetchTemplates ;
40+ private FetchFlow fetchFlow ;
4141
4242 @ Test
4343 void givenCreateFlowParameter_whenCreateFlow_thenReturnFlowId () throws Exception {
@@ -111,4 +111,28 @@ void givenCreateFlowParameter_whenCreateFlowWithUnexpectedError_thenReturnError(
111111 assertEquals ("something went wrong" , error .message ());
112112 assertEquals (500 , error .code ());
113113 }
114+
115+ @ Test
116+ void givenFlowId_whenFetching_thenReturnFlow () throws Exception {
117+ var mockFlow = newDummyInstance (Flow .class ).create ();
118+ when (fetchFlow .invoke (any ())).thenReturn (mockFlow );
119+
120+ var r = mvc .perform (get ("/v2/flows/" + mockFlow .getId ()))
121+ .andExpect (status ().is2xxSuccessful ())
122+ .andReturn ();
123+
124+ var fetched = objectMapper .readValue (r .getResponse ().getContentAsString (), Flow .class );
125+ assertEquals (mockFlow .getId (), fetched .getId ());
126+ }
127+
128+ @ Test
129+ void givenInvalidFlowId_whenFetching_thenReturnError () throws Exception {
130+ var r = mvc .perform (get ("/v2/flows/-1" ))
131+ .andExpect (status ().is4xxClientError ())
132+ .andReturn ();
133+
134+ var error = objectMapper .readValue (r .getResponse ().getContentAsString (), ErrorResponse .class );
135+ assertEquals (400 , error .code ());
136+ assertEquals ("invalid id" , error .message ());
137+ }
114138}
0 commit comments