d1ad191a0c2e248b8a8209cdfbbe1a38d2fef0f7
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / ExternalAccessRolesServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertNotEquals;
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertTrue;
45
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Set;
51 import java.util.SortedSet;
52 import java.util.TreeSet;
53
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56
57 import org.hibernate.SQLQuery;
58 import org.hibernate.Session;
59 import org.hibernate.SessionFactory;
60 import org.hibernate.Transaction;
61 import org.hibernate.criterion.Criterion;
62 import org.hibernate.criterion.Restrictions;
63 import org.json.JSONObject;
64 import org.junit.After;
65 import org.junit.Before;
66 import org.junit.Test;
67 import org.junit.runner.RunWith;
68 import org.mockito.InjectMocks;
69 import org.mockito.Matchers;
70 import org.mockito.Mock;
71 import org.mockito.Mockito;
72 import org.mockito.MockitoAnnotations;
73 import org.onap.portalapp.portal.core.MockEPUser;
74 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
75 import org.onap.portalapp.portal.domain.CentralizedApp;
76 import org.onap.portalapp.portal.domain.EPApp;
77 import org.onap.portalapp.portal.domain.EPAppRoleFunction;
78 import org.onap.portalapp.portal.domain.EPRole;
79 import org.onap.portalapp.portal.domain.EPUser;
80 import org.onap.portalapp.portal.domain.EPUserApp;
81 import org.onap.portalapp.portal.ecomp.model.UploadRoleFunctionExtSystem;
82 import org.onap.portalapp.portal.exceptions.InactiveApplicationException;
83 import org.onap.portalapp.portal.exceptions.InvalidUserException;
84 import org.onap.portalapp.portal.framework.MockitoTestSuite;
85 import org.onap.portalapp.portal.transport.BulkUploadRoleFunction;
86 import org.onap.portalapp.portal.transport.BulkUploadUserRoles;
87 import org.onap.portalapp.portal.transport.CentralRole;
88 import org.onap.portalapp.portal.transport.CentralUser;
89 import org.onap.portalapp.portal.transport.CentralV2Role;
90 import org.onap.portalapp.portal.transport.CentralizedAppRoles;
91 import org.onap.portalapp.portal.transport.EcompUserRoles;
92 import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
93 import org.onap.portalapp.portal.transport.GlobalRoleWithApplicationRoleFunction;
94 import org.onap.portalapp.portal.transport.LocalRole;
95 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
96 import org.onap.portalapp.portal.utils.EcompPortalUtils;
97 import org.onap.portalapp.portal.utils.PortalConstants;
98 import org.onap.portalapp.util.EPUserUtils;
99 import org.onap.portalsdk.core.domain.Role;
100 import org.onap.portalsdk.core.domain.RoleFunction;
101 import org.onap.portalsdk.core.restful.domain.EcompUser;
102 import org.onap.portalsdk.core.service.DataAccessService;
103 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
104 import org.onap.portalsdk.core.util.SystemProperties;
105 import org.powermock.api.mockito.PowerMockito;
106 import org.powermock.core.classloader.annotations.PrepareForTest;
107 import org.powermock.modules.junit4.PowerMockRunner;
108 import org.springframework.http.HttpEntity;
109 import org.springframework.http.HttpHeaders;
110 import org.springframework.http.HttpMethod;
111 import org.springframework.http.HttpStatus;
112 import org.springframework.http.ResponseEntity;
113 import org.springframework.web.client.HttpClientErrorException;
114 import org.springframework.web.client.RestTemplate;
115
116 import com.fasterxml.jackson.databind.ObjectMapper;
117
118 @RunWith(PowerMockRunner.class)
119 @PrepareForTest({ EcompPortalUtils.class, Criterion.class, EPUserUtils.class, Restrictions.class, SystemProperties.class,
120                 EPCommonSystemProperties.class })
121 public class ExternalAccessRolesServiceImplTest {
122         @Mock
123         DataAccessService dataAccessService = new DataAccessServiceImpl();
124
125         @Mock
126         RestTemplate template = new RestTemplate();
127
128         @InjectMocks
129         ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
130
131         @Mock
132         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
133
134         @Mock
135         SessionFactory sessionFactory;
136
137         @Mock
138         Session session;
139
140         @Mock
141         Transaction transaction;
142
143         @Before
144         public void setup() {
145                 MockitoAnnotations.initMocks(this);
146                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
147                 PowerMockito.mockStatic(PortalConstants.class);
148                 PowerMockito.mockStatic(EcompPortalUtils.class);
149                 PowerMockito.mockStatic(SystemProperties.class);
150                 PowerMockito.mockStatic(EPUserUtils.class);
151                 PowerMockito.mockStatic(Restrictions.class);
152                 PowerMockito.mockStatic(Criterion.class);
153                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
154                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
155         }
156
157         @After
158         public void after() {
159                 session.close();
160         }
161
162         private static final String APP_ROLE_NAME_PARAM = "appRoleName";
163
164         private static final String GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM = "getRoletoUpdateInExternalAuthSystem";
165
166         private static final String GET_PORTAL_APP_ROLES_QUERY = "getPortalAppRoles";
167
168         private static final String GET_ROLE_FUNCTION_QUERY = "getRoleFunction";
169
170         private static final String FUNCTION_CODE_PARAMS = "functionCode";
171
172         private static final String FUNCTION_PIPE = "|";
173
174         private static final String APP_ID = "appId";
175
176         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
177
178         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
179         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
180         NullPointerException nullPointerException = new NullPointerException();
181         MockEPUser mockUser = new MockEPUser();
182         String uebKey = "test-ueb-key";
183
184         public EPApp mockApp() {
185                 EPApp app = new EPApp();
186                 app.setName("Test");
187                 app.setImageUrl("test");
188                 app.setNameSpace("com.test.app");
189                 app.setCentralAuth(true);
190                 app.setDescription("test");
191                 app.setNotes("test");
192                 app.setUrl("test");
193                 app.setId((long) 10);
194                 app.setAppRestEndpoint("test");
195                 app.setAlternateUrl("test");
196                 app.setName("test");
197                 app.setMlAppName("test");
198                 app.setMlAppAdminId("test");
199                 app.setUsername("test");
200                 app.setAppPassword("test");
201                 app.setOpen(false);
202                 app.setEnabled(true);
203                 app.setUebKey("test");
204                 app.setUebSecret("test");
205                 app.setUebTopicName("test");
206                 app.setAppType(1);
207                 return app;
208         }
209
210         @SuppressWarnings("deprecation")
211         @Test
212         public void getAppRolesIfAppIsPortalTest() throws Exception {
213                 List<EPRole> applicationRoles = new ArrayList<>();
214                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
215                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
216                 assertEquals(expectedApplicationRoles, applicationRoles);
217         }
218
219         @SuppressWarnings("deprecation")
220         @Test
221         public void getAppRolesTest() throws Exception {
222                 List<EPRole> applicationRoles = new ArrayList<>();
223                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
224                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
225                 assertEquals(expectedApplicationRoles, applicationRoles);
226         }
227
228         @SuppressWarnings("deprecation")
229         @Test
230         public void getAppExceptionTest() throws Exception {
231                 List<EPApp> app = new ArrayList<>();
232                 Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
233                                 .thenReturn(app);
234                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
235                 assertEquals(app, expectedapp);
236         }
237
238         @Test(expected = InactiveApplicationException.class)
239         public void getAppErrorTest() throws Exception {
240                 List<EPApp> appList = new ArrayList<>();
241                 EPApp app = mockApp();
242                 app.setEnabled(false);
243                 appList.add(app);
244                 final Map<String, String> appUebkeyParams = new HashMap<>();
245                 appUebkeyParams.put("appKey", "test-ueb-key");
246                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
247                                 .thenReturn(appList);
248                 externalAccessRolesServiceImpl.getApp(uebKey);
249         }
250
251         @Test
252         public void getAppTest() throws Exception {
253                 List<EPApp> appList = new ArrayList<>();
254                 EPApp app = mockApp();
255                 app.setId((long) 1);
256                 appList.add(app);
257                 final Map<String, String> appUebkeyParams = new HashMap<>();
258                 appUebkeyParams.put("appKey", "test-ueb-key");
259                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
260                                 .thenReturn(appList);
261                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
262                 assertEquals(appList, expectedapp);
263         }
264
265         @Test
266         public void addRoleTest() throws Exception {
267                 HttpHeaders headers = new HttpHeaders();
268                 String uebKey = "test-ueb-key";
269                 Role role = new Role();
270                 role.setId((long) 25);
271                 EPApp app = mockApp();
272                 app.setEnabled(true);
273                 app.setId((long) 10);
274                 app.setNameSpace("test_namesapce");
275                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
276                 List<EPApp> appList = new ArrayList<>();
277                 appList.add(app);
278                 List<EPRole> roleList = new ArrayList<>();
279                 EPRole ePRole = new EPRole();
280                 role.setName("Test Role");
281                 roleList.add(ePRole);
282                 final Map<String, String> appUebkeyParams = new HashMap<>();
283                 appUebkeyParams.put("appKey", "test-ueb-key");
284                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
285                                 .thenReturn(appList);
286                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
287                 getPartnerAppRoleParams.put("appRoleId", role.getId());
288                 getPartnerAppRoleParams.put("appId", app.getId());
289                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
290                                 .thenReturn(roleList);
291                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
292                                 .thenReturn("Testurl");
293                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
294                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
295                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
296
297                 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
298         }
299
300         @Test
301         public void addRoleMethodNotAllowedTest() throws Exception {
302                 HttpHeaders headers = new HttpHeaders();
303                 Role role = new Role();
304                 role.setId((long) 25);
305                 EPApp app = mockApp();
306                 app.setEnabled(true);
307                 app.setId((long) 10);
308                 app.setNameSpace("test_namesapce");
309                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
310                 List<EPApp> appList = new ArrayList<>();
311                 appList.add(app);
312                 List<EPRole> roleList = new ArrayList<>();
313                 EPRole ePRole = new EPRole();
314                 role.setName("Test Role");
315                 roleList.add(ePRole);
316                 final Map<String, String> appUebkeyParams = new HashMap<>();
317                 appUebkeyParams.put("appKey", "test-ueb-key");
318                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
319                                 .thenReturn(appList);
320                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
321                 getPartnerAppRoleParams.put("appRoleId", role.getId());
322                 getPartnerAppRoleParams.put("appId", app.getId());
323                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
324                                 .thenReturn(roleList);
325                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
326                                 .thenReturn("Testurl");
327                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
328                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
329                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
330                 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
331         }
332
333         public EPApp getApp() {
334                 EPApp app = new EPApp();
335                 app.setName("Test");
336                 app.setImageUrl("test");
337                 app.setDescription("test");
338                 app.setNotes("test");
339                 app.setUrl("test");
340                 app.setId((long) 1);
341                 app.setAppRestEndpoint("test");
342                 app.setAlternateUrl("test");
343                 app.setName("test");
344                 app.setMlAppName("test");
345                 app.setMlAppAdminId("test");
346                 app.setUsername("test");
347                 app.setAppPassword("test");
348                 app.setOpen(true);
349                 app.setEnabled(false);
350                 app.setUebKey("test");
351                 app.setUebSecret("test");
352                 app.setUebTopicName("test");
353                 app.setAppType(1);
354                 return app;
355         }
356
357         @Test
358         public void deleteCentralRoleFunctionTest() throws Exception {
359                 final Map<String, String> params = new HashMap<>();
360                 EPApp app = mockApp();
361                 params.put("functionCode", "menu_fun_code");
362                 params.put("appId", String.valueOf(10));
363                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
364                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
365                 domainCentralRoleFunction.setCode("menu_fun_code");
366                 centralRoleFunctionList.add(domainCentralRoleFunction);
367                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null))
368                                 .thenReturn(centralRoleFunctionList);
369                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
370                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
371                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
372                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
373                 HttpHeaders headers = new HttpHeaders();
374                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
375                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class,
376                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
377                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
378         }
379
380         @Test
381         public void deleteRoleForApplicationTest() throws Exception {
382                 EPApp app = mockApp();
383                 app.setId(2l);
384                 List<EPApp> appList = new ArrayList<>();
385                 appList.add(app);
386                 final Map<String, String> appUebkeyParams = new HashMap<>();
387                 appUebkeyParams.put("appKey", app.getUebKey());
388                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
389                                 .thenReturn(appList);
390                 final Map<String, String> deleteRoleParams = new HashMap<>();
391                 deleteRoleParams.put(APP_ROLE_NAME_PARAM, "test_delete");
392                 deleteRoleParams.put(APP_ID, String.valueOf(app.getId()));
393                 List<EPRole> epRoleList = new ArrayList<>();
394                 EPRole epRole = new EPRole();
395                 epRole.setName("test_delete");
396                 epRole.setId(1l);
397                 epRole.setActive(true);
398                 epRole.setAppRoleId(11l);
399                 epRoleList.add(epRole);
400                 Mockito.when(
401                                 dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, deleteRoleParams, null))
402                                 .thenReturn(epRoleList);
403                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
404                 appRoleFuncsParams.put("appId", app.getId());
405                 appRoleFuncsParams.put("roleId", epRole.getId());
406                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
407                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
408                 epAppRoleFunction.setCode("test_code");
409                 epAppRoleFunction.setAppId(app.getId());
410                 epAppRoleFunction.setRoleAppId(null);
411                 appRoleFunctionList.add(epAppRoleFunction);
412                 Mockito.when(
413                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
414                                 .thenReturn(appRoleFunctionList);
415                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
416                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
417                 PowerMockito.mockStatic(EcompPortalUtils.class);
418                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
419                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
420                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
421                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
422                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
423                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
424                 assertTrue(externalAccessRolesServiceImpl.deleteRoleForApplication(epRole.getName(), app.getUebKey()));
425         }
426
427         @Test
428         public void deleteRoleForPortalApplicationTest() throws Exception {
429                 EPApp app = mockApp();
430                 app.setId(1l);
431                 List<EPApp> appList = new ArrayList<>();
432                 appList.add(app);
433                 final Map<String, String> appUebkeyParams = new HashMap<>();
434                 appUebkeyParams.put("appKey", app.getUebKey());
435                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
436                                 .thenReturn(appList);
437                 final Map<String, String> deleteRoleParams2 = new HashMap<>();
438                 deleteRoleParams2.put(APP_ROLE_NAME_PARAM, "test_delete");
439                 List<EPRole> epRoleList2 = new ArrayList<>();
440                 EPRole epRole = new EPRole();
441                 epRole.setName("test_delete");
442                 epRole.setId(1l);
443                 epRole.setActive(true);
444                 epRoleList2.add(epRole);
445                 Mockito.when(dataAccessService.executeNamedQuery(GET_PORTAL_APP_ROLES_QUERY, deleteRoleParams2, null))
446                                 .thenReturn(epRoleList2);
447                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
448                 appRoleFuncsParams.put("appId", app.getId());
449                 appRoleFuncsParams.put("roleId", epRole.getId());
450                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
451                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
452                 epAppRoleFunction.setCode("test_code");
453                 epAppRoleFunction.setAppId(app.getId());
454                 epAppRoleFunction.setRoleAppId(null);
455                 appRoleFunctionList.add(epAppRoleFunction);
456                 Mockito.when(
457                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
458                                 .thenReturn(appRoleFunctionList);
459                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
460                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
461                 PowerMockito.mockStatic(EcompPortalUtils.class);
462                 ResponseEntity<String> getResponse = new ResponseEntity<>(HttpStatus.OK);
463                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
464                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
465                 ResponseEntity<String> DelResponse = new ResponseEntity<>(HttpStatus.OK);
466                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
467                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(DelResponse);
468                 assertTrue(externalAccessRolesServiceImpl.deleteRoleForApplication(epRole.getName(), app.getUebKey()));
469         }
470
471         @Test
472         public void deleteCentralRoleFunctionFailTest() throws Exception {
473                 final Map<String, String> params = new HashMap<>();
474                 EPApp app = mockApp();
475                 params.put("functionCode", "menu_fun_code");
476                 params.put("appId", String.valueOf(10));
477                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
478                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
479                 domainCentralRoleFunction.setCode("menu_fun_code");
480                 centralRoleFunctionList.add(domainCentralRoleFunction);
481                 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
482                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null))
483                                 .thenReturn(centralRoleFunctionList);
484                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
485                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
486                 HttpHeaders headers = new HttpHeaders();
487                 PowerMockito.mockStatic(EcompPortalUtils.class);
488                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
489                 Mockito.doNothing().when(dataAccessService).deleteDomainObjects(EPAppRoleFunction.class,
490                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
491                 boolean returnedValue = externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app);
492                 assertTrue(returnedValue);
493         }
494
495         @Test
496         public void deleteCentralRoleFunctionExceptionTest() {
497                 final Map<String, String> params = new HashMap<>();
498                 EPApp app = mockApp();
499                 params.put("functionCd", "menu_fun_code");
500                 params.put("appId", String.valueOf(10));
501                 List<CentralV2RoleFunction> centralRoleFunctionList = new ArrayList<>();
502                 CentralV2RoleFunction domainCentralRoleFunction = new CentralV2RoleFunction();
503                 domainCentralRoleFunction.setCode("menu_fun_code");
504                 centralRoleFunctionList.add(domainCentralRoleFunction);
505                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionDetails", params, null))
506                                 .thenThrow(nullPointerException);
507                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
508         }
509
510         @Test
511         public void getRoleFunctionTest() throws Exception {
512                 EPApp app = mockApp();
513                 List<EPApp> appList = new ArrayList<>();
514                 appList.add(app);
515                 final Map<String, String> appUebkeyParams = new HashMap<>();
516                 appUebkeyParams.put("appKey", app.getUebKey());
517                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
518                                 .thenReturn(appList);
519                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type|type_code|*")).thenReturn("type_code");
520                 Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type");
521                 Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*");
522                 List<CentralV2RoleFunction> getRoleFuncList = new ArrayList<>();
523                 CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
524                 getRoleFuncList.add(getCenRole);
525                 final Map<String, String> params = new HashMap<>();
526                 params.put(FUNCTION_CODE_PARAMS, "test_type|type_code|*");
527                 params.put(APP_ID, String.valueOf(app.getId()));
528                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null))
529                                 .thenReturn(getRoleFuncList);
530                 CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*",
531                                 app.getUebKey());
532                 assertEquals("type_code", actual.getCode());
533         }
534
535         @Test
536         public void getRoleFunctionMutilpleFilterTest() throws Exception {
537                 EPApp app = mockApp();
538                 List<EPApp> appList = new ArrayList<>();
539                 appList.add(app);
540                 final Map<String, String> appUebkeyParams = new HashMap<>();
541                 appUebkeyParams.put("appKey", app.getUebKey());
542                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
543                                 .thenReturn(appList);
544                 CentralV2RoleFunction expected = new CentralV2RoleFunction(null, "type_code", "test_name", null, "test_type",
545                                 "*", null);
546                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type|type_code|*")).thenReturn("type_code");
547                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type_1|type_code_1|*")).thenReturn("type_code_1");
548                 Mockito.when(EcompPortalUtils.getFunctionType("test_type|type_code|*")).thenReturn("test_type");
549                 Mockito.when(EcompPortalUtils.getFunctionAction("test_type|type_code|*")).thenReturn("*");
550                 List<CentralV2RoleFunction> getRoleFuncList = new ArrayList<>();
551                 CentralV2RoleFunction getCenRole = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
552                 CentralV2RoleFunction getCenRole2 = new CentralV2RoleFunction("test_type_1|type_code_1|*", "test_name_1");
553                 getRoleFuncList.add(getCenRole);
554                 getRoleFuncList.add(getCenRole2);
555                 final Map<String, String> params = new HashMap<>();
556                 params.put(FUNCTION_CODE_PARAMS, "test_type|type_code|*");
557                 params.put(APP_ID, String.valueOf(app.getId()));
558                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, params, null))
559                                 .thenReturn(getRoleFuncList);
560                 CentralV2RoleFunction actual = externalAccessRolesServiceImpl.getRoleFunction("test_type|type_code|*",
561                                 app.getUebKey());
562                 assertEquals(expected.getCode(), actual.getCode());
563         }
564
565         @Test
566         public void getUserTest() throws InvalidUserException {
567                 List<EPUser> userList = new ArrayList<>();
568                 EPUser user = mockUser.mockEPUser();
569                 userList.add(user);
570                 final Map<String, String> userParams = new HashMap<>();
571                 userParams.put("org_user_id", "guestT");
572                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
573                                 .thenReturn(userList);
574                 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
575                 assertEquals(expectedUserList, userList);
576         }
577
578         @Test
579         public void getV2UserWithRolesTest() throws Exception {
580                 EPApp app = mockApp();
581                 app.setId(2l);
582                 EPUser user = mockUser.mockEPUser();
583                 List<EPApp> appList = new ArrayList<>();
584                 appList.add(app);
585                 final Map<String, String> appUebkeyParams = new HashMap<>();
586                 appUebkeyParams.put("appKey", app.getUebKey());
587                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
588                                 .thenReturn(appList);
589                 final Map<String, String> userParams = new HashMap<>();
590                 userParams.put("org_user_id", user.getOrgUserId());
591                 List<EPUser> userList = new ArrayList<>();
592                 Set<EPUserApp> userAppSet = user.getEPUserApps();
593                 EPUserApp epUserApp = new EPUserApp();
594                 EPRole epRole = new EPRole();
595                 epRole.setName("test");
596                 epRole.setId(1l);
597                 epRole.setActive(true);
598                 epRole.setAppRoleId(11l);
599                 epUserApp.setApp(app);
600                 epUserApp.setUserId(user.getId());
601                 epUserApp.setRole(epRole);
602                 userAppSet.add(epUserApp);
603                 user.setUserApps(userAppSet);
604                 userList.add(user);
605                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null))
606                                 .thenReturn(userList);
607                 final Map<String, Long> params = new HashMap<>();
608                 List<CentralV2RoleFunction> appRoleFunctionList = new ArrayList<>();
609                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
610                 centralV2RoleFunction.setCode("test_type|test_code|*");
611                 centralV2RoleFunction.setName("test name");
612                 centralV2RoleFunction.setAppId(app.getId());
613                 appRoleFunctionList.add(centralV2RoleFunction);
614                 params.put("roleId", epUserApp.getRole().getId());
615                 params.put(APP_ID, epUserApp.getApp().getId());
616                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
617                                 .thenReturn(appRoleFunctionList);
618                 String actual = externalAccessRolesServiceImpl.getV2UserWithRoles(user.getOrgUserId(), app.getUebKey());
619                 String notExpected = "";
620                 assertNotEquals(actual, notExpected);
621         }
622
623         @Test
624         public void saveCentralRoleFunctionNewTestForV2() throws Exception {
625                 PowerMockito.mockStatic(EcompPortalUtils.class);
626                 EPApp app = mockApp();
627                 app.setId((long) 1);
628                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
629                 centralV2RoleFunction.setCode("test_code");
630                 centralV2RoleFunction.setName("test name");
631                 centralV2RoleFunction.setAppId(app.getId());
632                 centralV2RoleFunction.setAction("*");
633                 centralV2RoleFunction.setType("test_type");
634                 final Map<String, String> params = new HashMap<>();
635                 params.put("appId", String.valueOf(1));
636
637                 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
638                 appRoleFunc.add(centralV2RoleFunction);
639                 params.put(FUNCTION_CODE_PARAMS, centralV2RoleFunction.getType() + FUNCTION_PIPE
640                                 + centralV2RoleFunction.getCode() + FUNCTION_PIPE + centralV2RoleFunction.getAction());
641                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)).thenReturn(appRoleFunc);
642                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
643                 HttpHeaders headers = new HttpHeaders();
644                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
645                 JSONObject mockJsonObjectPerm = new JSONObject();
646                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
647                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
648                 mockJsonObjectPerm.put("instance", "com.test.app.test_code");
649                 mockJsonObjectPerm.put("action", "*");
650                 mockJsonObjectPerm.put("description", "test name");
651                 List<JSONObject> mockJson = new ArrayList<>();
652                 mockJson.add(mockJsonObjectPerm);
653                 mockJsonObjectFinalPerm.put("perm", mockJson);
654                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
655                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
656                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
657                 ResponseEntity<String> updateResponse = new ResponseEntity<>(HttpStatus.OK);
658                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
659                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateResponse);
660                 Boolean actual = externalAccessRolesServiceImpl.saveCentralRoleFunction(centralV2RoleFunction, app);
661                 assertEquals(true, actual);
662         }
663
664         @Test
665         public void saveCentralRoleFunctionUpdateForV2Test() throws Exception {
666                 PowerMockito.mockStatic(EcompPortalUtils.class);
667                 EPApp app = mockApp();
668                 app.setId((long) 1);
669                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction();
670                 centralV2RoleFunction.setCode("test_code");
671                 centralV2RoleFunction.setName("test name2");
672                 centralV2RoleFunction.setAppId(app.getId());
673                 centralV2RoleFunction.setAction("*");
674                 centralV2RoleFunction.setType("test_type");
675                 CentralV2RoleFunction centralV2RoleFunctionExisting = new CentralV2RoleFunction();
676                 centralV2RoleFunctionExisting.setCode("test_code");
677                 centralV2RoleFunctionExisting.setName("test name");
678                 centralV2RoleFunctionExisting.setAppId(app.getId());
679                 centralV2RoleFunctionExisting.setAction("*");
680                 centralV2RoleFunctionExisting.setType("test_type");
681                 final Map<String, String> params = new HashMap<>();
682                 params.put("appId", String.valueOf(1));
683                 List<CentralV2RoleFunction> appRoleFunc = new ArrayList<>();
684                 appRoleFunc.add(centralV2RoleFunctionExisting);
685                 params.put(FUNCTION_CODE_PARAMS, centralV2RoleFunction.getType() + FUNCTION_PIPE
686                                 + centralV2RoleFunction.getCode() + FUNCTION_PIPE + centralV2RoleFunction.getAction());
687                 Mockito.when(dataAccessService.executeNamedQuery("getRoleFunction", params, null)).thenReturn(appRoleFunc);
688                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
689                 HttpHeaders headers = new HttpHeaders();
690                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
691                 JSONObject mockJsonObjectPerm = new JSONObject();
692                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
693                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
694                 mockJsonObjectPerm.put("instance", "test_code");
695                 mockJsonObjectPerm.put("action", "*");
696                 mockJsonObjectPerm.put("description", "test name");
697                 List<JSONObject> mockJson = new ArrayList<>();
698                 mockJson.add(mockJsonObjectPerm);
699                 mockJsonObjectFinalPerm.put("perm", mockJson);
700                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
701                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
702                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
703                 ResponseEntity<String> updateResponse = new ResponseEntity<>(HttpStatus.OK);
704                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
705                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateResponse);
706                 Boolean actual = externalAccessRolesServiceImpl.saveCentralRoleFunction(centralV2RoleFunction, app);
707                 assertEquals(true, actual);
708         }
709
710         @SuppressWarnings("deprecation")
711         @Test
712         public void getAllAppUsersTest() throws Exception {
713                 EPApp app = new EPApp();
714                 app.setEnabled(true);
715                 app.setId((long) 10);
716                 List<EPApp> appList = new ArrayList<>();
717                 final Map<String, String> appUebkeyParams = new HashMap<>();
718                 appList.add(app);
719                 appUebkeyParams.put("appKey", app.getUebKey());
720                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
721                                 .thenReturn(appList);
722                 List<EPRole> applicationRoles = new ArrayList<>();
723                 Mockito.when(dataAccessService.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
724                 Mockito.when(dataAccessService.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
725                                 .thenReturn(appList);
726                 final Map<String, Long> appParams = new HashMap<>();
727                 appParams.put("appId", app.getId());
728                 List<EcompUserRoles> userList = new ArrayList<>();
729                 EcompUserRoles ecompUserRoles = new EcompUserRoles();
730                 ecompUserRoles.setOrgUserId("guestT");
731                 ecompUserRoles.setRoleId((long) 1);
732                 ecompUserRoles.setRoleName("test");
733                 ecompUserRoles.setFunctionCode("test_type|test_instance|test_action");
734                 ecompUserRoles.setFunctionName("test1");
735                 EcompUserRoles ecompUserRoles2 = new EcompUserRoles();
736                 ecompUserRoles2.setOrgUserId("guestT");
737                 ecompUserRoles2.setRoleId((long) 2);
738                 ecompUserRoles2.setRoleName("test new");
739                 ecompUserRoles2.setFunctionCode("test_instance2");
740                 ecompUserRoles2.setFunctionName("test2");
741                 userList.add(ecompUserRoles);
742                 userList.add(ecompUserRoles2);
743                 Mockito.when(EcompPortalUtils.getFunctionCode(ecompUserRoles.getFunctionCode())).thenReturn("test_instance");
744         Mockito.when(EPUserUtils.decodeFunctionCode("test_instance")).thenReturn("test_instance");
745         Mockito.when(EcompPortalUtils.getFunctionCode(ecompUserRoles2.getFunctionCode())).thenReturn("test_instance2");
746         Mockito.when(EPUserUtils.decodeFunctionCode("test_instance2")).thenReturn("test_instance2");
747         Mockito.when(EcompPortalUtils.getFunctionType("test_type|test_instance|test_action")).thenReturn("test_type");
748         Mockito.when(EcompPortalUtils.getFunctionAction("test_type|test_instance|test_action")).thenReturn("test_action");
749         Mockito.when(dataAccessService.executeNamedQuery("ApplicationUserRoles", appParams, null)).thenReturn(userList);
750                 List<EcompUser> usersfinalList = externalAccessRolesServiceImpl.getAllAppUsers(app.getUebKey());
751                 assertEquals(usersfinalList.get(0).getRoles().size(), 2);
752         }
753
754         @Test
755         public void getGlobalRolesOfPortalTest() {
756                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(null);
757                 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), null);
758         }
759
760         @Test
761         public void getGlobalRolesOfPortalExceptionTest() {
762                 List<EPRole> globalRoles = new ArrayList<>();
763                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null))
764                                 .thenThrow(nullPointerException);
765                 assertEquals(externalAccessRolesServiceImpl.getGlobalRolesOfPortal(), globalRoles);
766         }
767
768         @Test
769         public void getRolesForAppTest() throws Exception {
770                 EPApp app = mockApp();
771                 app.setId(2l);
772                 List<EPApp> appList = new ArrayList<>();
773                 final Map<String, String> appUebkeyParams = new HashMap<>();
774                 appList.add(app);
775                 appUebkeyParams.put("appKey", app.getUebKey());
776                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
777                                 .thenReturn(appList);
778                 List<EPRole> applicationRoles = new ArrayList<>();
779                 EPRole appRole = new EPRole();
780                 appRole.setActive(true);
781                 appRole.setAppId(app.getId());
782                 appRole.setAppRoleId(100l);
783                 appRole.setId(10l);
784                 appRole.setName("test");
785                 applicationRoles.add(appRole);
786                 final Map<String, Long> appParams = new HashMap<>();
787                 appParams.put("appId", app.getId());
788                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
789                                 .thenReturn(applicationRoles);
790                 List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>();
791                 CentralV2RoleFunction v2RoleFunction = new CentralV2RoleFunction();
792                 v2RoleFunction.setAppId(app.getId());
793                 v2RoleFunction.setCode("test_type|test_code|*");
794                 v2RoleFunction.setName("test name");
795                 cenRoleFuncList.add(v2RoleFunction);
796                 final Map<String, Long> params = new HashMap<>();
797                 params.put("roleId", appRole.getId());
798                 params.put(APP_ID, appList.get(0).getId());
799                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
800                                 .thenReturn(cenRoleFuncList);
801                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
802                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
803                 mockGlobalRole.setActive(true);
804                 mockGlobalRole.setAppId(app.getId());
805                 mockGlobalRole.setRoleId(1111l);
806                 mockGlobalRole.setRoleName("global_test");
807                 mockGlobalRole.setFunctionCd("test_type|test_code|*");
808                 mockGlobalRole.setFunctionName("test name");
809                 mockGlobalRoles.add(mockGlobalRole);
810                 Map<String, Long> params2 = new HashMap<>();
811                 params2.put("appId", app.getId());
812                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleWithApplicationRoleFunctions", params2, null))
813                                 .thenReturn(mockGlobalRoles);
814                 List<EPRole> globalRoles = new ArrayList<>();
815                 EPRole globalRole = new EPRole();
816                 globalRole.setName("global_test");
817                 globalRole.setId(1111l);
818                 globalRole.setActive(true);
819                 globalRoles.add(globalRole);
820                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
821                 List<CentralV2Role> expected = new ArrayList<>();
822                 CentralV2Role cenV2Role = new CentralV2Role();
823                 CentralV2Role cenV2Role2 = new CentralV2Role();
824                 expected.add(cenV2Role);
825                 expected.add(cenV2Role2);
826                 List<CentralV2Role> actual = externalAccessRolesServiceImpl.getRolesForApp(app.getUebKey());
827                 assertEquals(expected.size(), actual.size());
828         }
829
830         @Test
831         public void getRoleFuncListTest() throws Exception {
832                 EPApp app = mockApp();
833                 List<EPApp> appList = new ArrayList<>();
834                 final Map<String, String> appUebkeyParams = new HashMap<>();
835                 appList.add(app);
836                 appUebkeyParams.put("appKey", app.getUebKey());
837                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
838                                 .thenReturn(appList);
839                 final Map<String, Long> params = new HashMap<>();
840                 params.put(APP_ID, app.getId());
841                 List<CentralV2RoleFunction> expected = new ArrayList<>();
842                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
843                 expected.add(centralV2RoleFunction);
844                 Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null)).thenReturn(expected);
845                 List<CentralV2RoleFunction> actual = externalAccessRolesServiceImpl.getRoleFuncList(app.getUebKey());
846                 assertEquals(expected, actual);
847         }
848
849         @Test
850         public void getRoleInfoTest() throws Exception {
851                 EPApp app = mockApp();
852                 app.setId(2l);
853                 List<EPApp> appList = new ArrayList<>();
854                 final Map<String, String> appUebkeyParams = new HashMap<>();
855                 appList.add(app);
856                 appUebkeyParams.put("appKey", app.getUebKey());
857                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
858                                 .thenReturn(appList);
859                 List<EPRole> globalRoles = new ArrayList<>();
860                 EPRole globalRole = new EPRole();
861                 globalRole.setName("global_test");
862                 globalRole.setId(2l);
863                 globalRole.setActive(true);
864                 globalRoles.add(globalRole);
865                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
866                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
867                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
868                 mockGlobalRole.setActive(true);
869                 mockGlobalRole.setAppId(app.getId());
870                 mockGlobalRole.setRoleId(2l);
871                 mockGlobalRole.setRoleName("global_test");
872                 mockGlobalRole.setFunctionCd("test_type|test_code|*");
873                 mockGlobalRole.setFunctionName("test name");
874                 mockGlobalRoles.add(mockGlobalRole);
875                 Map<String, Long> params = new HashMap<>();
876                 params.put("roleId", 2l);
877                 params.put("requestedAppId", 2l);
878                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", params, null))
879                                 .thenReturn(mockGlobalRoles);
880                 CentralV2Role actual = externalAccessRolesServiceImpl.getRoleInfo(2l, app.getUebKey());
881                 assertNotEquals(null, actual);
882         }
883
884         @Test
885         public void getPartnerRoleInfoTest() throws Exception {
886                 EPApp app = mockApp();
887                 app.setId(2l);
888                 List<EPApp> appList = new ArrayList<>();
889                 final Map<String, String> appUebkeyParams = new HashMap<>();
890                 appList.add(app);
891                 appUebkeyParams.put("appKey", app.getUebKey());
892                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
893                                 .thenReturn(appList);
894                 List<EPRole> applicationRoles = new ArrayList<>();
895                 EPRole appRole = new EPRole();
896                 appRole.setActive(true);
897                 appRole.setAppId(app.getId());
898                 appRole.setAppRoleId(100l);
899                 appRole.setId(10l);
900                 appRole.setName("test");
901                 applicationRoles.add(appRole);
902                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
903                 getPartnerAppRoleParams.put("appRoleId", 10l);
904                 getPartnerAppRoleParams.put("appId", app.getId());
905                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
906                                 .thenReturn(applicationRoles);
907                 final Map<String, Long> params = new HashMap<>();
908                 params.put("roleId", appRole.getId());
909                 params.put(APP_ID, app.getId());
910                 List<CentralV2RoleFunction> cenRoleFuncList = new ArrayList<>();
911                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction("test_type|type_code|*", "test_name");
912                 cenRoleFuncList.add(centralV2RoleFunction);
913                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionList", params, null))
914                                 .thenReturn(cenRoleFuncList);
915                 CentralV2Role actual = externalAccessRolesServiceImpl.getRoleInfo(10l, app.getUebKey());
916                 assertNotEquals(null, actual);
917         }
918
919         @Test
920         public void saveRoleForPortalApplicationNewTest() throws Exception {
921                 EPApp app = mockApp();
922                 app.setId(1l);
923                 Role addRoleTest = new Role();
924                 addRoleTest.setActive(true);
925                 addRoleTest.setName("Test");
926                 List<EPApp> appList = new ArrayList<>();
927                 final Map<String, String> appUebkeyParams = new HashMap<>();
928                 appList.add(app);
929                 appUebkeyParams.put("appKey", app.getUebKey());
930                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
931                                 .thenReturn(appList);
932                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
933                 HttpHeaders headers = new HttpHeaders();
934                 JSONObject mockJsonObjectRole = new JSONObject();
935                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
936                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectRole.toString(), HttpStatus.OK);
937                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
938                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
939                 final Map<String, String> epAppPortalRoleParams = new HashMap<>();
940                 epAppPortalRoleParams.put(APP_ROLE_NAME_PARAM, addRoleTest.getName());
941                 List<EPRole> getRoleCreated = new ArrayList<>();
942                 EPRole roleCreate = new EPRole();
943                 roleCreate.setActive(true);
944                 roleCreate.setId(10l);
945                 roleCreate.setName("test");
946                 getRoleCreated.add(roleCreate);
947                 Mockito.when(dataAccessService.executeNamedQuery(GET_PORTAL_APP_ROLES_QUERY, epAppPortalRoleParams, null))
948                                 .thenReturn(getRoleCreated);
949                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
950                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
951                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
952                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
953                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
954                                 app.getUebKey());
955                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
956                 assertEquals(expected, actual);
957         }
958
959         @Test
960         public void saveRoleForPortalApplicationUpdateTest() throws Exception {
961                 EPApp app = mockApp();
962                 app.setId(1l);
963                 Role addRoleTest = new Role();
964                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
965                 RoleFunction roleFunc = new RoleFunction();
966                 roleFunc.setName("Test Name");
967                 roleFunc.setCode("test_type|test_instance|*");
968                 RoleFunction roleFunc2 = new RoleFunction();
969                 roleFunc2.setName("Test Name3");
970                 roleFunc2.setCode("test_type3|test_instance3|*");
971                 roleFuncSet.add(roleFunc);
972                 roleFuncSet.add(roleFunc2);
973                 addRoleTest.setActive(true);
974                 addRoleTest.setName("Test2");
975                 addRoleTest.setId(2l);
976                 addRoleTest.setRoleFunctions(roleFuncSet);
977                 List<EPApp> appList = new ArrayList<>();
978                 final Map<String, String> appUebkeyParams = new HashMap<>();
979                 appList.add(app);
980                 appUebkeyParams.put("appKey", app.getUebKey());
981                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
982                                 .thenReturn(appList);
983                 List<EPRole> globalRoles = new ArrayList<>();
984                 EPRole globalRole = new EPRole();
985                 globalRole.setName("global_test");
986                 globalRole.setId(1111l);
987                 globalRole.setActive(true);
988                 globalRoles.add(globalRole);
989                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
990                 List<EPRole> epRoleList = new ArrayList<>();
991                 EPRole getEPRole = new EPRole();
992                 getEPRole.setName("Test");
993                 getEPRole.setId(2l);
994                 getEPRole.setActive(true);
995                 epRoleList.add(getEPRole);
996                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
997                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
998                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
999                                 .thenReturn(epRoleList);
1000                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1001                 HttpHeaders headers = new HttpHeaders();
1002                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1003                 JSONObject mockJsonObjectRole = new JSONObject();
1004                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1005                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1006                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1007                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1008                 mockJsonObjectPerm1.put("instance", "test_instance");
1009                 mockJsonObjectPerm1.put("action", "*");
1010                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1011                 mockJsonObjectPerm2.put("instance", "test_instance2");
1012                 mockJsonObjectPerm2.put("action", "*");
1013                 List<JSONObject> permsList = new ArrayList<>();
1014                 permsList.add(mockJsonObjectPerm1);
1015                 permsList.add(mockJsonObjectPerm2);
1016                 mockJsonObjectRole.put("name", "com.test.app.Test");
1017                 mockJsonObjectRole.put("perms", permsList);
1018                 mockJsonObjectRole.put("description",
1019                                 "{\"id\":\"2\",\"name\":\"Test\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"null\",\"appRoleId\":\"null\"}");
1020                 List<JSONObject> roleList = new ArrayList<>();
1021                 roleList.add(mockJsonObjectRole);
1022                 mockJsonObjectFinalRole.put("role", roleList);
1023                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1024                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1025                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1026                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
1027                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1028                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1029                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1030                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1031                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1032                 final Map<String, String> params = new HashMap<>();
1033                 params.put("uebKey", app.getUebKey());
1034                 params.put("roleId", String.valueOf(getEPRole.getId()));
1035                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1036                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1037                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1038                 bulkUploadUserRoles.setOrgUserId("guestT");
1039                 bulkUploadUserRoles.setRoleName("Test2");
1040                 userRolesList.add(bulkUploadUserRoles);
1041                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1042                                 .thenReturn(userRolesList);
1043                 Mockito.when(
1044                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1045                                 .thenReturn(true);
1046                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1047                                 .thenReturn("@test.com");
1048                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
1049                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1050                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
1051                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1052                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1053                 appRoleFuncsParams.put("appId", app.getId());
1054                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1055                 Mockito.when(
1056                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1057                                 .thenReturn(appRoleFunctionList);
1058                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1059                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1060                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1061                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1062                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1063                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1064                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1065                 v2RoleFunction.add(v2RoleFunction1);
1066                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1067                                 .thenReturn(v2RoleFunction);
1068                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1069                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1070                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1071                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1072                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1073                 v2RoleFunction2.add(v2RoleFunction3);
1074                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1075                                 .thenReturn(v2RoleFunction2);
1076                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1077                                 app.getUebKey());
1078                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1079                 assertEquals(expected, actual);
1080         }
1081
1082         @Test
1083         public void saveRoleExitsInDbButNotInExtAuthSystemTest() throws Exception {
1084                 EPApp app = mockApp();
1085                 app.setId(1l);
1086                 Role addRoleTest = new Role();
1087                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1088                 RoleFunction roleFunc = new RoleFunction();
1089                 roleFunc.setName("Test Name");
1090                 roleFunc.setCode("test_type|test_instance|*");
1091                 RoleFunction roleFunc2 = new RoleFunction();
1092                 roleFunc2.setName("Test Name3");
1093                 roleFunc2.setCode("test_type3|test_instance3|*");
1094                 roleFuncSet.add(roleFunc);
1095                 roleFuncSet.add(roleFunc2);
1096                 addRoleTest.setActive(true);
1097                 addRoleTest.setName("Test2");
1098                 addRoleTest.setId(2l);
1099                 addRoleTest.setRoleFunctions(roleFuncSet);
1100                 List<EPApp> appList = new ArrayList<>();
1101                 final Map<String, String> appUebkeyParams = new HashMap<>();
1102                 appList.add(app);
1103                 appUebkeyParams.put("appKey", app.getUebKey());
1104                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1105                                 .thenReturn(appList);
1106                 List<EPRole> globalRoles = new ArrayList<>();
1107                 EPRole globalRole = new EPRole();
1108                 globalRole.setName("global_test");
1109                 globalRole.setId(1111l);
1110                 globalRole.setActive(true);
1111                 globalRoles.add(globalRole);
1112                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1113                 List<EPRole> epRoleList = new ArrayList<>();
1114                 EPRole getEPRole = new EPRole();
1115                 getEPRole.setName("Test");
1116                 getEPRole.setId(2l);
1117                 getEPRole.setActive(true);
1118                 epRoleList.add(getEPRole);
1119                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1120                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
1121                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1122                                 .thenReturn(epRoleList);
1123                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1124                 HttpHeaders headers = new HttpHeaders();
1125                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1126                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1127                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1128                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1129                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1130                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1131                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1132                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1133                 final Map<String, String> params = new HashMap<>();
1134                 params.put("uebKey", app.getUebKey());
1135                 params.put("roleId", String.valueOf(getEPRole.getId()));
1136                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1137                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1138                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1139                 bulkUploadUserRoles.setOrgUserId("guestT");
1140                 bulkUploadUserRoles.setRoleName("Test2");
1141                 userRolesList.add(bulkUploadUserRoles);
1142                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1143                                 .thenReturn(userRolesList);
1144                 Mockito.when(
1145                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1146                                 .thenReturn(true);
1147                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1148                                 .thenReturn("@test.com");
1149                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1150                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1151                 appRoleFuncsParams.put("appId", app.getId());
1152                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1153                 Mockito.when(
1154                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1155                                 .thenReturn(appRoleFunctionList);
1156                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1157                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1158                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1159                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1160                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1161                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1162                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1163                 v2RoleFunction.add(v2RoleFunction1);
1164                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1165                                 .thenReturn(v2RoleFunction);
1166                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1167                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1168                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1169                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1170                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1171                 v2RoleFunction2.add(v2RoleFunction3);
1172                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1173                                 .thenReturn(v2RoleFunction2);
1174                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1175                                 app.getUebKey());
1176                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1177                 assertEquals(expected, actual);
1178         }
1179
1180         @Test
1181         public void saveGlobalRoleForPortalApplicationUpdateTest() throws Exception {
1182                 EPApp app = mockApp();
1183                 app.setId(1l);
1184                 Role addRoleTest = new Role();
1185                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1186                 RoleFunction roleFunc = new RoleFunction();
1187                 roleFunc.setName("Test Name");
1188                 roleFunc.setCode("test_type|test_instance|*");
1189                 RoleFunction roleFunc2 = new RoleFunction();
1190                 roleFunc2.setName("Test Name3");
1191                 roleFunc2.setCode("test_type3|test_instance3|*");
1192                 roleFuncSet.add(roleFunc);
1193                 roleFuncSet.add(roleFunc2);
1194                 addRoleTest.setActive(true);
1195                 addRoleTest.setName("global_test2");
1196                 addRoleTest.setId(1111l);
1197                 addRoleTest.setRoleFunctions(roleFuncSet);
1198                 List<EPApp> appList = new ArrayList<>();
1199                 final Map<String, String> appUebkeyParams = new HashMap<>();
1200                 appList.add(app);
1201                 appUebkeyParams.put("appKey", app.getUebKey());
1202                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1203                                 .thenReturn(appList);
1204                 List<EPRole> globalRoles = new ArrayList<>();
1205                 EPRole globalRole = new EPRole();
1206                 globalRole.setName("global_test");
1207                 globalRole.setId(1111l);
1208                 globalRole.setActive(true);
1209                 globalRoles.add(globalRole);
1210                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1211                 List<EPRole> epRoleList = new ArrayList<>();
1212                 EPRole getEPRole = new EPRole();
1213                 getEPRole.setName("global_test");
1214                 getEPRole.setId(1111l);
1215                 getEPRole.setActive(true);
1216                 epRoleList.add(getEPRole);
1217                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1218                 getPortalAppRoleParams.put("roleId", addRoleTest.getId());
1219                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1220                                 .thenReturn(epRoleList);
1221                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1222                 HttpHeaders headers = new HttpHeaders();
1223                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1224                 JSONObject mockJsonObjectRole = new JSONObject();
1225                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1226                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1227                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1228                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1229                 mockJsonObjectPerm1.put("instance", "test_instance");
1230                 mockJsonObjectPerm1.put("action", "*");
1231                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1232                 mockJsonObjectPerm2.put("instance", "test_instance2");
1233                 mockJsonObjectPerm2.put("action", "*");
1234                 List<JSONObject> permsList = new ArrayList<>();
1235                 permsList.add(mockJsonObjectPerm1);
1236                 permsList.add(mockJsonObjectPerm2);
1237                 mockJsonObjectRole.put("name", "com.test.app.global_test");
1238                 mockJsonObjectRole.put("perms", permsList);
1239                 mockJsonObjectRole.put("description",
1240                                 "{\"id\":\"1111\",\"name\":\"global_test\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"null\",\"appRoleId\":\"null\"}");
1241                 List<JSONObject> roleList = new ArrayList<>();
1242                 roleList.add(mockJsonObjectRole);
1243                 mockJsonObjectFinalRole.put("role", roleList);
1244                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1245                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1246                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1247                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
1248                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1249                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1250                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1251                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1252                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1253                 final Map<String, String> params = new HashMap<>();
1254                 params.put("uebKey", app.getUebKey());
1255                 params.put("roleId", String.valueOf(getEPRole.getId()));
1256                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1257                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1258                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1259                 bulkUploadUserRoles.setOrgUserId("guestT");
1260                 bulkUploadUserRoles.setRoleName("Test2");
1261                 userRolesList.add(bulkUploadUserRoles);
1262                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1263                                 .thenReturn(userRolesList);
1264                 Mockito.when(
1265                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1266                                 .thenReturn(true);
1267                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1268                                 .thenReturn("@test.com");
1269                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
1270                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1271                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
1272                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1273                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1274                 appRoleFuncsParams.put("appId", app.getId());
1275                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1276                 Mockito.when(
1277                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1278                                 .thenReturn(appRoleFunctionList);
1279                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1280                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1281                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1282                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1283                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1284                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1285                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1286                 v2RoleFunction.add(v2RoleFunction1);
1287                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1288                                 .thenReturn(v2RoleFunction);
1289                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1290                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1291                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1292                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1293                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1294                 v2RoleFunction2.add(v2RoleFunction3);
1295                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1296                                 .thenReturn(v2RoleFunction2);
1297                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1298                                 app.getUebKey());
1299                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1300                 assertEquals(expected, actual);
1301         }
1302
1303         @Test
1304         public void saveRoleForPartnerApplicationUpdateTest() throws Exception {
1305                 EPApp app = mockApp();
1306                 app.setId(2l);
1307                 Role addRoleTest = new Role();
1308                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1309                 RoleFunction roleFunc = new RoleFunction();
1310                 roleFunc.setName("Test Name");
1311                 roleFunc.setCode("test_type|test_instance|*");
1312                 RoleFunction roleFunc2 = new RoleFunction();
1313                 roleFunc2.setName("Test Name3");
1314                 roleFunc2.setCode("test_type3|test_instance3|*");
1315                 roleFuncSet.add(roleFunc);
1316                 roleFuncSet.add(roleFunc2);
1317                 addRoleTest.setActive(false);
1318                 addRoleTest.setName("Test2");
1319                 addRoleTest.setId(22l);
1320                 addRoleTest.setRoleFunctions(roleFuncSet);
1321                 List<EPApp> appList = new ArrayList<>();
1322                 final Map<String, String> appUebkeyParams = new HashMap<>();
1323                 appList.add(app);
1324                 appUebkeyParams.put("appKey", app.getUebKey());
1325                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1326                                 .thenReturn(appList);
1327                 List<EPRole> globalRoles = new ArrayList<>();
1328                 EPRole globalRole = new EPRole();
1329                 globalRole.setName("global_test");
1330                 globalRole.setId(1111l);
1331                 globalRole.setActive(true);
1332                 globalRoles.add(globalRole);
1333                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1334                 List<EPRole> epRoleList = new ArrayList<>();
1335                 EPRole getEPRole = new EPRole();
1336                 getEPRole.setName("Test2");
1337                 getEPRole.setId(2l);
1338                 getEPRole.setActive(true);
1339                 getEPRole.setAppRoleId(22l);
1340                 epRoleList.add(getEPRole);
1341                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1342                 getPortalAppRoleParams.put("appRoleId", addRoleTest.getId());
1343                 getPortalAppRoleParams.put("appId", app.getId());
1344                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPortalAppRoleParams, null))
1345                                 .thenReturn(epRoleList);
1346                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1347                 HttpHeaders headers = new HttpHeaders();
1348                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
1349                 JSONObject mockJsonObjectRole = new JSONObject();
1350                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1351                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1352                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1353                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1354                 mockJsonObjectPerm1.put("instance", "test_instance");
1355                 mockJsonObjectPerm1.put("action", "*");
1356                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1357                 mockJsonObjectPerm2.put("instance", "test_instance2");
1358                 mockJsonObjectPerm2.put("action", "*");
1359                 List<JSONObject> permsList = new ArrayList<>();
1360                 permsList.add(mockJsonObjectPerm1);
1361                 permsList.add(mockJsonObjectPerm2);
1362                 mockJsonObjectRole.put("name", "com.test.app.Test");
1363                 mockJsonObjectRole.put("perms", permsList);
1364                 mockJsonObjectRole.put("description",
1365                                 "{\"id\":\"2\",\"name\":\"Test2\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"22\"}");
1366                 List<JSONObject> roleList = new ArrayList<>();
1367                 roleList.add(mockJsonObjectRole);
1368                 mockJsonObjectFinalRole.put("role", roleList);
1369                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1370                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1371                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1372                 ResponseEntity<String> delResponse = new ResponseEntity<>(roleList.toString(), HttpStatus.OK);
1373                 final Map<String, String> getPartnerAppRoleParams = new HashMap<>();
1374                 getPartnerAppRoleParams.put(APP_ROLE_NAME_PARAM, addRoleTest.getName());
1375                 getPartnerAppRoleParams.put("appId", String.valueOf(app.getId()));
1376                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM,
1377                                 getPartnerAppRoleParams, null)).thenReturn(epRoleList);
1378                 ResponseEntity<String> updateRoleResponse = new ResponseEntity<>(HttpStatus.OK);
1379                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT),
1380                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(updateRoleResponse);
1381                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1382                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1383                 ResponseEntity<String> addRoleResponse = new ResponseEntity<>(HttpStatus.CREATED);
1384                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1385                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addRoleResponse);
1386                 final Map<String, String> params = new HashMap<>();
1387                 params.put("uebKey", app.getUebKey());
1388                 params.put("roleId", String.valueOf(getEPRole.getId()));
1389                 List<BulkUploadUserRoles> userRolesList = new ArrayList<>();
1390                 BulkUploadUserRoles bulkUploadUserRoles = new BulkUploadUserRoles();
1391                 bulkUploadUserRoles.setAppNameSpace("com.test.app");
1392                 bulkUploadUserRoles.setOrgUserId("guestT");
1393                 bulkUploadUserRoles.setRoleName("Test2");
1394                 userRolesList.add(bulkUploadUserRoles);
1395                 Mockito.when(dataAccessService.executeNamedQuery("getBulkUsersForSingleRole", params, null))
1396                                 .thenReturn(userRolesList);
1397                 Mockito.when(
1398                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1399                                 .thenReturn(true);
1400                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
1401                                 .thenReturn("@test.com");
1402                 ResponseEntity<String> mockBulkUsersUpload = new ResponseEntity<>(HttpStatus.OK);
1403                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1404                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(mockBulkUsersUpload);
1405                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1406                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1407                 appRoleFuncsParams.put("appId", app.getId());
1408                 appRoleFuncsParams.put("roleId", getEPRole.getId());
1409                 Mockito.when(
1410                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1411                                 .thenReturn(appRoleFunctionList);
1412                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1413                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1414                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1415                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1416                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1417                 List<CentralV2RoleFunction> v2RoleFunction = new ArrayList<>();
1418                 CentralV2RoleFunction v2RoleFunction1 = new CentralV2RoleFunction("test_type|test_instance|*", "Test Name");
1419                 v2RoleFunction.add(v2RoleFunction1);
1420                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1421                                 .thenReturn(v2RoleFunction);
1422                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1423                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1424                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1425                 List<CentralV2RoleFunction> v2RoleFunction2 = new ArrayList<>();
1426                 CentralV2RoleFunction v2RoleFunction3 = new CentralV2RoleFunction("test_type3|test_instance3|*", "Test Name3");
1427                 v2RoleFunction2.add(v2RoleFunction3);
1428                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1429                                 .thenReturn(v2RoleFunction2);
1430                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1431                                 app.getUebKey());
1432                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1433                 assertEquals(expected, actual);
1434         }
1435
1436         @Test
1437         public void saveGlobalRoleFunctionsForPartnerApplicationUpdateTest() throws Exception {
1438                 EPApp app = mockApp();
1439                 app.setId(2l);
1440                 Role addRoleTest = new Role();
1441                 SortedSet<RoleFunction> roleFuncSet = new TreeSet<>();
1442                 RoleFunction roleFunc = new RoleFunction();
1443                 roleFunc.setName("Test Name");
1444                 roleFunc.setCode("test_type|test_instance|*");
1445                 RoleFunction roleFunc2 = new RoleFunction();
1446                 roleFunc2.setName("Test Name3");
1447                 roleFunc2.setCode("test_type3|test_instance3|*");
1448                 roleFuncSet.add(roleFunc);
1449                 roleFuncSet.add(roleFunc2);
1450                 addRoleTest.setActive(true);
1451                 addRoleTest.setName("global_test");
1452                 addRoleTest.setId(1111l);
1453                 addRoleTest.setRoleFunctions(roleFuncSet);
1454                 List<EPApp> appList = new ArrayList<>();
1455                 final Map<String, String> appUebkeyParams = new HashMap<>();
1456                 appList.add(app);
1457                 appUebkeyParams.put("appKey", app.getUebKey());
1458                 Mockito.when(dataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", appUebkeyParams, null))
1459                                 .thenReturn(appList);
1460                 List<EPRole> globalRoles = new ArrayList<>();
1461                 EPRole globalRole = new EPRole();
1462                 globalRole.setName("global_test");
1463                 globalRole.setId(1111l);
1464                 globalRole.setActive(true);
1465                 EPRole globalRole2 = new EPRole();
1466                 globalRole2.setName("global_test2");
1467                 globalRole2.setId(2222l);
1468                 globalRole2.setActive(true);
1469                 globalRoles.add(globalRole);
1470                 globalRoles.add(globalRole2);
1471                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1472                 List<EPRole> getGlobalRoles = new ArrayList<>();
1473                 EPRole getEPGlobalRole = new EPRole();
1474                 getEPGlobalRole.setName("global_test");
1475                 getEPGlobalRole.setId(1111l);
1476                 getEPGlobalRole.setActive(true);
1477                 getGlobalRoles.add(getEPGlobalRole);
1478                 final Map<String, Long> getPortalAppRoleParams = new HashMap<>();
1479                 getPortalAppRoleParams.put("roleId", globalRole.getId());
1480                 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoleByRoleId", getPortalAppRoleParams, null))
1481                                 .thenReturn(getGlobalRoles);
1482                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
1483                 Mockito.when(epAppCommonServiceImpl.getApp(PortalConstants.PORTAL_APP_ID)).thenReturn(app);
1484                 JSONObject mockJsonObjectPerm = new JSONObject();
1485                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1486                 JSONObject mockJsonObjectPerm3 = new JSONObject();
1487                 JSONObject mockJsonObjectPerm4 = new JSONObject();
1488                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
1489                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
1490                 mockJsonObjectPerm.put("instance", "test_instance");
1491                 mockJsonObjectPerm.put("action", "*");
1492                 mockJsonObjectPerm.put("description", "Test Name");
1493                 mockJsonObjectPerm2.put("type", "com.test.app.access");
1494                 mockJsonObjectPerm2.put("instance", "test_instance2");
1495                 mockJsonObjectPerm2.put("action", "*");
1496                 mockJsonObjectPerm2.put("description", "Test Name2");
1497                 mockJsonObjectPerm3.put("type", "com.test.app.test_type3");
1498                 mockJsonObjectPerm3.put("instance", "test_instance3");
1499                 mockJsonObjectPerm3.put("action", "*");
1500                 mockJsonObjectPerm3.put("description", "Test Name3");
1501                 mockJsonObjectPerm4.put("type", "com.test.app.test_type4");
1502                 mockJsonObjectPerm4.put("instance", "test_instance4");
1503                 mockJsonObjectPerm4.put("action", "*");
1504                 mockJsonObjectPerm4.put("description", "Test Name4");
1505                 List<JSONObject> mockJson = new ArrayList<>();
1506                 mockJson.add(mockJsonObjectPerm);
1507                 mockJson.add(mockJsonObjectPerm2);
1508                 mockJson.add(mockJsonObjectPerm3);
1509                 mockJson.add(mockJsonObjectPerm4);
1510                 mockJsonObjectFinalPerm.put("perm", mockJson);
1511                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
1512                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1513                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1514                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1515                 ResponseEntity<String> postResponse = new ResponseEntity<>(HttpStatus.OK);
1516                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
1517                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(postResponse);
1518                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1519                 final Map<String, Long> epAppRoleFuncParams = new HashMap<>();
1520                 epAppRoleFuncParams.put("requestedAppId", app.getId());
1521                 epAppRoleFuncParams.put("roleId", globalRole.getId());
1522                 List<GlobalRoleWithApplicationRoleFunction> mockGlobalRoles = new ArrayList<>();
1523                 GlobalRoleWithApplicationRoleFunction mockGlobalRole = new GlobalRoleWithApplicationRoleFunction();
1524                 mockGlobalRole.setActive(true);
1525                 mockGlobalRole.setAppId(app.getId());
1526                 mockGlobalRole.setRoleId(1111l);
1527                 mockGlobalRole.setRoleName("global_test");
1528                 mockGlobalRole.setFunctionCd("test_type4|test_instance4|*");
1529                 mockGlobalRole.setFunctionName("test name");
1530                 mockGlobalRoles.add(mockGlobalRole);
1531                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRoleForRequestedApp", epAppRoleFuncParams, null))
1532                                 .thenReturn(mockGlobalRoles);
1533                 ResponseEntity<String> delResponse = new ResponseEntity<>(HttpStatus.OK);
1534                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
1535                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(delResponse);
1536                 final Map<String, Long> appRoleFuncsParams = new HashMap<>();
1537                 appRoleFuncsParams.put("appId", app.getId());
1538                 appRoleFuncsParams.put("roleId", globalRole.getId());
1539                 List<EPAppRoleFunction> appRoleFunctionList = new ArrayList<>();
1540                 EPAppRoleFunction epAppRoleFunction = new EPAppRoleFunction();
1541                 epAppRoleFunction.setAppId(app.getId());
1542                 epAppRoleFunction.setRoleAppId("1");
1543                 epAppRoleFunction.setCode("test");
1544                 epAppRoleFunction.setRoleId(1111l);
1545                 appRoleFunctionList.add(epAppRoleFunction);
1546                 Mockito.when(
1547                                 dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", appRoleFuncsParams, null))
1548                                 .thenReturn(appRoleFunctionList);
1549                 final Map<String, String> getAppFunctionParams = new HashMap<>();
1550                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc.getCode())).thenReturn("test_instance");
1551                 Mockito.when(EcompPortalUtils.getFunctionCode(roleFunc2.getCode())).thenReturn("test_instance3");
1552                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type4|test_instance4|*")).thenReturn("test_instance4");
1553                 getAppFunctionParams.put("appId", String.valueOf(app.getId()));
1554                 getAppFunctionParams.put(FUNCTION_CODE_PARAMS, roleFunc.getCode());
1555                 List<CentralV2RoleFunction> roleFunction = new ArrayList<>();
1556                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, roleFunc.getCode(),
1557                                 roleFunc.getName(), app.getId(), null);
1558                 roleFunction.add(centralV2RoleFunction);
1559                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams, null))
1560                                 .thenReturn(roleFunction);
1561                 final Map<String, String> getAppFunctionParams2 = new HashMap<>();
1562                 getAppFunctionParams2.put("appId", String.valueOf(app.getId()));
1563                 getAppFunctionParams2.put(FUNCTION_CODE_PARAMS, roleFunc2.getCode());
1564                 List<CentralV2RoleFunction> roleFunction2 = new ArrayList<>();
1565                 CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, roleFunc2.getCode(),
1566                                 roleFunc2.getName(), app.getId(), null);
1567                 roleFunction2.add(centralV2RoleFunction2);
1568                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_FUNCTION_QUERY, getAppFunctionParams2, null))
1569                                 .thenReturn(roleFunction2);
1570                 ExternalRequestFieldsValidator actual = externalAccessRolesServiceImpl.saveRoleForApplication(addRoleTest,
1571                                 app.getUebKey());
1572                 ExternalRequestFieldsValidator expected = new ExternalRequestFieldsValidator(true, "");
1573                 assertEquals(expected, actual);
1574         }
1575
1576         @Test
1577         public void syncRoleFunctionFromExternalAccessSystemTest() {
1578                 EPApp app = mockApp();
1579                 app.setId(2l);
1580                 JSONObject mockJsonObjectFinalPerm = new JSONObject();
1581                 JSONObject mockJsonObjectPerm = new JSONObject();
1582                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1583                 JSONObject mockJsonObjectPerm3 = new JSONObject();
1584                 mockJsonObjectPerm.put("type", "com.test.app.test_type");
1585                 mockJsonObjectPerm.put("instance", "test_instance");
1586                 mockJsonObjectPerm.put("action", "*");
1587                 mockJsonObjectPerm.put("description", "test_name");
1588                 List<String> rolePermList = new ArrayList<>();
1589                 rolePermList.add("com.test.app|test1");
1590                 mockJsonObjectPerm.put("roles", rolePermList);
1591                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1592                 mockJsonObjectPerm2.put("instance", "test_instance2");
1593                 List<String> rolePermList2 = new ArrayList<>();
1594                 rolePermList2.add("com.test.app|test1");
1595                 rolePermList2.add("com.test.app|test2");
1596                 rolePermList2.add("com.test.app|test6");
1597                 rolePermList2.add("com.test.app.new|global_test");
1598                 mockJsonObjectPerm2.put("action", "*");
1599                 mockJsonObjectPerm2.put("roles", rolePermList2);
1600                 mockJsonObjectPerm2.put("description", "test_name2");
1601                 mockJsonObjectPerm3.put("type", "com.test.app.access");
1602                 mockJsonObjectPerm3.put("instance", "test_instance3");
1603                 mockJsonObjectPerm3.put("action", "*");
1604                 mockJsonObjectPerm3.put("description", "test_name3");
1605                 List<JSONObject> permsList = new ArrayList<>();
1606                 permsList.add(mockJsonObjectPerm);
1607                 permsList.add(mockJsonObjectPerm2);
1608                 permsList.add(mockJsonObjectPerm3);
1609                 mockJsonObjectFinalPerm.put("perm", permsList);
1610                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalPerm.toString(), HttpStatus.OK);
1611                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1612                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1613                 final Map<String, Long> params = new HashMap<>();
1614                 params.put(APP_ID, app.getId());
1615                 List<CentralV2RoleFunction> appFunctions = new ArrayList<>();
1616                 CentralV2RoleFunction centralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*",
1617                                 "test_name", app.getId(), null);
1618                 CentralV2RoleFunction centralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_instance2", "test_name2",
1619                                 app.getId(), null);
1620                 CentralV2RoleFunction centralV2RoleFunction3 = new CentralV2RoleFunction(null, "test_instance5", "test_name5",
1621                                 app.getId(), null);
1622                 appFunctions.add(centralV2RoleFunction);
1623                 appFunctions.add(centralV2RoleFunction2);
1624                 appFunctions.add(centralV2RoleFunction3);
1625                 Mockito.when(dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null)).thenReturn(appFunctions);
1626                 List<EPRole> globalRoles = new ArrayList<>();
1627                 EPRole globalRole = new EPRole();
1628                 globalRole.setName("global_test");
1629                 globalRole.setId(1111l);
1630                 globalRole.setActive(true);
1631                 globalRoles.add(globalRole);
1632                 Mockito.when(dataAccessService.executeNamedQuery("getGlobalRolesOfPortal", null, null)).thenReturn(globalRoles);
1633                 List<EPRole> getCurrentRoleList = new ArrayList<>();
1634                 EPRole getEPRole = new EPRole();
1635                 getEPRole.setName("test1");
1636                 getEPRole.setId(2l);
1637                 getEPRole.setActive(true);
1638                 EPRole getEPRole2 = new EPRole();
1639                 getEPRole2.setName("global_test");
1640                 getEPRole2.setId(1111l);
1641                 getEPRole2.setActive(true);
1642                 EPRole getEPRole3 = new EPRole();
1643                 getEPRole3.setName("test2");
1644                 getEPRole3.setId(4l);
1645                 getEPRole3.setActive(true);
1646                 getCurrentRoleList.add(getEPRole);
1647                 getCurrentRoleList.add(getEPRole2);
1648                 getCurrentRoleList.add(getEPRole3);
1649                 final Map<String, Long> appParams = new HashMap<>();
1650                 appParams.put("appId", app.getId());
1651                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null))
1652                                 .thenReturn(getCurrentRoleList);
1653                 final Map<String, String> appSyncFuncsParams = new HashMap<>();
1654                 appSyncFuncsParams.put("appId", String.valueOf(app.getId()));
1655                 appSyncFuncsParams.put("functionCd", "");
1656                 List<CentralV2RoleFunction> roleFunctionList = new ArrayList<>();
1657                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1658                                 .thenReturn(roleFunctionList);
1659                 String code = centralV2RoleFunction.getCode();
1660                 appSyncFuncsParams.put("functionCd", code);
1661                 CentralV2RoleFunction getCentralV2RoleFunction = new CentralV2RoleFunction(null, "test_type|test_instance|*",
1662                                 "test_name", app.getId(), null);
1663                 roleFunctionList.add(getCentralV2RoleFunction);
1664                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1665                                 .thenReturn(roleFunctionList);
1666                 List<LocalRole> localRoles = new ArrayList<>();
1667                 LocalRole localRole = new LocalRole();
1668                 localRole.setRoleId(2);
1669                 localRole.setRolename("test1");
1670                 LocalRole localRole2 = new LocalRole();
1671                 localRole2.setRoleId(3);
1672                 localRole2.setRolename("test3");
1673                 localRoles.add(localRole);
1674                 localRoles.add(localRole2);
1675                 final Map<String, String> appRoleFuncParams = new HashMap<>();
1676                 appRoleFuncParams.put("functionCd", "test_type2|test_instance2|*");
1677                 appRoleFuncParams.put("appId", String.valueOf(app.getId()));
1678                 Mockito.when(dataAccessService.executeNamedQuery("getCurrentAppRoleFunctions", appRoleFuncParams, null))
1679                                 .thenReturn(localRoles);
1680                 Mockito.when(EcompPortalUtils.checkNameSpaceMatching("com.test.app", app.getNameSpace())).thenReturn(true);
1681                 Mockito.when(EcompPortalUtils.getFunctionCode("test_type2|test_instance2|*")).thenReturn("test_instance2");
1682                 appSyncFuncsParams.put("functionCd", "test_instance2");
1683                 List<CentralV2RoleFunction> roleFunctionList2 = new ArrayList<>();
1684                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1685                                 .thenReturn(roleFunctionList2);
1686                 String code2 = "test_type2|test_instance2|*";
1687                 appSyncFuncsParams.put("functionCd", code2);
1688                 CentralV2RoleFunction getCentralV2RoleFunction2 = new CentralV2RoleFunction(null, "test_type2|test_instance2|*",
1689                                 "test_name2", app.getId(), null);
1690                 roleFunctionList2.add(getCentralV2RoleFunction2);
1691                 Mockito.when(dataAccessService.executeNamedQuery("getAppFunctionOnCodeAndAppId", appSyncFuncsParams, null))
1692                                 .thenReturn(roleFunctionList2);
1693                 final Map<String, Long> params3 = new HashMap<>();
1694                 params3.put("appId", app.getId());
1695                 params3.put("roleId", getEPRole2.getId());
1696                 List<EPAppRoleFunction> currentGlobalRoleFunctionsList = new ArrayList<>();
1697                 EPAppRoleFunction addGlobalRoleFunction = new EPAppRoleFunction();
1698                 addGlobalRoleFunction.setAppId(app.getId());
1699                 addGlobalRoleFunction.setCode("test_type|test_instance|*");
1700                 addGlobalRoleFunction.setRoleId(1111l);
1701                 currentGlobalRoleFunctionsList.add(addGlobalRoleFunction);
1702                 Mockito.when(dataAccessService.executeNamedQuery("getAppRoleFunctionOnRoleIdandAppId", params3, null))
1703                                 .thenReturn(currentGlobalRoleFunctionsList);
1704                 final Map<String, String> roleParams = new HashMap<>();
1705                 roleParams.put(APP_ROLE_NAME_PARAM, "test6");
1706                 roleParams.put("appId", String.valueOf(app.getId()));
1707                 List<EPRole> roleCreated = new ArrayList<>();
1708                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM, roleParams, null))
1709                                 .thenReturn(roleCreated);
1710                 final Map<String, String> getRoleByNameParams = new HashMap<>();
1711                 getRoleByNameParams.put(APP_ROLE_NAME_PARAM, "test6");
1712                 getRoleByNameParams.put("appId", String.valueOf(app.getId()));
1713                 EPRole getNewEPRole = new EPRole();
1714                 getNewEPRole.setName("test6");
1715                 getNewEPRole.setId(8l);
1716                 getNewEPRole.setActive(true);
1717                 List<EPRole> roleCreated2 = new ArrayList<>();
1718                 roleCreated2.add(getNewEPRole);
1719                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM,
1720                                 getRoleByNameParams, null)).thenReturn(roleCreated2);
1721                 EPRole getNewEPRoleFinal = new EPRole();
1722                 getNewEPRoleFinal.setName("test6");
1723                 getNewEPRoleFinal.setId(8l);
1724                 getNewEPRoleFinal.setActive(true);
1725                 getNewEPRoleFinal.setAppRoleId(8l);
1726                 final Map<String, String> getRoleByNameParams2 = new HashMap<>();
1727                 getRoleByNameParams2.put(APP_ROLE_NAME_PARAM, "test6");
1728                 getRoleByNameParams2.put("appId", String.valueOf(app.getId()));
1729                 List<EPRole> roleCreated3 = new ArrayList<>();
1730                 roleCreated3.add(getNewEPRole);
1731                 Mockito.when(dataAccessService.executeNamedQuery(GET_ROLE_TO_UPDATE_IN_EXTERNAL_AUTH_SYSTEM,
1732                                 getRoleByNameParams2, null)).thenReturn(roleCreated3);
1733                 List<EPRole> roleInfo = new ArrayList<>();
1734                 roleInfo.add(getNewEPRoleFinal);
1735                 final Map<String, Long> getPartnerAppRoleParams = new HashMap<>();
1736                 getPartnerAppRoleParams.put("appRoleId", getNewEPRoleFinal.getId());
1737                 getPartnerAppRoleParams.put("appId", app.getId());
1738                 Mockito.when(dataAccessService.executeNamedQuery("getPartnerAppRoleByRoleId", getPartnerAppRoleParams, null))
1739                                 .thenReturn(roleInfo);
1740                 externalAccessRolesServiceImpl.syncRoleFunctionFromExternalAccessSystem(app);
1741         }
1742
1743         @Test
1744         public void syncApplicationRolesWithEcompDBTest() {
1745                 EPApp app = mockApp();
1746                 app.setId(2l);
1747                 JSONObject mockJsonObjectRole = new JSONObject();
1748                 JSONObject mockJsonObjectRole2 = new JSONObject();
1749                 JSONObject mockJsonObjectFinalRole = new JSONObject();
1750                 JSONObject mockJsonObjectPerm1 = new JSONObject();
1751                 JSONObject mockJsonObjectPerm2 = new JSONObject();
1752                 mockJsonObjectPerm1.put("type", "com.test.app.test_type");
1753                 mockJsonObjectPerm1.put("instance", "test_instance");
1754                 mockJsonObjectPerm1.put("action", "*");
1755                 mockJsonObjectPerm2.put("type", "com.test.app.test_type2");
1756                 mockJsonObjectPerm2.put("instance", "test_instance2");
1757                 mockJsonObjectPerm2.put("action", "*");
1758                 List<JSONObject> permsList = new ArrayList<>();
1759                 permsList.add(mockJsonObjectPerm1);
1760                 permsList.add(mockJsonObjectPerm2);
1761                 mockJsonObjectRole.put("name", "com.test.app.Test");
1762                 mockJsonObjectRole.put("perms", permsList);
1763                 mockJsonObjectRole.put("description",
1764                                 "{\"id\":\"2\",\"name\":\"test1\",\"active\":\"true\",\"priority\":\"null\",\"appId\":\"2\",\"appRoleId\":\"2\"}");
1765                 mockJsonObjectRole2.put("name", "com.test.app.Test2");
1766                 List<JSONObject> permsList2 = new ArrayList<>();
1767                 permsList2.add(mockJsonObjectPerm1);
1768                 mockJsonObjectRole2.put("perms", permsList2);
1769                 List<JSONObject> roleList = new ArrayList<>();
1770                 roleList.add(mockJsonObjectRole);
1771                 roleList.add(mockJsonObjectRole2);
1772                 mockJsonObjectFinalRole.put("role", roleList);
1773                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObjectFinalRole.toString(), HttpStatus.OK);
1774                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
1775                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
1776                 List<EPRole> getCurrentRoleList = new ArrayList<>();
1777                 EPRole getEPRole = new EPRole();
1778                 getEPRole.setName("Test");
1779                 getEPRole.setId(2l);
1780                 getEPRole.setAppId(app.getId());
1781                 getEPRole.setAppRoleId(2l);
1782                 getEPRole.setActive(true);
1783                 EPRole getEPRole2 = new EPRole();
1784                 getEPRole2.setName("Test3");
1785                 getEPRole2.setId(3l);
1786                 getEPRole.setAppId(app.getId());
1787                 getEPRole.setAppRoleId(3l);
1788                 getEPRole2.setActive(true);
1789                 getCurrentRoleList.add(getEPRole);
1790                 getCurrentRoleList.add(getEPRole2);
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");
1826                 extRoleParams.put(APP_ID, app.getId().toString());
1827                 EPRole getEPRoleDeactivate = new EPRole();
1828                 getEPRoleDeactivate.setName("Test3");
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(2l, "test1");
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 }