View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
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.List;
29  
30  import org.apache.catalina.Context;
31  import org.apache.catalina.Manager;
32  import org.apache.catalina.Session;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  import org.springframework.test.util.ReflectionTestUtils;
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   * The Class ListSessionsControllerTest.
46   */
47  class ListSessionsControllerTest {
48  
49    /** The controller. */
50    private ListSessionsController controller;
51  
52    /** The request. */
53    private HttpServletRequest request;
54  
55    /** The response. */
56    private HttpServletResponse response;
57  
58    /** The context. */
59    private Context context;
60  
61    /** The manager. */
62    private Manager manager;
63  
64    /** The container wrapper. */
65    private ContainerWrapperBean containerWrapper;
66  
67    /**
68     * Sets the up.
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      ReflectionTestUtils.setField(controller, "containerWrapper", containerWrapper);
80  
81      doReturn("sessions").when(controller).getViewName();
82    }
83  
84    /**
85     * Test handle request delegates to super.
86     *
87     * @throws Exception the exception
88     */
89    @Test
90    void testHandleRequestDelegatesToSuper() throws Exception {
91      ListSessionsController ctrl = spy(new ListSessionsController());
92      doReturn(new ModelAndView()).when(ctrl).handleRequest(any(), any());
93      ModelAndView mv = ctrl.handleRequest(request, response);
94      assertNotNull(mv);
95    }
96  
97    /**
98     * Test handle context with no context lists all sessions.
99     *
100    * @throws Exception the exception
101    */
102   @Test
103   void testHandleContextWithNoContextListsAllSessions() throws Exception {
104     List<Context> ctxs = new ArrayList<>();
105     Context ctx = mock(Context.class);
106     Manager mgr = mock(Manager.class);
107     Session s = mock(Session.class);
108     ApplicationSession appSession = mock(ApplicationSession.class);
109 
110     ctxs.add(ctx);
111     when(containerWrapper.getTomcatContainer()).thenReturn(mock(TomcatContainer.class));
112 
113     TomcatContainer tomcatContainer = mock(TomcatContainer.class);
114     when(containerWrapper.getTomcatContainer()).thenReturn(tomcatContainer);
115     when(tomcatContainer.findContexts()).thenReturn(ctxs);
116 
117     when(ctx.getManager()).thenReturn(mgr);
118     when(mgr.findSessions()).thenReturn(new Session[] {s});
119 
120     try (var mockedApplicationUtils = mockStatic(ApplicationUtils.class)) {
121       when(ApplicationUtils.getApplicationSession(any(), anyBoolean(), anyBoolean()))
122           .thenReturn(appSession);
123       when(appSession.getAttributes()).thenReturn(List.of());
124 
125       ModelAndView mv = controller.handleContext(null, null, request, response);
126       assertNotNull(mv);
127       assertTrue(mv.getModel().containsKey("sessions"));
128     }
129   }
130 
131   /**
132    * Test handle context with context lists sessions.
133    *
134    * @throws Exception the exception
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    * Test match session.
147    *
148    * @throws Exception the exception
149    */
150   @Test
151   void testMatchSession() throws Exception {
152     ApplicationSession appSession = mock(ApplicationSession.class);
153     SessionSearchInfo searchInfo = new SessionSearchInfo();
154     // Use reflection to call private method
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    * Test is context optional.
164    */
165   @Test
166   void testIsContextOptional() {
167     assertTrue(controller.isContextOptional());
168   }
169 
170   /**
171    * Test set view name.
172    */
173   @Test
174   void testSetViewName() {
175     controller.setViewName("sessions");
176     assertEquals("sessions", controller.getViewName());
177   }
178 
179 }