1 package org.openecomp.portalapp.portal.service;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
7 import java.util.ArrayList;
8 import java.util.HashMap;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.InjectMocks;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.openecomp.portalapp.portal.core.MockEPUser;
24 import org.openecomp.portalapp.portal.domain.CentralRoleFunction;
25 import org.openecomp.portalapp.portal.domain.EPApp;
26 import org.openecomp.portalapp.portal.domain.EPAppRoleFunction;
27 import org.openecomp.portalapp.portal.domain.EPRole;
28 import org.openecomp.portalapp.portal.domain.EPUser;
29 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
30 import org.openecomp.portalapp.portal.transport.EcompUserRoles;
31 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
32 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
33 import org.openecomp.portalsdk.core.domain.Role;
34 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
35 import org.openecomp.portalsdk.core.service.DataAccessService;
36 import org.openecomp.portalsdk.core.service.DataAccessServiceImpl;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.client.HttpClientErrorException;
47 import org.springframework.web.client.RestTemplate;
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class })
51 public class ExternalAccessRolesServiceImplTest {
53 DataAccessService dataAccessService1 = new DataAccessServiceImpl();
56 RestTemplate template = new RestTemplate();
59 ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
63 MockitoAnnotations.initMocks(this);
66 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
68 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
69 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
70 NullPointerException nullPointerException = new NullPointerException();
71 MockEPUser mockUser = new MockEPUser();
72 String uebKey = "test-ueb-key";
74 public EPApp mockApp() {
75 EPApp app = new EPApp();
77 app.setImageUrl("test");
78 app.setDescription("test");
82 app.setAppRestEndpoint("test");
83 app.setAlternateUrl("test");
85 app.setMlAppName("test");
86 app.setMlAppAdminId("test");
87 app.setUsername("test");
88 app.setAppPassword("test");
90 app.setEnabled(false);
91 app.setUebKey("test");
92 app.setUebSecret("test");
93 app.setUebTopicName("test");
99 public void getAppRolesIfAppIsPortalTest() throws Exception {
100 List<EPRole> applicationRoles = new ArrayList<>();
101 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
102 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
103 assertEquals(expectedApplicationRoles, applicationRoles);
107 public void getAppRolesTest() throws Exception {
108 List<EPRole> applicationRoles = new ArrayList<>();
109 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
110 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
111 assertEquals(expectedApplicationRoles, applicationRoles);
114 // @SuppressWarnings("null")
115 // @Test(expected = java.lang.Exception.class)
116 // public void getAppRolesExceptionTest() throws Exception{
117 // List<EPRole> applicationRoles = new ArrayList<>();
118 // DataAccessService dataAccessService = null ;
119 // Mockito.when(dataAccessService.getList(EPRole.class, "where app_id = 10",
120 // null, null)).thenThrow(nullPointerException);
121 // List<EPRole> expectedApplicationRoles =
122 // externalAccessRolesServiceImpl.getAppRoles((long) 10);
123 // assertEquals(expectedApplicationRoles,applicationRoles);
126 @Test(expected = java.lang.Exception.class)
127 public void getAppExceptionTest() throws Exception {
128 List<EPApp> app = new ArrayList<>();
129 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
131 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
132 assertEquals(app, expectedapp);
135 @Test(expected = java.lang.Exception.class)
136 public void getAppTest() throws Exception {
137 List<EPApp> appList = new ArrayList<>();
138 EPApp app = mockApp();
140 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
141 .thenReturn(appList);
142 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
146 public void getAppErrorTest() throws Exception {
147 List<EPApp> appList = new ArrayList<>();
148 EPApp app = mockApp();
151 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
152 .thenReturn(appList);
153 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
154 assertEquals(appList, expectedapp);
158 public void addRoleTest() throws Exception {
159 HttpHeaders headers = new HttpHeaders();
160 PowerMockito.mockStatic(EcompPortalUtils.class);
161 PowerMockito.mockStatic(SystemProperties.class);
162 PowerMockito.mockStatic(EPCommonSystemProperties.class);
163 String uebKey = "test-ueb-key";
164 Role role = new Role();
165 role.setId((long) 25);
166 EPApp app = mockApp();
167 app.setEnabled(true);
168 app.setId((long) 10);
169 app.setNameSpace("test_namesapce");
170 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
171 List<EPApp> appList = new ArrayList<>();
173 List<EPRole> roleList = new ArrayList<>();
174 EPRole ePRole = new EPRole();
175 role.setName("Test Role");
176 roleList.add(ePRole);
177 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
178 .thenReturn(appList);
179 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId() + " and app_id ="+app.getId(), null, null)).thenReturn(roleList);
180 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
181 .thenReturn("Testurl");
182 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
183 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
184 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
186 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
191 public void addRoleMethodNotAllowedTest() throws Exception {
192 HttpHeaders headers = new HttpHeaders();
193 PowerMockito.mockStatic(EcompPortalUtils.class);
194 PowerMockito.mockStatic(SystemProperties.class);
195 PowerMockito.mockStatic(EPCommonSystemProperties.class);
196 Role role = new Role();
197 role.setId((long) 25);
198 EPApp app = mockApp();
199 app.setEnabled(true);
200 app.setId((long) 10);
201 app.setNameSpace("test_namesapce");
202 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
203 List<EPApp> appList = new ArrayList<>();
205 List<EPRole> roleList = new ArrayList<>();
206 EPRole ePRole = new EPRole();
207 role.setName("Test Role");
208 roleList.add(ePRole);
209 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
210 .thenReturn(appList);
212 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId() + " and app_id ="+app.getId(), null, null)).thenReturn(roleList);
213 // Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId(), null, null))
214 // .thenReturn(roleList);
215 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
216 .thenReturn("Testurl");
217 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
218 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
219 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
221 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
224 public EPApp getApp() {
225 EPApp app = new EPApp();
227 app.setImageUrl("test");
228 app.setDescription("test");
229 app.setNotes("test");
232 app.setAppRestEndpoint("test");
233 app.setAlternateUrl("test");
235 app.setMlAppName("test");
236 app.setMlAppAdminId("test");
237 app.setUsername("test");
238 app.setAppPassword("test");
240 app.setEnabled(false);
241 app.setUebKey("test");
242 app.setUebSecret("test");
243 app.setUebTopicName("test");
249 public void deleteCentralRoleFunctionTest() throws Exception {
250 final Map<String, String> params = new HashMap<>();
251 EPApp app = mockApp();
252 params.put("functionCode", "menu_fun_code");
253 params.put("appId", String.valueOf(10));
254 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
255 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
256 domainCentralRoleFunction.setCode("menu_fun_code");
257 centralRoleFunctionList.add(domainCentralRoleFunction);
258 Mockito.when(dataAccessService1.executeNamedQuery("getRoleFunction", params, null))
259 .thenReturn(centralRoleFunctionList);
260 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
261 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
262 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
263 HttpHeaders headers = new HttpHeaders();
264 PowerMockito.mockStatic(EcompPortalUtils.class);
265 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
267 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
268 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
270 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
273 public void deleteCentralRoleFunctionFailTest() throws Exception {
274 final Map<String, String> params = new HashMap<>();
275 EPApp app = mockApp();
276 params.put("functionCode", "menu_fun_code");
277 params.put("appId", String.valueOf(10));
278 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
279 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
280 domainCentralRoleFunction.setCode("menu_fun_code");
281 centralRoleFunctionList.add(domainCentralRoleFunction);
282 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
283 Mockito.when(dataAccessService1.executeNamedQuery("getRoleFunction", params, null))
284 .thenReturn(centralRoleFunctionList);
285 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.NOT_FOUND);
286 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
287 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
288 HttpHeaders headers = new HttpHeaders();
289 PowerMockito.mockStatic(EcompPortalUtils.class);
290 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
292 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
293 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
295 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
299 public void deleteCentralRoleFunctionExceptionTest() {
300 final Map<String, String> params = new HashMap<>();
301 EPApp app = mockApp();
302 params.put("functionCd", "menu_fun_code");
303 params.put("appId", String.valueOf(10));
304 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
305 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
306 domainCentralRoleFunction.setCode("menu_fun_code");
307 centralRoleFunctionList.add(domainCentralRoleFunction);
308 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
309 .thenThrow(nullPointerException);
310 assertFalse(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
314 public void getUserTest() {
315 List<EPUser> userList = new ArrayList<>();
316 EPUser user = mockUser.mockEPUser();
318 Mockito.when(dataAccessService1.getList(EPUser.class, " where org_user_id = '" + "guestT" + "'", null, null))
319 .thenReturn(userList);
320 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
321 assertEquals(expectedUserList, userList);
325 public void saveCentralRoleFunctionTest() throws Exception {
326 CentralRoleFunction centralRoleFunction = new CentralRoleFunction();
327 centralRoleFunction.setCode("menu_test");
328 EPApp app = mockApp();
330 final Map<String, String> params = new HashMap<>();
331 params.put("functionCd", "menu_test");
332 params.put("appId", String.valueOf(1));
333 PowerMockito.mockStatic(EcompPortalUtils.class);
334 HttpHeaders headers = new HttpHeaders();
335 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
336 List<CentralRoleFunction> appRoleFunc = new ArrayList<>();
337 appRoleFunc.add(centralRoleFunction);
338 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params,
339 null)).thenReturn(appRoleFunc);
340 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
341 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
342 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
346 // public void getAllAppUsersIfAppIsPortalTest() throws Exception
348 // List<EPApp> expectedapps = new ArrayList<>();
349 // EPApp app = new EPApp();
350 // app.setEnabled(true);
351 // app.setId((long) 1);
352 // expectedapps.add(app);
353 // List<EPRole> applicationRoles = new ArrayList<>();
354 // Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
355 // Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)).thenReturn(expectedapps);
356 // externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
361 public void getAllAppUsersTest() throws Exception
363 List<EPApp> expectedapps = new ArrayList<>();
364 EPApp app = new EPApp();
365 app.setEnabled(true);
366 app.setId((long) 10);
367 expectedapps.add(app);
368 List<EPRole> applicationRoles = new ArrayList<>();
369 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
370 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null)).thenReturn(expectedapps);
371 final Map<String, Long> appParams = new HashMap<>();
372 appParams.put("appId", app.getId());
373 List<EcompUserRoles> userList = new ArrayList<>();
374 EcompUserRoles ecompUserRoles = new EcompUserRoles();
375 ecompUserRoles.setOrgUserId("guestT");
376 ecompUserRoles.setRoleId((long) 1);
377 ecompUserRoles.setRoleName("test");
379 EcompUserRoles ecompUserRoles2 = new EcompUserRoles();
380 ecompUserRoles2.setOrgUserId("guestT");
381 ecompUserRoles2.setRoleId((long) 2);
382 ecompUserRoles2.setRoleName("test new");
383 userList.add(ecompUserRoles);
384 userList.add(ecompUserRoles2);
386 Mockito.when(dataAccessService1.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList);
387 List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
388 assertEquals(usersfinalList.get(0).getRoles().size(), 2);