summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/service/OnBoardingApiServiceImplTest.java
blob: a10572a25025191a5e35b197658fc4954e3b7434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * ============LICENSE_START==========================================
 * ONAP Portal SDK
 * ===================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
package org.onap.portalapp.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.onap.portalsdk.core.auth.LoginStrategy;
import org.onap.portalsdk.core.domain.App;
import org.onap.portalsdk.core.domain.Role;
import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.domain.UserApp;
import org.onap.portalsdk.core.onboarding.client.AppContextManager;
import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
import org.onap.portalsdk.core.onboarding.listener.PortalTimeoutHandler;
import org.onap.portalsdk.core.onboarding.util.CipherUtil;
import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
import org.onap.portalsdk.core.restful.domain.EcompRole;
import org.onap.portalsdk.core.restful.domain.EcompUser;
import org.onap.portalsdk.core.service.AppService;
import org.onap.portalsdk.core.service.RestApiRequestBuilder;
import org.onap.portalsdk.core.service.RoleService;
import org.onap.portalsdk.core.service.UserProfileService;
import org.onap.portalsdk.core.service.UserService;
import org.onap.portalsdk.core.service.WebServiceCallService;
import org.onap.portalsdk.core.util.JSONUtil;
import org.onap.portalsdk.core.util.SystemProperties;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.ApplicationContext;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ AppContextManager.class, PortalApiProperties.class, JSONUtil.class, PortalTimeoutHandler.class, SystemProperties.class, CipherUtil.class })
public class OnBoardingApiServiceImplTest {

	@Mock
	private RoleService roleService;
	@Mock
	private UserProfileService userProfileService;
	@Mock
	private IAdminAuthExtension adminAuthExtensionServiceImpl;

	@Mock
	private LoginStrategy loginStrategy;
	@Mock
	private UserService userService;
	@Mock
	private RestApiRequestBuilder restApiRequestBuilder;
	@Mock
	private AppService appServiceImpl;

	@Before
	public void setup() {

		PowerMockito.mockStatic(AppContextManager.class);
		ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
		Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
		Mockito.when(appContext.getBean(RoleService.class)).thenReturn(roleService);
		Mockito.when(appContext.getBean(UserProfileService.class)).thenReturn(userProfileService);
		Mockito.when(appContext.getBean(LoginStrategy.class)).thenReturn(loginStrategy);
		Mockito.when(appContext.getBean(IAdminAuthExtension.class)).thenReturn(adminAuthExtensionServiceImpl);
		Mockito.when(appContext.getBean(UserService.class)).thenReturn(userService);
		Mockito.when(appContext.getBean(RestApiRequestBuilder.class)).thenReturn(restApiRequestBuilder);
		Mockito.when(appContext.getBean(AppService.class)).thenReturn(appServiceImpl);

	}

	@Ignore
	@Test
	public void pushUserTest() throws PortalAPIException {
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		EcompUser userJson = new EcompUser();
		onBoardingApiServiceImpl.pushUser(userJson);
		Assert.assertTrue(true);
	}

	@Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
	public void pushUserExceptionTest() throws Exception {
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		EcompUser userJson = new EcompUser();
		PowerMockito.mockStatic(JSONUtil.class);
		Mockito.when(JSONUtil.convertResponseToJSON(Mockito.anyString())).thenThrow(Exception.class);
		onBoardingApiServiceImpl.pushUser(userJson);
	}

	@Ignore
	@Test
	public void editUserTest() throws Exception {
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
		String loginId = "123";
		Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(new User());
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		EcompUser userJson = new EcompUser();
		userJson.setOrgUserId(loginId);
		onBoardingApiServiceImpl.editUser(loginId, userJson);
		Assert.assertTrue(true);
	}

	@Test(expected = PortalAPIException.class)
	public void editUserExceptionTest() throws Exception {
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");
		String loginId = "123";
		PowerMockito.mockStatic(JSONUtil.class);
		Mockito.when(JSONUtil.convertResponseToJSON(Mockito.anyString())).thenThrow(Exception.class);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		EcompUser userJson = new EcompUser();
		userJson.setOrgUserId(loginId);
		onBoardingApiServiceImpl.editUser(loginId, userJson);
	}

