989394d66937a9dffb981d09a5fb6cb95f5594cc
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017-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.service;
39
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.SortedSet;
46 import java.util.TreeSet;
47
48 import javax.servlet.http.HttpServletRequest;
49
50 import org.junit.Assert;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.onap.portalsdk.core.auth.LoginStrategy;
57 import org.onap.portalsdk.core.domain.App;
58 import org.onap.portalsdk.core.domain.Role;
59 import org.onap.portalsdk.core.domain.User;
60 import org.onap.portalsdk.core.domain.UserApp;
61 import org.onap.portalsdk.core.onboarding.client.AppContextManager;
62 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
63 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
64 import org.onap.portalsdk.core.onboarding.listener.PortalTimeoutHandler;
65 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
66 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
67 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
68 import org.onap.portalsdk.core.restful.domain.EcompRole;
69 import org.onap.portalsdk.core.restful.domain.EcompUser;
70 import org.onap.portalsdk.core.service.AppService;
71 import org.onap.portalsdk.core.service.RestApiRequestBuilder;
72 import org.onap.portalsdk.core.service.RoleService;
73 import org.onap.portalsdk.core.service.UserProfileService;
74 import org.onap.portalsdk.core.service.UserService;
75 import org.onap.portalsdk.core.service.WebServiceCallService;
76 import org.onap.portalsdk.core.util.JSONUtil;
77 import org.onap.portalsdk.core.util.SystemProperties;
78 import org.powermock.api.mockito.PowerMockito;
79 import org.powermock.core.classloader.annotations.PrepareForTest;
80 import org.powermock.modules.junit4.PowerMockRunner;
81 import org.springframework.context.ApplicationContext;
82
83 @RunWith(PowerMockRunner.class)
84 @PrepareForTest({ AppContextManager.class, PortalApiProperties.class, JSONUtil.class, PortalTimeoutHandler.class, SystemProperties.class, CipherUtil.class })
85 public class OnBoardingApiServiceImplTest {
86
87         @Mock
88         private RoleService roleService;
89         @Mock
90         private UserProfileService userProfileService;
91         @Mock
92         private IAdminAuthExtension adminAuthExtensionServiceImpl;
93
94         @Mock
95         private LoginStrategy loginStrategy;
96         @Mock
97         private UserService userService;
98         @Mock
99         private RestApiRequestBuilder restApiRequestBuilder;
100         @Mock
101         private AppService appServiceImpl;
102
103         @Before
104         public void setup() {
105
106                 PowerMockito.mockStatic(AppContextManager.class);
107                 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
108                 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
109                 PowerMockito.mockStatic(PortalApiProperties.class);
110                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
111                 Mockito.when(appContext.getBean(RoleService.class)).thenReturn(roleService);
112                 Mockito.when(appContext.getBean(UserProfileService.class)).thenReturn(userProfileService);
113                 Mockito.when(appContext.getBean(LoginStrategy.class)).thenReturn(loginStrategy);
114                 Mockito.when(appContext.getBean(IAdminAuthExtension.class)).thenReturn(adminAuthExtensionServiceImpl);
115                 Mockito.when(appContext.getBean(UserService.class)).thenReturn(userService);
116                 Mockito.when(appContext.getBean(RestApiRequestBuilder.class)).thenReturn(restApiRequestBuilder);
117                 Mockito.when(appContext.getBean(AppService.class)).thenReturn(appServiceImpl);
118
119         }
120
121         @Test
122         public void pushUserTest() throws PortalAPIException {
123                 PowerMockito.mockStatic(PortalApiProperties.class);
124                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
125                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
126                 EcompUser userJson = new EcompUser();
127                 onBoardingApiServiceImpl.pushUser(userJson);
128                 Assert.assertTrue(true);
129         }
130
131         @Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
132         public void pushUserExceptionTest() throws Exception {
133                 PowerMockito.mockStatic(PortalApiProperties.class);
134                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
135                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
136                 EcompUser userJson = new EcompUser();
137                 PowerMockito.mockStatic(JSONUtil.class);
138                 Mockito.when(JSONUtil.convertResponseToJSON(Mockito.anyString())).thenThrow(Exception.class);
139                 onBoardingApiServiceImpl.pushUser(userJson);
140         }
141
142         @Test
143         public void editUserTest() throws Exception {
144                 PowerMockito.mockStatic(PortalApiProperties.class);
145                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
146                 String loginId = "123";
147                 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(new User());
148                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
149                 EcompUser userJson = new EcompUser();
150                 userJson.setOrgUserId(loginId);
151                 onBoardingApiServiceImpl.editUser(loginId, userJson);
152                 Assert.assertTrue(true);
153         }
154
155         @Test(expected = PortalAPIException.class)
156         public void editUserExceptionTest() throws Exception {
157                 PowerMockito.mockStatic(PortalApiProperties.class);
158                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
159                 String loginId = "123";
160                 PowerMockito.mockStatic(JSONUtil.class);
161                 Mockito.when(JSONUtil.convertResponseToJSON(Mockito.anyString())).thenThrow(Exception.class);
162                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
163                 EcompUser userJson = new EcompUser();
164                 userJson.setOrgUserId(loginId);
165                 onBoardingApiServiceImpl.editUser(loginId, userJson);
166         }
167
168         @Test
169         public void getUserTest() throws Exception {
170                 String loginId = "123";
171                 PowerMockito.mockStatic(PortalApiProperties.class);
172                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
173
174                 String responseString = "Response";
175                 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
176                 Mockito.when(userService.userMapper(responseString)).thenReturn(new User());
177
178                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
179                 onBoardingApiServiceImpl.getUser(loginId);
180                 Assert.assertTrue(true);
181         }
182
183         @Test
184         public void getUserAsNullUsserTest() throws Exception {
185                 String loginId = "123";
186                 PowerMockito.mockStatic(PortalApiProperties.class);
187                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
188                 String responseString = "Response";
189                 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + loginId, true, loginId)).thenReturn(responseString);
190                 Mockito.when(userService.userMapper(responseString)).thenReturn(null);
191
192                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
193                 onBoardingApiServiceImpl.getUser(loginId);
194                 Assert.assertTrue(true);
195         }
196
197         @Test
198         public void getUserExceptionTest() throws Exception {
199                 String loginId = "123";
200                 PowerMockito.mockStatic(PortalApiProperties.class);
201                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
202                 String responseString = "Response";
203                 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
204                 Mockito.when(userService.userMapper(responseString)).thenReturn(null);
205
206                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
207                 onBoardingApiServiceImpl.getUser(loginId);
208                 Assert.assertTrue(true);
209         }
210
211         @Test
212         public void getUsersTest() throws Exception {
213                 PowerMockito.mockStatic(PortalApiProperties.class);
214                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
215                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
216
217                 String responseString = "[ {\"firstName\":\"Name\"}]";
218                 Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
219                 List<EcompUser> users = onBoardingApiServiceImpl.getUsers();
220                 Assert.assertNotNull(users);
221         }
222
223         @Test(expected = PortalAPIException.class)
224         public void getUsersExceptionTest() throws Exception {
225                 PowerMockito.mockStatic(PortalApiProperties.class);
226                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
227                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
228
229                 String responseString = " { [ {\"firstName\":\"Name\"} ] }";
230                 Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
231                 onBoardingApiServiceImpl.getUsers();
232         }
233
234         @Test
235         public void getAvailableRolesTest() throws Exception {
236                 String requestedLoginId = "123";
237                 Role role1 = new Role();
238                 role1.setId(123L);
239                 Role role2 = new Role();
240                 role2.setId(124L);
241                 List<Role> roles = new ArrayList<>();
242                 roles.add(role1);
243                 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
244                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
245                 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
246                 Assert.assertNotNull(ecompRoles);
247         }
248
249         @Test(expected = PortalAPIException.class)
250         public void getAvailableRolesExceptionTest() throws Exception {
251                 String requestedLoginId = "123";
252                 Role role1 = new Role();
253                 role1.setId(123L);
254                 Role role2 = new Role();
255                 role2.setId(124L);
256                 List<Role> roles = new ArrayList<>();
257                 roles.add(role1);
258                 roles.add(null);
259                 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
260                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
261                 onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
262         }
263
264         @Test
265         public void pushUserRoleTest() throws Exception {
266                 String loginId = "123";
267                 App app = new App();
268                 app.setId(1l);
269                 List<EcompRole> rolesJson = new ArrayList<>();
270                 EcompRole role1 = new EcompRole();
271                 role1.setId(123L);
272                 rolesJson.add(role1);
273                 Set<UserApp> userApps = new TreeSet<>();
274
275                 UserApp userApp = new UserApp();
276                 Role role = new Role();
277                 role.setId(123L);
278                 userApp.setRole(role);
279                 userApp.setApp(app);
280
281                 UserApp userApp2 = new UserApp();
282                 Role role2 = new Role();
283                 role2.setId(124L);
284                 userApp2.setRole(role2);
285                 userApp2.setApp(app);
286                 userApps.add(userApp);
287                 userApps.add(userApp2);
288                 User user = new User();
289                 user.setUserApps(userApps);
290                 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
291
292                 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
293                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
294                 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
295                 Assert.assertTrue(true);
296         }
297
298         @Test
299         public void pushUserRoleExceptionTest() throws Exception {
300                 String loginId = "123";
301                 App app = new App();
302                 List<EcompRole> rolesJson = new ArrayList<>();
303                 EcompRole role1 = new EcompRole();
304                 role1.setId(123L);
305                 rolesJson.add(role1);
306                 Set<UserApp> userApps = new TreeSet<>();
307
308                 UserApp userApp = new UserApp();
309                 Role role = new Role();
310                 role.setId(123L);
311                 userApp.setApp(app);
312                 userApp.setRole(role);
313                 userApps.add(userApp);
314                 User user = new User();
315                 user.setUserApps(userApps);
316                 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
317
318                 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
319                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
320                 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
321         }
322
323         @Test
324         public void getUserRolesTest() throws Exception {
325                 String loginId = "123";
326                 String responseString = "Response";
327                 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
328                 User user = new User();
329                 SortedSet<Role> currentRoles = new TreeSet<>();
330                 Role role = new Role();
331                 role.setId(123L);
332                 currentRoles.add(role);
333                 user.setRoles(currentRoles);
334                 Mockito.when(userService.userMapper(responseString)).thenReturn(user);
335                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
336                 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getUserRoles(loginId);
337                 Assert.assertNotNull(ecompRoles);
338         }
339
340         @Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
341         public void getUserRolesExceptionTest() throws Exception {
342                 String loginId = "123";
343                 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
344                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
345                 onBoardingApiServiceImpl.getUserRoles(loginId);
346         }
347
348         @Test
349         public void isAppAuthenticatedTest() throws Exception {
350                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
351                 String userName = "UserName";
352                 String password = "Password";
353                 Mockito.when(request.getHeader("username")).thenReturn(userName);
354                 Mockito.when(request.getHeader("password")).thenReturn(password);
355                 
356                 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
357                 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
358                 WebServiceCallService webService = Mockito.mock(WebServiceCallService.class);
359                 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(webService);
360                 Mockito.when(webService.verifyRESTCredential(null, userName, password)).thenReturn(true);
361                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
362                 boolean status = onBoardingApiServiceImpl.isAppAuthenticated(request);
363                 Assert.assertTrue(status);
364         }
365         
366         @Test(expected =PortalAPIException.class)
367         public void isAppAuthenticatedExceptionTest() throws Exception {
368                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
369                 String userName = "UserName";
370                 String password = "Password";
371                 Mockito.when(request.getHeader("username")).thenReturn(userName);
372                 Mockito.when(request.getHeader("password")).thenReturn(password);
373                 
374                 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
375                 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
376                 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(null);
377                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
378                 onBoardingApiServiceImpl.isAppAuthenticated(request);
379         }
380         
381         @Test
382         public void getSessionTimeOutsTEst() throws Exception {
383                 String session ="Session";
384                 PowerMockito.mockStatic(PortalTimeoutHandler.class);
385                 Mockito.when(PortalTimeoutHandler.gatherSessionExtensions()).thenReturn(session);
386                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
387                 String response = onBoardingApiServiceImpl.getSessionTimeOuts();
388                 Assert.assertEquals(response, session);
389         }
390
391         @Test
392         public void updateSessionTimeOutsTest() throws Exception {
393                 String sessionMap ="Session";
394                 PowerMockito.mockStatic(PortalTimeoutHandler.class);
395                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
396                 onBoardingApiServiceImpl.updateSessionTimeOuts(sessionMap);
397                 Assert.assertTrue(true);
398         }
399
400         @Test
401         public void getUserId() throws PortalAPIException {
402                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
403                 String userId = "123";
404                 Mockito.when(loginStrategy.getUserId(request)).thenReturn(userId);
405                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
406                 String response = onBoardingApiServiceImpl.getUserId(request);
407                 Assert.assertEquals(response, userId);
408         }
409         
410         @Test
411         public void getAppCredentialsTest() throws Exception{
412                 App app =new App();
413                 app.setName("App");
414                 app.setUsername("User");
415                 app.setAppPassword("Password");
416                 
417                 String key = "Key";
418                 
419                 PowerMockito.mockStatic(SystemProperties.class);
420                 PowerMockito.mockStatic(CipherUtil.class);
421                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
422                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenReturn(app.getAppPassword());
423                 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
424                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
425                 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
426                 Assert.assertNotNull(credentialsMap);
427         }
428         
429         @Test
430         public void getAppCredentialsAppNullTest() throws Exception{
431                 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(null);
432                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
433                 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
434                 Assert.assertNotNull(credentialsMap);
435         }
436         
437         @Test
438         public void getAppCredentialsExceptionTest() throws Exception{
439                 App app =new App();
440                 app.setName("App");
441                 app.setUsername("User");
442                 app.setAppPassword("Password");
443                 
444                 String key = "Key";
445                 
446                 PowerMockito.mockStatic(SystemProperties.class);
447                 PowerMockito.mockStatic(CipherUtil.class);
448                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
449                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenThrow(CipherUtilException.class);
450                 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
451                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
452                 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
453                 Assert.assertNotNull(credentialsMap);
454         }
455 }