3027cbeef1d2d34163bcf71fdd3937477e6eb5bb
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 package org.onap.portalsdk.core.onboarding.listener;
40
41 import static org.junit.Assert.assertEquals;
42
43 import java.util.Arrays;
44 import java.util.HashSet;
45 import java.util.Map;
46 import java.util.Set;
47
48 import javax.servlet.ServletContext;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpSession;
51
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.*;
56 import org.mockito.runners.MockitoJUnitRunner;
57 import org.onap.portalsdk.core.onboarding.listener.PortalTimeoutHandler.SessionCommInf;
58 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
59
60
61 import com.fasterxml.jackson.core.JsonProcessingException;
62
63 @RunWith(MockitoJUnitRunner.class)
64 public class PortalTimeoutHandlerTest {
65         
66         
67         @InjectMocks
68         PortalTimeoutHandler port;
69         
70         String portalJSessionId = "phrase"; 
71         
72         String jSessionId = "test";
73                         
74         String ecompRestURL,userName,pwd,uebKey;
75         
76         @Mock
77         HttpServletRequest request;
78         
79         @Mock
80         ServletContext servCont;
81         
82         @Mock
83         Object servlettest;
84         
85         @Mock
86         SessionCommInf _sessionComm;
87         
88         @Mock
89         HttpSession session;
90         
91         String jSessionKey;
92         
93         
94         String sessionTimeoutMapStr;
95         
96         @Mock (name="sessionMap")
97         Map<String, HttpSession> sessionMap;
98         
99         @Mock
100         Set<String> test_set = new HashSet<String>(Arrays.asList("test1", "test2"));
101         
102         @Before
103         public void init() {
104                 
105                 MockitoAnnotations.initMocks(this);
106                         
107                 //port = Mockito.spy(PortalTimeoutHandler.class);
108                 
109                 Mockito.when(session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID)).thenReturn("test-Phrase");
110                 
111                 session.setAttribute("test1","test2");
112                 
113                 Mockito.when(request.getSession()).thenReturn(session);
114                 
115                 Mockito.when(request.getSession(false)).thenReturn(session);
116                 
117                 Mockito.when(sessionMap.get(jSessionKey)).thenReturn(session);
118                 
119                 Mockito.when(sessionMap.keySet()).thenReturn(test_set);
120                 
121                 long lastAccessedTimeMilliSec = 200;
122                 long previousToLastAccessTime = 256;
123                 
124                 Mockito.when(session.getAttribute(PortalApiConstants.SESSION_PREVIOUS_ACCESS_TIME)).thenReturn(previousToLastAccessTime);               
125                 Mockito.when(session.getLastAccessedTime()).thenReturn(lastAccessedTimeMilliSec);
126                 Mockito.when(session.getMaxInactiveInterval()).thenReturn(1);
127                 
128                         
129         }
130         
131         @Test
132         public void getInstance_test() {
133                 
134                 port.getInstance();
135                                 
136         }
137         
138         @Test
139         public void sessionCreated_test() {
140                 
141                 PortalTimeoutHandler.sessionCreated(portalJSessionId, jSessionId, session);
142         }
143         
144         @Test
145         public void sessionDestroyed_test() {
146                 
147                 port.sessionDestroyed(session);
148         }
149         
150         @Test
151         public void invalidateSession_test() {
152                 
153                 assertEquals(false,port.invalidateSession(portalJSessionId));
154         }
155         
156         @Test
157         public void gatherSessionExtensions_test() {
158                 
159                 port.gatherSessionExtensions();
160                 
161                 
162         }
163         
164     @Test
165         public void updateSessionExtensions_test() {
166                 
167                 PortalTimeoutHandler.updateSessionExtensions(sessionTimeoutMapStr);
168         }
169         
170         @Test(expected=NullPointerException.class)
171         public void synchronizeSessionForLastMinuteRequests_test_null() throws JsonProcessingException {
172                                 
173                 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
174         }
175         
176         @Test(expected=NumberFormatException.class)
177         public void synchronizeSessionForLastMinuteRequests_test_notnull() throws JsonProcessingException {
178                 Mockito.when(session.getServletContext()).thenReturn(servCont);
179                 
180                 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(servlettest);
181         
182                 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
183
184         }
185         
186         @Test
187         public void synchronizeSessionForLastMinuteRequests_test_notnull_case2() throws JsonProcessingException {
188                 Mockito.when(session.getServletContext()).thenReturn(servCont);
189                 
190                 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(null);
191                 
192                 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
193
194         }
195         
196         @Test
197         public void synchronizeSessionForLastMinuteRequests_test_notnull_case3() throws JsonProcessingException {
198                 Mockito.when(session.getServletContext()).thenReturn(servCont);
199                 
200                 
201                 //Mockito.when(session.getAttribute(PortalApiConstants.SESSION_PREVIOUS_ACCESS_TIME)).thenReturn(previousToLastAccessTime);
202                                 
203                 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(null);
204                 
205                 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
206
207         }
208         
209         @Test
210         public void synchronizeSessionForLastMinuteRequests_test_notnull_case4() throws JsonProcessingException {
211                 Mockito.when(session.getServletContext()).thenReturn(servCont);
212                 
213                 
214                 
215                 
216                 
217                                 
218                 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(2000);
219         
220                 //session.setAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK,2000);
221                 
222                 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
223
224         }
225     
226         
227 }