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