11package io .sentrius .sso .controllers .api ;
22
3- import com .fasterxml .jackson .databind .ObjectMapper ;
4- import io .sentrius .sso .core .model .ErrorOutput ;
53import io .sentrius .sso .core .model .selfhealing .SelfHealingConfig ;
64import io .sentrius .sso .core .model .selfhealing .SelfHealingConfig .PatchingPolicy ;
75import io .sentrius .sso .core .model .selfhealing .SelfHealingSession ;
108import io .sentrius .sso .core .services .selfhealing .SelfHealingConfigService ;
119import io .sentrius .sso .core .services .selfhealing .SelfHealingSessionService ;
1210import org .junit .jupiter .api .Test ;
13- import org .springframework .beans .factory .annotation .Autowired ;
14- import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
15- import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
16- import org .springframework .boot .test .mock .mockito .MockBean ;
17- import org .springframework .http .MediaType ;
18- import org .springframework .security .test .context .support .WithMockUser ;
19- import org .springframework .test .web .servlet .MockMvc ;
11+ import org .junit .jupiter .api .extension .ExtendWith ;
12+ import org .mockito .Mock ;
13+ import org .mockito .junit .jupiter .MockitoExtension ;
14+ import org .springframework .http .ResponseEntity ;
2015
2116import java .util .Arrays ;
2217import java .util .List ;
2318import java .util .Optional ;
2419
20+ import static org .junit .jupiter .api .Assertions .*;
2521import static org .mockito .ArgumentMatchers .any ;
2622import static org .mockito .Mockito .*;
27- import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
28- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
29- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
3023
31- @ WebMvcTest (SelfHealingApiController .class )
32- @ AutoConfigureMockMvc (addFilters = false )
24+ @ ExtendWith (MockitoExtension .class )
3325class SelfHealingApiControllerTest {
3426
35- @ Autowired
36- private MockMvc mockMvc ;
37-
38- @ Autowired
39- private ObjectMapper objectMapper ;
40-
41- @ MockBean
27+ @ Mock
4228 private SelfHealingConfigService configService ;
4329
44- @ MockBean
30+ @ Mock
4531 private SelfHealingSessionService sessionService ;
4632
47- @ MockBean
33+ @ Mock
4834 private ErrorAnalysisService errorAnalysisService ;
4935
50- @ MockBean
36+ @ Mock
5137 private ErrorOutputService errorOutputService ;
5238
39+ @ org .mockito .InjectMocks
40+ private SelfHealingApiController controller ;
41+
5342 @ Test
54- @ WithMockUser (authorities = "CAN_MANAGE_APPLICATION" )
55- void testGetAllConfigs () throws Exception {
43+ void testGetAllConfigs () {
5644 SelfHealingConfig config = SelfHealingConfig .builder ()
5745 .id (1L )
5846 .podName ("test-pod" )
@@ -62,17 +50,18 @@ void testGetAllConfigs() throws Exception {
6250
6351 when (configService .getAllConfigs ()).thenReturn (Arrays .asList (config ));
6452
65- mockMvc .perform (get ("/api/v1/self-healing/config" ))
66- .andExpect (status ().isOk ())
67- .andExpect (jsonPath ("$[0].podName" ).value ("test-pod" ))
68- .andExpect (jsonPath ("$[0].patchingPolicy" ).value ("IMMEDIATE" ));
53+ ResponseEntity <List <SelfHealingConfig >> response = controller .getAllConfigs ();
6954
55+ assertEquals (200 , response .getStatusCode ().value ());
56+ assertNotNull (response .getBody ());
57+ assertEquals (1 , response .getBody ().size ());
58+ assertEquals ("test-pod" , response .getBody ().get (0 ).getPodName ());
59+ assertEquals (PatchingPolicy .IMMEDIATE , response .getBody ().get (0 ).getPatchingPolicy ());
7060 verify (configService ).getAllConfigs ();
7161 }
7262
7363 @ Test
74- @ WithMockUser (authorities = "CAN_MANAGE_APPLICATION" )
75- void testGetConfigByPodName () throws Exception {
64+ void testGetConfigByPodName () {
7665 SelfHealingConfig config = SelfHealingConfig .builder ()
7766 .id (1L )
7867 .podName ("test-pod" )
@@ -82,17 +71,17 @@ void testGetConfigByPodName() throws Exception {
8271
8372 when (configService .getConfigByPodName ("test-pod" )).thenReturn (Optional .of (config ));
8473
85- mockMvc .perform (get ("/api/v1/self-healing/config/test-pod" ))
86- .andExpect (status ().isOk ())
87- .andExpect (jsonPath ("$.podName" ).value ("test-pod" ))
88- .andExpect (jsonPath ("$.patchingPolicy" ).value ("OFF_HOURS" ));
74+ ResponseEntity <SelfHealingConfig > response = controller .getConfigByPodName ("test-pod" );
8975
76+ assertEquals (200 , response .getStatusCode ().value ());
77+ assertNotNull (response .getBody ());
78+ assertEquals ("test-pod" , response .getBody ().getPodName ());
79+ assertEquals (PatchingPolicy .OFF_HOURS , response .getBody ().getPatchingPolicy ());
9080 verify (configService ).getConfigByPodName ("test-pod" );
9181 }
9282
9383 @ Test
94- @ WithMockUser (authorities = "CAN_MANAGE_APPLICATION" )
95- void testSaveConfig () throws Exception {
84+ void testSaveConfig () {
9685 SelfHealingConfig config = SelfHealingConfig .builder ()
9786 .podName ("new-pod" )
9887 .patchingPolicy (PatchingPolicy .IMMEDIATE )
@@ -108,32 +97,27 @@ void testSaveConfig() throws Exception {
10897
10998 when (configService .saveConfig (any (SelfHealingConfig .class ))).thenReturn (savedConfig );
11099
111- mockMvc .perform (post ("/api/v1/self-healing/config" )
112- .with (csrf ())
113- .contentType (MediaType .APPLICATION_JSON )
114- .content (objectMapper .writeValueAsString (config )))
115- .andExpect (status ().isOk ())
116- .andExpect (jsonPath ("$.id" ).value (1 ))
117- .andExpect (jsonPath ("$.podName" ).value ("new-pod" ));
100+ ResponseEntity <SelfHealingConfig > response = controller .saveConfig (config );
118101
102+ assertEquals (200 , response .getStatusCode ().value ());
103+ assertNotNull (response .getBody ());
104+ assertEquals (1L , response .getBody ().getId ());
105+ assertEquals ("new-pod" , response .getBody ().getPodName ());
119106 verify (configService ).saveConfig (any (SelfHealingConfig .class ));
120107 }
121108
122109 @ Test
123- @ WithMockUser (authorities = "CAN_MANAGE_APPLICATION" )
124- void testDeleteConfig () throws Exception {
110+ void testDeleteConfig () {
125111 doNothing ().when (configService ).deleteConfig (1L );
126112
127- mockMvc .perform (delete ("/api/v1/self-healing/config/1" )
128- .with (csrf ()))
129- .andExpect (status ().isOk ());
113+ ResponseEntity <Void > response = controller .deleteConfig (1L );
130114
115+ assertEquals (200 , response .getStatusCode ().value ());
131116 verify (configService ).deleteConfig (1L );
132117 }
133118
134119 @ Test
135- @ WithMockUser (authorities = "CAN_MANAGE_APPLICATION" )
136- void testGetAllSessions () throws Exception {
120+ void testGetAllSessions () {
137121 SelfHealingSession session = SelfHealingSession .builder ()
138122 .id (1L )
139123 .podName ("test-pod" )
@@ -142,11 +126,13 @@ void testGetAllSessions() throws Exception {
142126
143127 when (sessionService .getAllSessions ()).thenReturn (Arrays .asList (session ));
144128
145- mockMvc .perform (get ("/api/v1/self-healing/sessions" ))
146- .andExpect (status ().isOk ())
147- .andExpect (jsonPath ("$[0].id" ).value (1 ))
148- .andExpect (jsonPath ("$[0].podName" ).value ("test-pod" ));
129+ ResponseEntity <List <SelfHealingSession >> response = controller .getAllSessions ();
149130
131+ assertEquals (200 , response .getStatusCode ().value ());
132+ assertNotNull (response .getBody ());
133+ assertEquals (1 , response .getBody ().size ());
134+ assertEquals (1L , response .getBody ().get (0 ).getId ());
135+ assertEquals ("test-pod" , response .getBody ().get (0 ).getPodName ());
150136 verify (sessionService ).getAllSessions ();
151137 }
152138}
0 commit comments