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