2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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.portal.service;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertTrue;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.InjectMocks;
56 import org.mockito.Matchers;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.mockito.MockitoAnnotations;
60 import org.onap.portalapp.portal.core.MockEPUser;
61 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
62 import org.onap.portalapp.portal.domain.EPApp;
63 import org.onap.portalapp.portal.domain.EPAppRoleFunction;
64 import org.onap.portalapp.portal.domain.EPRole;
65 import org.onap.portalapp.portal.domain.EPUser;
66 import org.onap.portalapp.portal.exceptions.InactiveApplicationException;
67 import org.onap.portalapp.portal.framework.MockitoTestSuite;
68 import org.onap.portalapp.portal.transport.EcompUserRoles;
69 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
70 import org.onap.portalapp.portal.utils.EcompPortalUtils;
71 import org.onap.portalsdk.core.domain.Role;
72 import org.onap.portalsdk.core.restful.domain.EcompUser;
73 import org.onap.portalsdk.core.service.DataAccessService;
74 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
75 import org.onap.portalsdk.core.util.SystemProperties;
76 import org.powermock.api.mockito.PowerMockito;
77 import org.powermock.core.classloader.annotations.PrepareForTest;
78 import org.powermock.modules.junit4.PowerMockRunner;
79 import org.springframework.http.HttpEntity;
80 import org.springframework.http.HttpHeaders;
81 import org.springframework.http.HttpMethod;
82 import org.springframework.http.HttpStatus;
83 import org.springframework.http.ResponseEntity;
84 import org.springframework.web.client.HttpClientErrorException;
85 import org.springframework.web.client.RestTemplate;
87 @RunWith(PowerMockRunner.class)
88 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class })
89 public class ExternalAccessRolesServiceImplTest {
91 DataAccessService dataAccessService1 = new DataAccessServiceImpl();
94 RestTemplate template = new RestTemplate();
97 ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
100 public void setup() {
101 MockitoAnnotations.initMocks(this);
104 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
106 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
107 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
108 NullPointerException nullPointerException = new NullPointerException();
109 MockEPUser mockUser = new MockEPUser();
110 String uebKey = "test-ueb-key";
112 public EPApp mockApp() {
113 EPApp app = new EPApp();
115 app.setImageUrl("test");
116 app.setDescription("test");
117 app.setNotes("test");
119 app.setId((long) 10);
120 app.setAppRestEndpoint("test");
121 app.setAlternateUrl("test");
123 app.setMlAppName("test");
124 app.setMlAppAdminId("test");
125 app.setUsername("test");
126 app.setAppPassword("test");
128 app.setEnabled(false);
129 app.setUebKey("test");
130 app.setUebSecret("test");
131 app.setUebTopicName("test");
137 public void getAppRolesIfAppIsPortalTest() throws Exception {
138 List<EPRole> applicationRoles = new ArrayList<>();
139 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
140 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
141 assertEquals(expectedApplicationRoles, applicationRoles);
145 public void getAppRolesTest() throws Exception {
146 List<EPRole> applicationRoles = new ArrayList<>();
147 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
148 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
149 assertEquals(expectedApplicationRoles, applicationRoles);
152 // @SuppressWarnings("null")
153 // @Test(expected = java.lang.Exception.class)
154 // public void getAppRolesExceptionTest() throws Exception{
155 // List<EPRole> applicationRoles = new ArrayList<>();
156 // DataAccessService dataAccessService = null ;
157 // Mockito.when(dataAccessService.getList(EPRole.class, "where app_id = 10",
158 // null, null)).thenThrow(nullPointerException);
159 // List<EPRole> expectedApplicationRoles =
160 // externalAccessRolesServiceImpl.getAppRoles((long) 10);
161 // assertEquals(expectedApplicationRoles,applicationRoles);
165 public void getAppExceptionTest() throws Exception {
166 List<EPApp> app = new ArrayList<>();
167 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
169 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
170 assertEquals(app, expectedapp);
173 @Test(expected = InactiveApplicationException.class)
174 public void getAppErrorTest() throws Exception {
175 List<EPApp> appList = new ArrayList<>();
176 EPApp app = mockApp();
178 final Map<String, String> appUebkeyParams = new HashMap<>();
179 appUebkeyParams.put("appKey", "test-ueb-key");
180 Mockito.when(dataAccessService1.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
181 .thenReturn(appList);
182 externalAccessRolesServiceImpl.getApp(uebKey);
186 public void getAppTest() throws Exception {
187 List<EPApp> appList = new ArrayList<>();
188 EPApp app = mockApp();
191 final Map<String, String> appUebkeyParams = new HashMap<>();
192 appUebkeyParams.put("appKey", "test-ueb-key");
193 Mockito.when(dataAccessService1.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
194 .thenReturn(appList);
195 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
196 assertEquals(appList, expectedapp);
200 public void addRoleTest() throws Exception {
201 HttpHeaders headers = new HttpHeaders();
202 PowerMockito.mockStatic(EcompPortalUtils.class);
203 PowerMockito.mockStatic(SystemProperties.class);
204 PowerMockito.mockStatic(EPCommonSystemProperties.class);
205 String uebKey = "test-ueb-key";
206 Role role = new Role();
207 role.setId((long) 25);
208 EPApp app = mockApp();
209 app.setEnabled(true);
210 app.setId((long) 10);
211 app.setNameSpace("test_namesapce");
212 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
213 List<EPApp> appList = new ArrayList<>();
215 List<EPRole> roleList = new ArrayList<>();
216 EPRole ePRole = new EPRole();
217 role.setName("Test Role");
218 roleList.add(ePRole);
219 final Map<String, String> appUebkeyParams = new HashMap<>();
220 appUebkeyParams.put("appKey", "test-ueb-key");
221 Mockito.when(dataAccessService1.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
222 .thenReturn(appList);
223 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
224 getPartnerAppRoleParams.put("appRoleId", role.getId());
225 getPartnerAppRoleParams.put("appId", app.getId());
226 Mockito.when(dataAccessService1.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)).thenReturn(roleList);
227 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
228 .thenReturn("Testurl");
229 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
230 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
231 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
233 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
238 public void addRoleMethodNotAllowedTest() throws Exception {
239 HttpHeaders headers = new HttpHeaders();
240 PowerMockito.mockStatic(EcompPortalUtils.class);
241 PowerMockito.mockStatic(SystemProperties.class);
242 PowerMockito.mockStatic(EPCommonSystemProperties.class);
243 Role role = new Role();
244 role.setId((long) 25);
245 EPApp app = mockApp();
246 app.setEnabled(true);
247 app.setId((long) 10);
248 app.setNameSpace("test_namesapce");
249 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
250 List<EPApp> appList = new ArrayList<>();
252 List<EPRole> roleList = new ArrayList<>();
253 EPRole ePRole = new EPRole();
254 role.setName("Test Role");
255 roleList.add(ePRole);
256 final Map<String, String> appUebkeyParams = new HashMap<>();
257 appUebkeyParams.put("appKey", "test-ueb-key");
258 Mockito.when(dataAccessService1.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
259 .thenReturn(appList);
260 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
261 getPartnerAppRoleParams.put("appRoleId", role.getId());
262 getPartnerAppRoleParams.put("appId", app.getId());
263 Mockito.when(dataAccessService1.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)).thenReturn(roleList);
264 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
265 .thenReturn("Testurl");
266 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
267 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
268 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
269 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
272 public EPApp getApp() {
273 EPApp app = new EPApp();
275 app.setImageUrl("test");
276 app.setDescription("test");
277 app.setNotes("test");
280 app.setAppRestEndpoint("test");
281 app.setAlternateUrl("test");
283 app.setMlAppName("test");
284 app.setMlAppAdminId("test");
285 app.setUsername("test");
286 app.setAppPassword("test");
288 app.setEnabled(false);
289 app.setUebKey("test");
290 app.setUebSecret("test");
291 app.setUebTopicName("test");
297 public void deleteCentralRoleFunctionTest() throws Exception {
298 final Map<String, String> params = new HashMap<>();
299 EPApp app = mockApp();
300 params.put("functionCode", "menu_fun_code");
301 params.put("appId", String.valueOf(10));
302 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
303 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
304 domainCentralRoleFunction.setCode("menu_fun_code");
305 centralRoleFunctionList.add(domainCentralRoleFunction);
306 Mockito.when(dataAccessService1.executeNamedQuery("getRoleFunction", params, null))
307 .thenReturn(centralRoleFunctionList);
308 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
309 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
310 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
311 HttpHeaders headers = new HttpHeaders();
312 PowerMockito.mockStatic(EcompPortalUtils.class);
313 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
315 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
316 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
318 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
321 public void deleteCentralRoleFunctionFailTest() throws Exception {
322 final Map<String, String> params = new HashMap<>();
323 EPApp app = mockApp();
324 params.put("functionCode", "menu_fun_code");
325 params.put("appId", String.valueOf(10));
326 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
327 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
328 domainCentralRoleFunction.setCode("menu_fun_code");
329 centralRoleFunctionList.add(domainCentralRoleFunction);
330 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
331 Mockito.when(dataAccessService1.executeNamedQuery("getRoleFunction", params, null))
332 .thenReturn(centralRoleFunctionList);
333 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.NOT_FOUND);
334 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
335 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
336 HttpHeaders headers = new HttpHeaders();
337 PowerMockito.mockStatic(EcompPortalUtils.class);
338 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
340 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
341 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
343 boolean returnedValue = externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app);
344 assertTrue(returnedValue);
348 public void deleteCentralRoleFunctionExceptionTest() {
349 final Map<String, String> params = new HashMap<>();
350 EPApp app = mockApp();
351 params.put("functionCd", "menu_fun_code");
352 params.put("appId", String.valueOf(10));
353 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
354 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
355 domainCentralRoleFunction.setCode("menu_fun_code");
356 centralRoleFunctionList.add(domainCentralRoleFunction);
357 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
358 .thenThrow(nullPointerException);
359 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
363 public void getUserTest() {
364 List<EPUser> userList = new ArrayList<>();
365 EPUser user = mockUser.mockEPUser();
367 final Map<String, String> userParams = new HashMap<>();
368 userParams.put("org_user_id", "guestT");
369 Mockito.when(dataAccessService1.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
370 .thenReturn(userList);
371 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
372 assertEquals(expectedUserList, userList);
376 public void saveCentralRoleFunctionTest() throws Exception {
377 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
378 centralV2RoleFunction.setCode("menu_test");
379 EPApp app = mockApp();
381 final Map<String, String> params = new HashMap<>();
382 params.put("functionCd", "menu_test");
383 params.put("appId", String.valueOf(1));
384 PowerMockito.mockStatic(EcompPortalUtils.class);
385 HttpHeaders headers = new HttpHeaders();
386 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
387 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
388 appRoleFunc.add(centralV2RoleFunction);
389 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params,
390 null)).thenReturn(appRoleFunc);
391 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
392 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
393 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
397 // public void getAllAppUsersIfAppIsPortalTest() throws Exception
399 // List<EPApp> expectedapps = new ArrayList<>();
400 // EPApp app = new EPApp();
401 // app.setEnabled(true);
402 // app.setId((long) 1);
403 // expectedapps.add(app);
404 // List<EPRole> applicationRoles = new ArrayList<>();
405 // Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
406 // Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)).thenReturn(expectedapps);
407 // externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
411 @Test(expected = IndexOutOfBoundsException.class)
412 public void getAllAppUsersTest() throws Exception
414 List<EPApp> expectedapps = new ArrayList<>();
415 EPApp app = new EPApp();
416 app.setEnabled(true);
417 app.setId((long) 10);
418 expectedapps.add(app);
419 List<EPRole> applicationRoles = new ArrayList<>();
420 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
421 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)).thenReturn(expectedapps);
422 final Map<String, Long> appParams = new HashMap<>();
423 appParams.put("appId", app.getId());
424 List<EcompUserRoles> userList = new ArrayList<>();
425 EcompUserRoles ecompUserRoles = new EcompUserRoles();
426 ecompUserRoles.setOrgUserId("guestT");
427 ecompUserRoles.setRoleId((long) 1);
428 ecompUserRoles.setRoleName("test");
430 EcompUserRoles ecompUserRoles2 = new EcompUserRoles();
431 ecompUserRoles2.setOrgUserId("guestT");
432 ecompUserRoles2.setRoleId((long) 2);
433 ecompUserRoles2.setRoleName("test new");
434 userList.add(ecompUserRoles);
435 userList.add(ecompUserRoles2);
437 Mockito.when(dataAccessService1.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList);
438 List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
439 assertEquals(usersfinalList.get(0).getRoles().size(), 2);
443 public void getGlobalRolesOfPortalTest()
445 Mockito.when(dataAccessService1.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(null);
446 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), null);
450 public void getGlobalRolesOfPortalExceptionTest()
452 List<EPRole> globalRoles = new ArrayList<>();
453 Mockito.when(dataAccessService1.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenThrow(nullPointerException);
454 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), globalRoles);