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.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.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;
85 @RunWith(PowerMockRunner.class)
86 @PrepareForTest({ AppContextManager.class, PortalApiProperties.class, JSONUtil.class, PortalTimeoutHandler.class, SystemProperties.class, CipherUtil.class })
87 public class OnBoardingApiServiceImplTest {
90 private RoleService roleService;
92 private UserProfileService userProfileService;
94 private IAdminAuthExtension adminAuthExtensionServiceImpl;
97 private LoginStrategy loginStrategy;
99 private UserService userService;
101 private RestApiRequestBuilder restApiRequestBuilder;
103 private AppService appServiceImpl;
106 public void setup() {
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);
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);
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);
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);
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);
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");
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());
182 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
183 onBoardingApiServiceImpl.getUser(loginId);
184 Assert.assertTrue(true);
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);
196 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
197 onBoardingApiServiceImpl.getUser(loginId);
198 Assert.assertTrue(true);
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);
210 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
211 onBoardingApiServiceImpl.getUser(loginId);
212 Assert.assertTrue(true);
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();
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);
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();
233 // String responseString = " { [ {\"firstName\":\"Name\"} ] }";
234 // Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
235 // onBoardingApiServiceImpl.getUsers();
239 public void getAvailableRolesTest() throws Exception {
240 String requestedLoginId = "123";
241 Role role1 = new Role();
243 Role role2 = new Role();
245 List<Role> roles = new ArrayList<>();
247 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
248 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
249 List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
250 Assert.assertNotNull(ecompRoles);
253 @Test(expected = PortalAPIException.class)
254 public void getAvailableRolesExceptionTest() throws Exception {
255 String requestedLoginId = "123";
256 Role role1 = new Role();
258 Role role2 = new Role();
260 List<Role> roles = new ArrayList<>();
263 Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
264 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
265 onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
269 public void pushUserRoleTest() throws Exception {
270 String loginId = "123";
273 List<EcompRole> rolesJson = new ArrayList<>();
274 EcompRole role1 = new EcompRole();
276 rolesJson.add(role1);
277 Set<UserApp> userApps = new TreeSet<>();
279 UserApp userApp = new UserApp();
280 Role role = new Role();
282 userApp.setRole(role);
285 UserApp userApp2 = new UserApp();
286 Role role2 = new Role();
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);
296 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
297 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
298 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
299 Assert.assertTrue(true);
303 public void pushUserRoleExceptionTest() throws Exception {
304 String loginId = "123";
306 List<EcompRole> rolesJson = new ArrayList<>();
307 EcompRole role1 = new EcompRole();
309 rolesJson.add(role1);
310 Set<UserApp> userApps = new TreeSet<>();
312 UserApp userApp = new UserApp();
313 Role role = new Role();
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);
322 Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
323 OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
324 onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
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();
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);
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);
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);
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);
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);
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);
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);
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);
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);
419 public void getAppCredentialsTest() throws Exception{
422 app.setUsername("User");
423 app.setAppPassword("Password");
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);
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);
446 public void getAppCredentialsExceptionTest() throws Exception{
449 app.setUsername("User");
450 app.setAppPassword("Password");
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);