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