1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. The ASF licenses this file to You
4+ * under the Apache License, Version 2.0 (the "License"); you may not
5+ * use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License. For additional information regarding
15+ * copyright in this work, please see the NOTICE file in the top level
16+ * directory of this distribution.
17+ */
18+
19+ package org .apache .roller .weblogger .ui .core ;
20+
21+ import org .apache .roller .weblogger .pojos .User ;
22+ import org .apache .roller .weblogger .util .cache .Cache ;
23+ import org .junit .jupiter .api .BeforeEach ;
24+ import org .junit .jupiter .api .Test ;
25+
26+ import static org .junit .jupiter .api .Assertions .assertEquals ;
27+ import static org .junit .jupiter .api .Assertions .assertNull ;
28+ import static org .mockito .ArgumentMatchers .any ;
29+ import static org .mockito .Mockito .*;
30+
31+ class RollerSessionManagerTest {
32+ private RollerSessionManager sessionManager ;
33+ private Cache mockCache ;
34+
35+ @ BeforeEach
36+ void setUp () {
37+ mockCache = mock (Cache .class );
38+ sessionManager = new RollerSessionManager (mockCache );
39+ }
40+
41+ @ Test
42+ void testRegisterSession () {
43+ RollerSession mockSession = mock (RollerSession .class );
44+ String userName = "testUser" ;
45+
46+ sessionManager .register (userName , mockSession );
47+
48+ verify (mockCache ).put (userName , mockSession );
49+ }
50+
51+ @ Test
52+ void testGetSession () {
53+ RollerSession mockSession = mock (RollerSession .class );
54+ String userName = "testUser" ;
55+ when (mockCache .get (userName )).thenReturn (mockSession );
56+
57+ RollerSession result = sessionManager .get (userName );
58+
59+ assertEquals (mockSession , result );
60+ verify (mockCache ).get (userName );
61+ }
62+
63+ @ Test
64+ void testInvalidateSession () {
65+ String userName = "testUser" ;
66+
67+ sessionManager .invalidate (userName );
68+
69+ verify (mockCache ).remove (userName );
70+ }
71+
72+ @ Test
73+ void testCacheHandlerInvalidation () {
74+ User mockUser = mock (User .class );
75+ String userName = "testUser" ;
76+ when (mockUser .getUserName ()).thenReturn (userName );
77+
78+ sessionManager .new SessionCacheHandler ().invalidate (mockUser );
79+
80+ verify (mockCache ).remove (userName );
81+ }
82+
83+ @ Test
84+ void testNullInputHandling () {
85+ RollerSession mockSession = mock (RollerSession .class );
86+
87+ sessionManager .register (null , mockSession );
88+ sessionManager .invalidate (null );
89+ sessionManager .get (null );
90+
91+ verify (mockCache , never ()).put (any (), any ());
92+ verify (mockCache , never ()).remove (any ());
93+ verify (mockCache , never ()).get (any ());
94+ }
95+
96+ @ Test
97+ void testSessionTimeout () {
98+ String userName = "testUser" ;
99+ when (mockCache .get (userName )).thenReturn (null );
100+
101+ RollerSession result = sessionManager .get (userName );
102+
103+ assertNull (result );
104+ verify (mockCache ).get (userName );
105+ }
106+ }
0 commit comments