	@Test
	public void getUserTest() throws Exception {
		String loginId = "123";
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("remote");

		String responseString = "Response";
		Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
		Mockito.when(userService.userMapper(responseString)).thenReturn(new User());

		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.getUser(loginId);
		Assert.assertTrue(true);
	}

	@Test
	public void getUserAsNullUsserTest() throws Exception {
		String loginId = "123";
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
		String responseString = "Response";
		Mockito.when(restApiRequestBuilder.getViaREST("/user/" + loginId, true, loginId)).thenReturn(responseString);
		Mockito.when(userService.userMapper(responseString)).thenReturn(null);

		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.getUser(loginId);
		Assert.assertTrue(true);
	}

	@Test
	public void getUserExceptionTest() throws Exception {
		String loginId = "123";
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
		String responseString = "Response";
		Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
		Mockito.when(userService.userMapper(responseString)).thenReturn(null);

		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.getUser(loginId);
		Assert.assertTrue(true);
	}

	@Test
	public void getUsersTest() throws Exception {
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();

		String responseString = "[ {\"firstName\":\"Name\"}]";
		Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
		List<EcompUser> users = onBoardingApiServiceImpl.getUsers();
		Assert.assertNotNull(users);
	}

	@Test(expected = PortalAPIException.class)
	public void getUsersExceptionTest() throws Exception {
		PowerMockito.mockStatic(PortalApiProperties.class);
		Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED)).thenReturn("local");
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();

		String responseString = " { [ {\"firstName\":\"Name\"} ] }";
		Mockito.when(restApiRequestBuilder.getViaREST("/v3/users", true, null)).thenReturn(responseString);
		onBoardingApiServiceImpl.getUsers();
	}

	@Test
	public void getAvailableRolesTest() throws Exception {
		String requestedLoginId = "123";
		Role role1 = new Role();
		role1.setId(123L);
		Role role2 = new Role();
		role2.setId(124L);
		List<Role> roles = new ArrayList<>();
		roles.add(role1);
		Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
		Assert.assertNotNull(ecompRoles);
	}

	@Test(expected = PortalAPIException.class)
	public void getAvailableRolesExceptionTest() throws Exception {
		String requestedLoginId = "123";
		Role role1 = new Role();
		role1.setId(123L);
		Role role2 = new Role();
		role2.setId(124L);
		List<Role> roles = new ArrayList<>();
		roles.add(role1);
		roles.add(null);
		Mockito.when(roleService.getActiveRoles(requestedLoginId)).thenReturn(roles);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.getAvailableRoles(requestedLoginId);
	}

	@Test
	public void pushUserRoleTest() throws Exception {
		String loginId = "123";
		App app = new App();
		app.setId(1l);
		List<EcompRole> rolesJson = new ArrayList<>();
		EcompRole role1 = new EcompRole();
		role1.setId(123L);
		rolesJson.add(role1);
		Set<UserApp> userApps = new TreeSet<>();

		UserApp userApp = new UserApp();
		Role role = new Role();
		role.setId(123L);
		userApp.setRole(role);
		userApp.setApp(app);

		UserApp userApp2 = new UserApp();
		Role role2 = new Role();
		role2.setId(124L);
		userApp2.setRole(role2);
		userApp2.setApp(app);
		userApps.add(userApp);
		userApps.add(userApp2);
		User user = new User();
		user.setUserApps(userApps);
		Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);

		Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
		Assert.assertTrue(true);
	}

	@Test
	public void pushUserRoleExceptionTest() throws Exception {
		String loginId = "123";
		App app = new App();
		List<EcompRole> rolesJson = new ArrayList<>();
		EcompRole role1 = new EcompRole();
		role1.setId(123L);
		rolesJson.add(role1);
		Set<UserApp> userApps = new TreeSet<>();

		UserApp userApp = new UserApp();
		Role role = new Role();
		role.setId(123L);
		userApp.setApp(app);
		userApp.setRole(role);
		userApps.add(userApp);
		User user = new User();
		user.setUserApps(userApps);
		Mockito.when(userProfileService.getUserByLoginId(loginId)).thenReturn(user);

		Mockito.when(roleService.getRole(loginId, role1.getId())).thenReturn(role);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.pushUserRole(loginId, rolesJson);
	}

	@Test
	public void getUserRolesTest() throws Exception {
		String loginId = "123";
		String responseString = "Response";
		Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenReturn(responseString);
		User user = new User();
		SortedSet<Role> currentRoles = new TreeSet<>();
		Role role = new Role();
		role.setId(123L);
		currentRoles.add(role);
		user.setRoles(currentRoles);
		Mockito.when(userService.userMapper(responseString)).thenReturn(user);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		List<EcompRole> ecompRoles = onBoardingApiServiceImpl.getUserRoles(loginId);
		Assert.assertNotNull(ecompRoles);
	}

	@Test(expected = org.onap.portalsdk.core.onboarding.exception.PortalAPIException.class)
	public void getUserRolesExceptionTest() throws Exception {
		String loginId = "123";
		Mockito.when(restApiRequestBuilder.getViaREST("/v3/user/" + loginId, true, loginId)).thenThrow(IOException.class);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.getUserRoles(loginId);
	}

	@Test
	public void isAppAuthenticatedTest() throws Exception {
		HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
		String userName = "UserName";
		String password = "Password";
		Mockito.when(request.getHeader("username")).thenReturn(userName);
		Mockito.when(request.getHeader("password")).thenReturn(password);
		
		ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
		Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
		WebServiceCallService webService = Mockito.mock(WebServiceCallService.class);
		Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(webService);
		Mockito.when(webService.verifyRESTCredential(null, userName, password)).thenReturn(true);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		boolean status = onBoardingApiServiceImpl.isAppAuthenticated(request);
		Assert.assertTrue(status);
	}
	
	@Test(expected =PortalAPIException.class)
	public void isAppAuthenticatedExceptionTest() throws Exception {
		HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
		String userName = "UserName";
		String password = "Password";
		Mockito.when(request.getHeader("username")).thenReturn(userName);
		Mockito.when(request.getHeader("password")).thenReturn(password);
		
		ApplicationContext appContext = Mockito.mock(ApplicationContext.class);
		Mockito.when(AppContextManager.getAppContext()).thenReturn(appContext);
		Mockito.when(appContext.getBean(WebServiceCallService.class)).thenReturn(null);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.isAppAuthenticated(request);
	}
	
	@Test
	public void getSessionTimeOutsTEst() throws Exception {
		String session ="Session";
		PowerMockito.mockStatic(PortalTimeoutHandler.class);
		Mockito.when(PortalTimeoutHandler.gatherSessionExtensions()).thenReturn(session);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		String response = onBoardingApiServiceImpl.getSessionTimeOuts();
		Assert.assertEquals(response, session);
	}

	@Test
	public void updateSessionTimeOutsTest() throws Exception {
		String sessionMap ="Session";
		PowerMockito.mockStatic(PortalTimeoutHandler.class);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		onBoardingApiServiceImpl.updateSessionTimeOuts(sessionMap);
		Assert.assertTrue(true);
	}

	@Test
	public void getUserId() throws PortalAPIException {
		HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
		String userId = "123";
		Mockito.when(loginStrategy.getUserId(request)).thenReturn(userId);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		String response = onBoardingApiServiceImpl.getUserId(request);
		Assert.assertEquals(response, userId);
	}
	
	@Test
	public void getAppCredentialsTest() throws Exception{
		App app =new App();
		app.setName("App");
		app.setUsername("User");
		app.setAppPassword("Password");
		
		String key = "Key";
		
		PowerMockito.mockStatic(SystemProperties.class);
		PowerMockito.mockStatic(CipherUtil.class);
		Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
		Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenReturn(app.getAppPassword());
		Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
		Assert.assertNotNull(credentialsMap);
	}
	
	@Test
	public void getAppCredentialsAppNullTest() throws Exception{
		Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(null);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
		Assert.assertNotNull(credentialsMap);
	}
	
	@Test
	public void getAppCredentialsExceptionTest() throws Exception{
		App app =new App();
		app.setName("App");
		app.setUsername("User");
		app.setAppPassword("Password");
		
		String key = "Key";
		
		PowerMockito.mockStatic(SystemProperties.class);
		PowerMockito.mockStatic(CipherUtil.class);
		Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(key);
		Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), key)).thenThrow(CipherUtilException.class);
		Mockito.when(appServiceImpl.getDefaultApp()).thenReturn(app);
		OnBoardingApiServiceImpl onBoardingApiServiceImpl = new OnBoardingApiServiceImpl();
		Map<String, String> credentialsMap = onBoardingApiServiceImpl.getAppCredentials();
		Assert.assertNotNull(credentialsMap);
	}
}