JUnits for coverage
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / authentication / SessionTimeoutInterceptorTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 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 package org.onap.portalapp.portal.authentication;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.Mockito.when;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
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;
65
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ EPUserUtils.class})
69 public class SessionTimeoutInterceptorTest {
70
71         
72         @Mock
73         LoginStrategy loginStrategy = new SimpleLoginStrategy();
74         
75         @Mock
76         EPFusionBaseController ePFusionBaseController = new EPFusionBaseController() {
77         };
78         
79         @Mock
80         FusionBaseController fusionBaseController;
81         
82         @Mock
83         HandlerMethod handlerMethod;
84         
85         @InjectMocks
86         SessionTimeoutInterceptor  sessionTimeoutInterceptor = new  SessionTimeoutInterceptor();
87
88         @Before
89         public void setup() {
90                 MockitoAnnotations.initMocks(this);
91         }
92
93         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
94
95         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
96         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
97         
98         @Test
99         public void preHandleTest() throws Exception{
100                 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, ePFusionBaseController));
101         }
102         
103         @Test
104         public void preHandleTestIfMethodIsinstanceOfHandlerMethod() throws Exception{
105                 
106                 EPUser user=new EPUser();
107                 user.setOrgUserId("test");
108                 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
109
110                 when(handlerMethod.getBean()).thenReturn(fusionBaseController);
111                 when(fusionBaseController.isAccessible()).thenReturn(false);
112                 PowerMockito.mockStatic(EPUserUtils.class);
113                 
114                 PowerMockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
115                 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
116                 
117         }
118         
119         @Test
120         public void preHandleTestLogout() throws Exception{
121                 
122                 EPUser user=new EPUser();
123                 user.setOrgUserId("test");
124                 when(mockedRequest.getRequestURI()).thenReturn("http://logout.html");
125
126                 when(handlerMethod.getBean()).thenReturn(fusionBaseController);
127                 when(fusionBaseController.isAccessible()).thenReturn(false);
128                 PowerMockito.mockStatic(EPUserUtils.class);
129                 
130                 PowerMockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
131                 assertFalse(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
132                 
133         }
134         
135         @Test
136         public void preHandleTestLogin() throws Exception{
137                 
138                 EPUser user=new EPUser();
139                 user.setOrgUserId("test");
140                 when(mockedRequest.getRequestURI()).thenReturn("http://login.html");
141
142                 when(handlerMethod.getBean()).thenReturn(fusionBaseController);
143                 when(fusionBaseController.isAccessible()).thenReturn(false);
144                 PowerMockito.mockStatic(EPUserUtils.class);
145                 
146                 PowerMockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
147                 assertTrue(sessionTimeoutInterceptor.preHandle(mockedRequest, mockedResponse, handlerMethod));
148                 
149         }
150 }