2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
39 package org.onap.portalsdk.core.onboarding.listener;
41 import static org.junit.Assert.assertEquals;
43 import java.util.Arrays;
44 import java.util.HashSet;
48 import javax.servlet.ServletContext;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpSession;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
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;
61 import com.fasterxml.jackson.core.JsonProcessingException;
63 @RunWith(MockitoJUnitRunner.class)
64 public class PortalTimeoutHandlerTest {
68 PortalTimeoutHandler port;
70 String portalJSessionId = "phrase";
72 String jSessionId = "test";
74 String ecompRestURL,userName,pwd,uebKey;
77 HttpServletRequest request;
80 ServletContext servCont;
86 SessionCommInf _sessionComm;
94 String sessionTimeoutMapStr;
96 @Mock (name="sessionMap")
97 Map<String, HttpSession> sessionMap;
100 Set<String> test_set = new HashSet<String>(Arrays.asList("test1", "test2"));
105 MockitoAnnotations.initMocks(this);
107 //port = Mockito.spy(PortalTimeoutHandler.class);
109 Mockito.when(session.getAttribute(PortalApiConstants.PORTAL_JSESSION_ID)).thenReturn("test-Phrase");
111 session.setAttribute("test1","test2");
113 Mockito.when(request.getSession()).thenReturn(session);
115 Mockito.when(request.getSession(false)).thenReturn(session);
117 Mockito.when(sessionMap.get(jSessionKey)).thenReturn(session);
119 Mockito.when(sessionMap.keySet()).thenReturn(test_set);
121 long lastAccessedTimeMilliSec = 200;
122 long previousToLastAccessTime = 256;
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);
132 public void getInstance_test() {
139 public void sessionCreated_test() {
141 PortalTimeoutHandler.sessionCreated(portalJSessionId, jSessionId, session);
145 public void sessionDestroyed_test() {
147 port.sessionDestroyed(session);
151 public void invalidateSession_test() {
153 assertEquals(false,port.invalidateSession(portalJSessionId));
157 public void gatherSessionExtensions_test() {
159 port.gatherSessionExtensions();
165 public void updateSessionExtensions_test() {
167 PortalTimeoutHandler.updateSessionExtensions(sessionTimeoutMapStr);
170 @Test(expected=NullPointerException.class)
171 public void synchronizeSessionForLastMinuteRequests_test_null() throws JsonProcessingException {
173 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
176 @Test(expected=NumberFormatException.class)
177 public void synchronizeSessionForLastMinuteRequests_test_notnull() throws JsonProcessingException {
178 Mockito.when(session.getServletContext()).thenReturn(servCont);
180 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(servlettest);
182 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
187 public void synchronizeSessionForLastMinuteRequests_test_notnull_case2() throws JsonProcessingException {
188 Mockito.when(session.getServletContext()).thenReturn(servCont);
190 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(null);
192 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
197 public void synchronizeSessionForLastMinuteRequests_test_notnull_case3() throws JsonProcessingException {
198 Mockito.when(session.getServletContext()).thenReturn(servCont);
201 //Mockito.when(session.getAttribute(PortalApiConstants.SESSION_PREVIOUS_ACCESS_TIME)).thenReturn(previousToLastAccessTime);
203 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(null);
205 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);
210 public void synchronizeSessionForLastMinuteRequests_test_notnull_case4() throws JsonProcessingException {
211 Mockito.when(session.getServletContext()).thenReturn(servCont);
218 Mockito.when(session.getServletContext().getAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK)).thenReturn(2000);
220 //session.setAttribute(PortalApiConstants.PORTAL_SESSION_SLOT_CHECK,2000);
222 port.synchronizeSessionForLastMinuteRequests(request, ecompRestURL, userName, pwd, uebKey, _sessionComm);