2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.core.interceptor;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.http.HttpSession;
43 import org.junit.Assert;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mockito;
48 import org.onap.portalsdk.core.controller.FusionBaseController;
49 import org.onap.portalsdk.core.domain.User;
50 import org.onap.portalsdk.core.util.SystemProperties;
51 import org.onap.portalsdk.core.web.support.AppUtils;
52 import org.onap.portalsdk.core.web.support.UserUtils;
53 import org.powermock.api.mockito.PowerMockito;
54 import org.powermock.core.classloader.annotations.PrepareForTest;
55 import org.powermock.modules.junit4.PowerMockRunner;
56 import org.springframework.mock.web.MockHttpServletRequest;
57 import org.springframework.mock.web.MockHttpServletResponse;
58 import org.springframework.web.method.HandlerMethod;
61 @RunWith(PowerMockRunner.class)
62 @PrepareForTest({ AppUtils.class, UserUtils.class, SystemProperties.class })
63 public class SessionTimeoutInterceptorTest {
66 private SessionTimeoutInterceptor sessionTimeoutInterceptor;
69 public void preHandleTest() throws Exception {
70 MockHttpServletRequest request = new MockHttpServletRequest();
71 HttpServletResponse response = new MockHttpServletResponse();
73 request.setRequestURI("xyz/resource");
74 HandlerMethod handler = PowerMockito.mock(HandlerMethod.class);
75 FusionBaseController controller = PowerMockito.mock(FusionBaseController.class);
77 Mockito.when(handler.getBean()).thenReturn(controller);
78 Mockito.when(controller.isAccessible()).thenReturn(false);
79 Mockito.when(controller.isRESTfulCall()).thenReturn(false);
81 PowerMockito.mockStatic(AppUtils.class);
82 PowerMockito.mockStatic(UserUtils.class);
83 HttpSession session = PowerMockito.mock(HttpSession.class);
84 Mockito.when(AppUtils.getSession(request)).thenReturn(session);
85 Mockito.when(UserUtils.getUserSession(request)).thenReturn(new User());
87 boolean status = sessionTimeoutInterceptor.preHandle(request, response, handler);
88 Assert.assertTrue(status);
92 public void preHandleSecurityExceptionTest() throws Exception {
93 MockHttpServletRequest request = new MockHttpServletRequest();
94 HttpServletResponse response = new MockHttpServletResponse();
96 request.setRequestURI("xyz/resource/logout.htm");
97 HandlerMethod handler = PowerMockito.mock(HandlerMethod.class);
98 FusionBaseController controller = PowerMockito.mock(FusionBaseController.class);
100 Mockito.when(handler.getBean()).thenReturn(controller);
101 Mockito.when(controller.isAccessible()).thenReturn(false);
102 Mockito.when(controller.isRESTfulCall()).thenReturn(false);
104 PowerMockito.mockStatic(AppUtils.class);
105 PowerMockito.mockStatic(UserUtils.class);
106 HttpSession session = PowerMockito.mock(HttpSession.class);
107 Mockito.when(AppUtils.getSession(request)).thenReturn(session);
108 Mockito.when(UserUtils.getUserSession(request)).thenReturn(new User());
110 boolean status = sessionTimeoutInterceptor.preHandle(request, response, handler);
111 Assert.assertFalse(status);
115 public void preHandleExceptionTest() throws Exception {
116 MockHttpServletRequest request = new MockHttpServletRequest();
117 HttpServletResponse response = new MockHttpServletResponse();
119 request.setRequestURI("xyz/resource");
120 HandlerMethod handler = PowerMockito.mock(HandlerMethod.class);
121 FusionBaseController controller = PowerMockito.mock(FusionBaseController.class);
123 Mockito.when(handler.getBean()).thenReturn(controller);
124 Mockito.when(controller.isAccessible()).thenReturn(false);
125 Mockito.when(controller.isRESTfulCall()).thenReturn(false);
127 PowerMockito.mockStatic(AppUtils.class);
128 PowerMockito.mockStatic(UserUtils.class);
129 HttpSession session = PowerMockito.mock(HttpSession.class);
130 Mockito.when(AppUtils.getSession(request)).thenReturn(session);
131 Mockito.when(UserUtils.getUserSession(request)).thenReturn(null);
133 boolean status = sessionTimeoutInterceptor.preHandle(request, response, handler);
134 Assert.assertFalse(status);
137 @Test(expected = SecurityException.class)
138 public void validateDomainTest() throws Exception {
139 String relativePath = "testUrl";
140 String redirectUrl = "http://www.xyz.com/" + relativePath;
142 PowerMockito.mockStatic(SystemProperties.class);
143 Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn(relativePath);
145 sessionTimeoutInterceptor.validateDomain(redirectUrl);