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.Ignore;
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);
124 public void pushUserTest() throws PortalAPIException {
125 PowerMockito.mockStatic(PortalApiProperties.class);
126 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
127 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
128 EcompUser userJson = new EcompUser();
129 onBoardingApiServiceImpl.pushUser(userJson);
130 Assert.assertTrue(true);
133 @Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
134 public void pushUserExceptionTest() throws Exception {
135 PowerMockito.mockStatic(PortalApiProperties.class);
136 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
137 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
138 EcompUser userJson = new EcompUser();
139 PowerMockito.mockStatic(JSONUtil.class);
140 Mockito.when(JSONUtil.convertResponseToJSON(Mockito.anyString())).thenThrow(Exception.class);
141 onBoardingApiServiceImpl.pushUser(userJson);
146 public void editUserTest() throws Exception {
147 PowerMockito.mockStatic(PortalApiProperties.class);
148 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
149 String loginId = "123";
150 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(new User());
151 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
152 EcompUser userJson = new EcompUser();
153 userJson.setOrgUserId(loginId);
154 onBoardingApiServiceImpl.editUser(loginId, userJson);
155 Assert.assertTrue(true);
158 @Test(expected = PortalAPIException.class)
159 public void editUserExceptionTest() throws Exception {
160 PowerMockito.mockStatic(PortalApiProperties.class);
161 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
162 String loginId = "123";
163 PowerMockito.mockStatic(JSONUtil.class);
164 Mockito.when(JSONUtil.convertResponseToJSON(Mockito.anyString())).thenThrow(Exception.class);
165 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
166 EcompUser userJson = new EcompUser();
167 userJson.setOrgUserId(loginId);
168 onBoardingApiServiceImpl.editUser(loginId, userJson);
172 public void getUserTest() throws Exception {
173 String loginId = "123";
174 PowerMockito.mockStatic(PortalApiProperties.class);
175 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
177 String responseString = "Response";
178 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
179 Mockito.when(userService.userMapper(responseString)).thenReturn(new User());
181 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
182 onBoardingApiServiceImpl.getUser(loginId);
183 Assert.assertTrue(true);
187 public void getUserAsNullUsserTest() throws Exception {
188 String loginId = "123";
189 PowerMockito.mockStatic(PortalApiProperties.class);
190 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
191 String responseString = "Response";
192 Mockito.when(restApiRequestBuilder.getViaREST("/user/" + loginId, true, loginId)).thenReturn(responseString);
193 Mockito.when(userService.userMapper(responseString)).thenReturn(null);
195 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
196 onBoardingApiServiceImpl.getUser(loginId);
197 Assert.assertTrue(true);
201 public void getUserExceptionTest() throws Exception {
202 String loginId = "123";
203 PowerMockito.mockStatic(PortalApiProperties.class);
204 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
205 String responseString = "Response";
206 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
207 Mockito.when(userService.userMapper(responseString)).thenReturn(null);
209 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
210 onBoardingApiServiceImpl.getUser(loginId);
211 Assert.assertTrue(true);
215 public void getUsersTest() throws Exception {
216 PowerMockito.mockStatic(PortalApiProperties.class);
217 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
218 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
220 String responseString = "[ {\"firstName\":\"Name\"}]";
221 Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
222 List<EcompUser> users = onBoardingApiServiceImpl.getUsers();
223 Assert.assertNotNull(users);
226 @Test(expected = PortalAPIException.class)
227 public void getUsersExceptionTest() throws Exception {
228 PowerMockito.mockStatic(PortalApiProperties.class);
229 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
230 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
232 String responseString = " { [ {\"firstName\":\"Name\"} ] }";
233 Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
234 onBoardingApiServiceImpl.getUsers();
238 public void getAvailableRolesTest() throws Exception {
239 String requestedLoginId = "123";
240 Role role1 = new Role();
242 Role role2 = new Role();
244 List<Role> roles = new ArrayList<>();
246 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
247 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
248 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
249 Assert.assertNotNull(ecompRoles);
252 @Test(expected = PortalAPIException.class)
253 public void getAvailableRolesExceptionTest() throws Exception {
254 String requestedLoginId = "123";
255 Role role1 = new Role();
257 Role role2 = new Role();
259 List<Role> roles = new ArrayList<>();
262 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
263 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
264 onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
268 public void pushUserRoleTest() throws Exception {
269 String loginId = "123";
272 List<EcompRole> rolesJson = new ArrayList<>();
273 EcompRole role1 = new EcompRole();
275 rolesJson.add(role1);
276 Set<UserApp> userApps = new TreeSet<>();
278 UserApp userApp = new UserApp();
279 Role role = new Role();
281 userApp.setRole(role);
284 UserApp userApp2 = new UserApp();
285 Role role2 = new Role();
287 userApp2.setRole(role2);
288 userApp2.setApp(app);
289 userApps.add(userApp);
290 userApps.add(userApp2);
291 User user = new User();
292 user.setUserApps(userApps);
293 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
295 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
296 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
297 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
298 Assert.assertTrue(true);
302 public void pushUserRoleExceptionTest() throws Exception {
303 String loginId = "123";
305 List<EcompRole> rolesJson = new ArrayList<>();
306 EcompRole role1 = new EcompRole();
308 rolesJson.add(role1);
309 Set<UserApp> userApps = new TreeSet<>();
311 UserApp userApp = new UserApp();
312 Role role = new Role();
315 userApp.setRole(role);
316 userApps.add(userApp);
317 User user = new User();
318 user.setUserApps(userApps);
319 Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);
321 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
322 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
323 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
327 public void getUserRolesTest() throws Exception {
328 String loginId = "123";
329 String responseString = "Response";
330 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
331 User user = new User();
332 SortedSet<Role> currentRoles = new TreeSet<>();
333 Role role = new Role();
335 currentRoles.add(role);
336 user.setRoles(currentRoles);
337 Mockito.when(userService.userMapper(responseString)).thenReturn(user);
338 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
339 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getUserRoles(loginId);
340 Assert.assertNotNull(ecompRoles);
343 @Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
344 public void getUserRolesExceptionTest() throws Exception {
345 String loginId = "123";
346 Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
347 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
348 onBoardingApiServiceImpl.getUserRoles(loginId);
352 public void isAppAuthenticatedTest() throws Exception {
353 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
354 String userName = "UserName";
355 String password = "Password";
356 Mockito.when(request.getHeader("username")).thenReturn(userName);
357 Mockito.when(request.getHeader("password")).thenReturn(password);
359 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
360 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
361 WebServiceCallService webService = Mockito.mock(WebServiceCallService.class);
362 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(webService);
363 Mockito.when(webService.verifyRESTCredential(null, userName, password)).thenReturn(true);
364 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
365 boolean status = onBoardingApiServiceImpl.isAppAuthenticated(request);
366 Assert.assertTrue(status);
369 @Test(expected =PortalAPIException.class)
370 public void isAppAuthenticatedExceptionTest() throws Exception {
371 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
372 String userName = "UserName";
373 String password = "Password";
374 Mockito.when(request.getHeader("username")).thenReturn(userName);
375 Mockito.when(request.getHeader("password")).thenReturn(password);
377 ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
378 Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
379 Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(null);
380 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
381 onBoardingApiServiceImpl.isAppAuthenticated(request);
385 public void getSessionTimeOutsTEst() throws Exception {
386 String session ="Session";
387 PowerMockito.mockStatic(PortalTimeoutHandler.class);
388 Mockito.when(PortalTimeoutHandler.gatherSessionExtensions()).thenReturn(session);
389 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
390 String response = onBoardingApiServiceImpl.getSessionTimeOuts();
391 Assert.assertEquals(response, session);
395 public void updateSessionTimeOutsTest() throws Exception {
396 String sessionMap ="Session";
397 PowerMockito.mockStatic(PortalTimeoutHandler.class);
398 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
399 onBoardingApiServiceImpl.updateSessionTimeOuts(sessionMap);
400 Assert.assertTrue(true);
404 public void getUserId() throws PortalAPIException {
405 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
406 String userId = "123";
407 Mockito.when(loginStrategy.getUserId(request)).thenReturn(userId);
408 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
409 String response = onBoardingApiServiceImpl.getUserId(request);
410 Assert.assertEquals(response, userId);
414 public void getAppCredentialsTest() throws Exception{
417 app.setUsername("User");
418 app.setAppPassword("Password");
422 PowerMockito.mockStatic(SystemProperties.class);
423 PowerMockito.mockStatic(CipherUtil.class);
424 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
425 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenReturn(app.getAppPassword());
426 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
427 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
428 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
429 Assert.assertNotNull(credentialsMap);
433 public void getAppCredentialsAppNullTest() throws Exception{
434 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(null);
435 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
436 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
437 Assert.assertNotNull(credentialsMap);
441 public void getAppCredentialsExceptionTest() throws Exception{
444 app.setUsername("User");
445 app.setAppPassword("Password");
449 PowerMockito.mockStatic(SystemProperties.class);
450 PowerMockito.mockStatic(CipherUtil.class);
451 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
452 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenThrow(CipherUtilException.class);
453 Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
454 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
455 Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
456 Assert.assertNotNull(credentialsMap);