0e59d643416c3ac5e4c60c05ee37f1ce837fbd89
[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  * 
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.assertNotEquals;
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertTrue;
45
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Set;
51 import java.util.SortedSet;
52 import java.util.TreeSet;
53
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56
57 import org.hibernate.SQLQuery;
58 import org.hibernate.Session;
59 import org.hibernate.SessionFactory;
60 import org.hibernate.Transaction;
61 import org.hibernate.criterion.Criterion;
62 import org.hibernate.criterion.Restrictions;
63 import org.json.JSONObject;
64 import org.junit.After;
65 import org.junit.Before;
66 import org.junit.Test;
67 import org.junit.runner.RunWith;
68 import org.mockito.InjectMocks;
69 import org.mockito.Matchers;
70 import org.mockito.Mock;
71 import org.mockito.Mockito;
72 import org.mockito.MockitoAnnotations;
73 import org.onap.portalapp.portal.core.MockEPUser;
74 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
75 import org.onap.portalapp.portal.domain.CentralizedApp;
76 import org.onap.portalapp.portal.domain.EPApp;
77 import org.onap.portalapp.portal.domain.EPAppRoleFunction;
78 import org.onap.portalapp.portal.domain.EPRole;
79 import org.onap.portalapp.portal.domain.EPUser;
80 import org.onap.portalapp.portal.domain.EPUserApp;
81 import org.onap.portalapp.portal.ecomp.model.UploadRoleFunctionExtSystem;
82 import org.onap.portalapp.portal.exceptions.InactiveApplicationException;
83 import org.onap.portalapp.portal.exceptions.InvalidUserException;
84 import org.onap.portalapp.portal.framework.MockitoTestSuite;
85 import org.onap.portalapp.portal.transport.BulkUploadRoleFunction;
86 import org.onap.portalapp.portal.transport.BulkUploadUserRoles;
87 import org.onap.portalapp.portal.transport.CentralRole;
88 import org.onap.portalapp.portal.transport.CentralUser;
89 import org.onap.portalapp.portal.transport.CentralV2Role;
90 import org.onap.portalapp.portal.transport.CentralizedAppRoles;
91 import org.onap.portalapp.portal.transport.EcompUserRoles;
92 import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
93 import org.onap.portalapp.portal.transport.GlobalRoleWithApplicationRoleFunction;
94 import org.onap.portalapp.portal.transport.LocalRole;
95 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
96 import org.onap.portalapp.portal.utils.EcompPortalUtils;
97 import org.onap.portalapp.portal.utils.PortalConstants;
98 import org.onap.portalapp.util.EPUserUtils;
99 import org.onap.portalsdk.core.domain.Role;
100 import org.onap.portalsdk.core.domain.RoleFunction;
101 import org.onap.portalsdk.core.restful.domain.EcompUser;
102 import org.onap.portalsdk.core.service.DataAccessService;
103 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
104 import org.onap.portalsdk.core.util.SystemProperties;
105 import org.powermock.api.mockito.PowerMockito;
106 import org.powermock.core.classloader.annotations.PrepareForTest;
107 import org.powermock.modules.junit4.PowerMockRunner;
108 import org.springframework.http.HttpEntity;
109 import org.springframework.http.HttpHeaders;
110 import org.springframework.http.HttpMethod;
111 import org.springframework.http.HttpStatus;
112 import org.springframework.http.ResponseEntity;
113 import org.springframework.web.client.HttpClientErrorException;
114 import org.springframework.web.client.RestTemplate;
115
116 import com.fasterxml.jackson.databind.ObjectMapper;
117
118 @RunWith(PowerMockRunner.class)
119 @PrepareForTest({ EcompPortalUtils.class, Criterion.class, EPUserUtils.class, Restrictions.class, SystemProperties.class,
120                 EPCommonSystemProperties.class })
121 public class ExternalAccessRolesServiceImplTest {
122         @Mock
123         DataAccessService dataAccessService = new DataAccessServiceImpl();
124
125         @Mock
126         RestTemplate template = new RestTemplate();
127
128         @InjectMocks
129         ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
130
131         @Mock
132         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
133
134         @Mock
135         SessionFactory sessionFactory;
136
137         @Mock
138         Session session;
139
140         @Mock
141         Transaction transaction;
142
143         @Before
144         public void setup() {
145                 MockitoAnnotations.initMocks(this);
146                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
147                 PowerMockito.mockStatic(PortalConstants.class);
148                 PowerMockito.mockStatic(EcompPortalUtils.class);
149                 PowerMockito.mockStatic(SystemProperties.class);
150                 PowerMockito.mockStatic(EPUserUtils.class);
151                 PowerMockito.mockStatic(Restrictions.class);
152                 PowerMockito.mockStatic(Criterion.class);
153                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
154                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
155         }
156
157         @After
158         public void after() {
159                 session.close();
160         }
161
162         private static final String APP_ROLE_NAME_PARAM = "appRoleName";
163
164         private static final String GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM = "getRoletoUpdateInExternalAuthSystem";
165
166         private static final String GET_PORTAL_APP_ROLES_QUERY = "getPortalAppRoles";
167
168         private static final String GET_ROLE_FUNCTION_QUERY = "getRoleFunction";
169
170         private static final String FUNCTION_CODE_PARAMS = "functionCode";
171
172         private static final String FUNCTION_PIPE = "|";
173
174         private static final String APP_ID = "appId";
175
176         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
177
178         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
179         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
180         NullPointerException nullPointerException = new NullPointerException();
181         MockEPUser mockUser = new MockEPUser();
182         String uebKey = "test-ueb-key";
183
184         public EPApp mockApp() {
185                 EPApp app = new EPApp();
186                 app.setName("Test");
187                 app.setImageUrl("test");
188                 app.setNameSpace("com.test.app");
189                 app.setCentralAuth(true);
190                 app.setDescription("test");
191                 app.setNotes("test");
192                 app.setUrl("test");
193                 app.setId((long) 10);
194                 app.setAppRestEndpoint("test");
195                 app.setAlternateUrl("test");
196                 app.setName("test");
197                 app.setMlAppName("test");
198                 app.setMlAppAdminId("test");
199                 app.setUsername("test");
200                 app.setAppPassword("test");
201                 app.setOpen(false);
202                 app.setEnabled(true);
203                 app.setUebKey("test");
204                 app.setUebSecret("test");
205                 app.setUebTopicName("test");
206                 app.setAppType(1);
207                 return app;
208         }
209
210         @SuppressWarnings("deprecation")
211         @Test
212         public void getAppRolesIfAppIsPortalTest() throws Exception {
213                 List<EPRole> applicationRoles = new ArrayList<>();
214                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
215                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
216                 assertEquals(expectedApplicationRoles, applicationRoles);
217         }
218
219         @SuppressWarnings("deprecation")
220         @Test
221         public void getAppRolesTest() throws Exception {
222                 List<EPRole> applicationRoles = new ArrayList<>();
223                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
224                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
225                 assertEquals(expectedApplicationRoles, applicationRoles);
226         }
227
228         @SuppressWarnings("deprecation")
229         @Test
230         public void getAppExceptionTest() throws Exception {
231                 List<EPApp> app = new ArrayList<>();
232                 Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
233                                 .thenReturn(app);
234                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
235                 assertEquals(app, expectedapp);
236         }
237
238         @Test(expected = InactiveApplicationException.class)
239         public void getAppErrorTest() throws Exception {
240                 List<EPApp> appList = new ArrayList<>();
241                 EPApp app = mockApp();
242                 app.setEnabled(false);
243                 appList.add(app);
244                 final Map<String, String> appUebkeyParams = new HashMap<>();
245                 appUebkeyParams.put("appKey", "test-ueb-key");
246                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
247                                 .thenReturn(appList);
248                 externalAccessRolesServiceImpl.getApp(uebKey);
249         }
250
251         @Test
252         public void getAppTest() throws Exception {
253                 List<EPApp> appList = new ArrayList<>();
254                 EPApp app = mockApp();
255                 app.setId((long) 1);
256                 appList.add(app);
257                 final Map<String, String> appUebkeyParams = new HashMap<>();
258                 appUebkeyParams.put("appKey", "test-ueb-key");
259                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
260                                 .thenReturn(appList);
261                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
262                 assertEquals(appList, expectedapp);
263         }
264
265         @Test
266         public void addRoleTest() throws Exception {
267                 HttpHeaders headers = new HttpHeaders();
268                 String uebKey = "test-ueb-key";
269                 Role role = new Role();
270                 role.setId((long) 25);
271                 EPApp app = mockApp();
272                 app.setEnabled(true);
273                 app.setId((long) 10);
274                 app.setNameSpace("test_namesapce");
275                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
276                 List<EPApp> appList = new ArrayList<>();
277                 appList.add(app);
278                 List<EPRole> roleList = new ArrayList<>();
279                 EPRole ePRole = new EPRole();
280                 role.setName("Test Role");
281                 roleList.add(ePRole);
282                 final Map<String, String> appUebkeyParams = new HashMap<>();
283                 appUebkeyParams.put("appKey", "test-ueb-key");
284                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
285                                 .thenReturn(appList);
286                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
287                 getPartnerAppRoleParams.put("appRoleId", role.getId());
288                 getPartnerAppRoleParams.put("appId", app.getId());
289                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
290                                 .thenReturn(roleList);
291                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
292                                 .thenReturn("Testurl");
293                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
294                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
295                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
296
297                 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
298         }
299
300         @Test
301         public void addRoleMethodNotAllowedTest() throws Exception {
302                 HttpHeaders headers = new HttpHeaders();
303                 Role role = new Role();
304                 role.setId((long) 25);
305                 EPApp app = mockApp();
306                 app.setEnabled(true);
307                 app.setId((long) 10);
308                 app.setNameSpace("test_namesapce");
309                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
310                 List<EPApp> appList = new ArrayList<>();
311                 appList.add(app);
312                 List<EPRole> roleList = new ArrayList<>();
313                 EPRole ePRole = new EPRole();
314                 role.setName("Test Role");
315                 roleList.add(ePRole);
316                 final Map<String, String> appUebkeyParams = new HashMap<>();
317                 appUebkeyParams.put("appKey", "test-ueb-key");
318                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
319                                 .thenReturn(appList);
320                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
321                 getPartnerAppRoleParams.put("appRoleId", role.getId());
322                 getPartnerAppRoleParams.put("appId", app.getId());
323                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
324                                 .thenReturn(roleList);
325                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
326                                 .thenReturn("Testurl");
327                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
328                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
329                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
330                 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
331         }
332
333         public EPApp getApp() {
334                 EPApp app = new EPApp();
335                 app.setName("Test");
336                 app.setImageUrl("test");
337                 app.setDescription("test");
338                 app.setNotes("test");
339                 app.setUrl("test");
340                 app.setId((long) 1);
341                 app.setAppRestEndpoint("test");
342                 app.setAlternateUrl("test");
343                 app.setName("test");
344                 app.setMlAppName("test");
345                 app.setMlAppAdminId("test");
346                 app.setUsername("test");
347                 app.setAppPassword("test");
348                 app.setOpen(true);
349                 app.setEnabled(false);
350                 app.setUebKey("test");
351                 app.setUebSecret("test");
352                 app.setUebTopicName("test");
353                 app.setAppType(1);
354                 return app;
355         }
356
357         @Test
358         public void deleteCentralRoleFunctionTest() throws Exception {
359                 final Map<String, String> params = new HashMap<>();
360                 EPApp app = mockApp();
361                 params.put("functionCode", "menu_fun_code");
362                 params.put("appId", String.valueOf(10));
363                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
364                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
365                 domainCentralRoleFunction.setCode("menu_fun_code");
366                 centralRoleFunctionList.add(domainCentralRoleFunction);
367                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null))
368                                 .thenReturn(centralRoleFunctionList);
369                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
370                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
371                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
372                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
373                 HttpHeaders headers = new HttpHeaders();
374                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
375                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class,
376                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
377                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
378         }
379
380         @Test
381         public void deleteRoleForApplicationTest() throws Exception {
382                 EPApp app = mockApp();
383                 app.setId(2l);
384                 List<EPApp> appList = new ArrayList<>();
385                 appList.add(app);
386                 final Map<String, String> appUebkeyParams = new HashMap<>();
387                 appUebkeyParams.put("appKey", app.getUebKey());
388                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
389                                 .thenReturn(appList);
390                 final Map<String, String> deleteRoleParams = new HashMap<>();
391                 deleteRoleParams.put(APP_ROLE_NAME_PARAM, "test_delete");
392                 deleteRoleParams.put(APP_ID, String.valueOf(app.getId()));
393                 List<EPRole> epRoleList = new ArrayList<>();
394                 EPRole epRole = new EPRole();
395                 epRole.setName("test_delete");
396                 epRole.setId(1l);
397                 epRole.setActive(true);
398                 epRole.setAppRoleId(11l);
399                 epRoleList.add(epRole);
400                 Mockito.when(
401                                 dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, deleteRoleParams, null))
402                                 .thenReturn(epRoleList);
403                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
404                 appRoleFuncsParams.put("appId", app.getId());
405                 appRoleFuncsParams.put("roleId", epRole.getId());
406                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
407                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
408                 epAppRoleFunction.setCode("test_code");
409                 epAppRoleFunction.setAppId(app.getId());
410                 epAppRoleFunction.setRoleAppId(null);
411                 appRoleFunctionList.add(epAppRoleFunction);
412                 Mockito.when(
413                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
414                                 .thenReturn(appRoleFunctionList);
415                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
416                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
417                 PowerMockito.mockStatic(EcompPortalUtils.class);
418                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
419                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
420                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
421                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
422                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
423                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
424                 assertTrue(externalAccessRolesServiceImpl.deleteRoleForApplication(epRole.getName(), app.getUebKey()));
425         }
426
427         @Test
428         public void deleteRoleForPortalApplicationTest() throws Exception {
429                 EPApp app = mockApp();
430                 app.setId(1l);
431                 List<EPApp> appList = new ArrayList<>();
432                 appList.add(app);
433                 final Map<String, String> appUebkeyParams = new HashMap<>();
434                 appUebkeyParams.put("appKey", app.getUebKey());
435                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
436                                 .thenReturn(appList);
437                 final Map<String, String> deleteRoleParams2 = new HashMap<>();
438                 deleteRoleParams2.put(APP_ROLE_NAME_PARAM, "test_delete");
439                 List<EPRole> epRoleList2 = new ArrayList<>();
440                 EPRole epRole = new EPRole();
441                 epRole.setName("test_delete");
442                 epRole.setId(1l);
443                 epRole.setActive(true);
444                 epRoleList2.add(epRole);
445                 Mockito.when(dataAccessService.executeNamedQuery(GET_PORTAL_APP_ROLES_QUERY, deleteRoleParams2, null))
446                                 .thenReturn(epRoleList2);
447                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
448                 appRoleFuncsParams.put("appId", app.getId());
449                 appRoleFuncsParams.put("roleId", epRole.getId());
450                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
451                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
452                 epAppRoleFunction.setCode("test_code");
453                 epAppRoleFunction.setAppId(app.getId());
454                 epAppRoleFunction.setRoleAppId(null);
455                 appRoleFunctionList.add(epAppRoleFunction);
456                 Mockito.when(
457                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
458                                 .thenReturn(appRoleFunctionList);
459                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
460                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
461                 PowerMockito.mockStatic(EcompPortalUtils.class);
462                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
463                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
464                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
465                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
466                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
467                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
468                 assertTrue(externalAccessRolesServiceImpl.deleteRoleForApplication(epRole.getName(), app.getUebKey()));
469         }
470
471         @Test
472         public void deleteCentralRoleFunctionFailTest() throws Exception {
473                 final Map<String, String> params = new HashMap<>();
474                 EPApp app = mockApp();
475                 params.put("functionCode", "menu_fun_code");
476                 params.put("appId", String.valueOf(10));
477                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
478                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
479                 domainCentralRoleFunction.setCode("menu_fun_code");
480                 centralRoleFunctionList.add(domainCentralRoleFunction);
481                 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
482                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null))
483                                 .thenReturn(centralRoleFunctionList);
484                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
485                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
486                 HttpHeaders headers = new HttpHeaders();
487                 PowerMockito.mockStatic(EcompPortalUtils.class);
488                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
489                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class,
490                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
491                 boolean returnedValue = externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app);
492                 assertTrue(returnedValue);
493         }
494
495         @Test
496         public void deleteCentralRoleFunctionExceptionTest() {
497                 final Map<String, String> params = new HashMap<>();
498                 EPApp app = mockApp();
499                 params.put("functionCd", "menu_fun_code");
500                 params.put("appId", String.valueOf(10));
501                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
502                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
503                 domainCentralRoleFunction.setCode("menu_fun_code");
504                 centralRoleFunctionList.add(domainCentralRoleFunction);
505                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionDetails", params, null))
506                                 .thenThrow(nullPointerException);
507                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
508         }
509
510         @Test
511         public void getRoleFunctionTest() throws Exception {
512                 EPApp app = mockApp();
513                 List<EPApp> appList = new ArrayList<>();
514                 appList.add(app);
515                 final Map<String, String> appUebkeyParams = new HashMap<>();
516                 appUebkeyParams.put("appKey", app.getUebKey());
517                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
518                                 .thenReturn(appList);
519                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type|type_code|*")).thenReturn("type_code");
520                 Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type");
521                 Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*");
522                 List<CentralV2RoleFunction> getRoleFuncList = new ArrayList<>();
523                 CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
524                 getRoleFuncList.add(getCenRole);
525                 final Map<String, String> params = new HashMap<>();
526                 params.put(FUNCTION_CODE_PARAMS, "test_type|type_code|*");
527                 params.put(APP_ID, String.valueOf(app.getId()));
528                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null))
529                                 .thenReturn(getRoleFuncList);
530                 CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*",
531                                 app.getUebKey());
532                 assertEquals("type_code", actual.getCode());
533         }
534
535         @Test
536         public void getRoleFunctionMutilpleFilterTest() throws Exception {
537                 EPApp app = mockApp();
538                 List<EPApp> appList = new ArrayList<>();
539                 appList.add(app);
540                 final Map<String, String> appUebkeyParams = new HashMap<>();
541                 appUebkeyParams.put("appKey", app.getUebKey());
542                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
543                                 .thenReturn(appList);
544                 CentralV2RoleFunction expected = new CentralV2RoleFunction(null, "type_code", "test_name", null, "test_type",
545                                 "*", null);
546                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type|type_code|*")).thenReturn("type_code");
547                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type_1|type_code_1|*")).thenReturn("type_code_1");
548                 Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type");
549                 Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*");
550                 Mockito.when(EcompPortalUtils.encodeFunctionCode("type_code")).thenReturn("type_code");
551                 List<CentralV2RoleFunction> getRoleFuncList = new ArrayList<>();
552                 CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
553                 CentralV2RoleFunction getCenRole2 = new CentralV2RoleFunction("test_type_1|type_code_1|*", "test_name_1");
554                 getRoleFuncList.add(getCenRole);
555                 getRoleFuncList.add(getCenRole2);
556                 final Map<String, String> params = new HashMap<>();
557                 params.put(FUNCTION_CODE_PARAMS, "test_type|type_code|*");
558                 params.put(APP_ID, String.valueOf(app.getId()));
559                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null))
560                                 .thenReturn(getRoleFuncList);
561                 CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*",
562                                 app.getUebKey());
563                 assertEquals(expected.getCode(), actual.getCode());
564         }
565
566         @Test
567         public void getUserTest() throws InvalidUserException {
568                 List<EPUser> userList = new ArrayList<>();
569                 EPUser user = mockUser.mockEPUser();
570                 userList.add(user);
571                 final Map<String, String> userParams = new HashMap<>();
572                 userParams.put("org_user_id", "guestT");
573                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
574                                 .thenReturn(userList);
575                 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
576                 assertEquals(expectedUserList, userList);
577         }
578
579         @Test
580         public void getV2UserWithRolesTest() throws Exception {
581                 EPApp app = mockApp();
582                 app.setId(2l);
583                 EPUser user = mockUser.mockEPUser();
584                 List<EPApp> appList = new ArrayList<>();
585                 appList.add(app);
586                 final Map<String, String> appUebkeyParams = new HashMap<>();
587                 appUebkeyParams.put("appKey", app.getUebKey());
588                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
589                                 .thenReturn(appList);
590                 final Map<String, String> userParams = new HashMap<>();
591                 userParams.put("org_user_id", user.getOrgUserId());
592                 List<EPUser> userList = new ArrayList<>();
593                 Set<EPUserApp> userAppSet = user.getEPUserApps();
594                 EPUserApp epUserApp = new EPUserApp();
595                 EPRole epRole = new EPRole();
596                 epRole.setName("test");
597                 epRole.setId(1l);
598                 epRole.setActive(true);
599                 epRole.setAppRoleId(11l);
600                 epUserApp.setApp(app);
601                 epUserApp.setUserId(user.getId());
602                 epUserApp.setRole(epRole);
603                 userAppSet.add(epUserApp);
604                 user.setUserApps(userAppSet);
605                 userList.add(user);
606                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
607                                 .thenReturn(userList);
608                 final Map<String, Long> params = new HashMap<>();
609                 List<CentralV2RoleFunction> appRoleFunctionList = new ArrayList<>();
610                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
611                 centralV2RoleFunction.setCode("test_type|test_code|*");
612                 centralV2RoleFunction.setName("test name");
613                 centralV2RoleFunction.setAppId(app.getId());
614                 appRoleFunctionList.add(centralV2RoleFunction);
615                 params.put("roleId", epUserApp.getRole().getId());
616                 params.put(APP_ID, epUserApp.getApp().getId());
617                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
618                                 .thenReturn(appRoleFunctionList);
619                 String actual = externalAccessRolesServiceImpl.getV2UserWithRoles(user.getOrgUserId(), app.getUebKey());
620                 String notExpected = "";
621                 assertNotEquals(actual, notExpected);
622         }
623
624         @Test
625         public void saveCentralRoleFunctionNewTestForV2() throws Exception {
626                 PowerMockito.mockStatic(EcompPortalUtils.class);
627                 EPApp app = mockApp();
628                 app.setId((long) 1);
629                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
630                 centralV2RoleFunction.setCode("test_code");
631                 centralV2RoleFunction.setName("test name");
632                 centralV2RoleFunction.setAppId(app.getId());
633                 centralV2RoleFunction.setAction("*");
634                 centralV2RoleFunction.setType("test_type");
635                 final Map<String, String> params = new HashMap<>();
636                 params.put("appId", String.valueOf(1));
637
638                 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
639                 appRoleFunc.add(centralV2RoleFunction);
640                 params.put(FUNCTION_CODE_PARAMS, centralV2RoleFunction.getType() + FUNCTION_PIPE
641                                 + centralV2RoleFunction.getCode() + FUNCTION_PIPE + centralV2RoleFunction.getAction());
642                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)).thenReturn(appRoleFunc);
643                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
644                 HttpHeaders headers = new HttpHeaders();
645                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
646                 JSONObject mockJsonObjectPerm = new JSONObject();
647                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
648                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
649                 mockJsonObjectPerm.put("instance", "com.test.app.test_code");
650                 mockJsonObjectPerm.put("action", "*");
651                 mockJsonObjectPerm.put("description", "test name");
652                 List<JSONObject> mockJson = new ArrayList<>();
653                 mockJson.add(mockJsonObjectPerm);
654                 mockJsonObjectFinalPerm.put("perm", mockJson);
655                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
656                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
657                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
658                 ResponseEntity<String> updateResponse = new ResponseEntity<>(HttpStatus.OK);
659                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
660                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateResponse);
661                 Boolean actual = externalAccessRolesServiceImpl.saveCentralRoleFunction(centralV2RoleFunction, app);
662                 assertEquals(true, actual);
663         }
664
665         @Test
666         public void saveCentralRoleFunctionUpdateForV2Test() throws Exception {
667                 PowerMockito.mockStatic(EcompPortalUtils.class);
668                 EPApp app = mockApp();
669                 app.setId((long) 1);
670                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
671                 centralV2RoleFunction.setCode("test_code");
672                 centralV2RoleFunction.setName("test name2");
673                 centralV2RoleFunction.setAppId(app.getId());
674                 centralV2RoleFunction.setAction("*");
675                 centralV2RoleFunction.setType("test_type");
676                 CentralV2RoleFunction centralV2RoleFunctionExisting = new CentralV2RoleFunction();
677                 centralV2RoleFunctionExisting.setCode("test_code");
678                 centralV2RoleFunctionExisting.setName("test name");
679                 centralV2RoleFunctionExisting.setAppId(app.getId());
680                 centralV2RoleFunctionExisting.setAction("*");
681                 centralV2RoleFunctionExisting.setType("test_type");
682                 final Map<String, String> params = new HashMap<>();
683                 params.put("appId", String.valueOf(1));
684                 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
685                 appRoleFunc.add(centralV2RoleFunctionExisting);
686                 params.put(FUNCTION_CODE_PARAMS, centralV2RoleFunction.getType() + FUNCTION_PIPE
687                                 + centralV2RoleFunction.getCode() + FUNCTION_PIPE + centralV2RoleFunction.getAction());
688                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)).thenReturn(appRoleFunc);
689                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
690                 HttpHeaders headers = new HttpHeaders();
691                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
692                 JSONObject mockJsonObjectPerm = new JSONObject();
693                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
694                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
695                 mockJsonObjectPerm.put("instance", "test_code");
696                 mockJsonObjectPerm.put("action", "*");
697                 mockJsonObjectPerm.put("description", "test name");
698                 List<JSONObject> mockJson = new ArrayList<>();
699                 mockJson.add(mockJsonObjectPerm);
700                 mockJsonObjectFinalPerm.put("perm", mockJson);
701                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
702                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
703                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
704                 ResponseEntity<String> updateResponse = new ResponseEntity<>(HttpStatus.OK);
705                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
706                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateResponse);
707                 Boolean actual = externalAccessRolesServiceImpl.saveCentralRoleFunction(centralV2RoleFunction, app);
708                 assertEquals(true, actual);
709         }
710
711         @SuppressWarnings("deprecation")
712         @Test
713         public void getAllAppUsersTest() throws Exception {
714                 EPApp app = new EPApp();
715                 app.setEnabled(true);
716                 app.setId((long) 10);
717                 List<EPApp> appList = new ArrayList<>();
718                 final Map<String, String> appUebkeyParams = new HashMap<>();
719                 appList.add(app);
720                 appUebkeyParams.put("appKey", app.getUebKey());
721                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
722                                 .thenReturn(appList);
723                 List<EPRole> applicationRoles = new ArrayList<>();
724                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
725                 Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
726                                 .thenReturn(appList);
727                 final Map<String, Long> appParams = new HashMap<>();
728                 appParams.put("appId", app.getId());
729                 List<EcompUserRoles> userList = new ArrayList<>();
730                 EcompUserRoles ecompUserRoles = new EcompUserRoles();
731                 ecompUserRoles.setOrgUserId("guestT");
732                 ecompUserRoles.setRoleId((long) 1);
733                 ecompUserRoles.setRoleName("test");
734                 ecompUserRoles.setFunctionCode("test_type|test_instance|test_action");
735                 ecompUserRoles.setFunctionName("test1");
736                 EcompUserRoles ecompUserRoles2 = new EcompUserRoles();
737                 ecompUserRoles2.setOrgUserId("guestT");
738                 ecompUserRoles2.setRoleId((long) 2);
739                 ecompUserRoles2.setRoleName("test new");
740                 ecompUserRoles2.setFunctionCode("test_instance2");
741                 ecompUserRoles2.setFunctionName("test2");
742                 userList.add(ecompUserRoles);
743                 userList.add(ecompUserRoles2);
744                 Mockito.when(EcompPortalUtils.getFunctionCode(ecompUserRoles.getFunctionCode())).thenReturn("test_instance");
745         Mockito.when(EPUserUtils.decodeFunctionCode("test_instance")).thenReturn("test_instance");
746         Mockito.when(EcompPortalUtils.getFunctionCode(ecompUserRoles2.getFunctionCode())).thenReturn("test_instance2");
747         Mockito.when(EPUserUtils.decodeFunctionCode("test_instance2")).thenReturn("test_instance2");
748         Mockito.when(EcompPortalUtils.getFunctionType("test_type|test_instance|test_action")).thenReturn("test_type");
749         Mockito.when(EcompPortalUtils.getFunctionAction("test_type|test_instance|test_action")).thenReturn("test_action");
750         Mockito.when(dataAccessService.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList);
751                 List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(app.getUebKey());
752                 assertEquals(usersfinalList.get(0).getRoles().size(), 2);
753         }
754
755         @Test
756         public void getGlobalRolesOfPortalTest() {
757                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(null);
758                 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), null);
759         }
760
761         @Test
762         public void getGlobalRolesOfPortalExceptionTest() {
763                 List<EPRole> globalRoles = new ArrayList<>();
764                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null))
765                                 .thenThrow(nullPointerException);
766                 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), globalRoles);
767         }
768
769         @Test
770         public void getRolesForAppTest() throws Exception {
771                 EPApp app = mockApp();
772                 app.setId(2l);
773                 List<EPApp> appList = new ArrayList<>();
774                 final Map<String, String> appUebkeyParams = new HashMap<>();
775                 appList.add(app);
776                 appUebkeyParams.put("appKey", app.getUebKey());
777                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
778                                 .thenReturn(appList);
779                 List<EPRole> applicationRoles = new ArrayList<>();
780                 EPRole appRole = new EPRole();
781                 appRole.setActive(true);
782                 appRole.setAppId(app.getId());
783                 appRole.setAppRoleId(100l);
784                 appRole.setId(10l);
785                 appRole.setName("test");
786                 applicationRoles.add(appRole);
787                 final Map<String, Long> appParams = new HashMap<>();
788                 appParams.put("appId", app.getId());
789                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
790                                 .thenReturn(applicationRoles);
791                 List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>();
792                 CentralV2RoleFunction v2RoleFunction = new CentralV2RoleFunction();
793                 v2RoleFunction.setAppId(app.getId());
794                 v2RoleFunction.setCode("test_type|test_code|*");
795                 v2RoleFunction.setName("test name");
796                 cenRoleFuncList.add(v2RoleFunction);
797                 final Map<String, Long> params = new HashMap<>();
798                 params.put("roleId", appRole.getId());
799                 params.put(APP_ID, appList.get(0).getId());
800                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
801                                 .thenReturn(cenRoleFuncList);
802                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
803                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
804                 mockGlobalRole.setActive(true);
805                 mockGlobalRole.setAppId(app.getId());
806                 mockGlobalRole.setRoleId(1111l);
807                 mockGlobalRole.setRoleName("global_test");
808                 mockGlobalRole.setFunctionCd("test_type|test_code|*");
809                 mockGlobalRole.setFunctionName("test name");
810                 mockGlobalRoles.add(mockGlobalRole);
811                 Map<String, Long> params2 = new HashMap<>();
812                 params2.put("appId", app.getId());
813                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleWithApplicationRoleFunctions", params2, null))
814                                 .thenReturn(mockGlobalRoles);
815                 List<EPRole> globalRoles = new ArrayList<>();
816                 EPRole globalRole = new EPRole();
817                 globalRole.setName("global_test");
818                 globalRole.setId(1111l);
819                 globalRole.setActive(true);
820                 globalRoles.add(globalRole);
821                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
822                 List<CentralV2Role> expected = new ArrayList<>();
823                 CentralV2Role cenV2Role = new CentralV2Role();
824                 CentralV2Role cenV2Role2 = new CentralV2Role();
825                 expected.add(cenV2Role);
826                 expected.add(cenV2Role2);
827                 List<CentralV2Role> actual = externalAccessRolesServiceImpl.getRolesForApp(app.getUebKey());
828                 assertEquals(expected.size(), actual.size());
829         }
830
831         @Test
832         public void getRoleFuncListTest() throws Exception {
833                 EPApp app = mockApp();
834                 List<EPApp> appList = new ArrayList<>();
835                 final Map<String, String> appUebkeyParams = new HashMap<>();
836                 appList.add(app);
837                 appUebkeyParams.put("appKey", app.getUebKey());
838                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
839                                 .thenReturn(appList);
840                 final Map<String, Long> params = new HashMap<>();
841                 params.put(APP_ID, app.getId());
842                 List<CentralV2RoleFunction> expected = new ArrayList<>();
843                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
844                 expected.add(centralV2RoleFunction);
845                 Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null)).thenReturn(expected);
846                 List<CentralV2RoleFunction> actual = externalAccessRolesServiceImpl.getRoleFuncList(app.getUebKey());
847                 assertEquals(expected, actual);
848         }
849
850         @Test
851         public void getRoleInfoTest() throws Exception {
852                 EPApp app = mockApp();
853                 app.setId(2l);
854                 List<EPApp> appList = new ArrayList<>();
855                 final Map<String, String> appUebkeyParams = new HashMap<>();
856                 appList.add(app);
857                 appUebkeyParams.put("appKey", app.getUebKey());
858                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
859                                 .thenReturn(appList);
860                 List<EPRole> globalRoles = new ArrayList<>();
861                 EPRole globalRole = new EPRole();
862                 globalRole.setName("global_test");
863                 globalRole.setId(2l);
864                 globalRole.setActive(true);
865                 globalRoles.add(globalRole);
866                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
867                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
868                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
869                 mockGlobalRole.setActive(true);
870                 mockGlobalRole.setAppId(app.getId());
871                 mockGlobalRole.setRoleId(2l);
872                 mockGlobalRole.setRoleName("global_test");
873                 mockGlobalRole.setFunctionCd("test_type|test_code|*");
874                 mockGlobalRole.setFunctionName("test name");
875                 mockGlobalRoles.add(mockGlobalRole);
876                 Map<String, Long> params = new HashMap<>();
877                 params.put("roleId", 2l);
878                 params.put("requestedAppId", 2l);
879                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", params, null))
880                                 .thenReturn(mockGlobalRoles);
881                 CentralV2Role actual = externalAccessRolesServiceImpl.getRoleInfo(2l, app.getUebKey());
882                 assertNotEquals(null, actual);
883         }
884
885         @Test
886         public void getPartnerRoleInfoTest() throws Exception {
887                 EPApp app = mockApp();
888                 app.setId(2l);
889                 List<EPApp> appList = new ArrayList<>();
890                 final Map<String, String> appUebkeyParams = new HashMap<>();
891                 appList.add(app);
892                 appUebkeyParams.put("appKey", app.getUebKey());
893                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
894                                 .thenReturn(appList);
895                 List<EPRole> applicationRoles = new ArrayList<>();
896                 EPRole appRole = new EPRole();
897                 appRole.setActive(true);
898                 appRole.setAppId(app.getId());
899                 appRole.setAppRoleId(100l);
900                 appRole.setId(10l);
901                 appRole.setName("test");
902                 applicationRoles.add(appRole);
903                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
904                 getPartnerAppRoleParams.put("appRoleId", 10l);
905                 getPartnerAppRoleParams.put("appId", app.getId());
906                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
907                                 .thenReturn(applicationRoles);
908                 final Map<String, Long> params = new HashMap<>();
909                 params.put("roleId", appRole.getId());
910                 params.put(APP_ID, app.getId());
911                 List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>();
912                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
913                 cenRoleFuncList.add(centralV2RoleFunction);
914                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
915                                 .thenReturn(cenRoleFuncList);
916                 CentralV2Role actual = externalAccessRolesServiceImpl.getRoleInfo(10l, app.getUebKey());
917                 assertNotEquals(null, actual);
918         }
919
920         @Test
921         public void saveRoleForPortalApplicationNewTest() throws Exception {
922                 EPApp app = mockApp();
923                 app.setId(1l);
924                 Role addRoleTest = new Role();
925                 addRoleTest.setActive(true);
926                 addRoleTest.setName("Test");
927                 List<EPApp> appList = new ArrayList<>();
928                 final Map<String, String> appUebkeyParams = new HashMap<>();
929                 appList.add(app);
930                 appUebkeyParams.put("appKey", app.getUebKey());
931                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
932                                 .thenReturn(appList);
933                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
934                 HttpHeaders headers = new HttpHeaders();
935                 JSONObject mockJsonObjectRole = new JSONObject();
936                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
937                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectRole.toString(), HttpStatus.OK);
938                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
939                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
940                 final Map<String, String> epAppPortalRoleParams = new HashMap<>();
941                 epAppPortalRoleParams.put(APP_ROLE_NAME_PARAM, addRoleTest.getName());
942                 List<EPRole> getRoleCreated = new ArrayList<>();
943                 EPRole roleCreate = new EPRole();
944                 roleCreate.setActive(true);
945                 roleCreate.setId(10l);
946                 roleCreate.setName("test");
947                 getRoleCreated.add(roleCreate);
948                 Mockito.when(dataAccessService.executeNamedQuery(GET_PORTAL_APP_ROLES_QUERY, epAppPortalRoleParams, null))
949                                 .thenReturn(getRoleCreated);
950                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
951                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
952                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
953                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
954                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
955                                 app.getUebKey());
956                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
957                 assertEquals(expected, actual);
958         }
959
960         @Test
961         public void saveRoleForPortalApplicationUpdateTest() throws Exception {
962                 EPApp app = mockApp();
963                 app.setId(1l);
964                 Role addRoleTest = new Role();
965                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
966                 RoleFunction roleFunc = new RoleFunction();
967                 roleFunc.setName("Test Name");
968                 roleFunc.setCode("test_type|test_instance|*");
969                 RoleFunction roleFunc2 = new RoleFunction();
970                 roleFunc2.setName("Test Name3");
971                 roleFunc2.setCode("test_type3|test_instance3|*");
972                 roleFuncSet.add(roleFunc);
973                 roleFuncSet.add(roleFunc2);
974                 addRoleTest.setActive(true);
975                 addRoleTest.setName("Test2");
976                 addRoleTest.setId(2l);
977                 addRoleTest.setRoleFunctions(roleFuncSet);
978                 List<EPApp> appList = new ArrayList<>();
979                 final Map<String, String> appUebkeyParams = new HashMap<>();
980                 appList.add(app);
981                 appUebkeyParams.put("appKey", app.getUebKey());
982                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
983                                 .thenReturn(appList);
984                 List<EPRole> globalRoles = new ArrayList<>();
985                 EPRole globalRole = new EPRole();
986                 globalRole.setName("global_test");
987                 globalRole.setId(1111l);
988                 globalRole.setActive(true);
989                 globalRoles.add(globalRole);
990                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
991                 List<EPRole> epRoleList = new ArrayList<>();
992                 EPRole getEPRole = new EPRole();
993                 getEPRole.setName("Test");
994                 getEPRole.setId(2l);
995                 getEPRole.setActive(true);
996                 epRoleList.add(getEPRole);
997                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
998                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
999                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1000                                 .thenReturn(epRoleList);
1001                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1002                 HttpHeaders headers = new HttpHeaders();
1003                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1004                 JSONObject mockJsonObjectRole = new JSONObject();
1005                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1006                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1007                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1008                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1009                 mockJsonObjectPerm1.put("instance", "test_instance");
1010                 mockJsonObjectPerm1.put("action", "*");
1011                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1012                 mockJsonObjectPerm2.put("instance", "test_instance2");
1013                 mockJsonObjectPerm2.put("action", "*");
1014                 List<JSONObject> permsList = new ArrayList<>();
1015                 permsList.add(mockJsonObjectPerm1);
1016                 permsList.add(mockJsonObjectPerm2);
1017                 mockJsonObjectRole.put("name", "com.test.app.Test");
1018                 mockJsonObjectRole.put("perms", permsList);
1019                 mockJsonObjectRole.put("description",
1020                                 "{\"id\":\"2\",\"name\":\"Test\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"null\",\"appRoleId\":\"null\"}");
1021                 List<JSONObject> roleList = new ArrayList<>();
1022                 roleList.add(mockJsonObjectRole);
1023                 mockJsonObjectFinalRole.put("role", roleList);
1024                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1025                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1026                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1027                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
1028                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1029                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1030                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1031                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1032                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1033                 final Map<String, String> params = new HashMap<>();
1034                 params.put("uebKey", app.getUebKey());
1035                 params.put("roleId", String.valueOf(getEPRole.getId()));
1036                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1037                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1038                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1039                 bulkUploadUserRoles.setOrgUserId("guestT");
1040                 bulkUploadUserRoles.setRoleName("Test2");
1041                 userRolesList.add(bulkUploadUserRoles);
1042                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1043                                 .thenReturn(userRolesList);
1044                 Mockito.when(
1045                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1046                                 .thenReturn(true);
1047                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1048                                 .thenReturn("@test.com");
1049                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
1050                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1051                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
1052                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1053                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1054                 appRoleFuncsParams.put("appId", app.getId());
1055                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1056                 Mockito.when(
1057                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1058                                 .thenReturn(appRoleFunctionList);
1059                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1060                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1061                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1062                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1063                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1064                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1065                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1066                 v2RoleFunction.add(v2RoleFunction1);
1067                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1068                                 .thenReturn(v2RoleFunction);
1069                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1070                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1071                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1072                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1073                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1074                 v2RoleFunction2.add(v2RoleFunction3);
1075                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1076                                 .thenReturn(v2RoleFunction2);
1077                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1078                                 app.getUebKey());
1079                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1080                 assertEquals(expected, actual);
1081         }
1082
1083         @Test
1084         public void saveRoleExitsInDbButNotInExtAuthSystemTest() throws Exception {
1085                 EPApp app = mockApp();
1086                 app.setId(1l);
1087                 Role addRoleTest = new Role();
1088                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1089                 RoleFunction roleFunc = new RoleFunction();
1090                 roleFunc.setName("Test Name");
1091                 roleFunc.setCode("test_type|test_instance|*");
1092                 RoleFunction roleFunc2 = new RoleFunction();
1093                 roleFunc2.setName("Test Name3");
1094                 roleFunc2.setCode("test_type3|test_instance3|*");
1095                 roleFuncSet.add(roleFunc);
1096                 roleFuncSet.add(roleFunc2);
1097                 addRoleTest.setActive(true);
1098                 addRoleTest.setName("Test2");
1099                 addRoleTest.setId(2l);
1100                 addRoleTest.setRoleFunctions(roleFuncSet);
1101                 List<EPApp> appList = new ArrayList<>();
1102                 final Map<String, String> appUebkeyParams = new HashMap<>();
1103                 appList.add(app);
1104                 appUebkeyParams.put("appKey", app.getUebKey());
1105                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1106                                 .thenReturn(appList);
1107                 List<EPRole> globalRoles = new ArrayList<>();
1108                 EPRole globalRole = new EPRole();
1109                 globalRole.setName("global_test");
1110                 globalRole.setId(1111l);
1111                 globalRole.setActive(true);
1112                 globalRoles.add(globalRole);
1113                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1114                 List<EPRole> epRoleList = new ArrayList<>();
1115                 EPRole getEPRole = new EPRole();
1116                 getEPRole.setName("Test");
1117                 getEPRole.setId(2l);
1118                 getEPRole.setActive(true);
1119                 epRoleList.add(getEPRole);
1120                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1121                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
1122                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1123                                 .thenReturn(epRoleList);
1124                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1125                 HttpHeaders headers = new HttpHeaders();
1126                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1127                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1128                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1129                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1130                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1131                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1132                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1133                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1134                 final Map<String, String> params = new HashMap<>();
1135                 params.put("uebKey", app.getUebKey());
1136                 params.put("roleId", String.valueOf(getEPRole.getId()));
1137                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1138                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1139                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1140                 bulkUploadUserRoles.setOrgUserId("guestT");
1141                 bulkUploadUserRoles.setRoleName("Test2");
1142                 userRolesList.add(bulkUploadUserRoles);
1143                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1144                                 .thenReturn(userRolesList);
1145                 Mockito.when(
1146                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1147                                 .thenReturn(true);
1148                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1149                                 .thenReturn("@test.com");
1150                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1151                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1152                 appRoleFuncsParams.put("appId", app.getId());
1153                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1154                 Mockito.when(
1155                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1156                                 .thenReturn(appRoleFunctionList);
1157                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1158                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1159                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1160                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1161                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1162                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1163                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1164                 v2RoleFunction.add(v2RoleFunction1);
1165                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1166                                 .thenReturn(v2RoleFunction);
1167                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1168                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1169                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1170                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1171                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1172                 v2RoleFunction2.add(v2RoleFunction3);
1173                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1174                                 .thenReturn(v2RoleFunction2);
1175                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1176                                 app.getUebKey());
1177                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1178                 assertEquals(expected, actual);
1179         }
1180
1181         @Test
1182         public void saveGlobalRoleForPortalApplicationUpdateTest() throws Exception {
1183                 EPApp app = mockApp();
1184                 app.setId(1l);
1185                 Role addRoleTest = new Role();
1186                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1187                 RoleFunction roleFunc = new RoleFunction();
1188                 roleFunc.setName("Test Name");
1189                 roleFunc.setCode("test_type|test_instance|*");
1190                 RoleFunction roleFunc2 = new RoleFunction();
1191                 roleFunc2.setName("Test Name3");
1192                 roleFunc2.setCode("test_type3|test_instance3|*");
1193                 roleFuncSet.add(roleFunc);
1194                 roleFuncSet.add(roleFunc2);
1195                 addRoleTest.setActive(true);
1196                 addRoleTest.setName("global_test2");
1197                 addRoleTest.setId(1111l);
1198                 addRoleTest.setRoleFunctions(roleFuncSet);
1199                 List<EPApp> appList = new ArrayList<>();
1200                 final Map<String, String> appUebkeyParams = new HashMap<>();
1201                 appList.add(app);
1202                 appUebkeyParams.put("appKey", app.getUebKey());
1203                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1204                                 .thenReturn(appList);
1205                 List<EPRole> globalRoles = new ArrayList<>();
1206                 EPRole globalRole = new EPRole();
1207                 globalRole.setName("global_test");
1208                 globalRole.setId(1111l);
1209                 globalRole.setActive(true);
1210                 globalRoles.add(globalRole);
1211                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1212                 List<EPRole> epRoleList = new ArrayList<>();
1213                 EPRole getEPRole = new EPRole();
1214                 getEPRole.setName("global_test");
1215                 getEPRole.setId(1111l);
1216                 getEPRole.setActive(true);
1217                 epRoleList.add(getEPRole);
1218                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1219                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
1220                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1221                                 .thenReturn(epRoleList);
1222                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1223                 HttpHeaders headers = new HttpHeaders();
1224                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1225                 JSONObject mockJsonObjectRole = new JSONObject();
1226                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1227                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1228                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1229                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1230                 mockJsonObjectPerm1.put("instance", "test_instance");
1231                 mockJsonObjectPerm1.put("action", "*");
1232                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1233                 mockJsonObjectPerm2.put("instance", "test_instance2");
1234                 mockJsonObjectPerm2.put("action", "*");
1235                 List<JSONObject> permsList = new ArrayList<>();
1236                 permsList.add(mockJsonObjectPerm1);
1237                 permsList.add(mockJsonObjectPerm2);
1238                 mockJsonObjectRole.put("name", "com.test.app.global_test");
1239                 mockJsonObjectRole.put("perms", permsList);
1240                 mockJsonObjectRole.put("description",
1241                                 "{\"id\":\"1111\",\"name\":\"global_test\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"null\",\"appRoleId\":\"null\"}");
1242                 List<JSONObject> roleList = new ArrayList<>();
1243                 roleList.add(mockJsonObjectRole);
1244                 mockJsonObjectFinalRole.put("role", roleList);
1245                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1246                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1247                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1248                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
1249                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1250                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1251                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1252                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1253                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1254                 final Map<String, String> params = new HashMap<>();
1255                 params.put("uebKey", app.getUebKey());
1256                 params.put("roleId", String.valueOf(getEPRole.getId()));
1257                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1258                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1259                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1260                 bulkUploadUserRoles.setOrgUserId("guestT");
1261                 bulkUploadUserRoles.setRoleName("Test2");
1262                 userRolesList.add(bulkUploadUserRoles);
1263                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1264                                 .thenReturn(userRolesList);
1265                 Mockito.when(
1266                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1267                                 .thenReturn(true);
1268                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1269                                 .thenReturn("@test.com");
1270                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
1271                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1272                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
1273                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1274                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1275                 appRoleFuncsParams.put("appId", app.getId());
1276                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1277                 Mockito.when(
1278                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1279                                 .thenReturn(appRoleFunctionList);
1280                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1281                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1282                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1283                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1284                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1285                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1286                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1287                 v2RoleFunction.add(v2RoleFunction1);
1288                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1289                                 .thenReturn(v2RoleFunction);
1290                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1291                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1292                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1293                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1294                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1295                 v2RoleFunction2.add(v2RoleFunction3);
1296                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1297                                 .thenReturn(v2RoleFunction2);
1298                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1299                                 app.getUebKey());
1300                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1301                 assertEquals(expected, actual);
1302         }
1303
1304         @Test
1305         public void saveRoleForPartnerApplicationUpdateTest() throws Exception {
1306                 EPApp app = mockApp();
1307                 app.setId(2l);
1308                 Role addRoleTest = new Role();
1309                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1310                 RoleFunction roleFunc = new RoleFunction();
1311                 roleFunc.setName("Test Name");
1312                 roleFunc.setCode("test_type|test_instance|*");
1313                 RoleFunction roleFunc2 = new RoleFunction();
1314                 roleFunc2.setName("Test Name3");
1315                 roleFunc2.setCode("test_type3|test_instance3|*");
1316                 roleFuncSet.add(roleFunc);
1317                 roleFuncSet.add(roleFunc2);
1318                 addRoleTest.setActive(false);
1319                 addRoleTest.setName("Test2");
1320                 addRoleTest.setId(22l);
1321                 addRoleTest.setRoleFunctions(roleFuncSet);
1322                 List<EPApp> appList = new ArrayList<>();
1323                 final Map<String, String> appUebkeyParams = new HashMap<>();
1324                 appList.add(app);
1325                 appUebkeyParams.put("appKey", app.getUebKey());
1326                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1327                                 .thenReturn(appList);
1328                 List<EPRole> globalRoles = new ArrayList<>();
1329                 EPRole globalRole = new EPRole();
1330                 globalRole.setName("global_test");
1331                 globalRole.setId(1111l);
1332                 globalRole.setActive(true);
1333                 globalRoles.add(globalRole);
1334                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1335                 List<EPRole> epRoleList = new ArrayList<>();
1336                 EPRole getEPRole = new EPRole();
1337                 getEPRole.setName("Test2");
1338                 getEPRole.setId(2l);
1339                 getEPRole.setActive(true);
1340                 getEPRole.setAppRoleId(22l);
1341                 epRoleList.add(getEPRole);
1342                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1343                 getPortalAppRoleParams.put("appRoleId", addRoleTest.getId());
1344                 getPortalAppRoleParams.put("appId", app.getId());
1345                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPortalAppRoleParams, null))
1346                                 .thenReturn(epRoleList);
1347                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1348                 HttpHeaders headers = new HttpHeaders();
1349                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1350                 JSONObject mockJsonObjectRole = new JSONObject();
1351                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1352                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1353                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1354                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1355                 mockJsonObjectPerm1.put("instance", "test_instance");
1356                 mockJsonObjectPerm1.put("action", "*");
1357                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1358                 mockJsonObjectPerm2.put("instance", "test_instance2");
1359                 mockJsonObjectPerm2.put("action", "*");
1360                 List<JSONObject> permsList = new ArrayList<>();
1361                 permsList.add(mockJsonObjectPerm1);
1362                 permsList.add(mockJsonObjectPerm2);
1363                 mockJsonObjectRole.put("name", "com.test.app.Test");
1364                 mockJsonObjectRole.put("perms", permsList);
1365                 mockJsonObjectRole.put("description",
1366                                 "{\"id\":\"2\",\"name\":\"Test2\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"22\"}");
1367                 List<JSONObject> roleList = new ArrayList<>();
1368                 roleList.add(mockJsonObjectRole);
1369                 mockJsonObjectFinalRole.put("role", roleList);
1370                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1371                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1372                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1373                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
1374                 final Map<String, String> getPartnerAppRoleParams = new HashMap<>();
1375                 getPartnerAppRoleParams.put(APP_ROLE_NAME_PARAM, addRoleTest.getName());
1376                 getPartnerAppRoleParams.put("appId", String.valueOf(app.getId()));
1377                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM,
1378                                 getPartnerAppRoleParams, null)).thenReturn(epRoleList);
1379                 ResponseEntity<String> updateRoleResponse = new ResponseEntity<>(HttpStatus.OK);
1380                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
1381                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateRoleResponse);
1382                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1383                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1384                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1385                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1386                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1387                 final Map<String, String> params = new HashMap<>();
1388                 params.put("uebKey", app.getUebKey());
1389                 params.put("roleId", String.valueOf(getEPRole.getId()));
1390                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1391                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1392                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1393                 bulkUploadUserRoles.setOrgUserId("guestT");
1394                 bulkUploadUserRoles.setRoleName("Test2");
1395                 userRolesList.add(bulkUploadUserRoles);
1396                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1397                                 .thenReturn(userRolesList);
1398                 Mockito.when(
1399                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1400                                 .thenReturn(true);
1401                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1402                                 .thenReturn("@test.com");
1403                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
1404                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1405                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
1406                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1407                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1408                 appRoleFuncsParams.put("appId", app.getId());
1409                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1410                 Mockito.when(
1411                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1412                                 .thenReturn(appRoleFunctionList);
1413                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1414                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1415                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1416                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1417                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1418                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1419                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1420                 v2RoleFunction.add(v2RoleFunction1);
1421                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1422                                 .thenReturn(v2RoleFunction);
1423                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1424                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1425                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1426                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1427                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1428                 v2RoleFunction2.add(v2RoleFunction3);
1429                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1430                                 .thenReturn(v2RoleFunction2);
1431                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1432                                 app.getUebKey());
1433                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1434                 assertEquals(expected, actual);
1435         }
1436
1437         @Test
1438         public void saveGlobalRoleFunctionsForPartnerApplicationUpdateTest() throws Exception {
1439                 EPApp app = mockApp();
1440                 app.setId(2l);
1441                 Role addRoleTest = new Role();
1442                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1443                 RoleFunction roleFunc = new RoleFunction();
1444                 roleFunc.setName("Test Name");
1445                 roleFunc.setCode("test_type|test_instance|*");
1446                 RoleFunction roleFunc2 = new RoleFunction();
1447                 roleFunc2.setName("Test Name3");
1448                 roleFunc2.setCode("test_type3|test_instance3|*");
1449                 roleFuncSet.add(roleFunc);
1450                 roleFuncSet.add(roleFunc2);
1451                 addRoleTest.setActive(true);
1452                 addRoleTest.setName("global_test");
1453                 addRoleTest.setId(1111l);
1454                 addRoleTest.setRoleFunctions(roleFuncSet);
1455                 List<EPApp> appList = new ArrayList<>();
1456                 final Map<String, String> appUebkeyParams = new HashMap<>();
1457                 appList.add(app);
1458                 appUebkeyParams.put("appKey", app.getUebKey());
1459                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1460                                 .thenReturn(appList);
1461                 List<EPRole> globalRoles = new ArrayList<>();
1462                 EPRole globalRole = new EPRole();
1463                 globalRole.setName("global_test");
1464                 globalRole.setId(1111l);
1465                 globalRole.setActive(true);
1466                 EPRole globalRole2 = new EPRole();
1467                 globalRole2.setName("global_test2");
1468                 globalRole2.setId(2222l);
1469                 globalRole2.setActive(true);
1470                 globalRoles.add(globalRole);
1471                 globalRoles.add(globalRole2);
1472                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1473                 List<EPRole> getGlobalRoles = new ArrayList<>();
1474                 EPRole getEPGlobalRole = new EPRole();
1475                 getEPGlobalRole.setName("global_test");
1476                 getEPGlobalRole.setId(1111l);
1477                 getEPGlobalRole.setActive(true);
1478                 getGlobalRoles.add(getEPGlobalRole);
1479                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1480                 getPortalAppRoleParams.put("roleId", globalRole.getId());
1481                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1482                                 .thenReturn(getGlobalRoles);
1483                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1484                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
1485                 JSONObject mockJsonObjectPerm = new JSONObject();
1486                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1487                 JSONObject mockJsonObjectPerm3 = new JSONObject();
1488                 JSONObject mockJsonObjectPerm4 = new JSONObject();
1489                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
1490                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
1491                 mockJsonObjectPerm.put("instance", "test_instance");
1492                 mockJsonObjectPerm.put("action", "*");
1493                 mockJsonObjectPerm.put("description", "Test Name");
1494                 mockJsonObjectPerm2.put("type", "com.test.app.access");
1495                 mockJsonObjectPerm2.put("instance", "test_instance2");
1496                 mockJsonObjectPerm2.put("action", "*");
1497                 mockJsonObjectPerm2.put("description", "Test Name2");
1498                 mockJsonObjectPerm3.put("type", "com.test.app.test_type3");
1499                 mockJsonObjectPerm3.put("instance", "test_instance3");
1500                 mockJsonObjectPerm3.put("action", "*");
1501                 mockJsonObjectPerm3.put("description", "Test Name3");
1502                 mockJsonObjectPerm4.put("type", "com.test.app.test_type4");
1503                 mockJsonObjectPerm4.put("instance", "test_instance4");
1504                 mockJsonObjectPerm4.put("action", "*");
1505                 mockJsonObjectPerm4.put("description", "Test Name4");
1506                 List<JSONObject> mockJson = new ArrayList<>();
1507                 mockJson.add(mockJsonObjectPerm);
1508                 mockJson.add(mockJsonObjectPerm2);
1509                 mockJson.add(mockJsonObjectPerm3);
1510                 mockJson.add(mockJsonObjectPerm4);
1511                 mockJsonObjectFinalPerm.put("perm", mockJson);
1512                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
1513                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1514                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1515                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1516                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
1517                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1518                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
1519                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1520                 final Map<String, Long> epAppRoleFuncParams = new HashMap<>();
1521                 epAppRoleFuncParams.put("requestedAppId", app.getId());
1522                 epAppRoleFuncParams.put("roleId", globalRole.getId());
1523                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
1524                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
1525                 mockGlobalRole.setActive(true);
1526                 mockGlobalRole.setAppId(app.getId());
1527                 mockGlobalRole.setRoleId(1111l);
1528                 mockGlobalRole.setRoleName("global_test");
1529                 mockGlobalRole.setFunctionCd("test_type4|test_instance4|*");
1530                 mockGlobalRole.setFunctionName("test name");
1531                 mockGlobalRoles.add(mockGlobalRole);
1532                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", epAppRoleFuncParams, null))
1533                                 .thenReturn(mockGlobalRoles);
1534                 ResponseEntity<String> delResponse = new ResponseEntity<>(HttpStatus.OK);
1535                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1536                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1537                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1538                 appRoleFuncsParams.put("appId", app.getId());
1539                 appRoleFuncsParams.put("roleId", globalRole.getId());
1540                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1541                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
1542                 epAppRoleFunction.setAppId(app.getId());
1543                 epAppRoleFunction.setRoleAppId("1");
1544                 epAppRoleFunction.setCode("test");
1545                 epAppRoleFunction.setRoleId(1111l);
1546                 appRoleFunctionList.add(epAppRoleFunction);
1547                 Mockito.when(
1548                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1549                                 .thenReturn(appRoleFunctionList);
1550                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1551                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1552                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1553                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type4|test_instance4|*")).thenReturn("test_instance4");
1554                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1555                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1556                 List<CentralV2RoleFunction> roleFunction = new ArrayList<>();
1557                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, roleFunc.getCode(),
1558                                 roleFunc.getName(), app.getId(), null);
1559                 roleFunction.add(centralV2RoleFunction);
1560                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1561                                 .thenReturn(roleFunction);
1562                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1563                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1564                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1565                 List<CentralV2RoleFunction> roleFunction2 = new ArrayList<>();
1566                 CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, roleFunc2.getCode(),
1567                                 roleFunc2.getName(), app.getId(), null);
1568                 roleFunction2.add(centralV2RoleFunction2);
1569                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1570                                 .thenReturn(roleFunction2);
1571                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1572                                 app.getUebKey());
1573                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1574                 assertEquals(expected, actual);
1575         }
1576
1577         @Test
1578         public void syncRoleFunctionFromExternalAccessSystemTest() {
1579                 EPApp app = mockApp();
1580                 app.setId(2l);
1581                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
1582                 JSONObject mockJsonObjectPerm = new JSONObject();
1583                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1584                 JSONObject mockJsonObjectPerm3 = new JSONObject();
1585                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
1586                 mockJsonObjectPerm.put("instance", "test_instance");
1587                 mockJsonObjectPerm.put("action", "*");
1588                 mockJsonObjectPerm.put("description", "test_name");
1589                 List<String> rolePermList = new ArrayList<>();
1590                 rolePermList.add("com.test.app|test1");
1591                 mockJsonObjectPerm.put("roles", rolePermList);
1592                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1593                 mockJsonObjectPerm2.put("instance", "test_instance2");
1594                 List<String> rolePermList2 = new ArrayList<>();
1595                 rolePermList2.add("com.test.app|test1");
1596                 rolePermList2.add("com.test.app|test2");
1597                 rolePermList2.add("com.test.app|test6");
1598                 rolePermList2.add("com.test.app.new|global_test");
1599                 mockJsonObjectPerm2.put("action", "*");
1600                 mockJsonObjectPerm2.put("roles", rolePermList2);
1601                 mockJsonObjectPerm2.put("description", "test_name2");
1602                 mockJsonObjectPerm3.put("type", "com.test.app.access");
1603                 mockJsonObjectPerm3.put("instance", "test_instance3");
1604                 mockJsonObjectPerm3.put("action", "*");
1605                 mockJsonObjectPerm3.put("description", "test_name3");
1606                 List<JSONObject> permsList = new ArrayList<>();
1607                 permsList.add(mockJsonObjectPerm);
1608                 permsList.add(mockJsonObjectPerm2);
1609                 permsList.add(mockJsonObjectPerm3);
1610                 mockJsonObjectFinalPerm.put("perm", permsList);
1611                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
1612                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1613                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1614                 final Map<String, Long> params = new HashMap<>();
1615                 params.put(APP_ID, app.getId());
1616                 List<CentralV2RoleFunction> appFunctions = new ArrayList<>();
1617                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*",
1618                                 "test_name", app.getId(), null);
1619                 CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_instance2", "test_name2",
1620                                 app.getId(), null);
1621                 CentralV2RoleFunction centralV2RoleFunction3 = new CentralV2RoleFunction(null, "test_instance5", "test_name5",
1622                                 app.getId(), null);
1623                 appFunctions.add(centralV2RoleFunction);
1624                 appFunctions.add(centralV2RoleFunction2);
1625                 appFunctions.add(centralV2RoleFunction3);
1626                 Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null)).thenReturn(appFunctions);
1627                 List<EPRole> globalRoles = new ArrayList<>();
1628                 EPRole globalRole = new EPRole();
1629                 globalRole.setName("global_test");
1630                 globalRole.setId(1111l);
1631                 globalRole.setActive(true);
1632                 globalRoles.add(globalRole);
1633                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1634                 List<EPRole> getCurrentRoleList = new ArrayList<>();
1635                 EPRole getEPRole = new EPRole();
1636                 getEPRole.setName("test1");
1637                 getEPRole.setId(2l);
1638                 getEPRole.setActive(true);
1639                 EPRole getEPRole2 = new EPRole();
1640                 getEPRole2.setName("global_test");
1641                 getEPRole2.setId(1111l);
1642                 getEPRole2.setActive(true);
1643                 EPRole getEPRole3 = new EPRole();
1644                 getEPRole3.setName("test2");
1645                 getEPRole3.setId(4l);
1646                 getEPRole3.setActive(true);
1647                 getCurrentRoleList.add(getEPRole);
1648                 getCurrentRoleList.add(getEPRole2);
1649                 getCurrentRoleList.add(getEPRole3);
1650                 final Map<String, Long> appParams = new HashMap<>();
1651                 appParams.put("appId", app.getId());
1652                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
1653                                 .thenReturn(getCurrentRoleList);
1654                 final Map<String, String> appSyncFuncsParams = new HashMap<>();
1655                 appSyncFuncsParams.put("appId", String.valueOf(app.getId()));
1656                 appSyncFuncsParams.put("functionCd", "");
1657                 List<CentralV2RoleFunction> roleFunctionList = new ArrayList<>();
1658                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1659                                 .thenReturn(roleFunctionList);
1660                 String code = centralV2RoleFunction.getCode();
1661                 appSyncFuncsParams.put("functionCd", code);
1662                 CentralV2RoleFunction getCentralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*",
1663                                 "test_name", app.getId(), null);
1664                 roleFunctionList.add(getCentralV2RoleFunction);
1665                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1666                                 .thenReturn(roleFunctionList);
1667                 List<LocalRole> localRoles = new ArrayList<>();
1668                 LocalRole localRole = new LocalRole();
1669                 localRole.setRoleId(2);
1670                 localRole.setRolename("test1");
1671                 LocalRole localRole2 = new LocalRole();
1672                 localRole2.setRoleId(3);
1673                 localRole2.setRolename("test3");
1674                 localRoles.add(localRole);
1675                 localRoles.add(localRole2);
1676                 final Map<String, String> appRoleFuncParams = new HashMap<>();
1677                 appRoleFuncParams.put("functionCd", "test_type2|test_instance2|*");
1678                 appRoleFuncParams.put("appId", String.valueOf(app.getId()));
1679                 Mockito.when(dataAccessService.executeNamedQuery("getCurrentAppRoleFunctions", appRoleFuncParams, null))
1680                                 .thenReturn(localRoles);
1681                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app", app.getNameSpace())).thenReturn(true);
1682                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type2|test_instance2|*")).thenReturn("test_instance2");
1683                 appSyncFuncsParams.put("functionCd", "test_instance2");
1684                 List<CentralV2RoleFunction> roleFunctionList2 = new ArrayList<>();
1685                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1686                                 .thenReturn(roleFunctionList2);
1687                 String code2 = "test_type2|test_instance2|*";
1688                 appSyncFuncsParams.put("functionCd", code2);
1689                 CentralV2RoleFunction getCentralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_type2|test_instance2|*",
1690                                 "test_name2", app.getId(), null);
1691                 roleFunctionList2.add(getCentralV2RoleFunction2);
1692                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1693                                 .thenReturn(roleFunctionList2);
1694                 final Map<String, Long> params3 = new HashMap<>();
1695                 params3.put("appId", app.getId());
1696                 params3.put("roleId", getEPRole2.getId());
1697                 List<EPAppRoleFunction> currentGlobalRoleFunctionsList = new ArrayList<>();
1698                 EPAppRoleFunction addGlobalRoleFunction = new EPAppRoleFunction();
1699                 addGlobalRoleFunction.setAppId(app.getId());
1700                 addGlobalRoleFunction.setCode("test_type|test_instance|*");
1701                 addGlobalRoleFunction.setRoleId(1111l);
1702                 currentGlobalRoleFunctionsList.add(addGlobalRoleFunction);
1703                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", params3, null))
1704                                 .thenReturn(currentGlobalRoleFunctionsList);
1705                 final Map<String, String> roleParams = new HashMap<>();
1706                 roleParams.put(APP_ROLE_NAME_PARAM, "test6");
1707                 roleParams.put("appId", String.valueOf(app.getId()));
1708                 List<EPRole> roleCreated = new ArrayList<>();
1709                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null))
1710                                 .thenReturn(roleCreated);
1711                 final Map<String, String> getRoleByNameParams = new HashMap<>();
1712                 getRoleByNameParams.put(APP_ROLE_NAME_PARAM, "test6");
1713                 getRoleByNameParams.put("appId", String.valueOf(app.getId()));
1714                 EPRole getNewEPRole = new EPRole();
1715                 getNewEPRole.setName("test6");
1716                 getNewEPRole.setId(8l);
1717                 getNewEPRole.setActive(true);
1718                 List<EPRole> roleCreated2 = new ArrayList<>();
1719                 roleCreated2.add(getNewEPRole);
1720                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM,
1721                                 getRoleByNameParams, null)).thenReturn(roleCreated2);
1722                 EPRole getNewEPRoleFinal = new EPRole();
1723                 getNewEPRoleFinal.setName("test6");
1724                 getNewEPRoleFinal.setId(8l);
1725                 getNewEPRoleFinal.setActive(true);
1726                 getNewEPRoleFinal.setAppRoleId(8l);
1727                 final Map<String, String> getRoleByNameParams2 = new HashMap<>();
1728                 getRoleByNameParams2.put(APP_ROLE_NAME_PARAM, "test6");
1729                 getRoleByNameParams2.put("appId", String.valueOf(app.getId()));
1730                 List<EPRole> roleCreated3 = new ArrayList<>();
1731                 roleCreated3.add(getNewEPRole);
1732                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM,
1733                                 getRoleByNameParams2, null)).thenReturn(roleCreated3);
1734                 List<EPRole> roleInfo = new ArrayList<>();
1735                 roleInfo.add(getNewEPRoleFinal);
1736                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1737                 getPartnerAppRoleParams.put("appRoleId", getNewEPRoleFinal.getId());
1738                 getPartnerAppRoleParams.put("appId", app.getId());
1739                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
1740                                 .thenReturn(roleInfo);
1741                 externalAccessRolesServiceImpl.syncRoleFunctionFromExternalAccessSystem(app);
1742         }
1743
1744         @Test
1745         public void syncApplicationRolesWithEcompDBTest() {
1746                 EPApp app = mockApp();
1747                 app.setId(2l);
1748                 JSONObject mockJsonObjectRole = new JSONObject();
1749                 JSONObject mockJsonObjectRole2 = new JSONObject();
1750                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1751                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1752                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1753                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1754                 mockJsonObjectPerm1.put("instance", "test_instance");
1755                 mockJsonObjectPerm1.put("action", "*");
1756                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1757                 mockJsonObjectPerm2.put("instance", "test_instance2");
1758                 mockJsonObjectPerm2.put("action", "*");
1759                 List<JSONObject> permsList = new ArrayList<>();
1760                 permsList.add(mockJsonObjectPerm1);
1761                 permsList.add(mockJsonObjectPerm2);
1762                 mockJsonObjectRole.put("name", "com.test.app.Test");
1763                 mockJsonObjectRole.put("perms", permsList);
1764                 mockJsonObjectRole.put("description",
1765                                 "Test role");
1766                 mockJsonObjectRole2.put("name", "com.test.app.Test2_role");
1767                 List<JSONObject> permsList2 = new ArrayList<>();
1768                 permsList2.add(mockJsonObjectPerm1);
1769                 mockJsonObjectRole2.put("perms", permsList2);
1770                 List<JSONObject> roleList = new ArrayList<>();
1771                 roleList.add(mockJsonObjectRole);
1772                 roleList.add(mockJsonObjectRole2);
1773                 mockJsonObjectFinalRole.put("role", roleList);
1774                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1775                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1776                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1777                 List<EPRole> getCurrentRoleList = new ArrayList<>();
1778                 EPRole getEPRole = new EPRole();
1779                 getEPRole.setName("Test role");
1780                 getEPRole.setId(2l);
1781                 getEPRole.setAppId(app.getId());
1782                 getEPRole.setAppRoleId(2l);
1783                 getEPRole.setActive(true);
1784                 EPRole getEPRole2 = new EPRole();
1785                 getEPRole2.setName("Test2_role");
1786                 getEPRole2.setId(3l);
1787                 getEPRole2.setAppId(app.getId());
1788                 getEPRole2.setAppRoleId(3l);
1789                 getEPRole2.setActive(true);
1790                 EPRole getEPRole3 = new EPRole();
1791                 getEPRole3.setName("Test3_role");
1792                 getEPRole3.setId(3l);
1793                 getEPRole3.setAppId(app.getId());
1794                 getEPRole3.setAppRoleId(3l);
1795                 getEPRole3.setActive(true);
1796                 getCurrentRoleList.add(getEPRole);
1797                 getCurrentRoleList.add(getEPRole2);
1798                 getCurrentRoleList.add(getEPRole3);
1799                 final Map<String, Long> appParams = new HashMap<>();
1800                 appParams.put("appId", app.getId());
1801                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
1802                                 .thenReturn(getCurrentRoleList);
1803                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type", app.getNameSpace()))
1804                                 .thenReturn(true);
1805                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app.test_type2", app.getNameSpace()))
1806                                 .thenReturn(true);
1807                 List<EPAppRoleFunction> appRoleFunctions = new ArrayList<>();
1808                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
1809                 epAppRoleFunction.setAppId(app.getId());
1810                 epAppRoleFunction.setCode("test_type|test_instance|*");
1811                 epAppRoleFunction.setRoleId(getEPRole.getId());
1812                 appRoleFunctions.add(epAppRoleFunction);
1813                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1814                 appRoleFuncsParams.put("appId", app.getId());
1815                 appRoleFuncsParams.put("roleId", Long.valueOf(getEPRole.getId()));
1816                 Mockito.when(
1817                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1818                                 .thenReturn(appRoleFunctions);
1819                 List<CentralV2RoleFunction> getV2RoleFunction = new ArrayList<>();
1820                 final Map<String, String> appFuncsParams = new HashMap<>();
1821                 appFuncsParams.put("appId", String.valueOf(app.getId()));
1822                 appFuncsParams.put("functionCd", "test_instance2");
1823                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null))
1824                                 .thenReturn(getV2RoleFunction);
1825                 appFuncsParams.put("functionCd", "test_type2|test_instance2|*");
1826                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
1827                 centralV2RoleFunction.setAppId(app.getId());
1828                 centralV2RoleFunction.setCode("test_type2|test_instance2|*");
1829                 centralV2RoleFunction.setName("test_name2");
1830                 getV2RoleFunction.add(centralV2RoleFunction);
1831                 final Map<String, String> extRoleParams = new HashMap<>();
1832                 List<EPRole> roleListDeactivate = new ArrayList<>();
1833                 extRoleParams.put(APP_ROLE_NAME_PARAM, "Test3_role");
1834                 extRoleParams.put(APP_ID, app.getId().toString());
1835                 EPRole getEPRoleDeactivate = new EPRole();
1836                 getEPRoleDeactivate.setName("Test3_role");
1837                 getEPRoleDeactivate.setId(3l);
1838                 getEPRoleDeactivate.setAppId(app.getId());
1839                 getEPRoleDeactivate.setAppRoleId(3l);
1840                 roleListDeactivate.add(getEPRoleDeactivate);
1841                 Mockito.when(
1842                                 dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, extRoleParams, null))
1843                                 .thenReturn(roleListDeactivate);
1844                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appFuncsParams, null))
1845                                 .thenReturn(getV2RoleFunction);
1846                 List<EPRole> updateLocalFromExtAuth = new ArrayList<>();
1847                 updateLocalFromExtAuth.add(getEPRole);
1848                 final Map<String, String> roleParams = new HashMap<>();
1849                 roleParams.put(APP_ROLE_NAME_PARAM, getEPRole.getName());
1850                 roleParams.put(APP_ID, app.getId().toString());
1851                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null))
1852                                 .thenReturn(updateLocalFromExtAuth);
1853                 roleParams.put(APP_ROLE_NAME_PARAM, getEPRole2.getName());
1854                 List<EPRole> updateLocalFromExtAuth2 = new ArrayList<>();
1855                 updateLocalFromExtAuth.add(getEPRole);
1856                 Mockito.when(dataAccessService.executeNamedQuery("getRoletoUpdateInExternalAuthSystem", roleParams, null))
1857                                 .thenReturn(updateLocalFromExtAuth2);
1858                 final Map<String, String> globalRoleParams = new HashMap<>();
1859                 globalRoleParams.put("appId", String.valueOf(app.getId()));
1860                 globalRoleParams.put("appRoleName", "Test2");
1861                 List<EPRole> addNewRoleList = new ArrayList<>();
1862                 EPRole addRoleInLocal = new EPRole();
1863                 addRoleInLocal.setName("Test2");
1864                 addRoleInLocal.setId(4l);
1865                 addRoleInLocal.setAppId(app.getId());
1866                 addRoleInLocal.setActive(true);
1867                 addNewRoleList.add(addRoleInLocal);
1868                 Mockito.when(
1869                                 dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, globalRoleParams, null))
1870                                 .thenReturn(addNewRoleList);
1871                 final Map<String, String> params = new HashMap<>();
1872                 params.put(APP_ROLE_NAME_PARAM, "Test2");
1873                 params.put(APP_ID, app.getId().toString());
1874                 addRoleInLocal.setAppRoleId(4l);
1875                 addNewRoleList.add(addRoleInLocal);
1876                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, params, null))
1877                                 .thenReturn(addNewRoleList);
1878                 externalAccessRolesServiceImpl.syncApplicationRolesWithEcompDB(app);
1879         }
1880
1881         @Test
1882         public void deleteDependencyRoleRecord() throws Exception {
1883                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
1884                 EPApp app = mockApp();
1885                 app.setId(2l);
1886                 EPUser user = mockUser.mockEPUser();
1887                 List<EPApp> appList = new ArrayList<>();
1888                 final Map<String, String> appUebkeyParams = new HashMap<>();
1889                 appList.add(app);
1890                 appUebkeyParams.put("appKey", app.getUebKey());
1891                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1892                                 .thenReturn(appList);
1893                 List<EPRole> epRoleList = new ArrayList<>();
1894                 EPRole getEPRole = new EPRole();
1895                 getEPRole.setName("Test");
1896                 getEPRole.setId(2l);
1897                 getEPRole.setAppRoleId(2l);
1898                 getEPRole.setActive(true);
1899                 epRoleList.add(getEPRole);
1900                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1901                 getPartnerAppRoleParams.put("appRoleId", getEPRole.getId());
1902                 getPartnerAppRoleParams.put("appId", app.getId());
1903                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
1904                                 .thenReturn(epRoleList);
1905                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1906                 JSONObject getUser = new JSONObject();
1907                 getUser.put("name", "com.test.app.test1");
1908                 ResponseEntity<String> getResponse = new ResponseEntity<>(getUser.toString(), HttpStatus.OK);
1909                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1910                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1911                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
1912                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1913                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
1914                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
1915                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l,
1916                                 app.getUebKey(), user.getOrgUserId());
1917                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1918                 assertEquals(expected, actual);
1919         }
1920
1921         @Test
1922         public void deleteDependencyRoleRecordForPortal() throws Exception {
1923                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
1924                 EPApp app = mockApp();
1925                 app.setId(1l);
1926                 EPUser user = mockUser.mockEPUser();
1927                 List<EPApp> appList = new ArrayList<>();
1928                 final Map<String, String> appUebkeyParams = new HashMap<>();
1929                 appList.add(app);
1930                 appUebkeyParams.put("appKey", app.getUebKey());
1931                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1932                                 .thenReturn(appList);
1933                 List<EPRole> epRoleList = new ArrayList<>();
1934                 EPRole getEPRole = new EPRole();
1935                 getEPRole.setName("Test");
1936                 getEPRole.setId(2l);
1937                 getEPRole.setAppRoleId(2l);
1938                 getEPRole.setActive(true);
1939                 epRoleList.add(getEPRole);
1940                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1941                 getPartnerAppRoleParams.put("roleId", getEPRole.getId());
1942                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPartnerAppRoleParams, null))
1943                                 .thenReturn(epRoleList);
1944                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1945                 JSONObject getUser = new JSONObject();
1946                 getUser.put("name", "com.test.app.test1");
1947                 ResponseEntity<String> getResponse = new ResponseEntity<>(getUser.toString(), HttpStatus.OK);
1948                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1949                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1950                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
1951                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1952                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
1953                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
1954                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.deleteDependencyRoleRecord(2l,
1955                                 app.getUebKey(), user.getOrgUserId());
1956                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1957                 assertEquals(expected, actual);
1958         }
1959
1960         @Test
1961         public void bulkUploadFunctionsTest() throws Exception {
1962                 EPApp app = mockApp();
1963                 app.setId(2l);
1964                 List<EPApp> appList = new ArrayList<>();
1965                 final Map<String, String> appUebkeyParams = new HashMap<>();
1966                 appList.add(app);
1967                 appUebkeyParams.put("appKey", app.getUebKey());
1968                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1969                                 .thenReturn(appList);
1970                 List<RoleFunction> roleFuncList = new ArrayList<>();
1971                 RoleFunction roleFunc = new RoleFunction();
1972                 roleFunc.setCode("test_code");
1973                 roleFunc.setName("test_name");
1974                 RoleFunction roleFunc2 = new RoleFunction();
1975                 roleFunc2.setCode("test_code2");
1976                 roleFunc2.setName("test_name2");
1977                 roleFuncList.add(roleFunc);
1978                 roleFuncList.add(roleFunc2);
1979                 Mockito.when(dataAccessService.executeNamedQuery("getAllFunctions", null, null)).thenReturn(roleFuncList);
1980                 JSONObject perm = new JSONObject();
1981                 JSONObject permList = new JSONObject();
1982                 perm.put("type", app.getNameSpace() + ".access");
1983                 perm.put("instance", "type_instance");
1984                 perm.put("action", "*");
1985                 List<JSONObject> addPerms = new ArrayList<>();
1986                 addPerms.add(perm);
1987                 permList.put("perm", addPerms);
1988                 ResponseEntity<String> getResponse = new ResponseEntity<>(permList.toString(), HttpStatus.OK);
1989                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1990                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1991                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
1992                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1993                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
1994                 Integer actual = externalAccessRolesServiceImpl.bulkUploadFunctions(app.getUebKey());
1995                 Integer expected = 2;
1996                 assertEquals(expected, actual);
1997         }
1998
1999         @Test
2000         public void bulkUploadRolesTest() throws Exception {
2001                 EPApp app = mockApp();
2002                 app.setId(2l);
2003                 List<EPApp> appList = new ArrayList<>();
2004                 final Map<String, String> appUebkeyParams = new HashMap<>();
2005                 appList.add(app);
2006                 appUebkeyParams.put("appKey", app.getUebKey());
2007                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2008                                 .thenReturn(appList);
2009                 List<EPRole> epRoleList = new ArrayList<>();
2010                 EPRole getEPRole = new EPRole();
2011                 getEPRole.setName("Test");
2012                 getEPRole.setId(2l);
2013                 getEPRole.setAppRoleId(2l);
2014                 getEPRole.setActive(true);
2015                 EPRole getEPRole2 = new EPRole();
2016                 getEPRole2.setName("Test2");
2017                 getEPRole2.setId(3l);
2018                 getEPRole2.setAppRoleId(3l);
2019                 getEPRole2.setActive(true);
2020                 epRoleList.add(getEPRole);
2021                 epRoleList.add(getEPRole2);
2022                 final Map<String, Long> appParams = new HashMap<>();
2023                 appParams.put("appId", app.getId());
2024                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
2025                                 .thenReturn(epRoleList);
2026                 List<EPRole> epRoleList1 = new ArrayList<>();
2027                 EPRole ePRole = new EPRole();
2028                 ePRole.setName("Test");
2029                 ePRole.setId(2l);
2030                 ePRole.setAppRoleId(2l);
2031                 ePRole.setActive(true);
2032                 epRoleList1.add(ePRole);
2033                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
2034                 getPartnerAppRoleParams.put("appRoleId", ePRole.getId());
2035                 getPartnerAppRoleParams.put("appId", app.getId());
2036                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
2037                                 .thenReturn(epRoleList1);
2038                 List<EPRole> epRoleList2 = new ArrayList<>();
2039                 EPRole ePRole2 = new EPRole();
2040                 ePRole2.setName("Test2");
2041                 ePRole2.setId(3l);
2042                 ePRole2.setAppRoleId(3l);
2043                 ePRole2.setActive(true);
2044                 epRoleList2.add(ePRole2);
2045                 final Map<String, Long> getPartnerAppRoleParams2 = new HashMap<>();
2046                 getPartnerAppRoleParams2.put("appRoleId", ePRole2.getId());
2047                 getPartnerAppRoleParams2.put("appId", app.getId());
2048                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams2, null))
2049                                 .thenReturn(epRoleList2);
2050                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
2051                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2052                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
2053                 Integer actual = externalAccessRolesServiceImpl.bulkUploadRoles(app.getUebKey());
2054                 Integer expected = 3;
2055                 assertEquals(expected, actual);
2056         }
2057
2058         @Test
2059         public void bulkUploadUserRolesTest() throws Exception {
2060                 EPApp app = mockApp();
2061                 app.setId(2l);
2062                 EPUser user = mockUser.mockEPUser();
2063                 List<EPApp> appList = new ArrayList<>();
2064                 final Map<String, String> appUebkeyParams = new HashMap<>();
2065                 appList.add(app);
2066                 appUebkeyParams.put("appKey", app.getUebKey());
2067                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2068                                 .thenReturn(appList);
2069                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
2070                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
2071                 bulkUploadUserRoles.setAppNameSpace(app.getName());
2072                 bulkUploadUserRoles.setOrgUserId(user.getOrgUserId());
2073                 bulkUploadUserRoles.setRoleName("Test1");
2074                 BulkUploadUserRoles bulkUploadUserRoles2 = new BulkUploadUserRoles();
2075                 bulkUploadUserRoles2.setAppNameSpace(app.getName());
2076                 bulkUploadUserRoles2.setOrgUserId(user.getOrgUserId());
2077                 bulkUploadUserRoles2.setRoleName("Test2");
2078                 userRolesList.add(bulkUploadUserRoles);
2079                 userRolesList.add(bulkUploadUserRoles2);
2080                 final Map<String, String> appParams = new HashMap<>();
2081                 appParams.put("uebKey", app.getUebKey());
2082                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUserRoles", appParams, null))
2083                                 .thenReturn(userRolesList);
2084                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
2085                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2086                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
2087                 Integer actual = externalAccessRolesServiceImpl.bulkUploadUserRoles(app.getUebKey());
2088                 Integer expected = 2;
2089                 assertEquals(expected, actual);
2090         }
2091
2092         @Test
2093         public void getUserRolesTest() throws Exception {
2094                 EPApp app = mockApp();
2095                 EPUser user = mockUser.mockEPUser();
2096                 List<EPApp> appList = new ArrayList<>();
2097                 final Map<String, String> appUebkeyParams = new HashMap<>();
2098                 appList.add(app);
2099                 appUebkeyParams.put("appKey", app.getUebKey());
2100                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2101                                 .thenReturn(appList);
2102                 final Map<String, String> userParams = new HashMap<>();
2103                 userParams.put("org_user_id", user.getOrgUserId());
2104                 List<EPUser> userList = new ArrayList<>();
2105                 Set<EPUserApp> userAppSet = user.getEPUserApps();
2106                 EPUserApp epUserApp = new EPUserApp();
2107                 EPRole epRole = new EPRole();
2108                 epRole.setName("test");
2109                 epRole.setId(1l);
2110                 epRole.setActive(true);
2111                 epRole.setAppRoleId(11l);
2112                 epUserApp.setApp(app);
2113                 epUserApp.setUserId(user.getId());
2114                 epUserApp.setRole(epRole);
2115                 userAppSet.add(epUserApp);
2116                 user.setUserApps(userAppSet);
2117                 userList.add(user);
2118                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
2119                                 .thenReturn(userList);
2120                 CentralUser actual = externalAccessRolesServiceImpl.getUserRoles(user.getOrgUserId(), app.getUebKey());
2121                 assertNotNull(actual);
2122         }
2123
2124         @SuppressWarnings("unchecked")
2125         @Test
2126         public void getActiveRolesTest() throws Exception {
2127                 EPApp app = mockApp();
2128                 app.setId(1l);
2129                 List<EPApp> appList = new ArrayList<>();
2130                 final Map<String, String> appUebkeyParams = new HashMap<>();
2131                 appList.add(app);
2132                 appUebkeyParams.put("appKey", app.getUebKey());
2133                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2134                                 .thenReturn(appList);
2135                 List<EPRole> epRoles = new ArrayList<>();
2136                 EPRole getEPRole = new EPRole();
2137                 getEPRole.setName("Test");
2138                 getEPRole.setId(2l);
2139                 getEPRole.setActive(true);
2140                 EPRole getEPRole2 = new EPRole();
2141                 getEPRole2.setName("Test2");
2142                 getEPRole2.setId(3l);
2143                 getEPRole2.setActive(true);
2144                 epRoles.add(getEPRole);
2145                 epRoles.add(getEPRole2);
2146                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
2147                 Criterion active_ynCrt = Restrictions.eq("active", Boolean.TRUE);
2148                 Criterion appIdCrt = Restrictions.isNull("appId");
2149                 Criterion andCrit = Restrictions.and(active_ynCrt, appIdCrt);
2150                 restrictionsList.add(andCrit);
2151                 Mockito.when((List<EPRole>) dataAccessService.getList(EPRole.class, null, restrictionsList, null))
2152                                 .thenReturn(epRoles);
2153                 final Map<String, Long> params = new HashMap<>();
2154                 params.put("roleId", getEPRole.getId());
2155                 params.put(APP_ID, app.getId());
2156                 List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>();
2157                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|test_instance|*",
2158                                 "test_name");
2159                 CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction("test_instance2", "test_name2");
2160                 cenRoleFuncList.add(centralV2RoleFunction);
2161                 cenRoleFuncList.add(centralV2RoleFunction2);
2162                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
2163                                 .thenReturn(cenRoleFuncList);
2164                 final Map<String, Long> params2 = new HashMap<>();
2165                 params2.put("roleId", getEPRole2.getId());
2166                 params2.put(APP_ID, app.getId());
2167                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params2, null))
2168                                 .thenReturn(cenRoleFuncList);
2169                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
2170                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
2171                 mockGlobalRole.setActive(true);
2172                 mockGlobalRole.setAppId(app.getId());
2173                 mockGlobalRole.setRoleId(1111l);
2174                 mockGlobalRole.setRoleName("global_test");
2175                 mockGlobalRole.setFunctionCd("test_type|test_code|*");
2176                 mockGlobalRole.setFunctionName("test name");
2177                 mockGlobalRoles.add(mockGlobalRole);
2178                 Map<String, Long> params3 = new HashMap<>();
2179                 params3.put("appId", app.getId());
2180                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleWithApplicationRoleFunctions", params3, null))
2181                                 .thenReturn(mockGlobalRoles);
2182                 Mockito.when(EcompPortalUtils.getFunctionCode(centralV2RoleFunction.getCode())).thenReturn("test_instance");
2183                 Mockito.when(EcompPortalUtils.getFunctionCode(centralV2RoleFunction2.getCode())).thenReturn("test_instance2");
2184                 List<CentralV2Role> actual = externalAccessRolesServiceImpl.getActiveRoles(app.getUebKey());
2185                 assertEquals(actual.size(), 3);
2186         }
2187
2188         @Test
2189         public void bulkUploadRolesFunctionsTest() throws Exception {
2190                 EPApp app = mockApp();
2191                 List<EPApp> appList = new ArrayList<>();
2192                 final Map<String, String> appUebkeyParams = new HashMap<>();
2193                 appList.add(app);
2194                 appUebkeyParams.put("appKey", app.getUebKey());
2195                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2196                                 .thenReturn(appList);
2197                 List<EPRole> applicationRoles = new ArrayList<>();
2198                 EPRole getEPRole = new EPRole();
2199                 getEPRole.setName("Test");
2200                 getEPRole.setId(2l);
2201                 getEPRole.setActive(true);
2202                 EPRole getEPRole2 = new EPRole();
2203                 getEPRole2.setName("Test2");
2204                 getEPRole2.setId(3l);
2205                 getEPRole2.setActive(true);
2206                 applicationRoles.add(getEPRole);
2207                 applicationRoles.add(getEPRole2);
2208                 final Map<String, Long> appParams = new HashMap<>();
2209                 appParams.put("appId", app.getId());
2210                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
2211                                 .thenReturn(applicationRoles);
2212                 final Map<String, Long> params = new HashMap<>();
2213                 params.put("roleId", getEPRole.getId());
2214                 List<BulkUploadRoleFunction> appRoleFunc = new ArrayList<>();
2215                 BulkUploadRoleFunction bulkUploadRoleFunction = new BulkUploadRoleFunction();
2216                 bulkUploadRoleFunction.setFunctionCd("testcode");
2217                 bulkUploadRoleFunction.setFunctionName("test_name");
2218                 BulkUploadRoleFunction bulkUploadRoleFunction2 = new BulkUploadRoleFunction();
2219                 bulkUploadRoleFunction2.setFunctionCd("menu_testcode2");
2220                 bulkUploadRoleFunction2.setFunctionName("test_name2");
2221                 appRoleFunc.add(bulkUploadRoleFunction);
2222                 appRoleFunc.add(bulkUploadRoleFunction2);
2223                 Mockito.when(dataAccessService.executeNamedQuery("uploadAllRoleFunctions", params, null))
2224                                 .thenReturn(appRoleFunc);
2225                 final Map<String, Long> params2 = new HashMap<>();
2226                 params2.put("roleId", getEPRole2.getId());
2227                 List<BulkUploadRoleFunction> appRoleFunc2 = new ArrayList<>();
2228                 appRoleFunc2.add(bulkUploadRoleFunction);
2229                 appRoleFunc2.add(bulkUploadRoleFunction2);
2230                 Mockito.when(dataAccessService.executeNamedQuery("uploadAllRoleFunctions", params2, null))
2231                                 .thenReturn(appRoleFunc2);
2232                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.CREATED);
2233                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2234                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
2235                 Integer actual = externalAccessRolesServiceImpl.bulkUploadRolesFunctions(app.getUebKey());
2236                 Integer expected = 4;
2237                 assertEquals(actual, expected);
2238         }
2239
2240         @Test
2241         public void bulkUploadPartnerRoleFunctionsTest() throws Exception {
2242                 EPApp app = mockApp();
2243                 List<EPApp> appList = new ArrayList<>();
2244                 final Map<String, String> appUebkeyParams = new HashMap<>();
2245                 appList.add(app);
2246                 appUebkeyParams.put("appKey", app.getUebKey());
2247                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2248                                 .thenReturn(appList);
2249                 List<EPRole> applicationRoles = new ArrayList<>();
2250                 EPRole getEPRole = new EPRole();
2251                 getEPRole.setName("Test");
2252                 getEPRole.setId(2l);
2253                 getEPRole.setActive(true);
2254                 EPRole getEPRole2 = new EPRole();
2255                 getEPRole2.setName("Test2");
2256                 getEPRole2.setId(3l);
2257                 getEPRole2.setActive(true);
2258                 applicationRoles.add(getEPRole);
2259                 applicationRoles.add(getEPRole2);
2260                 final Map<String, Long> paramsRoles = new HashMap<>();
2261                 paramsRoles.put("appId", app.getId());
2262                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", paramsRoles, null))
2263                                 .thenReturn(applicationRoles);
2264                 final Map<String, Long> params = new HashMap<>();
2265                 params.put("roleId", getEPRole.getId());
2266                 List<BulkUploadRoleFunction> appRoleFunc = new ArrayList<>();
2267                 BulkUploadRoleFunction bulkUploadRoleFunction = new BulkUploadRoleFunction();
2268                 bulkUploadRoleFunction.setFunctionCd("testcode");
2269                 bulkUploadRoleFunction.setFunctionName("test_name");
2270                 BulkUploadRoleFunction bulkUploadRoleFunction2 = new BulkUploadRoleFunction();
2271                 bulkUploadRoleFunction2.setFunctionCd("menu_testcode2");
2272                 bulkUploadRoleFunction2.setFunctionName("test_name2");
2273                 appRoleFunc.add(bulkUploadRoleFunction);
2274                 appRoleFunc.add(bulkUploadRoleFunction2);
2275                 Mockito.when(dataAccessService.executeNamedQuery("uploadPartnerRoleFunctions", params, null))
2276                                 .thenReturn(appRoleFunc);
2277                 final Map<String, Long> params2 = new HashMap<>();
2278                 params2.put("roleId", getEPRole2.getId());
2279                 List<BulkUploadRoleFunction> appRoleFunc2 = new ArrayList<>();
2280                 appRoleFunc2.add(bulkUploadRoleFunction);
2281                 appRoleFunc2.add(bulkUploadRoleFunction2);
2282                 Mockito.when(dataAccessService.executeNamedQuery("uploadPartnerRoleFunctions", params2, null))
2283                                 .thenReturn(appRoleFunc2);
2284                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
2285                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
2286                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
2287                 // GlobalRoleFunctionsTest
2288                 final Map<String, Long> partnerAppParams = new HashMap<>();
2289                 partnerAppParams.put("appId", app.getId());
2290                 Mockito.when(epAppCommonServiceImpl.getApp(1l)).thenReturn(app);
2291                 List<GlobalRoleWithApplicationRoleFunction> globalRoleFuncsList = new ArrayList<>();
2292                 GlobalRoleWithApplicationRoleFunction globalRoleFunc = new GlobalRoleWithApplicationRoleFunction();
2293                 globalRoleFunc.setActive(true);
2294                 globalRoleFunc.setAppId(10l);
2295                 globalRoleFunc.setFunctionCd("test|test|test");
2296                 globalRoleFunc.setRoleId(2l);
2297                 globalRoleFunc.setFunctionName("test");
2298                 globalRoleFunc.setRoleName("global_test");
2299                 globalRoleFuncsList.add(globalRoleFunc);
2300                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUploadPartnerGlobalRoleFunctions", partnerAppParams, null))
2301                                 .thenReturn(globalRoleFuncsList);
2302                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2303                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
2304                 Integer actual = externalAccessRolesServiceImpl.bulkUploadPartnerRoleFunctions(app.getUebKey());
2305                 Integer expected = 5;
2306                 assertEquals(expected, actual);
2307         }
2308
2309         @Test
2310         public void getMenuFunctionsListTest() throws Exception {
2311                 EPApp app = mockApp();
2312                 List<EPApp> appList = new ArrayList<>();
2313                 final Map<String, String> appUebkeyParams = new HashMap<>();
2314                 appList.add(app);
2315                 appUebkeyParams.put("appKey", app.getUebKey());
2316                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2317                                 .thenReturn(appList);
2318                 final Map<String, Long> appParams = new HashMap<>();
2319                 appParams.put(APP_ID, app.getId());
2320                 List<String> expected = new ArrayList<>();
2321                 expected.add("test_menu1");
2322                 expected.add("test_menu2");
2323                 Mockito.when(dataAccessService.executeNamedQuery("getMenuFunctions", appParams, null)).thenReturn(expected);
2324                 List<String> actual = externalAccessRolesServiceImpl.getMenuFunctionsList(app.getUebKey());
2325                 assertEquals(expected, actual);
2326         }
2327
2328         @Test
2329         public void getCentralizedAppsOfUserTest() {
2330                 EPUser user = mockUser.mockEPUser();
2331                 Map<String, String> params = new HashMap<>();
2332                 params.put("userId", user.getOrgUserId());
2333                 List<CentralizedApp> expected = new ArrayList<>();
2334                 CentralizedApp centralizedApp = new CentralizedApp();
2335                 centralizedApp.setAppId(2);
2336                 centralizedApp.setAppName("testapp1");
2337                 expected.add(centralizedApp);
2338                 Mockito.when(dataAccessService.executeNamedQuery("getCentralizedAppsOfUser", params, null))
2339                                 .thenReturn(expected);
2340                 List<CentralizedApp> actual = externalAccessRolesServiceImpl.getCentralizedAppsOfUser(user.getOrgUserId());
2341                 assertEquals(expected, actual);
2342         }
2343
2344         @Test
2345         public void ConvertCentralRoleToRoleTest() throws Exception {
2346                 ObjectMapper mapper = new ObjectMapper();
2347                 Role role = new Role();
2348                 role.setName("Test");
2349                 role.setId(3l);
2350                 role.setActive(true);
2351                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
2352                 RoleFunction roleFunc = new RoleFunction();
2353                 roleFunc.setName("Test Name");
2354                 roleFunc.setCode("testcode");
2355                 RoleFunction roleFunc2 = new RoleFunction();
2356                 roleFunc2.setName("Test Name3");
2357                 roleFunc2.setCode("menu_testcode2");
2358                 roleFuncSet.add(roleFunc);
2359                 roleFuncSet.add(roleFunc2);
2360                 role.setRoleFunctions(roleFuncSet);
2361                 String roleInfo = mapper.writeValueAsString(role);
2362                 Role actual = externalAccessRolesServiceImpl.ConvertCentralRoleToRole(roleInfo);
2363                 assertNotNull(actual);
2364         }
2365
2366         @Test
2367         public void convertV2CentralRoleListToOldVerisonCentralRoleListTest() {
2368                 List<CentralV2Role> v2CenRoleList = new ArrayList<>();
2369                 CentralV2Role cenV2Role = new CentralV2Role(2l, "test1");
2370                 CentralV2RoleFunction CentralV2Role = new CentralV2RoleFunction("testcode", "test_name");
2371                 SortedSet<CentralV2RoleFunction> setV2RoleFuncs = new TreeSet<>();
2372                 setV2RoleFuncs.add(CentralV2Role);
2373                 cenV2Role.setRoleFunctions(setV2RoleFuncs);
2374                 v2CenRoleList.add(cenV2Role);
2375                 List<CentralRole> actual = externalAccessRolesServiceImpl.convertV2CentralRoleListToOldVerisonCentralRoleList(v2CenRoleList);
2376                 assertEquals(1, actual.size());
2377         }
2378         
2379         @Test
2380         public void bulkUploadRoleFuncTest() throws Exception {
2381                 EPApp app = mockApp();
2382                 UploadRoleFunctionExtSystem  data = new UploadRoleFunctionExtSystem();
2383                 data.setRoleName("test");
2384                 data.setType("test");
2385                 data.setInstance("test");
2386                 data.setIsGlobalRolePartnerFunc(false);
2387                 data.setAction("test");
2388                 data.setName("test");
2389                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
2390                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2391                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
2392                 externalAccessRolesServiceImpl.bulkUploadRoleFunc(data, app);
2393         }
2394         
2395         @Test
2396         public void bulkUploadGlobalRoleFuncTest() throws Exception {
2397                 EPApp app = mockApp();
2398                 EPApp portalApp = mockApp();
2399                 portalApp.setId(1L);
2400                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(portalApp);
2401                 UploadRoleFunctionExtSystem  data = new UploadRoleFunctionExtSystem();
2402                 data.setRoleName("test");
2403                 data.setType("test");
2404                 data.setInstance("test");
2405                 data.setIsGlobalRolePartnerFunc(true);
2406                 data.setAction("test");
2407                 data.setName("test");
2408                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
2409                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2410                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
2411                 externalAccessRolesServiceImpl.bulkUploadRoleFunc(data, app);
2412         }
2413         
2414         @Test(expected = HttpClientErrorException.class)
2415         public void bulkUploadRoleFuncExcpetionTest() throws Exception {
2416                 UploadRoleFunctionExtSystem  data = new UploadRoleFunctionExtSystem();
2417                 data.setRoleName("test");
2418                 data.setType("test");
2419                 data.setInstance("test");
2420                 data.setAction("test");
2421                 data.setName("test");
2422                 data.setInstance("test");
2423                 EPApp app = mockApp();
2424                 Mockito.doThrow(new HttpClientErrorException(HttpStatus.CONFLICT)).when(template).exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
2425                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class));
2426                 externalAccessRolesServiceImpl.bulkUploadRoleFunc(data, app);
2427         }
2428         
2429         @Test
2430         public void syncApplicationUserRolesFromExtAuthSystemTest() throws Exception {
2431                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(new HttpHeaders());
2432                 Mockito.when(EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)).thenReturn(true);
2433                 JSONObject mockJsonObjectRole = new JSONObject();
2434                 JSONObject mockJsonObjectRole2 = new JSONObject();
2435                 JSONObject mockJsonObjectRole3 = new JSONObject();
2436                 mockJsonObjectRole.put("name", "com.test.app.test_role");
2437                 mockJsonObjectRole2.put("name", "com.test.app2.test_role");
2438                 mockJsonObjectRole3.put("name", "com.test.app2.Account_Administrator");
2439                 List<JSONObject> userRolesList = new ArrayList<>();
2440                 JSONObject mockJsonObjectFinalUserRole = new JSONObject();
2441                 userRolesList.add(mockJsonObjectRole);
2442                 userRolesList.add(mockJsonObjectRole2);
2443                 userRolesList.add(mockJsonObjectRole3);
2444                 mockJsonObjectFinalUserRole.put("role", userRolesList);
2445                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalUserRole.toString(),HttpStatus.OK);
2446                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
2447                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
2448                 List<EPUser> users = new ArrayList<>();
2449                 EPUser user = mockUser.mockEPUser();
2450                 user.setOrgUserId("test");
2451                 users.add(user);
2452                 List<EPApp> apps = new ArrayList<>();
2453                 EPApp app = mockApp();
2454                 app.setNameSpace("com.test.app");
2455                 app.setId(1l);
2456                 EPApp app2 = mockApp();
2457                 app2.setNameSpace("com.test.app2");
2458                 app2.setId(2l);
2459                 apps.add(app);
2460                 apps.add(app2);
2461                 Mockito.when(dataAccessService
2462                 .executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps);
2463                 HashMap<String, String> userParams = new HashMap<>();
2464                 userParams.put("org_user_id", "test");
2465                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)).thenReturn(users);
2466                 List<CentralizedAppRoles> currentUserAppRoles = new ArrayList<>();
2467                 CentralizedAppRoles currentUserAppRole = new CentralizedAppRoles();
2468                 currentUserAppRole.setAppId(1l);
2469                 currentUserAppRole.setAppNameSpace("com.test.app");
2470                 currentUserAppRole.setRoleId(2l);
2471                 currentUserAppRole.setRoleName("test role");
2472                 currentUserAppRoles.add(currentUserAppRole);
2473                 HashMap<String, String> userParams2 = new HashMap<>();
2474                 userParams2.put("orgUserId", user.getOrgUserId());
2475                 Mockito.when(dataAccessService
2476                 .executeNamedQuery("getUserCentralizedAppRoles", userParams2, null)).thenReturn(currentUserAppRoles);
2477                 List<CentralizedAppRoles> centralizedAppRoles = new ArrayList<>();
2478                 CentralizedAppRoles centralizedAppRole = new CentralizedAppRoles();
2479                 centralizedAppRole.setAppId(1l);
2480                 centralizedAppRole.setAppNameSpace("com.test.app");
2481                 centralizedAppRole.setRoleId(2l);
2482                 centralizedAppRole.setRoleName("test role");
2483                 CentralizedAppRoles centralizedAppRole2 = new CentralizedAppRoles();
2484                 centralizedAppRole2.setAppId(1l);
2485                 centralizedAppRole2.setAppNameSpace("com.test.app2");
2486                 centralizedAppRole2.setRoleId(2l);
2487                 centralizedAppRole2.setRoleName("test role");
2488                 centralizedAppRoles.add(centralizedAppRole);
2489                 centralizedAppRoles.add(centralizedAppRole2);
2490                 Mockito.when(dataAccessService
2491                 .executeNamedQuery("getAllCentralizedAppsRoles", null, null)).thenReturn(centralizedAppRoles);
2492                 externalAccessRolesServiceImpl.syncApplicationUserRolesFromExtAuthSystem(user.getOrgUserId());
2493         }
2494         
2495         @Test
2496         public void updateAppRoleDescriptionTest() {
2497                 EPApp app = mockUpdateAppRoleDescription();
2498                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
2499                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
2500                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
2501                 Integer actual = externalAccessRolesServiceImpl.updateAppRoleDescription(app.getUebKey());
2502                 Integer expected = 1;
2503                 assertEquals(expected, actual);
2504         }
2505         
2506         @Test
2507         public void updateAppRoleDescriptionExceptionTest() {
2508                 EPApp app = mockUpdateAppRoleDescription();
2509                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
2510                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.NOT_ACCEPTABLE));
2511                 Integer actual = externalAccessRolesServiceImpl.updateAppRoleDescription(app.getUebKey());
2512                 Integer expected = 0;
2513                 assertEquals(expected, actual);
2514         }
2515         
2516         @Test
2517         public void updateAppRoleDescriptionExceptionTest2() throws Exception {
2518                 EPApp app = mockUpdateAppRoleDescription();
2519                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenThrow(new NullPointerException());
2520                 Integer actual = externalAccessRolesServiceImpl.updateAppRoleDescription(app.getUebKey());
2521                 Integer expected = 0;
2522                 assertEquals(expected, actual);
2523         }
2524
2525         private EPApp mockUpdateAppRoleDescription() {
2526                 EPApp app = mockApp();
2527                 app.setId(2l);
2528                 List<EPApp> appList = new ArrayList<>();
2529                 final Map<String, String> appUebkeyParams = new HashMap<>();
2530                 appList.add(app);
2531                 appUebkeyParams.put("appKey", app.getUebKey());
2532                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
2533                                 .thenReturn(appList);
2534                 List<EPRole> epRoleList = new ArrayList<>();
2535                 EPRole getEPRole = new EPRole();
2536                 getEPRole.setName("Test");
2537                 getEPRole.setId(2l);
2538                 getEPRole.setAppRoleId(2l);
2539                 getEPRole.setActive(true);
2540                 epRoleList.add(getEPRole);
2541                 final Map<String, Long> appParams = new HashMap<>();
2542                 appParams.put("appId", app.getId());
2543                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
2544                 .thenReturn(epRoleList);
2545                 return app;
2546         }
2547         
2548 }