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