ae6eb9757b54ae6f7739c0453641ab89299ad8c6
[portal/sdk.git] /
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
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.portalsdk.external.authorization.service;
39
40 import static org.junit.Assert.assertNotNull;
41
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Set;
48 import java.util.TreeSet;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.mockito.InjectMocks;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.onap.portalsdk.core.command.LoginBean;
60 import org.onap.portalsdk.core.domain.App;
61 import org.onap.portalsdk.core.domain.Role;
62 import org.onap.portalsdk.core.domain.RoleFunction;
63 import org.onap.portalsdk.core.domain.User;
64 import org.onap.portalsdk.core.domain.UserApp;
65 import org.onap.portalsdk.core.service.DataAccessService;
66 import org.onap.portalsdk.core.util.SystemProperties;
67 import org.onap.portalsdk.core.web.support.AppUtils;
68 import org.onap.portalsdk.core.web.support.UserUtils;
69 import org.onap.portalsdk.external.framework.MockitoTestSuite;
70 import org.powermock.api.mockito.PowerMockito;
71 import org.powermock.core.classloader.annotations.PrepareForTest;
72 import org.powermock.modules.junit4.PowerMockRunner;
73
74 @SuppressWarnings({ "unchecked", "rawtypes" })
75 @RunWith(PowerMockRunner.class)
76 @PrepareForTest({AppUtils.class, UserUtils.class, SystemProperties.class})
77 public class LoginExternalAuthServiceImplTest {
78
79         @InjectMocks
80         private LoginExternalAuthServiceImpl loginExternalAuthServiceImpl;
81         
82         @Mock
83         private DataAccessService dataAccessService;
84         
85         @Mock
86         private UserApiService userApiService;
87         
88         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
89
90         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
91         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
92         
93         @Before
94         public void setup() {
95                 PowerMockito.mockStatic(AppUtils.class);
96                 PowerMockito.mockStatic(UserUtils.class);
97                 PowerMockito.mockStatic(SystemProperties.class);
98         }
99         
100         private User userObj() {
101                 User user = new User();
102                 user.setEmail("test@mail.com");
103                 user.setFirstName("Test_firstname");
104                 user.setHrid("test_hrid");
105                 user.setJobTitle("test_jobtitle");
106                 user.setLastName("test_lastname");
107                 user.setLoginId("test123");
108                 user.setOrgManagerUserId("test456");
109                 user.setMiddleInitial("test_middlename");
110                 user.setOrgCode("testcode");
111                 user.setOrgId(1l);
112                 user.setOrgUserId("test123");
113                 user.setActive(true);
114                 user.setLastLoginDate(new Date());
115                 RoleFunction roleFunction = new RoleFunction();
116                 roleFunction.setId(12L);
117                 roleFunction.setName("Role Function");
118                 
119                 Set roleFunctions = new TreeSet();
120                 roleFunctions.add(roleFunction);
121                 
122                 Role role = new Role();
123                 role.setName("Role");
124                 role.setActive(true);
125                 role.setRoleFunctions(roleFunctions);   
126                 Set userApps = new TreeSet();
127                 UserApp userApp = new UserApp();
128                 userApp.setUserId(1L);
129                 userApp.setApp(getApp());
130                 userApp.setRole(role);
131                 userApps.add(userApp);
132                 user.setUserApps(userApps);
133                 return user;
134         }
135         
136         public App getApp() {
137                 App app = new App();
138                 app.setId(new Long(1));
139                 app.setName("Default");
140                 return app;
141         }
142         
143         @Test
144         public void findUserTest() throws Exception {
145                 LoginBean bean = new LoginBean();
146                 bean.setUserid("test123");
147                 Map additionalParams = new HashMap<>();
148                 User user = userObj();
149                 user.setId(1l);
150                 List usersId = new ArrayList<>();
151                 usersId.add(user.getId());
152                 List users = new ArrayList<>();
153                 users.add(user);
154                 Mockito.when(userApiService.getUser(bean.getUserid(), mockedRequest)).thenReturn(user);
155                 Map<String, String> params = new HashMap<>();
156                 params.put("orgUserId", "test123");
157                 Mockito.when(dataAccessService.executeNamedQuery("getUserIdByorgUserId", params, null)).thenReturn(usersId);
158                 Map<String, String> params2 = new HashMap<>();
159                 params.put("org_user_id", "test123");
160                 Mockito.when(dataAccessService.executeNamedQuery("getUserByOrgUserId", params2, new HashMap())).thenReturn(users);
161                 LoginBean expected = loginExternalAuthServiceImpl.findUser(bean, "menu", additionalParams, mockedRequest);
162                 assertNotNull(expected);
163         }
164         
165         @Test
166         public void findUserForNewUserTest() throws Exception {
167                 LoginBean bean = new LoginBean();
168                 bean.setUserid("test123");
169                 Map additionalParams = new HashMap<>();
170                 User user = userObj();
171                 List usersId = new ArrayList<>();
172                 usersId.add(user.getId());
173                 List users = new ArrayList<>();
174                 users.add(user);
175                 Mockito.when(userApiService.getUser(bean.getUserid(), mockedRequest)).thenReturn(user);
176                 Map<String, String> params = new HashMap<>();
177                 params.put("orgUserId", "test123");
178                 Mockito.when(dataAccessService.executeNamedQuery("getUserIdByorgUserId", params, null)).thenReturn(usersId);
179                 Map<String, String> params2 = new HashMap<>();
180                 params.put("org_user_id", "test123");
181                 Mockito.when(dataAccessService.executeNamedQuery("getUserByOrgUserId", params2, new HashMap())).thenReturn(null);
182                 LoginBean expected = loginExternalAuthServiceImpl.findUser(bean, "menu", additionalParams, mockedRequest);
183                 assertNotNull(expected);
184         }
185 }