2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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("/v1/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("/v1/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("/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("/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";
267 List<EcompRole> rolesJson = new ArrayList<>();
268 EcompRole role1 = new EcompRole();
270 rolesJson.add(role1);
271 Set<UserApp> userApps = new TreeSet<>();
273 UserApp userApp = new UserApp();
274 Role role = new Role();
276 userApp.setRole(role);
278 UserApp userApp2 = new UserApp();
279 Role role2 = new Role();
281 userApp2.setRole(role2);
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);
289 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
290 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
291 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
292 Assert.assertTrue(true);
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();
301 rolesJson.add(role1);
302 Set<UserApp> userApps = new TreeSet<>();
304 UserApp userApp = new UserApp();
305 Role role = new Role();
308 userApps.add(userApp);
309 User user = new User();
310 user.setUserApps(userApps);
311 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
313 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
314 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
315 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
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();
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);
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);
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);
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);
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);
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);
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);
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);
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);
406 public void getAppCredentialsTest() throws Exception{
409 app.setUsername("User");
410 app.setAppPassword("Password");
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);
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);
433 public void getAppCredentialsExceptionTest() throws Exception{
436 app.setUsername("User");
437 app.setAppPassword("Password");
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);