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;
12  
13  import jakarta.servlet.ServletException;
14  import jakarta.servlet.http.HttpServletRequest;
15  import jakarta.servlet.http.HttpSession;
16  
17  import java.io.IOException;
18  
19  import org.apache.catalina.Valve;
20  import org.apache.catalina.connector.Request;
21  import org.apache.catalina.connector.Response;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.extension.ExtendWith;
24  import org.mockito.Mock;
25  import org.mockito.Mockito;
26  import org.mockito.junit.jupiter.MockitoExtension;
27  
28  /**
29   * The Class Tomcat11AgentValveTest.
30   */
31  @ExtendWith(MockitoExtension.class)
32  class Tomcat11AgentValveTest {
33  
34    /** The valve. */
35    Tomcat11AgentValve valve;
36  
37    /** The request. */
38    @Mock
39    Request request;
40  
41    /** The response. */
42    @Mock
43    Response response;
44  
45    /** The http session. */
46    @Mock
47    HttpSession session;
48  
49    /** The servlet request. */
50    @Mock
51    HttpServletRequest servletRequest;
52  
53    /** The valve mock. */
54    @Mock
55    Valve valveMock;
56  
57    /**
58     * Invoke.
59     *
60     * @throws IOException Signals that an I/O exception has occurred.
61     * @throws ServletException the servlet exception
62     */
63    @Test
64    void invoke() throws IOException, ServletException {
65      Mockito.when(request.getSession(Mockito.anyBoolean())).thenReturn(session);
66      Mockito.when(request.getRequest()).thenReturn(servletRequest);
67  
68      valve = new Tomcat11AgentValve();
69      valve.setNext(valveMock);
70      valve.invoke(request, response);
71      Mockito.verify(session, Mockito.times(2)).setAttribute(Mockito.anyString(), Mockito.any());
72    }
73  
74  }