2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.service;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
45 import java.util.SortedSet;
46 import java.util.TreeSet;
48 import javax.servlet.http.HttpServletRequest;
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;
83 @RunWith(PowerMockRunner.class)
84 @PrepareForTest({ AppContextManager.class, PortalApiProperties.class, JSONUtil.class, PortalTimeoutHandler.class, SystemProperties.class, CipherUtil.class })
85 public class OnBoardingApiServiceImplTest {
88 private RoleService roleService;
90 private UserProfileService userProfileService;
92 private IAdminAuthExtension adminAuthExtensionServiceImpl;
95 private LoginStrategy loginStrategy;
97 private UserService userService;
99 private RestApiRequestBuilder restApiRequestBuilder;
101 private AppService appServiceImpl;
104 public void setup() {
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);
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);
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);
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);
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);
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");
174 String responseString = "Response";
175 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
176 Mockito.when(userService.userMapper(responseString)).thenReturn(new User());
178 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
179 onBoardingApiServiceImpl.getUser(loginId);
180 Assert.assertTrue(true);
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);
192 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
193 onBoardingApiServiceImpl.getUser(loginId);
194 Assert.assertTrue(true);
198 public void getUserExceptionTest() throws Exception {
199 String loginId = "123";
200 PowerMockito.mockStatic(PortalApiProperties.class);
201 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
202 String responseString = "Response";
203 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
204 Mockito.when(userService.userMapper(responseString)).thenReturn(null);
206 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
207 onBoardingApiServiceImpl.getUser(loginId);
208 Assert.assertTrue(true);
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();
217 String responseString = "[ {\"firstName\":\"Name\"}]";
218 Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
219 List<EcompUser> users = onBoardingApiServiceImpl.getUsers();
220 Assert.assertNotNull(users);
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();
229 String responseString = " { [ {\"firstName\":\"Name\"} ] }";
230 Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
231 onBoardingApiServiceImpl.getUsers();
235 public void getAvailableRolesTest() throws Exception {
236 String requestedLoginId = "123";
237 Role role1 = new Role();
239 Role role2 = new Role();
241 List<Role> roles = new ArrayList<>();
243 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
244 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
245 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
246 Assert.assertNotNull(ecompRoles);
249 @Test(expected = PortalAPIException.class)
250 public void getAvailableRolesExceptionTest() throws Exception {
251 String requestedLoginId = "123";
252 Role role1 = new Role();
254 Role role2 = new Role();
256 List<Role> roles = new ArrayList<>();
259 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
260 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
261 onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
265 public void pushUserRoleTest() throws Exception {
266 String loginId = "123";
269 List<EcompRole> rolesJson = new ArrayList<>();
270 EcompRole role1 = new EcompRole();
272 rolesJson.add(role1);
273 Set<UserApp> userApps = new TreeSet<>();
275 UserApp userApp = new UserApp();
276 Role role = new Role();
278 userApp.setRole(role);
281 UserApp userApp2 = new UserApp();
282 Role role2 = new Role();
284 userApp2.setRole(role2);
285 userApp2.setApp(app);
286 userApps.add(userApp);
287 userApps.add(userApp2);
288 User user = new User();
289 user.setUserApps(userApps);
290 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
292 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
293 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
294 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
295 Assert.assertTrue(true);
299 public void pushUserRoleExceptionTest() throws Exception {
300 String loginId = "123";
302 List<EcompRole> rolesJson = new ArrayList<>();
303 EcompRole role1 = new EcompRole();
305 rolesJson.add(role1);
306 Set<UserApp> userApps = new TreeSet<>();
308 UserApp userApp = new UserApp();
309 Role role = new Role();
312 userApp.setRole(role);
313 userApps.add(userApp);
314 User user = new User();
315 user.setUserApps(userApps);
316 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
318 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
319 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
320 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
324 public void getUserRolesTest() throws Exception {
325 String loginId = "123";
326 String responseString = "Response";
327 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
328 User user = new User();
329 SortedSet<Role> currentRoles = new TreeSet<>();
330 Role role = new Role();
332 currentRoles.add(role);
333 user.setRoles(currentRoles);
334 Mockito.when(userService.userMapper(responseString)).thenReturn(user);
335 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
336 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getUserRoles(loginId);
337 Assert.assertNotNull(ecompRoles);
340 @Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
341 public void getUserRolesExceptionTest() throws Exception {
342 String loginId = "123";
343 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
344 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
345 onBoardingApiServiceImpl.getUserRoles(loginId);
349 public void isAppAuthenticatedTest() throws Exception {
350 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
351 String userName = "UserName";
352 String password = "Password";
353 Mockito.when(request.getHeader("username")).thenReturn(userName);
354 Mockito.when(request.getHeader("password")).thenReturn(password);
356 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
357 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
358 WebServiceCallService webService = Mockito.mock(WebServiceCallService.class);
359 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(webService);
360 Mockito.when(webService.verifyRESTCredential(null, userName, password)).thenReturn(true);
361 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
362 boolean status = onBoardingApiServiceImpl.isAppAuthenticated(request);
363 Assert.assertTrue(status);
366 @Test(expected =PortalAPIException.class)
367 public void isAppAuthenticatedExceptionTest() throws Exception {
368 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
369 String userName = "UserName";
370 String password = "Password";
371 Mockito.when(request.getHeader("username")).thenReturn(userName);
372 Mockito.when(request.getHeader("password")).thenReturn(password);
374 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
375 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
376 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(null);
377 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
378 onBoardingApiServiceImpl.isAppAuthenticated(request);
382 public void getSessionTimeOutsTEst() throws Exception {
383 String session ="Session";
384 PowerMockito.mockStatic(PortalTimeoutHandler.class);
385 Mockito.when(PortalTimeoutHandler.gatherSessionExtensions()).thenReturn(session);
386 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
387 String response = onBoardingApiServiceImpl.getSessionTimeOuts();
388 Assert.assertEquals(response, session);
392 public void updateSessionTimeOutsTest() throws Exception {
393 String sessionMap ="Session";
394 PowerMockito.mockStatic(PortalTimeoutHandler.class);
395 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
396 onBoardingApiServiceImpl.updateSessionTimeOuts(sessionMap);
397 Assert.assertTrue(true);
401 public void getUserId() throws PortalAPIException {
402 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
403 String userId = "123";
404 Mockito.when(loginStrategy.getUserId(request)).thenReturn(userId);
405 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
406 String response = onBoardingApiServiceImpl.getUserId(request);
407 Assert.assertEquals(response, userId);
411 public void getAppCredentialsTest() throws Exception{
414 app.setUsername("User");
415 app.setAppPassword("Password");
419 PowerMockito.mockStatic(SystemProperties.class);
420 PowerMockito.mockStatic(CipherUtil.class);
421 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
422 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenReturn(app.getAppPassword());
423 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
424 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
425 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
426 Assert.assertNotNull(credentialsMap);
430 public void getAppCredentialsAppNullTest() throws Exception{
431 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(null);
432 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
433 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
434 Assert.assertNotNull(credentialsMap);
438 public void getAppCredentialsExceptionTest() throws Exception{
441 app.setUsername("User");
442 app.setAppPassword("Password");
446 PowerMockito.mockStatic(SystemProperties.class);
447 PowerMockito.mockStatic(CipherUtil.class);
448 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
449 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenThrow(CipherUtilException.class);
450 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
451 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
452 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
453 Assert.assertNotNull(credentialsMap);