80ca142474f91a646004644084844733a0969131
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / util / EPUserUtilsTest.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.util;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.Mockito.mock;
44
45 import java.util.ArrayList;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.Set;
51 import java.util.SortedSet;
52 import java.util.TreeSet;
53 import java.util.UUID;
54 import java.util.Vector;
55 import java.util.regex.Pattern;
56
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59 import javax.servlet.http.HttpSession;
60
61 import org.apache.commons.codec.binary.Hex;
62 import org.drools.core.command.assertion.AssertEquals;
63 import org.junit.Before;
64 import org.junit.Test;
65 import org.junit.runner.RunWith;
66 import org.mockito.InjectMocks;
67 import org.mockito.Matchers;
68 import org.mockito.Mock;
69 import org.mockito.Mockito;
70 import org.mockito.MockitoAnnotations;
71 import org.onap.portalapp.portal.core.MockEPUser;
72 import org.onap.portalapp.portal.domain.EPRole;
73 import org.onap.portalapp.portal.domain.EPUser;
74 import org.onap.portalapp.portal.domain.EPUserApp;
75 import org.onap.portalapp.portal.exceptions.RoleFunctionException;
76 import org.onap.portalapp.portal.framework.MockitoTestSuite;
77 import org.onap.portalapp.portal.service.EPRoleFunctionService;
78 import org.onap.portalsdk.core.domain.RoleFunction;
79 import org.onap.portalsdk.core.exception.SessionExpiredException;
80 import org.onap.portalsdk.core.menu.MenuBuilder;
81 import org.onap.portalsdk.core.service.DataAccessService;
82 import org.onap.portalsdk.core.util.SystemProperties;
83 import org.onap.portalsdk.core.web.support.AppUtils;
84 import org.powermock.api.mockito.PowerMockito;
85 import org.powermock.core.classloader.annotations.PrepareForTest;
86 import org.powermock.modules.junit4.PowerMockRunner;
87
88 import org.apache.commons.codec.DecoderException;
89
90 @RunWith(PowerMockRunner.class)
91 @PrepareForTest({SystemProperties.class,AppUtils.class,Hex.class,MenuBuilder.class})
92 public class EPUserUtilsTest {
93
94 /*      @Before
95         public void setup() {
96                  //DataAccessService dataAccessService=Mockito.mock(DataAccessService.class);
97         }*/
98         
99         @Mock
100         DataAccessService dataAccessService;
101         
102         @Mock
103         EPRoleFunctionService epRoleFunctionService;
104         
105         /*@Mock
106         DataAccessService dataAccessService;*/
107         
108         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
109
110         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
111         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
112         
113         MockEPUser mockUser = new MockEPUser();
114
115         
116         @Test
117         public void getUserIdAsLongTest() {
118                 Long expectedUserId=1L;
119         PowerMockito.mockStatic(SystemProperties.class);
120                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
121                 Long userId=EPUserUtils.getUserIdAsLong(mockedRequest);
122                 assertEquals(userId,expectedUserId);
123         }
124         
125         @Test
126         public void getUserIdAsLongTestWithRequestNull() {
127                 Long expectedUserId=-1L;
128                 EPUser user = mockUser.mockEPUser();
129                 PowerMockito.mockStatic(SystemProperties.class);
130                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
131                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
132                 Long userId=EPUserUtils.getUserIdAsLong(mockedRequest);
133                 assertEquals(userId,expectedUserId);
134
135         }
136         
137         @Test
138         public void setDataAccessServiceTest() throws RoleFunctionException{
139                 
140         }
141         
142         @Test
143         public void getUserIdAsLongWithExceptionTest() {
144                 Long expectedUserId=1L;
145         PowerMockito.mockStatic(SystemProperties.class);
146                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
147                 Long userId=EPUserUtils.getUserIdAsLong(mockedRequest);
148                 assertEquals(userId,expectedUserId);
149         }
150         
151         @Test
152         public void getRequestIdTestWithException() {
153                 String expectedRequestId="c6f9542c-7378-4995-9ee3-cb498710e1ea";
154         PowerMockito.mockStatic(SystemProperties.class);
155                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
156                 String requestId=EPUserUtils.getRequestId(mockedRequest);
157                 assertNotNull(requestId);
158         }
159         
160         
161         
162         @Test
163         public void getRequestIdTest() {
164                 String expectedRequestId=UUID.randomUUID().toString();
165                 Enumeration<String> enums;
166                 Vector<String> attrs = new Vector<String>();
167                 attrs.add("ff_test");
168                 enums = attrs.elements();
169                 Mockito.when(mockedRequest.getHeaderNames()).thenReturn(enums);
170         PowerMockito.mockStatic(SystemProperties.class);
171                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
172                 String requestId=EPUserUtils.getRequestId(mockedRequest);
173                 assertNotNull(requestId);
174         }
175         
176         
177         @Test
178         public void getRequestIdTestWithSess() {
179                 Enumeration<String> enums;
180                 Vector<String> attrs = new Vector<String>();
181                 attrs.add("X-ECOMP-RequestID");
182                 enums = attrs.elements();
183                 Mockito.when(mockedRequest.getHeaderNames()).thenReturn(enums);
184         PowerMockito.mockStatic(SystemProperties.class);
185                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
186                 Mockito.when(mockedRequest.getHeader("X-ECOMP-RequestID")).thenReturn("test");
187                 String requestId=EPUserUtils.getRequestId(mockedRequest);
188                 assertNotNull(requestId);
189                 
190         }
191         
192         @Test
193         public void getFullURLTest(){
194                 String expected="test?testString";
195                 StringBuffer stringBuffer = new StringBuffer("test");
196                 Mockito.when(mockedRequest.getRequestURL()).thenReturn(stringBuffer);
197                 Mockito.when(mockedRequest.getQueryString()).thenReturn("testString");
198                 String actual=EPUserUtils.getFullURL(mockedRequest);
199                 assertEquals(actual,expected);
200         }
201         
202         @Test
203         public void getFullURLTestWithQueryStringNull(){
204                 String expected="test";
205                 StringBuffer stringBuffer = new StringBuffer("test");
206                 Mockito.when(mockedRequest.getRequestURL()).thenReturn(stringBuffer);
207                 Mockito.when(mockedRequest.getQueryString()).thenReturn(null);
208                 String actual=EPUserUtils.getFullURL(mockedRequest);
209                 assertEquals(actual,expected);
210
211         }
212         
213         
214         
215         @Test
216         public void getFullURLTestWithRequestNull(){
217                 String expected="";
218                 StringBuffer stringBuffer = new StringBuffer("test");
219                 Mockito.when(mockedRequest.getRequestURL()).thenReturn(stringBuffer);
220                 Mockito.when(mockedRequest.getQueryString()).thenReturn(null);
221                 String actual=EPUserUtils.getFullURL(null);
222                 assertEquals(actual,expected);
223
224         }
225         
226         @Test
227         public void getUserIdTest(){
228                 int expected=1;
229                 PowerMockito.mockStatic(SystemProperties.class);
230                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("1");
231                 int actual=EPUserUtils.getUserId(mockedRequest);
232                 assertEquals(actual,expected);
233
234         }
235         
236         @Test
237         public void hasRoleTest(){
238                 boolean expected=false;
239                 EPUser user = mockUser.mockEPUser();
240                 boolean actual=EPUserUtils.hasRole(user, "12");
241                 assertEquals(actual,expected);
242
243         }
244         
245         @Test
246         public void getRolesTest(){
247                 HashMap roles=new HashMap<>();
248                 roles.put((long) 1, "test");
249                 HashMap expected=new HashMap<>();
250                 expected.put((long) 1, "test");
251                 Set menuSet = new HashSet<>();
252                 menuSet.add(1);
253                 HttpSession session = mock(HttpSession.class);
254                 PowerMockito.mockStatic(SystemProperties.class);
255                 PowerMockito.mockStatic(AppUtils.class);
256                 PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(session);
257                 PowerMockito.when(SystemProperties.getProperty(Matchers.anyString())).thenReturn("12");
258                 Mockito.when(session.getAttribute(Matchers.anyString())).thenReturn(roles);
259                 roles= (HashMap) EPUserUtils.getRoles(mockedRequest);
260                 assertEquals(roles,expected);
261
262
263         }
264         
265         @Test
266         public void getRolesTestWithNoRoles(){
267                 EPUser user=mock(EPUser.class);
268                 HashMap roles=new HashMap<>();
269                 HashMap expected=new HashMap<>();
270                 SortedSet<EPRole> role= new TreeSet<>();
271                 role.add(new EPRole());
272                 //user.setEPRoles(role);
273                 HttpSession session = mock(HttpSession.class);
274                 PowerMockito.mockStatic(SystemProperties.class);
275                 PowerMockito.mockStatic(AppUtils.class);
276                 PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(session);
277                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME)).thenReturn("12");
278                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("attr_name");
279                 Mockito.when(session.getAttribute("attr_name")).thenReturn(user);
280                 Mockito.when(user.getEPRoles()).thenReturn(role);
281                 Mockito.when(session.getAttribute("12")).thenReturn(null);
282                 roles= (HashMap) EPUserUtils.getRoles(mockedRequest);
283                 assertEquals(roles,expected);
284
285         }
286         
287         @Test
288         public void getRolesTestWithNoRolesException(){
289                 EPUser user=mock(EPUser.class);
290                  EPRole epRole=mock(EPRole.class);
291                  HashMap roles=new HashMap<>();
292                         
293                 SortedSet<EPRole> role= new TreeSet<>();
294                  role.add(epRole);
295
296                 SortedSet<EPRole> childRoles=new TreeSet<>();
297                  EPRole epRole2=new EPRole();
298                  epRole2.setActive(true);
299                  epRole2.setId(3L);
300                         childRoles.add(epRole2);
301                         
302                         SortedSet<EPUserApp> epUserApps= new TreeSet<>();
303                         EPUserApp epUserApp=new EPUserApp();
304                         EPRole epRole3=new EPRole();
305                         epRole3.setActive(true);
306                          epRole3.setId(999L);
307                         epUserApp.setRole(epRole3);
308                         epUserApps.add(epUserApp);
309                         
310                 HttpSession session = mock(HttpSession.class);
311                 PowerMockito.mockStatic(SystemProperties.class);
312                 PowerMockito.mockStatic(AppUtils.class);
313                 PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(session);
314                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME)).thenReturn("12");
315                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("attr_name");
316                 Mockito.when(session.getAttribute("attr_name")).thenReturn(user);
317                 Mockito.when(user.getEPRoles()).thenReturn(role);
318                 Mockito.when(epRole.getActive()).thenReturn(true);
319                 Mockito.when(epRole.getId()).thenReturn(2L);
320                 Mockito.when(epRole.getChildRoles()).thenReturn(childRoles);
321                 Mockito.when(user.getEPUserApps()).thenReturn(epUserApps);
322                 Mockito.when(session.getAttribute("12")).thenReturn(null);
323                 roles= (HashMap) EPUserUtils.getRoles(mockedRequest);
324                 assertNotNull(roles);
325         }
326         
327         @Test
328         public void decodeFunctionCodeTest() throws Exception{
329                 String expected="test_instance";
330        String actual= EPUserUtils.decodeFunctionCode("test_instance");
331        assertEquals(expected, actual);
332         }
333         
334         @Test(expected=RoleFunctionException.class)
335         public void decodeFunctionCodeTestWithException() throws DecoderException, RoleFunctionException {
336                 PowerMockito.mockStatic(Hex.class);
337                 PowerMockito.when(Hex.decodeHex(Matchers.any())).thenThrow(DecoderException.class);
338                 EPUserUtils.decodeFunctionCode("n+");
339         }
340         
341         @Test
342         public void setUserSessionTest() throws Exception{
343                 EPUser user = mockUser.mockEPUser();
344         Set<String> applicationMenuData = new HashSet<String>();
345         applicationMenuData.add("test_application_menuData");
346         Set<String> businessDirectMenuData = new HashSet<String>();
347         businessDirectMenuData.add("test_businessDirect_menuData");
348         HttpSession session = mockedRequest.getSession();
349                 PowerMockito.mockStatic(SystemProperties.class);
350                 PowerMockito.mockStatic(EPRoleFunctionService.class);
351                 PowerMockito.mockStatic(MenuBuilder.class);
352                 List<RoleFunction> roleFunctions=new ArrayList<RoleFunction>();
353                 RoleFunction roleFunction=new RoleFunction();
354                 roleFunction.setId(1L);
355                 roleFunction.setCode("test_code");
356                 roleFunctions.add(roleFunction);
357                 
358                 Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(roleFunctions);
359                 Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
360                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
361         EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData,  epRoleFunctionService);
362         assertNotNull(session);
363
364
365         }
366         
367         @Test
368         public void setUserSessionTestWithException() throws Exception{
369                 EPUser user = mockUser.mockEPUser();
370         Set<String> applicationMenuData = new HashSet<String>();
371         applicationMenuData.add("test_application_menuData");
372         Set<String> businessDirectMenuData = new HashSet<String>();
373         businessDirectMenuData.add("test_businessDirect_menuData");
374         HttpSession session = mockedRequest.getSession();
375                 PowerMockito.mockStatic(SystemProperties.class);
376                 PowerMockito.mockStatic(EPRoleFunctionService.class);
377                 PowerMockito.mockStatic(MenuBuilder.class);
378                 List<RoleFunction> roleFunctions=new ArrayList<RoleFunction>();
379                 RoleFunction roleFunction=new RoleFunction();
380                 roleFunction.setId(1L);
381                 roleFunction.setCode("test_code");
382                 roleFunctions.add(roleFunction);
383                 Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null);
384                 Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
385                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
386         EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData,  epRoleFunctionService);
387         assertNotNull(session);
388         }
389         
390         @Test(expected=SessionExpiredException.class)
391         public void setUserSessionTestWithsessionException() throws Exception{
392                 EPUser user = mockUser.mockEPUser();
393         Set<String> applicationMenuData = new HashSet<String>();
394         applicationMenuData.add("test_application_menuData");
395         Set<String> businessDirectMenuData = new HashSet<String>();
396         businessDirectMenuData.add("test_businessDirect_menuData");
397         HttpSession session = mockedRequest.getSession();
398                 PowerMockito.mockStatic(SystemProperties.class);
399                 PowerMockito.mockStatic(EPRoleFunctionService.class);
400                 PowerMockito.mockStatic(AppUtils.class);
401                 PowerMockito.mockStatic(MenuBuilder.class);
402                 List<RoleFunction> roleFunctions=new ArrayList<RoleFunction>();
403                 RoleFunction roleFunction=new RoleFunction();
404                 roleFunction.setId(1L);
405                 roleFunction.setCode("test_code");
406                 roleFunctions.add(roleFunction);
407         Mockito.when(AppUtils.getSession(mockedRequest)).thenReturn(null);
408                 Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null);
409                 Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
410                 PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
411         EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService);
412         assertNotNull(session);
413
414
415         }
416         
417         
418         @Test(expected=SessionExpiredException.class)
419         public void getUserSessionTest(){
420                 PowerMockito.mockStatic(AppUtils.class);
421                 PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(null);
422                 EPUserUtils.getUserSession(mockedRequest);
423         }
424         
425 }