CADI AAF Integration and merging the code
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / interceptor / PortalResourceInterceptorTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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 package org.onap.portalapp.portal.interceptor;
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Matchers.anyString;
41 import static org.mockito.Mockito.when;
42
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Set;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import javax.servlet.http.HttpSession;
52
53 import org.junit.Before;
54 import org.junit.Ignore;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.InjectMocks;
58 import org.mockito.Mock;
59 import org.mockito.MockitoAnnotations;
60 import org.onap.portalapp.portal.controller.BasicAuthenticationController;
61 import org.onap.portalapp.portal.core.MockEPUser;
62 import org.onap.portalapp.portal.domain.EPApp;
63 import org.onap.portalapp.portal.domain.EPUser;
64 import org.onap.portalapp.portal.service.ExternalAccessRolesService;
65 import org.onap.portalsdk.core.controller.FusionBaseController;
66 import org.onap.portalsdk.core.util.SystemProperties;
67 import org.powermock.api.mockito.PowerMockito;
68 import org.powermock.core.classloader.annotations.PrepareForTest;
69 import org.powermock.modules.junit4.PowerMockRunner;
70 import org.springframework.web.method.HandlerMethod;
71
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ SystemProperties.class })
74 public class PortalResourceInterceptorTest {
75         
76         @InjectMocks
77         PortalResourceInterceptor portalResourceInterceptor;
78         @Mock
79         HttpServletRequest request;
80         @Mock
81         HttpServletResponse response;
82         @Mock
83         HandlerMethod handler;
84         @Mock
85         HttpSession session;
86         @Mock
87         FusionBaseController fusionBaseController;
88         @Mock
89         BasicAuthenticationController basicAuthenticationController;
90         @Mock
91         private ExternalAccessRolesService externalAccessRolesService;
92         @Mock
93         PrintWriter printWriter;
94         
95         MockEPUser mockUser = new MockEPUser();
96         
97         @Before
98         public void setup() {
99                 MockitoAnnotations.initMocks(this);
100                 PowerMockito.mockStatic(SystemProperties.class);
101                 
102         }
103         
104         @Test
105         public void testPreHandle()throws Exception {
106                 Set<String> data=new HashSet<>();
107                 data.add("test");
108                 when(request.getRequestURI()).thenReturn("test/portalApi/test");
109                 
110                 when(session.getAttribute(anyString())).thenReturn(data);
111                 when(request.getSession()).thenReturn(session);
112                 when(fusionBaseController.isAccessible()).thenReturn(true);
113                 when(handler.getBean()).thenReturn(fusionBaseController);
114                 
115                 
116         boolean result= portalResourceInterceptor.preHandle(request, response, handler);
117         assertEquals(true, result);
118                 
119                 
120         }
121         
122         @Ignore
123         @Test
124         public void testPreHandlePass()throws Exception {
125                 Set<String> data=new HashSet<>();
126                 data.add("test/test");
127                 Set<String> allFunctions=new HashSet<>();
128                 allFunctions.add("test/test");
129                 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
130                 EPUser user = mockUser.mockEPUser();
131                 when(request.getSession()).thenReturn(session);
132                 when(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME)).thenReturn("role_functions_attribute_name");
133                 when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name");
134                 when(session.getAttribute("user_attribute_name")).thenReturn(user);
135                 when(session.getAttribute("role_functions_attribute_name")).thenReturn(data);
136                 when(session.getAttribute("allRoleFunctions")).thenReturn(allFunctions);
137                 when(fusionBaseController.isAccessible()).thenReturn(false);
138                 when(handler.getBean()).thenReturn(fusionBaseController);
139                 
140                 
141         boolean result= portalResourceInterceptor.preHandle(request, response, handler);
142         
143         assertEquals(true, result);
144                 
145         }
146         
147         
148
149         @Test
150         public void testPreHandleAuth()throws Exception {
151                 Set<String> data=new HashSet<>();
152                 data.add("test/test");
153                 List<EPApp> apps=new ArrayList<>();
154                 EPApp app=new EPApp();
155                 app.setUsername("test");
156                 apps.add(app);
157                 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
158                 
159                 when(request.getHeader("Authorization")).thenReturn("Basictest");
160                 when(request.getHeader("uebkey")).thenReturn("test");
161                                 when(request.getSession()).thenReturn(session);
162                 when(fusionBaseController.isAccessible()).thenReturn(false);
163                 when(handler.getBean()).thenReturn(basicAuthenticationController);
164                 when( externalAccessRolesService.getApp("test")).thenReturn(apps);
165                 when(response.getWriter()).thenReturn(printWriter);
166                 
167         boolean result= portalResourceInterceptor.preHandle(request, response, handler);
168         
169         assertEquals(false, result);
170                 
171         }
172         
173         
174
175 }