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