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.HashMap;
43 import java.util.List;
46 import java.util.SortedSet;
47 import java.util.TreeSet;
49 import javax.servlet.http.HttpServletRequest;
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;
84 @RunWith(PowerMockRunner.class)
85 @PrepareForTest({ AppContextManager.class, PortalApiProperties.class, JSONUtil.class, PortalTimeoutHandler.class, SystemProperties.class, CipherUtil.class })
86 public class OnBoardingApiServiceImplTest {
89 private RoleService roleService;
91 private UserProfileService userProfileService;
93 private IAdminAuthExtension adminAuthExtensionServiceImpl;
96 private LoginStrategy loginStrategy;
98 private UserService userService;
100 private RestApiRequestBuilder restApiRequestBuilder;
102 private AppService appServiceImpl;
105 public void setup() {
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);
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);
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);
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);
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);
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");
175 String responseString = "Response";
176 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + loginId, true, loginId)).thenReturn(responseString);
177 Mockito.when(userService.userMapper(responseString)).thenReturn(new User());
179 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
180 onBoardingApiServiceImpl.getUser(loginId);
181 Assert.assertTrue(true);
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);
193 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
194 onBoardingApiServiceImpl.getUser(loginId);
195 Assert.assertTrue(true);
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);
207 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
208 onBoardingApiServiceImpl.getUser(loginId);
209 Assert.assertTrue(true);
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();
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);
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();
230 String responseString = " { [ {\"firstName\":\"Name\"} ] }";
231 Mockito.when(restApiRequestBuilder.getViaREST("/users", true, null)).thenReturn(responseString);
232 onBoardingApiServiceImpl.getUsers();
236 public void getAvailableRolesTest() throws Exception {
237 String requestedLoginId = "123";
238 Role role1 = new Role();
240 Role role2 = new Role();
242 List<Role> roles = new ArrayList<>();
244 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
245 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
246 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
247 Assert.assertNotNull(ecompRoles);
250 @Test(expected = PortalAPIException.class)
251 public void getAvailableRolesExceptionTest() throws Exception {
252 String requestedLoginId = "123";
253 Role role1 = new Role();
255 Role role2 = new Role();
257 List<Role> roles = new ArrayList<>();
260 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
261 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
262 onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
266 public void pushUserRoleTest() throws Exception {
267 String loginId = "123";
268 List<EcompRole> rolesJson = new ArrayList<>();
269 EcompRole role1 = new EcompRole();
271 rolesJson.add(role1);
272 Set<UserApp> userApps = new TreeSet<>();
274 UserApp userApp = new UserApp();
275 Role role = new Role();
277 userApp.setRole(role);
279 UserApp userApp2 = new UserApp();
280 Role role2 = new Role();
282 userApp2.setRole(role2);
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);
290 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
291 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
292 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
293 Assert.assertTrue(true);
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();
302 rolesJson.add(role1);
303 Set<UserApp> userApps = new TreeSet<>();
305 UserApp userApp = new UserApp();
306 Role role = new Role();
309 userApps.add(userApp);
310 User user = new User();
311 user.setUserApps(userApps);
312 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
314 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
315 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
316 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
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();
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);
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);
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);
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);
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);
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);
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);
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);
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);
407 public void getAppCredentialsTest() throws Exception{
410 app.setUsername("User");
411 app.setAppPassword("Password");
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);
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);
434 public void getAppCredentialsExceptionTest() throws Exception{
437 app.setUsername("User");
438 app.setAppPassword("Password");
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);