1
2
3
4
5
6
7
8
9
10
11 package psiprobe.controllers.sessions;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.ArgumentMatchers.anyBoolean;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.mockStatic;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23
24 import jakarta.servlet.http.HttpServletRequest;
25 import jakarta.servlet.http.HttpServletResponse;
26
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30
31 import org.apache.catalina.Context;
32 import org.apache.catalina.Manager;
33 import org.apache.catalina.Session;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.springframework.web.servlet.ModelAndView;
37
38 import psiprobe.TomcatContainer;
39 import psiprobe.beans.ContainerWrapperBean;
40 import psiprobe.model.ApplicationSession;
41 import psiprobe.model.SessionSearchInfo;
42 import psiprobe.tools.ApplicationUtils;
43
44
45
46
47 class ListSessionsControllerTest {
48
49
50 private ListSessionsController controller;
51
52
53 private HttpServletRequest request;
54
55
56 private HttpServletResponse response;
57
58
59 private Context context;
60
61
62 private Manager manager;
63
64
65 private ContainerWrapperBean containerWrapper;
66
67
68
69
70 @BeforeEach
71 void setUp() {
72 controller = spy(new ListSessionsController());
73 request = mock(HttpServletRequest.class);
74 response = mock(HttpServletResponse.class);
75 context = mock(Context.class);
76 manager = mock(Manager.class);
77 containerWrapper = mock(ContainerWrapperBean.class);
78
79 doReturn(containerWrapper).when(controller).getContainerWrapper();
80 doReturn("sessions").when(controller).getViewName();
81 }
82
83
84
85
86
87
88 @Test
89 void testHandleRequestDelegatesToSuper() throws Exception {
90 ListSessionsController ctrl = spy(new ListSessionsController());
91 doReturn(new ModelAndView()).when(ctrl).handleRequest(any(), any());
92 ModelAndView mv = ctrl.handleRequest(request, response);
93 assertNotNull(mv);
94 }
95
96
97
98
99
100
101 @Test
102 void testHandleContextWithNoContextListsAllSessions() throws Exception {
103 List<Context> ctxs = new ArrayList<>();
104 Context ctx = mock(Context.class);
105 Manager mgr = mock(Manager.class);
106 Session s = mock(Session.class);
107 ApplicationSession appSession = mock(ApplicationSession.class);
108
109 ctxs.add(ctx);
110 when(containerWrapper.getTomcatContainer()).thenReturn(mock(TomcatContainer.class));
111
112 TomcatContainer tomcatContainer = mock(TomcatContainer.class);
113 when(containerWrapper.getTomcatContainer()).thenReturn(tomcatContainer);
114 when(tomcatContainer.findContexts()).thenReturn(ctxs);
115
116 when(ctx.getManager()).thenReturn(mgr);
117 when(mgr.findSessions()).thenReturn(new Session[] {s});
118
119 @SuppressWarnings("unused")
120 var unused = mockStatic(ApplicationUtils.class);
121
122 when(ApplicationUtils.getApplicationSession(any(), anyBoolean(), anyBoolean()))
123 .thenReturn(appSession);
124 when(appSession.getAttributes()).thenReturn(Collections.emptyList());
125
126 ModelAndView mv = controller.handleContext(null, null, request, response);
127 assertNotNull(mv);
128 assertTrue(mv.getModel().containsKey("sessions"));
129 }
130
131
132
133
134
135
136 @Test
137 void testHandleContextWithContextListsSessions() throws Exception {
138 when(context.getManager()).thenReturn(manager);
139 when(manager.findSessions()).thenReturn(new Session[0]);
140 ModelAndView mv = controller.handleContext("test", context, request, response);
141 assertNotNull(mv);
142 assertTrue(mv.getModel().containsKey("sessions"));
143 }
144
145
146
147
148
149
150 @Test
151 void testMatchSession() throws Exception {
152 ApplicationSession appSession = mock(ApplicationSession.class);
153 SessionSearchInfo searchInfo = new SessionSearchInfo();
154
155 var method = ListSessionsController.class.getDeclaredMethod("matchSession",
156 ApplicationSession.class, SessionSearchInfo.class);
157 method.setAccessible(true);
158 boolean result = (boolean) method.invoke(controller, appSession, searchInfo);
159 assertTrue(result);
160 }
161
162
163
164
165 @Test
166 void testIsContextOptional() {
167 assertTrue(controller.isContextOptional());
168 }
169
170
171
172
173 @Test
174 void testSetViewName() {
175 controller.setViewName("sessions");
176 assertEquals("sessions", controller.getViewName());
177 }
178
179 }