Added Junits
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / ExternalAccessRolesServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertTrue;
43
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.SortedSet;
49 import java.util.TreeSet;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.hibernate.SQLQuery;
55 import org.hibernate.Session;
56 import org.hibernate.SessionFactory;
57 import org.hibernate.Transaction;
58 import org.json.JSONObject;
59 import org.junit.After;
60 import org.junit.Before;
61 import org.junit.Test;
62 import org.junit.runner.RunWith;
63 import org.mockito.InjectMocks;
64 import org.mockito.Matchers;
65 import org.mockito.Mock;
66 import org.mockito.Mockito;
67 import org.mockito.MockitoAnnotations;
68 import org.onap.portalapp.portal.core.MockEPUser;
69 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
70 import org.onap.portalapp.portal.domain.EPApp;
71 import org.onap.portalapp.portal.domain.EPAppRoleFunction;
72 import org.onap.portalapp.portal.domain.EPRole;
73 import org.onap.portalapp.portal.domain.EPUser;
74 import org.onap.portalapp.portal.exceptions.InactiveApplicationException;
75 import org.onap.portalapp.portal.exceptions.InvalidUserException;
76 import org.onap.portalapp.portal.framework.MockitoTestSuite;
77 import org.onap.portalapp.portal.transport.BulkUploadUserRoles;
78 import org.onap.portalapp.portal.transport.CentralV2Role;
79 import org.onap.portalapp.portal.transport.EcompUserRoles;
80 import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
81 import org.onap.portalapp.portal.transport.GlobalRoleWithApplicationRoleFunction;
82 import org.onap.portalapp.portal.transport.LocalRole;
83 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
84 import org.onap.portalapp.portal.utils.EcompPortalUtils;
85 import org.onap.portalapp.portal.utils.PortalConstants;
86 import org.onap.portalsdk.core.domain.Role;
87 import org.onap.portalsdk.core.domain.RoleFunction;
88 import org.onap.portalsdk.core.restful.domain.EcompUser;
89 import org.onap.portalsdk.core.service.DataAccessService;
90 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
91 import org.onap.portalsdk.core.util.SystemProperties;
92 import org.powermock.api.mockito.PowerMockito;
93 import org.powermock.core.classloader.annotations.PrepareForTest;
94 import org.powermock.modules.junit4.PowerMockRunner;
95 import org.springframework.http.HttpEntity;
96 import org.springframework.http.HttpHeaders;
97 import org.springframework.http.HttpMethod;
98 import org.springframework.http.HttpStatus;
99 import org.springframework.http.ResponseEntity;
100 import org.springframework.web.client.HttpClientErrorException;
101 import org.springframework.web.client.RestTemplate;
102
103 @RunWith(PowerMockRunner.class)
104 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class })
105 public class ExternalAccessRolesServiceImplTest {
106         @Mock
107         DataAccessService dataAccessService = new DataAccessServiceImpl();
108
109         @Mock
110         RestTemplate template = new RestTemplate();
111
112         @InjectMocks
113         ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
114
115         @Mock 
116         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
117         
118         @Mock
119         SessionFactory sessionFactory;
120
121         @Mock
122         Session session;
123
124         @Mock
125         Transaction transaction;
126         
127         @Before
128         public void setup() {
129                 MockitoAnnotations.initMocks(this);
130                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
131                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
132         }
133         
134         @After
135         public void after() {
136                 session.close();
137         }
138
139         private static final String APP_ROLE_NAME_PARAM = "appRoleName";
140
141         private static final String GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM = "getRoletoUpdateInExternalAuthSystem";
142
143         private static final String GET_PORTAL_APP_ROLES_QUERY = "getPortalAppRoles";
144
145         private static final String GET_ROLE_FUNCTION_QUERY = "getRoleFunction";
146
147         private static final String FUNCTION_CODE_PARAMS = "functionCode";
148
149         private static final String AND_FUNCTION_CD_EQUALS = " and function_cd = '";
150
151         private static final String OWNER = ".owner";
152
153         private static final String ADMIN = ".admin";
154
155         private static final String ACCOUNT_ADMINISTRATOR = ".Account_Administrator";
156
157         private static final String FUNCTION_PIPE = "|";
158
159         private static final String IS_NULL_STRING = "null";
160
161         private static final String EXTERNAL_AUTH_PERMS = "perms";
162
163         private static final String EXTERNAL_AUTH_ROLE_DESCRIPTION = "description";
164
165         private static final String IS_EMPTY_JSON_STRING = "{}";
166
167         private static final String CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE = "Connecting to External Auth system";
168
169         private static final String APP_ROLE_ID = "appRoleId";
170
171         private static final String APP_ID = "appId";
172
173         private static final String PRIORITY = "priority";
174
175         private static final String ACTIVE = "active";
176
177         private static final String ROLE_NAME = "name";
178
179         private static final String ID = "id";
180
181         private static final String APP_ID_EQUALS = " app_id = ";
182
183         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
184
185         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
186         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
187         NullPointerException nullPointerException = new NullPointerException();
188         MockEPUser mockUser = new MockEPUser();
189         String uebKey = "test-ueb-key";
190
191         public EPApp mockApp() {
192                 EPApp app = new EPApp();
193                 app.setName("Test");
194                 app.setImageUrl("test");
195                 app.setNameSpace("com.test.app");
196                 app.setCentralAuth(true);
197                 app.setDescription("test");
198                 app.setNotes("test");
199                 app.setUrl("test");
200                 app.setId((long) 10);
201                 app.setAppRestEndpoint("test");
202                 app.setAlternateUrl("test");
203                 app.setName("test");
204                 app.setMlAppName("test");
205                 app.setMlAppAdminId("test");
206                 app.setUsername("test");
207                 app.setAppPassword("test");
208                 app.setOpen(false);
209                 app.setEnabled(true);
210                 app.setUebKey("test");
211                 app.setUebSecret("test");
212                 app.setUebTopicName("test");
213                 app.setAppType(1);
214                 return app;
215         }
216
217         @SuppressWarnings("deprecation")
218         @Test
219         public void getAppRolesIfAppIsPortalTest() throws Exception {
220                 List<EPRole> applicationRoles = new ArrayList<>();
221                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
222                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
223                 assertEquals(expectedApplicationRoles, applicationRoles);
224         }
225
226         @SuppressWarnings("deprecation")
227         @Test
228         public void getAppRolesTest() throws Exception {
229                 List<EPRole> applicationRoles = new ArrayList<>();
230                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
231                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
232                 assertEquals(expectedApplicationRoles, applicationRoles);
233         }
234
235         @SuppressWarnings("deprecation")
236         @Test
237         public void getAppExceptionTest() throws Exception {
238                 List<EPApp> app = new ArrayList<>();
239                 Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
240                                 .thenReturn(app);
241                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
242                 assertEquals(app, expectedapp);
243         }
244
245         @Test(expected = InactiveApplicationException.class)
246         public void getAppErrorTest() throws Exception {
247                 List<EPApp> appList = new ArrayList<>();
248                 EPApp app = mockApp();
249                 app.setEnabled(false);
250                 appList.add(app);
251                 final Map<String, String> appUebkeyParams = new HashMap<>();
252                 appUebkeyParams.put("appKey", "test-ueb-key");
253                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
254                                 .thenReturn(appList);
255                 externalAccessRolesServiceImpl.getApp(uebKey);
256         }
257
258         @Test
259         public void getAppTest() throws Exception {
260                 List<EPApp> appList = new ArrayList<>();
261                 EPApp app = mockApp();
262                 app.setId((long) 1);
263                 appList.add(app);
264                 final Map<String, String> appUebkeyParams = new HashMap<>();
265                 appUebkeyParams.put("appKey", "test-ueb-key");
266                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
267                                 .thenReturn(appList);
268                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
269                 assertEquals(appList, expectedapp);
270         }
271
272         @Test
273         public void addRoleTest() throws Exception {
274                 HttpHeaders headers = new HttpHeaders();
275                 PowerMockito.mockStatic(EcompPortalUtils.class);
276                 PowerMockito.mockStatic(SystemProperties.class);
277                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
278                 String uebKey = "test-ueb-key";
279                 Role role = new Role();
280                 role.setId((long) 25);
281                 EPApp app = mockApp();
282                 app.setEnabled(true);
283                 app.setId((long) 10);
284                 app.setNameSpace("test_namesapce");
285                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
286                 List<EPApp> appList = new ArrayList<>();
287                 appList.add(app);
288                 List<EPRole> roleList = new ArrayList<>();
289                 EPRole ePRole = new EPRole();
290                 role.setName("Test Role");
291                 roleList.add(ePRole);
292                 final Map<String, String> appUebkeyParams = new HashMap<>();
293                 appUebkeyParams.put("appKey", "test-ueb-key");
294                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
295                                 .thenReturn(appList);
296                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
297                 getPartnerAppRoleParams.put("appRoleId", role.getId());
298                 getPartnerAppRoleParams.put("appId", app.getId());
299                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
300                                 .thenReturn(roleList);
301                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
302                                 .thenReturn("Testurl");
303                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
304                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
305                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
306
307                 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
308         }
309
310         @Test
311         public void addRoleMethodNotAllowedTest() throws Exception {
312                 HttpHeaders headers = new HttpHeaders();
313                 PowerMockito.mockStatic(EcompPortalUtils.class);
314                 PowerMockito.mockStatic(SystemProperties.class);
315                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
316                 Role role = new Role();
317                 role.setId((long) 25);
318                 EPApp app = mockApp();
319                 app.setEnabled(true);
320                 app.setId((long) 10);
321                 app.setNameSpace("test_namesapce");
322                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
323                 List<EPApp> appList = new ArrayList<>();
324                 appList.add(app);
325                 List<EPRole> roleList = new ArrayList<>();
326                 EPRole ePRole = new EPRole();
327                 role.setName("Test Role");
328                 roleList.add(ePRole);
329                 final Map<String, String> appUebkeyParams = new HashMap<>();
330                 appUebkeyParams.put("appKey", "test-ueb-key");
331                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
332                                 .thenReturn(appList);
333                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
334                 getPartnerAppRoleParams.put("appRoleId", role.getId());
335                 getPartnerAppRoleParams.put("appId", app.getId());
336                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
337                                 .thenReturn(roleList);
338                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
339                                 .thenReturn("Testurl");
340                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
341                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
342                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
343                 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
344         }
345
346         public EPApp getApp() {
347                 EPApp app = new EPApp();
348                 app.setName("Test");
349                 app.setImageUrl("test");
350                 app.setDescription("test");
351                 app.setNotes("test");
352                 app.setUrl("test");
353                 app.setId((long) 1);
354                 app.setAppRestEndpoint("test");
355                 app.setAlternateUrl("test");
356                 app.setName("test");
357                 app.setMlAppName("test");
358                 app.setMlAppAdminId("test");
359                 app.setUsername("test");
360                 app.setAppPassword("test");
361                 app.setOpen(true);
362                 app.setEnabled(false);
363                 app.setUebKey("test");
364                 app.setUebSecret("test");
365                 app.setUebTopicName("test");
366                 app.setAppType(1);
367                 return app;
368         }
369
370         @Test
371         public void deleteCentralRoleFunctionTest() throws Exception {
372                 final Map<String, String> params = new HashMap<>();
373                 EPApp app = mockApp();
374                 params.put("functionCode", "menu_fun_code");
375                 params.put("appId", String.valueOf(10));
376                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
377                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
378                 domainCentralRoleFunction.setCode("menu_fun_code");
379                 centralRoleFunctionList.add(domainCentralRoleFunction);
380                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null))
381                                 .thenReturn(centralRoleFunctionList);
382                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
383                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
384                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
385                 HttpHeaders headers = new HttpHeaders();
386                 PowerMockito.mockStatic(EcompPortalUtils.class);
387                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
388
389                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class,
390                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
391
392                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
393         }
394
395         @Test
396         public void deleteCentralRoleFunctionFailTest() throws Exception {
397                 final Map<String, String> params = new HashMap<>();
398                 EPApp app = mockApp();
399                 params.put("functionCode", "menu_fun_code");
400                 params.put("appId", String.valueOf(10));
401                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
402                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
403                 domainCentralRoleFunction.setCode("menu_fun_code");
404                 centralRoleFunctionList.add(domainCentralRoleFunction);
405                 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
406                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null))
407                                 .thenReturn(centralRoleFunctionList);
408                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
409                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
410                 HttpHeaders headers = new HttpHeaders();
411                 PowerMockito.mockStatic(EcompPortalUtils.class);
412                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
413                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class,
414                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
415
416                 boolean returnedValue = externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app);
417                 assertTrue(returnedValue);
418         }
419
420         @Test
421         public void deleteCentralRoleFunctionExceptionTest() {
422                 final Map<String, String> params = new HashMap<>();
423                 EPApp app = mockApp();
424                 params.put("functionCd", "menu_fun_code");
425                 params.put("appId", String.valueOf(10));
426                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
427                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
428                 domainCentralRoleFunction.setCode("menu_fun_code");
429                 centralRoleFunctionList.add(domainCentralRoleFunction);
430                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionDetails", params, null))
431                                 .thenThrow(nullPointerException);
432                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
433         }
434
435         @Test
436         public void getUserTest() throws InvalidUserException {
437                 List<EPUser> userList = new ArrayList<>();
438                 EPUser user = mockUser.mockEPUser();
439                 userList.add(user);
440                 final Map<String, String> userParams = new HashMap<>();
441                 userParams.put("org_user_id", "guestT");
442                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
443                                 .thenReturn(userList);
444                 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
445                 assertEquals(expectedUserList, userList);
446         }
447
448         @Test
449         public void saveCentralRoleFunctionNewTestForV2() throws Exception {
450                 PowerMockito.mockStatic(EcompPortalUtils.class);
451                 EPApp app = mockApp();
452                 app.setId((long) 1);
453                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
454                 centralV2RoleFunction.setCode("test_code");
455                 centralV2RoleFunction.setName("test name");
456                 centralV2RoleFunction.setAppId(app.getId());
457                 centralV2RoleFunction.setAction("*");
458                 centralV2RoleFunction.setType("test_type");
459                 final Map<String, String> params = new HashMap<>();
460                 params.put("appId", String.valueOf(1));
461
462                 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
463                 appRoleFunc.add(centralV2RoleFunction);
464                 params.put(FUNCTION_CODE_PARAMS, centralV2RoleFunction.getType() + FUNCTION_PIPE
465                                 + centralV2RoleFunction.getCode() + FUNCTION_PIPE + centralV2RoleFunction.getAction());
466                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)).thenReturn(appRoleFunc);
467                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
468                 HttpHeaders headers = new HttpHeaders();
469                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
470                 JSONObject mockJsonObjectPerm = new JSONObject();
471                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
472                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
473                 mockJsonObjectPerm.put("instance", "com.test.app.test_code");
474                 mockJsonObjectPerm.put("action", "*");
475                 mockJsonObjectPerm.put("description", "test name");
476                 List<JSONObject> mockJson = new ArrayList<>();
477                 mockJson.add(mockJsonObjectPerm);
478                 mockJsonObjectFinalPerm.put("perm", mockJson);
479                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
480                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
481                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
482                 ResponseEntity<String> updateResponse = new ResponseEntity<>(HttpStatus.OK);
483                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
484                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateResponse);
485                 Boolean actual = externalAccessRolesServiceImpl.saveCentralRoleFunction(centralV2RoleFunction, app);
486                 assertEquals(true, actual);
487         }
488
489         @Test
490         public void saveCentralRoleFunctionUpdateForV2Test() throws Exception {
491                 PowerMockito.mockStatic(EcompPortalUtils.class);
492                 EPApp app = mockApp();
493                 app.setId((long) 1);
494                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
495                 centralV2RoleFunction.setCode("test_code");
496                 centralV2RoleFunction.setName("test name2");
497                 centralV2RoleFunction.setAppId(app.getId());
498                 centralV2RoleFunction.setAction("*");
499                 centralV2RoleFunction.setType("test_type");
500                 CentralV2RoleFunction centralV2RoleFunctionExisting = new CentralV2RoleFunction();
501                 centralV2RoleFunctionExisting.setCode("test_code");
502                 centralV2RoleFunctionExisting.setName("test name");
503                 centralV2RoleFunctionExisting.setAppId(app.getId());
504                 centralV2RoleFunctionExisting.setAction("*");
505                 centralV2RoleFunctionExisting.setType("test_type");
506                 final Map<String, String> params = new HashMap<>();
507                 params.put("appId", String.valueOf(1));
508                 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
509                 appRoleFunc.add(centralV2RoleFunctionExisting);
510                 params.put(FUNCTION_CODE_PARAMS, centralV2RoleFunction.getType() + FUNCTION_PIPE
511                                 + centralV2RoleFunction.getCode() + FUNCTION_PIPE + centralV2RoleFunction.getAction());
512                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)).thenReturn(appRoleFunc);
513                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
514                 HttpHeaders headers = new HttpHeaders();
515                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
516                 JSONObject mockJsonObjectPerm = new JSONObject();
517                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
518                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
519                 mockJsonObjectPerm.put("instance", "test_code");
520                 mockJsonObjectPerm.put("action", "*");
521                 mockJsonObjectPerm.put("description", "test name");
522                 List<JSONObject> mockJson = new ArrayList<>();
523                 mockJson.add(mockJsonObjectPerm);
524                 mockJsonObjectFinalPerm.put("perm", mockJson);
525                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
526                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
527                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
528                 ResponseEntity<String> updateResponse = new ResponseEntity<>(HttpStatus.OK);
529                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
530                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateResponse);
531                 Boolean actual = externalAccessRolesServiceImpl.saveCentralRoleFunction(centralV2RoleFunction, app);
532                 assertEquals(true, actual);
533         }
534
535         @SuppressWarnings("deprecation")
536         @Test(expected = IndexOutOfBoundsException.class)
537         public void getAllAppUsersTest() throws Exception {
538                 List<EPApp> expectedapps = new ArrayList<>();
539                 EPApp app = new EPApp();
540                 app.setEnabled(true);
541                 app.setId((long) 10);
542                 expectedapps.add(app);
543                 List<EPRole> applicationRoles = new ArrayList<>();
544                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
545                 Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
546                                 .thenReturn(expectedapps);
547                 final Map<String, Long> appParams = new HashMap<>();
548                 appParams.put("appId", app.getId());
549                 List<EcompUserRoles> userList = new ArrayList<>();
550                 EcompUserRoles ecompUserRoles = new EcompUserRoles();
551                 ecompUserRoles.setOrgUserId("guestT");
552                 ecompUserRoles.setRoleId((long) 1);
553                 ecompUserRoles.setRoleName("test");
554
555                 EcompUserRoles ecompUserRoles2 = new EcompUserRoles();
556                 ecompUserRoles2.setOrgUserId("guestT");
557                 ecompUserRoles2.setRoleId((long) 2);
558                 ecompUserRoles2.setRoleName("test new");
559                 userList.add(ecompUserRoles);
560                 userList.add(ecompUserRoles2);
561
562                 Mockito.when(dataAccessService.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList);
563                 List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(uebKey);
564                 assertEquals(usersfinalList.get(0).getRoles().size(), 2);
565         }
566
567         @Test
568         public void getGlobalRolesOfPortalTest() {
569                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(null);
570                 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), null);
571         }
572
573         @Test
574         public void getGlobalRolesOfPortalExceptionTest() {
575                 List<EPRole> globalRoles = new ArrayList<>();
576                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null))
577                                 .thenThrow(nullPointerException);
578                 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), globalRoles);
579         }
580
581         @Test
582         public void getRolesForAppTest() throws Exception {
583                 EPApp app = mockApp();
584                 app.setId(2l);
585                 List<EPApp> appList = new ArrayList<>();
586                 final Map<String, String> appUebkeyParams = new HashMap<>();
587                 appList.add(app);
588                 appUebkeyParams.put("appKey", app.getUebKey());
589                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
590                                 .thenReturn(appList);
591                 List<EPRole> applicationRoles = new ArrayList<>();
592                 EPRole appRole = new EPRole();
593                 appRole.setActive(true);
594                 appRole.setAppId(app.getId());
595                 appRole.setAppRoleId(100l);
596                 appRole.setId(10l);
597                 appRole.setName("test");
598                 applicationRoles.add(appRole);
599                 final Map<String, Long> appParams = new HashMap<>();
600                 appParams.put("appId", app.getId());
601                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
602                                 .thenReturn(applicationRoles);
603                 List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>();
604                 CentralV2RoleFunction v2RoleFunction = new CentralV2RoleFunction();
605                 v2RoleFunction.setAppId(app.getId());
606                 v2RoleFunction.setCode("test_type|test_code|*");
607                 v2RoleFunction.setName("test name");
608                 cenRoleFuncList.add(v2RoleFunction);
609                 final Map<String, Long> params = new HashMap<>();
610                 params.put("roleId", appRole.getId());
611                 params.put(APP_ID, appList.get(0).getId());
612                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
613                                 .thenReturn(cenRoleFuncList);
614                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
615                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
616                 mockGlobalRole.setActive(true);
617                 mockGlobalRole.setAppId(app.getId());
618                 mockGlobalRole.setRoleId(1111l);
619                 mockGlobalRole.setRoleName("global_test");
620                 mockGlobalRole.setFunctionCd("test_type|test_code|*");
621                 mockGlobalRole.setFunctionName("test name");
622                 mockGlobalRoles.add(mockGlobalRole);
623                 Map<String, Long> params2 = new HashMap<>();
624                 params2.put("appId", app.getId());
625                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleWithApplicationRoleFunctions", params2, null))
626                                 .thenReturn(mockGlobalRoles);
627                 List<EPRole> globalRoles = new ArrayList<>();
628                 EPRole globalRole = new EPRole();
629                 globalRole.setName("global_test");
630                 globalRole.setId(1111l);
631                 globalRole.setActive(true);
632                 globalRoles.add(globalRole);
633                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
634                 List<CentralV2Role> expected = new ArrayList<>();
635                 CentralV2Role cenV2Role = new CentralV2Role();
636                 CentralV2Role cenV2Role2 = new CentralV2Role();
637                 expected.add(cenV2Role);
638                 expected.add(cenV2Role2);
639                 List<CentralV2Role> actual = externalAccessRolesServiceImpl.getRolesForApp(app.getUebKey());
640                 assertEquals(expected.size(), actual.size());
641         }
642
643         @Test
644         public void saveRoleForPortalApplicationNewTest() throws Exception {
645                 PowerMockito.mockStatic(EcompPortalUtils.class);
646                 EPApp app = mockApp();
647                 app.setId(1l);
648                 Role addRoleTest = new Role();
649                 addRoleTest.setActive(true);
650                 addRoleTest.setName("Test");
651                 List<EPApp> appList = new ArrayList<>();
652                 final Map<String, String> appUebkeyParams = new HashMap<>();
653                 appList.add(app);
654                 appUebkeyParams.put("appKey", app.getUebKey());
655                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
656                                 .thenReturn(appList);
657                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
658                 HttpHeaders headers = new HttpHeaders();
659                 JSONObject mockJsonObjectRole = new JSONObject();
660                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
661                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectRole.toString(), HttpStatus.OK);
662                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
663                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
664                 final Map<String, String> epAppPortalRoleParams = new HashMap<>();
665                 epAppPortalRoleParams.put(APP_ROLE_NAME_PARAM, addRoleTest.getName());
666                 List<EPRole> getRoleCreated = new ArrayList<>();
667                 EPRole roleCreate = new EPRole();
668                 roleCreate.setActive(true);
669                 roleCreate.setId(10l);
670                 roleCreate.setName("test");
671                 getRoleCreated.add(roleCreate);
672                 Mockito.when(dataAccessService.executeNamedQuery(GET_PORTAL_APP_ROLES_QUERY, epAppPortalRoleParams, null))
673                                 .thenReturn(getRoleCreated);
674                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
675                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
676                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
677                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
678                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
679                                 app.getUebKey());
680                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
681                 assertEquals(expected, actual);
682         }
683
684         @Test
685         public void saveRoleForPortalApplicationUpdateTest() throws Exception {
686                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
687                 PowerMockito.mockStatic(EcompPortalUtils.class);
688                 PowerMockito.mockStatic(SystemProperties.class);
689                 EPApp app = mockApp();
690                 app.setId(1l);
691                 Role addRoleTest = new Role();
692                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
693                 RoleFunction roleFunc= new RoleFunction();
694                 roleFunc.setName("Test Name");
695                 roleFunc.setCode("test_type|test_instance|*");
696                 RoleFunction roleFunc2 = new RoleFunction();
697                 roleFunc2.setName("Test Name3");
698                 roleFunc2.setCode("test_type3|test_instance3|*");
699                 roleFuncSet.add(roleFunc);
700                 roleFuncSet.add(roleFunc2);
701                 addRoleTest.setActive(true);
702                 addRoleTest.setName("Test2");
703                 addRoleTest.setId(2l);
704                 addRoleTest.setRoleFunctions(roleFuncSet);
705                 List<EPApp> appList = new ArrayList<>();
706                 final Map<String, String> appUebkeyParams = new HashMap<>();
707                 appList.add(app);
708                 appUebkeyParams.put("appKey", app.getUebKey());
709                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
710                                 .thenReturn(appList);
711                 List<EPRole> globalRoles = new ArrayList<>();
712                 EPRole globalRole = new EPRole();
713                 globalRole.setName("global_test");
714                 globalRole.setId(1111l);
715                 globalRole.setActive(true);
716                 globalRoles.add(globalRole);
717                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
718                 List<EPRole> epRoleList = new ArrayList<>();
719                 EPRole getEPRole = new EPRole();
720                 getEPRole.setName("Test");
721                 getEPRole.setId(2l);
722                 getEPRole.setActive(true);
723                 epRoleList.add(getEPRole);
724                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
725                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
726                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
727                                 .thenReturn(epRoleList);
728                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
729                 HttpHeaders headers = new HttpHeaders();
730                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
731                 JSONObject mockJsonObjectRole = new JSONObject();
732                 JSONObject mockJsonObjectFinalRole = new JSONObject();
733                 JSONObject mockJsonObjectPerm1 = new JSONObject();
734                 JSONObject mockJsonObjectPerm2 = new JSONObject();
735                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
736                 mockJsonObjectPerm1.put("instance", "test_instance");
737                 mockJsonObjectPerm1.put("action", "*");
738                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
739                 mockJsonObjectPerm2.put("instance", "test_instance2");
740                 mockJsonObjectPerm2.put("action", "*");
741                 List<JSONObject> permsList =  new ArrayList<>();
742                 permsList.add(mockJsonObjectPerm1);
743                 permsList.add(mockJsonObjectPerm2);
744                 mockJsonObjectRole.put("name", "com.test.app.Test");
745                 mockJsonObjectRole.put("perms", permsList);
746                 mockJsonObjectRole.put("description",
747                                 "{\"id\":\"2\",\"name\":\"Test\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"null\",\"appRoleId\":\"null\"}");
748                 List<JSONObject> roleList = new ArrayList<>();
749                 roleList.add(mockJsonObjectRole);
750                 mockJsonObjectFinalRole.put("role", roleList);
751                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
752                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
753                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
754                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
755                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
756                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
757                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
758                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
759                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
760                 final Map<String, String> params = new HashMap<>();
761                 params.put("uebKey", app.getUebKey());
762                 params.put("roleId", String.valueOf(getEPRole.getId()));
763                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
764                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
765                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
766                 bulkUploadUserRoles.setOrgUserId("guestT");
767                 bulkUploadUserRoles.setRoleName("Test2");
768                 userRolesList.add(bulkUploadUserRoles);
769                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
770                                 .thenReturn(userRolesList);
771                 Mockito.when(
772                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
773                                 .thenReturn(true);
774                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)).thenReturn("@test.com");
775                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
776                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
777                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
778                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>(); 
779                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
780                 appRoleFuncsParams.put("appId", app.getId());
781                 appRoleFuncsParams.put("roleId", getEPRole.getId());
782                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)).thenReturn(appRoleFunctionList);
783                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
784                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
785                 final Map<String, String> getAppFunctionParams = new HashMap<>(); 
786                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
787                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
788                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
789                 CentralV2RoleFunction v2RoleFunction1 =  new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
790                 v2RoleFunction.add(v2RoleFunction1);
791                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)).thenReturn(v2RoleFunction);
792                 final Map<String, String> getAppFunctionParams2 = new HashMap<>(); 
793                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
794                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
795                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
796                 CentralV2RoleFunction v2RoleFunction3 =  new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
797                 v2RoleFunction2.add(v2RoleFunction3);
798                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)).thenReturn(v2RoleFunction2);
799                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
800                                 app.getUebKey());
801                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
802                 assertEquals(expected, actual);
803         }
804         
805         @Test
806         public void saveGlobalRoleFunctionsForPartnerApplicationUpdateTest() throws Exception {
807                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
808                 PowerMockito.mockStatic(EcompPortalUtils.class);
809                 PowerMockito.mockStatic(SystemProperties.class);
810                 EPApp app = mockApp();
811                 app.setId(2l);
812                 Role addRoleTest = new Role();
813                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
814                 RoleFunction roleFunc= new RoleFunction();
815                 roleFunc.setName("Test Name");
816                 roleFunc.setCode("test_type|test_instance|*");
817                 RoleFunction roleFunc2 = new RoleFunction();
818                 roleFunc2.setName("Test Name3");
819                 roleFunc2.setCode("test_type3|test_instance3|*");
820                 roleFuncSet.add(roleFunc);
821                 roleFuncSet.add(roleFunc2);
822                 addRoleTest.setActive(true);
823                 addRoleTest.setName("global_test");
824                 addRoleTest.setId(1111l);
825                 addRoleTest.setRoleFunctions(roleFuncSet);
826                 List<EPApp> appList = new ArrayList<>();
827                 final Map<String, String> appUebkeyParams = new HashMap<>();
828                 appList.add(app);
829                 appUebkeyParams.put("appKey", app.getUebKey());
830                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
831                                 .thenReturn(appList);
832                 List<EPRole> globalRoles = new ArrayList<>();
833                 EPRole globalRole = new EPRole();
834                 globalRole.setName("global_test");
835                 globalRole.setId(1111l);
836                 globalRole.setActive(true);
837                 EPRole globalRole2 = new EPRole();
838                 globalRole2.setName("global_test2");
839                 globalRole2.setId(2222l);
840                 globalRole2.setActive(true);
841                 globalRoles.add(globalRole);
842                 globalRoles.add(globalRole2);
843                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
844                 List<EPRole> getGlobalRoles = new ArrayList<>();
845                 EPRole getEPGlobalRole = new EPRole();
846                 getEPGlobalRole.setName("global_test");
847                 getEPGlobalRole.setId(1111l);
848                 getEPGlobalRole.setActive(true);
849                 getGlobalRoles.add(getEPGlobalRole);
850                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
851                 getPortalAppRoleParams.put("roleId", globalRole.getId());
852                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
853                                 .thenReturn(getGlobalRoles);
854                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
855                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
856                 JSONObject mockJsonObjectPerm = new JSONObject();
857                 JSONObject mockJsonObjectPerm2 = new JSONObject();
858                 JSONObject mockJsonObjectPerm3 = new JSONObject();
859                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
860                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
861                 mockJsonObjectPerm.put("instance", "test_instance");
862                 mockJsonObjectPerm.put("action", "*");
863                 mockJsonObjectPerm.put("description", "Test Name");
864                 mockJsonObjectPerm2.put("type", "com.test.app.access");
865                 mockJsonObjectPerm2.put("instance", "test_instance2");
866                 mockJsonObjectPerm2.put("action", "*");
867                 mockJsonObjectPerm2.put("description", "Test Name2");
868                 mockJsonObjectPerm3.put("type", "com.test.app.test_type3");
869                 mockJsonObjectPerm3.put("instance", "test_instance3");
870                 mockJsonObjectPerm3.put("action", "*");
871                 mockJsonObjectPerm3.put("description", "Test Name3");
872                 List<JSONObject> mockJson = new ArrayList<>();
873                 mockJson.add(mockJsonObjectPerm);
874                 mockJson.add(mockJsonObjectPerm2);
875                 mockJson.add(mockJsonObjectPerm3);
876                 mockJsonObjectFinalPerm.put("perm", mockJson);
877                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
878                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
879                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
880                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
881                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
882                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
883                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
884                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
885                 final Map<String, Long> epAppRoleFuncParams =  new HashMap<>();
886                 epAppRoleFuncParams.put("requestedAppId", app.getId());
887                 epAppRoleFuncParams.put("roleId",globalRole.getId());
888                 List<GlobalRoleWithApplicationRoleFunction> globalRoleFunctionList = new ArrayList<>();
889                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", epAppRoleFuncParams, null)).thenReturn(globalRoleFunctionList);
890                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
891                 appRoleFuncsParams.put("appId", app.getId());
892                 appRoleFuncsParams.put("roleId", globalRole.getId());
893                 List<EPAppRoleFunction> appRoleFunctionList =  new ArrayList<>();
894                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
895                 epAppRoleFunction.setAppId(app.getId());
896                 epAppRoleFunction.setRoleAppId("1");
897                 epAppRoleFunction.setCode("test");
898                 epAppRoleFunction.setRoleId(1111l);
899                 appRoleFunctionList.add(epAppRoleFunction);
900                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)).thenReturn(appRoleFunctionList);
901                 final Map<String, String> getAppFunctionParams = new HashMap<>();
902                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
903                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
904                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
905                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
906                 List<CentralV2RoleFunction> roleFunction = new ArrayList<>();
907                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, roleFunc.getCode(), roleFunc.getName(), app.getId(), null);
908                 roleFunction.add(centralV2RoleFunction);
909                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null)).thenReturn(roleFunction);
910                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
911                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
912                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
913                 List<CentralV2RoleFunction> roleFunction2 = new ArrayList<>();
914                 CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, roleFunc2.getCode(), roleFunc2.getName(), app.getId(), null);
915                 roleFunction2.add(centralV2RoleFunction2);
916                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null)).thenReturn(roleFunction2);
917                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
918                                 app.getUebKey());
919                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
920                 assertEquals(expected, actual);
921         }
922         
923         @Test
924         public void syncRoleFunctionFromExternalAccessSystemTest() {
925                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
926                 PowerMockito.mockStatic(EcompPortalUtils.class);
927                 PowerMockito.mockStatic(SystemProperties.class);
928                 EPApp app = mockApp();
929                 app.setId(2l);
930                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
931                 JSONObject mockJsonObjectPerm = new JSONObject();
932                 JSONObject mockJsonObjectPerm2 = new JSONObject();
933                 JSONObject mockJsonObjectPerm3 = new JSONObject();
934                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
935                 mockJsonObjectPerm.put("instance", "test_instance");
936                 mockJsonObjectPerm.put("action", "*");
937                 mockJsonObjectPerm.put("description", "test_name");
938                 List<String> rolePermList =  new ArrayList<>();
939                 rolePermList.add("com.test.app|test1");
940                 mockJsonObjectPerm.put("roles", rolePermList);
941                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
942                 mockJsonObjectPerm2.put("instance", "test_instance2");
943                 mockJsonObjectPerm2.put("action", "*");
944                 mockJsonObjectPerm2.put("description", "test_name2");
945                 mockJsonObjectPerm3.put("type", "com.test.app.access");
946                 mockJsonObjectPerm3.put("instance", "test_instance3");
947                 mockJsonObjectPerm3.put("action", "*");
948                 mockJsonObjectPerm3.put("description", "test_name3");
949                 List<JSONObject> permsList =  new ArrayList<>();
950                 permsList.add(mockJsonObjectPerm);
951                 permsList.add(mockJsonObjectPerm2);
952                 permsList.add(mockJsonObjectPerm3);
953                 mockJsonObjectFinalPerm.put("perm", permsList);
954                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
955                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
956                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
957                 final Map<String, Long> params = new HashMap<>();
958                 params.put(APP_ID, app.getId());
959                 List<CentralV2RoleFunction> appFunctions = new ArrayList<>();   
960                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*", "test_name", app.getId(), null);
961                 appFunctions.add(centralV2RoleFunction);
962                 Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params,null)).thenReturn(appFunctions);
963                 List<EPRole> globalRoles = new ArrayList<>();
964                 EPRole globalRole = new EPRole();
965                 globalRole.setName("global_test");
966                 globalRole.setId(1111l);
967                 globalRole.setActive(true);
968                 globalRoles.add(globalRole);
969                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
970                 List<EPRole> getCurrentRoleList = new ArrayList<>();
971                 EPRole getEPRole = new EPRole();
972                 getEPRole.setName("test1");
973                 getEPRole.setId(2l);
974                 getEPRole.setActive(true);
975                 EPRole getEPRole2 = new EPRole();
976                 getEPRole2.setName("global_test");
977                 getEPRole2.setId(1111l);
978                 getEPRole2.setActive(true);
979                 getCurrentRoleList.add(getEPRole);
980                 getCurrentRoleList.add(getEPRole2);
981                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRolesList", null, null)).thenReturn(getCurrentRoleList);
982                 final Map<String, String> appSyncFuncsParams = new HashMap<>();
983                 appSyncFuncsParams.put("appId", String.valueOf(app.getId()));
984                 appSyncFuncsParams.put("functionCd", "");
985                 List<CentralV2RoleFunction> roleFunctionList = new ArrayList<>();
986                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams,
987                                 null)).thenReturn(roleFunctionList);
988                 String code = centralV2RoleFunction.getCode();
989                 appSyncFuncsParams.put("functionCd", code);
990                 CentralV2RoleFunction getCentralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*", "test_name", app.getId(), null);
991                 roleFunctionList.add(getCentralV2RoleFunction);
992                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams,
993                                 null)).thenReturn(roleFunctionList);
994                 final Map<String, String> appRoleFuncParams = new HashMap<>();
995                 appRoleFuncParams.put("functionCd", roleFunctionList.get(0).getCode());
996                 appRoleFuncParams.put("appId", String.valueOf(app.getId()));
997                 Mockito.when(dataAccessService.executeNamedQuery("getCurrentAppRoleFunctions",
998                                 appRoleFuncParams, null)).thenReturn(new ArrayList<LocalRole>());
999                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app", app.getNameSpace())).thenReturn(true);
1000                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type2|test_instance2|*")).thenReturn("test_instance2");
1001                 appSyncFuncsParams.put("functionCd", "test_instance2");
1002                 List<CentralV2RoleFunction> roleFunctionList2 = new ArrayList<>();
1003                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams,
1004                                 null)).thenReturn(roleFunctionList2);
1005                 String code2 = "test_type2|test_instance2|*";
1006                 appSyncFuncsParams.put("functionCd", code2);
1007                 CentralV2RoleFunction getCentralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_type2|test_instance2|*", "test_name2", app.getId(), null);
1008                 roleFunctionList2.add(getCentralV2RoleFunction2);
1009                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams,
1010                                 null)).thenReturn(roleFunctionList2);
1011                 externalAccessRolesServiceImpl.syncRoleFunctionFromExternalAccessSystem(app);
1012         }
1013         
1014         @Test
1015         public void syncApplicationRolesWithEcompDBTest(){
1016                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
1017                 PowerMockito.mockStatic(EcompPortalUtils.class);
1018                 PowerMockito.mockStatic(SystemProperties.class);
1019                 EPApp app = mockApp();
1020                 app.setId(2l);
1021                 JSONObject mockJsonObjectRole = new JSONObject();
1022                 JSONObject mockJsonObjectRole2 = new JSONObject();
1023                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1024                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1025                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1026                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1027                 mockJsonObjectPerm1.put("instance", "test_instance");
1028                 mockJsonObjectPerm1.put("action", "*");
1029                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1030                 mockJsonObjectPerm2.put("instance", "test_instance2");
1031                 mockJsonObjectPerm2.put("action", "*");
1032                 List<JSONObject> permsList =  new ArrayList<>();
1033                 permsList.add(mockJsonObjectPerm1);
1034                 permsList.add(mockJsonObjectPerm2);
1035                 mockJsonObjectRole.put("name", "com.test.app.Test");
1036                 mockJsonObjectRole.put("perms", permsList);
1037                 mockJsonObjectRole.put("description",
1038                                 "{\"id\":\"2\",\"name\":\"test1\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"2\"}");
1039                 mockJsonObjectRole2.put("name", "com.test.app.Test2");
1040                 List<JSONObject> permsList2 =  new ArrayList<>();
1041                 permsList2.add(mockJsonObjectPerm1);
1042                 mockJsonObjectRole2.put("perms", permsList2);
1043                 List<JSONObject> roleList = new ArrayList<>();
1044                 roleList.add(mockJsonObjectRole);
1045                 roleList.add(mockJsonObjectRole2);
1046                 mockJsonObjectFinalRole.put("role", roleList);
1047                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1048                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1049                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1050                 List<EPRole> getCurrentRoleList = new ArrayList<>();
1051                 EPRole getEPRole = new EPRole();
1052                 getEPRole.setName("Test");
1053                 getEPRole.setId(2l);
1054                 getEPRole.setAppId(app.getId());
1055                 getEPRole.setAppRoleId(2l);
1056                 getEPRole.setActive(true);
1057                 EPRole getEPRole2 = new EPRole();
1058                 getEPRole2.setName("Test3");
1059                 getEPRole2.setId(3l);
1060                 getEPRole.setAppId(app.getId());
1061                 getEPRole.setAppRoleId(3l);
1062                 getEPRole2.setActive(true);
1063                 getCurrentRoleList.add(getEPRole);
1064                 getCurrentRoleList.add(getEPRole2);
1065                 final Map<String, Long> appParams = new HashMap<>();
1066                 appParams.put("appId", app.getId());
1067                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)).thenReturn(getCurrentRoleList);
1068                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type", app.getNameSpace())).thenReturn(true);
1069                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type2", app.getNameSpace())).thenReturn(true);
1070                 List<EPAppRoleFunction> appRoleFunctions = new ArrayList<>();
1071                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
1072                 epAppRoleFunction.setAppId(app.getId());
1073                 epAppRoleFunction.setCode("test_type|test_instance|*");
1074                 epAppRoleFunction.setRoleId(getEPRole.getId());
1075                 appRoleFunctions.add(epAppRoleFunction);
1076                 final Map<String, Long> appRoleFuncsParams = new  HashMap<>();
1077                 appRoleFuncsParams.put("appId", app.getId());
1078                 appRoleFuncsParams.put("roleId", Long.valueOf(getEPRole.getId()));
1079                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null)).thenReturn(appRoleFunctions);
1080                 List<CentralV2RoleFunction> getV2RoleFunction =  new ArrayList<>();
1081                 final Map<String, String> appFuncsParams = new  HashMap<>();
1082                 appFuncsParams.put("appId", String.valueOf(app.getId()));
1083                 appFuncsParams.put("functionCd", "test_instance2");
1084                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null)).thenReturn(getV2RoleFunction);
1085                 appFuncsParams.put("functionCd", "test_type2|test_instance2|*");
1086                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
1087                 centralV2RoleFunction.setAppId(app.getId());
1088                 centralV2RoleFunction.setCode("test_type2|test_instance2|*");
1089                 centralV2RoleFunction.setName("test_name2");
1090                 getV2RoleFunction.add(centralV2RoleFunction);
1091                 final Map<String, String> extRoleParams = new HashMap<>();
1092                 List<EPRole> roleListDeactivate = new ArrayList<>();
1093                 extRoleParams.put(APP_ROLE_NAME_PARAM, "Test3");
1094                 extRoleParams.put(APP_ID, app.getId().toString());
1095                 EPRole getEPRoleDeactivate = new EPRole();
1096                 getEPRoleDeactivate.setName("Test3");
1097                 getEPRoleDeactivate.setId(3l);
1098                 getEPRoleDeactivate.setAppId(app.getId());
1099                 getEPRoleDeactivate.setAppRoleId(3l);
1100                 roleListDeactivate.add(getEPRoleDeactivate);
1101                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, extRoleParams, null)).thenReturn(roleListDeactivate);
1102                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null)).thenReturn(getV2RoleFunction);
1103                 List<EPRole> updateLocalFromExtAuth = new ArrayList<>();
1104                 updateLocalFromExtAuth.add(getEPRole);
1105                 final Map<String, String> roleParams = new HashMap<>();
1106                 roleParams.put(APP_ROLE_NAME_PARAM, getEPRole.getName());
1107                 roleParams.put(APP_ID, app.getId().toString());
1108                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null)).thenReturn(updateLocalFromExtAuth);
1109                 roleParams.put(APP_ROLE_NAME_PARAM, getEPRole2.getName());
1110                 List<EPRole> updateLocalFromExtAuth2 = new ArrayList<>();
1111                 updateLocalFromExtAuth.add(getEPRole);
1112                 Mockito.when(dataAccessService.executeNamedQuery("getRoletoUpdateInExternalAuthSystem", roleParams, null)).thenReturn(updateLocalFromExtAuth2);
1113                 final Map<String, String> globalRoleParams = new HashMap<>();
1114                 globalRoleParams.put("appId", String.valueOf(app.getId()));
1115                 globalRoleParams.put("appRoleName", "Test2");
1116                 List<EPRole> addNewRoleList = new ArrayList<>();
1117                 EPRole addRoleInLocal = new EPRole();
1118                 addRoleInLocal.setName("Test2");
1119                 addRoleInLocal.setId(4l);
1120                 addRoleInLocal.setAppId(app.getId());
1121                 addRoleInLocal.setActive(true);
1122                 addNewRoleList.add(addRoleInLocal);
1123                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, globalRoleParams, null)).thenReturn(addNewRoleList);
1124                 final Map<String, String> params = new HashMap<>();
1125                 params.put(APP_ROLE_NAME_PARAM, "Test2");
1126                 params.put(APP_ID, app.getId().toString());
1127                 addRoleInLocal.setAppRoleId(4l);
1128                 addNewRoleList.add(addRoleInLocal);
1129                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, params, null)).thenReturn(addNewRoleList);
1130                 externalAccessRolesServiceImpl.syncApplicationRolesWithEcompDB(app);
1131         }
1132         
1133         @Test 
1134         public void deleteDependencyRoleRecord() throws Exception {
1135                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
1136                 PowerMockito.mockStatic(EcompPortalUtils.class);
1137                 PowerMockito.mockStatic(SystemProperties.class);
1138                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
1139                 EPApp app = mockApp();
1140                 app.setId(2l);
1141                 EPUser user = mockUser.mockEPUser();
1142                 List<EPApp> appList = new ArrayList<>();
1143                 final Map<String, String> appUebkeyParams = new HashMap<>();
1144                 appList.add(app);
1145                 appUebkeyParams.put("appKey", app.getUebKey());
1146                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1147                                 .thenReturn(appList);
1148                 List<EPRole> epRoleList = new ArrayList<>();
1149                 EPRole getEPRole = new EPRole();
1150                 getEPRole.setName("Test");
1151                 getEPRole.setId(2l);
1152                 getEPRole.setAppRoleId(2l);
1153                 getEPRole.setActive(true);
1154                 epRoleList.add(getEPRole);
1155                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1156                 getPartnerAppRoleParams.put("appRoleId", getEPRole.getId());
1157                 getPartnerAppRoleParams.put("appId", app.getId());
1158                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
1159                                 .thenReturn(epRoleList);
1160                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1161                 JSONObject getUser =  new JSONObject();
1162                 getUser.put("name", "com.test.app.test1");
1163                 ResponseEntity<String> getResponse = new ResponseEntity<>(getUser.toString(), HttpStatus.OK);
1164                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1165                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1166                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
1167                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1168                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
1169                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
1170                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l, app.getUebKey(), user.getOrgUserId());
1171                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1172                 assertEquals(expected, actual);
1173         }
1174         
1175         @Test 
1176         public void deleteDependencyRoleRecordForPortal() throws Exception {
1177                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
1178                 PowerMockito.mockStatic(EcompPortalUtils.class);
1179                 PowerMockito.mockStatic(SystemProperties.class);
1180                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
1181                 EPApp app = mockApp();
1182                 app.setId(1l);
1183                 EPUser user = mockUser.mockEPUser();
1184                 List<EPApp> appList = new ArrayList<>();
1185                 final Map<String, String> appUebkeyParams = new HashMap<>();
1186                 appList.add(app);
1187                 appUebkeyParams.put("appKey", app.getUebKey());
1188                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1189                                 .thenReturn(appList);
1190                 List<EPRole> epRoleList = new ArrayList<>();
1191                 EPRole getEPRole = new EPRole();
1192                 getEPRole.setName("Test");
1193                 getEPRole.setId(2l);
1194                 getEPRole.setAppRoleId(2l);
1195                 getEPRole.setActive(true);
1196                 epRoleList.add(getEPRole);
1197                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1198                 getPartnerAppRoleParams.put("roleId", getEPRole.getId());
1199                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPartnerAppRoleParams, null))
1200                                 .thenReturn(epRoleList);
1201                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1202                 JSONObject getUser =  new JSONObject();
1203                 getUser.put("name", "com.test.app.test1");
1204                 ResponseEntity<String> getResponse = new ResponseEntity<>(getUser.toString(), HttpStatus.OK);
1205                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1206                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1207                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
1208                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1209                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
1210                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
1211                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l, app.getUebKey(), user.getOrgUserId());
1212                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1213                 assertEquals(expected, actual);
1214         }
1215         
1216         @Test 
1217         public void bulkUploadFunctionsTest() throws Exception {
1218                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
1219                 PowerMockito.mockStatic(EcompPortalUtils.class);
1220                 PowerMockito.mockStatic(SystemProperties.class);
1221                 EPApp app = mockApp();
1222                 app.setId(2l);
1223                 List<EPApp> appList = new ArrayList<>();
1224                 final Map<String, String> appUebkeyParams = new HashMap<>();
1225                 appList.add(app);
1226                 appUebkeyParams.put("appKey", app.getUebKey());
1227                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1228                                 .thenReturn(appList);
1229                 List<RoleFunction> roleFuncList = new ArrayList<>();
1230                 RoleFunction roleFunc = new RoleFunction();
1231                 roleFunc.setCode("test_code");
1232                 roleFunc.setName("test_name");
1233                 RoleFunction roleFunc2 = new RoleFunction();
1234                 roleFunc2.setCode("test_code2");
1235                 roleFunc2.setName("test_name2");
1236                 roleFuncList.add(roleFunc);
1237                 roleFuncList.add(roleFunc2);
1238                 Mockito.when(dataAccessService.executeNamedQuery("getAllFunctions", null, null)).thenReturn(roleFuncList);
1239                 JSONObject perm =  new JSONObject();
1240                 JSONObject permList =  new JSONObject();
1241                 perm.put("type", app.getNameSpace()+".access");
1242                 perm.put("instance", "type_instance");
1243                 perm.put("action", "*");
1244                 List<JSONObject> addPerms =  new ArrayList<>();
1245                 addPerms.add(perm);
1246                 permList.put("perm", addPerms);
1247                 ResponseEntity<String> getResponse = new ResponseEntity<>(permList.toString(), HttpStatus.OK);
1248                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1249                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1250                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
1251                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1252                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
1253                 Integer actual = externalAccessRolesServiceImpl.bulkUploadFunctions(app.getUebKey());
1254                 Integer expected = 2;
1255                 assertEquals(expected, actual);
1256         }
1257         
1258         @Test
1259         public void bulkUploadRolesTest() throws Exception{
1260                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
1261                 PowerMockito.mockStatic(EcompPortalUtils.class);
1262                 PowerMockito.mockStatic(SystemProperties.class);
1263                 EPApp app = mockApp();
1264                 app.setId(2l);
1265                 List<EPApp> appList = new ArrayList<>();
1266                 final Map<String, String> appUebkeyParams = new HashMap<>();
1267                 appList.add(app);
1268                 appUebkeyParams.put("appKey", app.getUebKey());
1269                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1270                                 .thenReturn(appList);
1271                 List<EPRole> epRoleList = new ArrayList<>();
1272                 EPRole getEPRole = new EPRole();
1273                 getEPRole.setName("Test");
1274                 getEPRole.setId(2l);
1275                 getEPRole.setAppRoleId(2l);
1276                 getEPRole.setActive(true);
1277                 EPRole getEPRole2 = new EPRole();
1278                 getEPRole2.setName("Test2");
1279                 getEPRole2.setId(3l);
1280                 getEPRole2.setAppRoleId(3l);
1281                 getEPRole2.setActive(true);
1282                 epRoleList.add(getEPRole);
1283                 epRoleList.add(getEPRole2);
1284                 final Map<String, Long> appParams = new HashMap<>();
1285                 appParams.put("appId", app.getId());
1286                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null)).thenReturn(epRoleList);
1287                 List<EPRole> epRoleList1 = new ArrayList<>();
1288                 EPRole ePRole = new EPRole();
1289                 ePRole.setName("Test");
1290                 ePRole.setId(2l);
1291                 ePRole.setAppRoleId(2l);
1292                 ePRole.setActive(true);
1293                 epRoleList1.add(ePRole);
1294                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1295                 getPartnerAppRoleParams.put("appRoleId", ePRole.getId());
1296                 getPartnerAppRoleParams.put("appId", app.getId());                              
1297                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null)).thenReturn(epRoleList1);
1298                 List<EPRole> epRoleList2 = new ArrayList<>();
1299                 EPRole ePRole2 = new EPRole();
1300                 ePRole2.setName("Test2");
1301                 ePRole2.setId(3l);
1302                 ePRole2.setAppRoleId(3l);
1303                 ePRole2.setActive(true);
1304                 epRoleList2.add(ePRole2);
1305                 final Map<String, Long> getPartnerAppRoleParams2 = new HashMap<>();
1306                 getPartnerAppRoleParams2.put("appRoleId", ePRole2.getId());
1307                 getPartnerAppRoleParams2.put("appId", app.getId());             
1308                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams2, null)).thenReturn(epRoleList2);
1309                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
1310                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1311                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
1312                 Integer actual = externalAccessRolesServiceImpl.bulkUploadRoles(app.getUebKey());
1313                 Integer expected = 2;
1314                 assertEquals(expected, actual);
1315         }
1316         
1317         @Test
1318         public void bulkUploadUserRolesTest() throws Exception{
1319                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
1320                 PowerMockito.mockStatic(EcompPortalUtils.class);
1321                 PowerMockito.mockStatic(SystemProperties.class);
1322                 EPApp app = mockApp();
1323                 app.setId(2l);
1324                 EPUser user = mockUser.mockEPUser();
1325                 List<EPApp> appList = new ArrayList<>();
1326                 final Map<String, String> appUebkeyParams = new HashMap<>();
1327                 appList.add(app);
1328                 appUebkeyParams.put("appKey", app.getUebKey());
1329                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1330                                 .thenReturn(appList);
1331                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1332                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1333                 bulkUploadUserRoles.setAppNameSpace(app.getName());
1334                 bulkUploadUserRoles.setOrgUserId(user.getOrgUserId());
1335                 bulkUploadUserRoles.setRoleName("Test1");
1336                 BulkUploadUserRoles bulkUploadUserRoles2 = new BulkUploadUserRoles();
1337                 bulkUploadUserRoles2.setAppNameSpace(app.getName());
1338                 bulkUploadUserRoles2.setOrgUserId(user.getOrgUserId());
1339                 bulkUploadUserRoles2.setRoleName("Test2");
1340                 userRolesList.add(bulkUploadUserRoles);
1341                 userRolesList.add(bulkUploadUserRoles2);
1342                 final Map<String, String> appParams = new HashMap<>();
1343                 appParams.put("uebKey", app.getUebKey());
1344                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUserRoles", appParams, null)).thenReturn(userRolesList);
1345                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
1346                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1347                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
1348                 Integer actual = externalAccessRolesServiceImpl.bulkUploadUserRoles(app.getUebKey());
1349                 Integer expected = 2;
1350                 assertEquals(expected, actual);
1351         }
1352         
1353         
1354 }