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.openecomp.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.openecomp.portalapp.portal.core.MockEPUser;
61 import org.openecomp.portalapp.portal.domain.CentralRoleFunction;
62 import org.openecomp.portalapp.portal.domain.EPApp;
63 import org.openecomp.portalapp.portal.domain.EPAppRoleFunction;
64 import org.openecomp.portalapp.portal.domain.EPRole;
65 import org.openecomp.portalapp.portal.domain.EPUser;
66 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
67 import org.openecomp.portalapp.portal.transport.EcompUserRoles;
68 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
69 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
70 import org.openecomp.portalsdk.core.domain.Role;
71 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
72 import org.openecomp.portalsdk.core.service.DataAccessService;
73 import org.openecomp.portalsdk.core.service.DataAccessServiceImpl;
74 import org.openecomp.portalsdk.core.util.SystemProperties;
75 import org.powermock.api.mockito.PowerMockito;
76 import org.powermock.core.classloader.annotations.PrepareForTest;
77 import org.powermock.modules.junit4.PowerMockRunner;
78 import org.springframework.http.HttpEntity;
79 import org.springframework.http.HttpHeaders;
80 import org.springframework.http.HttpMethod;
81 import org.springframework.http.HttpStatus;
82 import org.springframework.http.ResponseEntity;
83 import org.springframework.web.client.HttpClientErrorException;
84 import org.springframework.web.client.RestTemplate;
86 @RunWith(PowerMockRunner.class)
87 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class })
88 public class ExternalAccessRolesServiceImplTest {
90 DataAccessService dataAccessService1 = new DataAccessServiceImpl();
93 RestTemplate template = new RestTemplate();
96 ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
100 MockitoAnnotations.initMocks(this);
103 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
105 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
106 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
107 NullPointerException nullPointerException = new NullPointerException();
108 MockEPUser mockUser = new MockEPUser();
109 String uebKey = "test-ueb-key";
111 public EPApp mockApp() {
112 EPApp app = new EPApp();
114 app.setImageUrl("test");
115 app.setDescription("test");
116 app.setNotes("test");
118 app.setId((long) 10);
119 app.setAppRestEndpoint("test");
120 app.setAlternateUrl("test");
122 app.setMlAppName("test");
123 app.setMlAppAdminId("test");
124 app.setUsername("test");
125 app.setAppPassword("test");
127 app.setEnabled(false);
128 app.setUebKey("test");
129 app.setUebSecret("test");
130 app.setUebTopicName("test");
136 public void getAppRolesIfAppIsPortalTest() throws Exception {
137 List<EPRole> applicationRoles = new ArrayList<>();
138 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
139 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
140 assertEquals(expectedApplicationRoles, applicationRoles);
144 public void getAppRolesTest() throws Exception {
145 List<EPRole> applicationRoles = new ArrayList<>();
146 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
147 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
148 assertEquals(expectedApplicationRoles, applicationRoles);
151 // @SuppressWarnings("null")
152 // @Test(expected = java.lang.Exception.class)
153 // public void getAppRolesExceptionTest() throws Exception{
154 // List<EPRole> applicationRoles = new ArrayList<>();
155 // DataAccessService dataAccessService = null ;
156 // Mockito.when(dataAccessService.getList(EPRole.class, "where app_id = 10",
157 // null, null)).thenThrow(nullPointerException);
158 // List<EPRole> expectedApplicationRoles =
159 // externalAccessRolesServiceImpl.getAppRoles((long) 10);
160 // assertEquals(expectedApplicationRoles,applicationRoles);
163 @Test(expected = java.lang.Exception.class)
164 public void getAppExceptionTest() throws Exception {
165 List<EPApp> app = new ArrayList<>();
166 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
168 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
169 assertEquals(app, expectedapp);
172 @Test(expected = java.lang.Exception.class)
173 public void getAppTest() throws Exception {
174 List<EPApp> appList = new ArrayList<>();
175 EPApp app = mockApp();
177 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
178 .thenReturn(appList);
179 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
183 public void getAppErrorTest() throws Exception {
184 List<EPApp> appList = new ArrayList<>();
185 EPApp app = mockApp();
188 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
189 .thenReturn(appList);
190 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
191 assertEquals(appList, expectedapp);
195 public void addRoleTest() throws Exception {
196 HttpHeaders headers = new HttpHeaders();
197 PowerMockito.mockStatic(EcompPortalUtils.class);
198 PowerMockito.mockStatic(SystemProperties.class);
199 PowerMockito.mockStatic(EPCommonSystemProperties.class);
200 String uebKey = "test-ueb-key";
201 Role role = new Role();
202 role.setId((long) 25);
203 EPApp app = mockApp();
204 app.setEnabled(true);
205 app.setId((long) 10);
206 app.setNameSpace("test_namesapce");
207 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
208 List<EPApp> appList = new ArrayList<>();
210 List<EPRole> roleList = new ArrayList<>();
211 EPRole ePRole = new EPRole();
212 role.setName("Test Role");
213 roleList.add(ePRole);
214 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
215 .thenReturn(appList);
216 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId() + " and app_id ="+app.getId(), null, null)).thenReturn(roleList);
217 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
218 .thenReturn("Testurl");
219 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
220 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
221 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
223 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
228 public void addRoleMethodNotAllowedTest() throws Exception {
229 HttpHeaders headers = new HttpHeaders();
230 PowerMockito.mockStatic(EcompPortalUtils.class);
231 PowerMockito.mockStatic(SystemProperties.class);
232 PowerMockito.mockStatic(EPCommonSystemProperties.class);
233 Role role = new Role();
234 role.setId((long) 25);
235 EPApp app = mockApp();
236 app.setEnabled(true);
237 app.setId((long) 10);
238 app.setNameSpace("test_namesapce");
239 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
240 List<EPApp> appList = new ArrayList<>();
242 List<EPRole> roleList = new ArrayList<>();
243 EPRole ePRole = new EPRole();
244 role.setName("Test Role");
245 roleList.add(ePRole);
246 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
247 .thenReturn(appList);
249 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId() + " and app_id ="+app.getId(), null, null)).thenReturn(roleList);
250 // Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId(), null, null))
251 // .thenReturn(roleList);
252 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
253 .thenReturn("Testurl");
254 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
255 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
256 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
258 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
261 public EPApp getApp() {
262 EPApp app = new EPApp();
264 app.setImageUrl("test");
265 app.setDescription("test");
266 app.setNotes("test");
269 app.setAppRestEndpoint("test");
270 app.setAlternateUrl("test");
272 app.setMlAppName("test");
273 app.setMlAppAdminId("test");
274 app.setUsername("test");
275 app.setAppPassword("test");
277 app.setEnabled(false);
278 app.setUebKey("test");
279 app.setUebSecret("test");
280 app.setUebTopicName("test");
286 public void deleteCentralRoleFunctionTest() throws Exception {
287 final Map<String, String> params = new HashMap<>();
288 EPApp app = mockApp();
289 params.put("functionCode", "menu_fun_code");
290 params.put("appId", String.valueOf(10));
291 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
292 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
293 domainCentralRoleFunction.setCode("menu_fun_code");
294 centralRoleFunctionList.add(domainCentralRoleFunction);
295 Mockito.when(dataAccessService1.executeNamedQuery("getRoleFunction", params, null))
296 .thenReturn(centralRoleFunctionList);
297 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
298 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
299 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
300 HttpHeaders headers = new HttpHeaders();
301 PowerMockito.mockStatic(EcompPortalUtils.class);
302 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
304 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
305 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
307 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
310 public void deleteCentralRoleFunctionFailTest() throws Exception {
311 final Map<String, String> params = new HashMap<>();
312 EPApp app = mockApp();
313 params.put("functionCode", "menu_fun_code");
314 params.put("appId", String.valueOf(10));
315 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
316 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
317 domainCentralRoleFunction.setCode("menu_fun_code");
318 centralRoleFunctionList.add(domainCentralRoleFunction);
319 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
320 Mockito.when(dataAccessService1.executeNamedQuery("getRoleFunction", params, null))
321 .thenReturn(centralRoleFunctionList);
322 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.NOT_FOUND);
323 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
324 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
325 HttpHeaders headers = new HttpHeaders();
326 PowerMockito.mockStatic(EcompPortalUtils.class);
327 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
329 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
330 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
332 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
336 public void deleteCentralRoleFunctionExceptionTest() {
337 final Map<String, String> params = new HashMap<>();
338 EPApp app = mockApp();
339 params.put("functionCd", "menu_fun_code");
340 params.put("appId", String.valueOf(10));
341 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
342 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
343 domainCentralRoleFunction.setCode("menu_fun_code");
344 centralRoleFunctionList.add(domainCentralRoleFunction);
345 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
346 .thenThrow(nullPointerException);
347 assertFalse(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
351 public void getUserTest() {
352 List<EPUser> userList = new ArrayList<>();
353 EPUser user = mockUser.mockEPUser();
355 Mockito.when(dataAccessService1.getList(EPUser.class, " where org_user_id = '" + "guestT" + "'", null, null))
356 .thenReturn(userList);
357 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
358 assertEquals(expectedUserList, userList);
362 public void saveCentralRoleFunctionTest() throws Exception {
363 CentralRoleFunction centralRoleFunction = new CentralRoleFunction();
364 centralRoleFunction.setCode("menu_test");
365 EPApp app = mockApp();
367 final Map<String, String> params = new HashMap<>();
368 params.put("functionCd", "menu_test");
369 params.put("appId", String.valueOf(1));
370 PowerMockito.mockStatic(EcompPortalUtils.class);
371 HttpHeaders headers = new HttpHeaders();
372 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
373 List<CentralRoleFunction> appRoleFunc = new ArrayList<>();
374 appRoleFunc.add(centralRoleFunction);
375 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params,
376 null)).thenReturn(appRoleFunc);
377 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
378 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
379 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
383 // public void getAllAppUsersIfAppIsPortalTest() throws Exception
385 // List<EPApp> expectedapps = new ArrayList<>();
386 // EPApp app = new EPApp();
387 // app.setEnabled(true);
388 // app.setId((long) 1);
389 // expectedapps.add(app);
390 // List<EPRole> applicationRoles = new ArrayList<>();
391 // Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
392 // Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)).thenReturn(expectedapps);
393 // externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
398 public void getAllAppUsersTest() throws Exception
400 List<EPApp> expectedapps = new ArrayList<>();
401 EPApp app = new EPApp();
402 app.setEnabled(true);
403 app.setId((long) 10);
404 expectedapps.add(app);
405 List<EPRole> applicationRoles = new ArrayList<>();
406 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
407 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)).thenReturn(expectedapps);
408 final Map<String, Long> appParams = new HashMap<>();
409 appParams.put("appId", app.getId());
410 List<EcompUserRoles> userList = new ArrayList<>();
411 EcompUserRoles ecompUserRoles = new EcompUserRoles();
412 ecompUserRoles.setOrgUserId("guestT");
413 ecompUserRoles.setRoleId((long) 1);
414 ecompUserRoles.setRoleName("test");
416 EcompUserRoles ecompUserRoles2 = new EcompUserRoles();
417 ecompUserRoles2.setOrgUserId("guestT");
418 ecompUserRoles2.setRoleId((long) 2);
419 ecompUserRoles2.setRoleName("test new");
420 userList.add(ecompUserRoles);
421 userList.add(ecompUserRoles2);
423 Mockito.when(dataAccessService1.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList);
424 List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
425 assertEquals(usersfinalList.get(0).getRoles().size(), 2);