857a25ebb9265726c4ad3cbeae483ed9eccec4f8
[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  * 
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("/v1/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("/v1/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("/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("/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                 List<EcompRole> rolesJson = new ArrayList<>();
268                 EcompRole role1 = new EcompRole();
269                 role1.setId(123L);
270                 rolesJson.add(role1);
271                 Set<UserApp> userApps = new TreeSet<>();
272
273                 UserApp userApp = new UserApp();
274                 Role role = new Role();
275                 role.setId(123L);
276                 userApp.setRole(role);
277
278                 UserApp userApp2 = new UserApp();
279                 Role role2 = new Role();
280                 role2.setId(124L);
281                 userApp2.setRole(role2);
282
283                 userApps.add(userApp);
284                 userApps.add(userApp2);
285                 User user = new User();
286                 user.setUserApps(userApps);
287                 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
288
289                 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
290                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
291                 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
292                 Assert.assertTrue(true);
293         }
294
295         @Test(expected = PortalAPIException.class)
296         public void pushUserRoleExceptionTest() throws Exception {
297                 String loginId = "123";
298                 List<EcompRole> rolesJson = new ArrayList<>();
299                 EcompRole role1 = new EcompRole();
300                 role1.setId(123L);
301                 rolesJson.add(role1);
302                 Set<UserApp> userApps = new TreeSet<>();
303
304                 UserApp userApp = new UserApp();
305                 Role role = new Role();
306                 role.setId(123L);
307
308                 userApps.add(userApp);
309                 User user = new User();
310                 user.setUserApps(userApps);
311                 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
312
313                 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
314                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
315                 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
316         }
317
318         @Test
319         public void getUserRolesTest() throws Exception {
320                 String loginId = "123";
321                 String responseString = "Response";
322                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/user/" + loginId, true, loginId)).thenReturn(responseString);
323                 User user = new User();
324                 SortedSet<Role> currentRoles = new TreeSet<>();
325                 Role role = new Role();
326                 role.setId(123L);
327                 currentRoles.add(role);
328                 user.setRoles(currentRoles);
329                 Mockito.when(userService.userMapper(responseString)).thenReturn(user);
330                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
331                 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getUserRoles(loginId);
332                 Assert.assertNotNull(ecompRoles);
333         }
334
335         @Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
336         public void getUserRolesExceptionTest() throws Exception {
337                 String loginId = "123";
338                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/user/" + loginId, true, loginId)).thenThrow(IOException.class);
339                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
340                 onBoardingApiServiceImpl.getUserRoles(loginId);
341         }
342
343         @Test
344         public void isAppAuthenticatedTest() throws Exception {
345                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
346                 String userName = "UserName";
347                 String password = "Password";
348                 Mockito.when(request.getHeader("username")).thenReturn(userName);
349                 Mockito.when(request.getHeader("password")).thenReturn(password);
350                 
351                 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
352                 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
353                 WebServiceCallService webService = Mockito.mock(WebServiceCallService.class);
354                 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(webService);
355                 Mockito.when(webService.verifyRESTCredential(null, userName, password)).thenReturn(true);
356                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
357                 boolean status = onBoardingApiServiceImpl.isAppAuthenticated(request);
358                 Assert.assertTrue(status);
359         }
360         
361         @Test(expected =PortalAPIException.class)
362         public void isAppAuthenticatedExceptionTest() throws Exception {
363                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
364                 String userName = "UserName";
365                 String password = "Password";
366                 Mockito.when(request.getHeader("username")).thenReturn(userName);
367                 Mockito.when(request.getHeader("password")).thenReturn(password);
368                 
369                 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
370                 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
371                 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(null);
372                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
373                 onBoardingApiServiceImpl.isAppAuthenticated(request);
374         }
375         
376         @Test
377         public void getSessionTimeOutsTEst() throws Exception {
378                 String session ="Session";
379                 PowerMockito.mockStatic(PortalTimeoutHandler.class);
380                 Mockito.when(PortalTimeoutHandler.gatherSessionExtensions()).thenReturn(session);
381                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
382                 String response = onBoardingApiServiceImpl.getSessionTimeOuts();
383                 Assert.assertEquals(response, session);
384         }
385
386         @Test
387         public void updateSessionTimeOutsTest() throws Exception {
388                 String sessionMap ="Session";
389                 PowerMockito.mockStatic(PortalTimeoutHandler.class);
390                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
391                 onBoardingApiServiceImpl.updateSessionTimeOuts(sessionMap);
392                 Assert.assertTrue(true);
393         }
394
395         @Test
396         public void getUserId() throws PortalAPIException {
397                 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
398                 String userId = "123";
399                 Mockito.when(loginStrategy.getUserId(request)).thenReturn(userId);
400                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
401                 String response = onBoardingApiServiceImpl.getUserId(request);
402                 Assert.assertEquals(response, userId);
403         }
404         
405         @Test
406         public void getAppCredentialsTest() throws Exception{
407                 App app =new App();
408                 app.setName("App");
409                 app.setUsername("User");
410                 app.setAppPassword("Password");
411                 
412                 String key = "Key";
413                 
414                 PowerMockito.mockStatic(SystemProperties.class);
415                 PowerMockito.mockStatic(CipherUtil.class);
416                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
417                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenReturn(app.getAppPassword());
418                 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
419                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
420                 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
421                 Assert.assertNotNull(credentialsMap);
422         }
423         
424         @Test
425         public void getAppCredentialsAppNullTest() throws Exception{
426                 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(null);
427                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
428                 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
429                 Assert.assertNotNull(credentialsMap);
430         }
431         
432         @Test
433         public void getAppCredentialsExceptionTest() throws Exception{
434                 App app =new App();
435                 app.setName("App");
436                 app.setUsername("User");
437                 app.setAppPassword("Password");
438                 
439                 String key = "Key";
440                 
441                 PowerMockito.mockStatic(SystemProperties.class);
442                 PowerMockito.mockStatic(CipherUtil.class);
443                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
444                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenThrow(CipherUtilException.class);
445                 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
446                 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
447                 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
448                 Assert.assertNotNull(credentialsMap);
449         }
450 }