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