2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 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============================================
38 package org.onap.portalapp.portal.authentication;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.Mockito.when;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.onap.portalapp.authentication.LoginStrategy;
54 import org.onap.portalapp.authentication.SimpleLoginStrategy;
55 import org.onap.portalapp.controller.EPFusionBaseController;
56 import org.onap.portalapp.portal.domain.EPUser;
57 import org.onap.portalapp.portal.framework.MockitoTestSuite;
58 import org.onap.portalapp.portal.interceptor.SessionTimeoutInterceptor;
59 import org.onap.portalapp.util.EPUserUtils;
60 import org.onap.portalsdk.core.controller.FusionBaseController;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64 import org.springframework.web.method.HandlerMethod;
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ EPUserUtils.class})
69 public class SessionTimeoutInterceptorTest {
73 LoginStrategy loginStrategy = new SimpleLoginStrategy();
76 EPFusionBaseController ePFusionBaseController = new EPFusionBaseController() {
80 FusionBaseController fusionBaseController;
83 HandlerMethod handlerMethod;
86 SessionTimeoutInterceptor sessionTimeoutInterceptor = new SessionTimeoutInterceptor();
90 MockitoAnnotations.initMocks(this);
93 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
95 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
96 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
99 public void preHandleTest() throws Exception{
100 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, ePFusionBaseController));
104 public void preHandleTestIfMethodIsinstanceOfHandlerMethod() throws Exception{
106 EPUser user=new EPUser();
107 user.setOrgUserId("test");
108 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
110 when(handlerMethod.getBean()).thenReturn(fusionBaseController);
111 when(fusionBaseController.isAccessible()).thenReturn(false);
112 PowerMockito.mockStatic(EPUserUtils.class);
114 PowerMockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
115 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
120 public void preHandleTestLogout() throws Exception{
122 EPUser user=new EPUser();
123 user.setOrgUserId("test");
124 when(mockedRequest.getRequestURI()).thenReturn("http://logout.html");
126 when(handlerMethod.getBean()).thenReturn(fusionBaseController);
127 when(fusionBaseController.isAccessible()).thenReturn(false);
128 PowerMockito.mockStatic(EPUserUtils.class);
130 PowerMockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
131 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
136 public void preHandleTestLogin() throws Exception{
138 EPUser user=new EPUser();
139 user.setOrgUserId("test");
140 when(mockedRequest.getRequestURI()).thenReturn("http://login.html");
142 when(handlerMethod.getBean()).thenReturn(fusionBaseController);
143 when(fusionBaseController.isAccessible()).thenReturn(false);
144 PowerMockito.mockStatic(EPUserUtils.class);
146 PowerMockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
147 assertTrue(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));