Reduce number of parameters
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / UserRolesCommonServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 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.assertNull;
44 import static org.junit.Assert.assertTrue;
45
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.HashMap;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Set;
52 import java.util.SortedSet;
53 import java.util.TreeSet;
54
55 import javax.servlet.http.HttpServletResponse;
56
57 import org.apache.cxf.transport.http.HTTPException;
58 import org.hibernate.Query;
59 import org.hibernate.SQLQuery;
60 import org.hibernate.Session;
61 import org.hibernate.SessionFactory;
62 import org.hibernate.Transaction;
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.externalsystemapproval.model.ExternalSystemRoleApproval;
74 import org.onap.portalapp.externalsystemapproval.model.ExternalSystemUser;
75 import org.onap.portalapp.portal.core.MockEPUser;
76 import org.onap.portalapp.portal.domain.EPApp;
77 import org.onap.portalapp.portal.domain.EPRole;
78 import org.onap.portalapp.portal.domain.EPUser;
79 import org.onap.portalapp.portal.domain.EPUserApp;
80 import org.onap.portalapp.portal.domain.EPUserAppCatalogRoles;
81 import org.onap.portalapp.portal.domain.EPUserAppRoles;
82 import org.onap.portalapp.portal.domain.EPUserAppRolesRequest;
83 import org.onap.portalapp.portal.domain.EPUserAppRolesRequestDetail;
84 import org.onap.portalapp.portal.domain.ExternalSystemAccess;
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.portalsdk.core.domain.Role;
90 import org.onap.portalsdk.core.restful.domain.EcompRole;
91 import org.onap.portalsdk.core.service.DataAccessService;
92 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
93 import org.onap.portalsdk.core.service.RoleServiceImpl;
94 import org.onap.portalsdk.core.util.SystemProperties;
95 import org.powermock.api.mockito.PowerMockito;
96 import org.powermock.core.classloader.annotations.PrepareForTest;
97 import org.powermock.modules.junit4.PowerMockRunner;
98 import org.springframework.http.HttpEntity;
99 import org.springframework.http.HttpHeaders;
100 import org.springframework.http.HttpMethod;
101 import org.springframework.http.HttpStatus;
102 import org.springframework.http.ResponseEntity;
103 import org.springframework.web.client.RestTemplate;
104
105 import com.fasterxml.jackson.core.JsonProcessingException;
106 import com.fasterxml.jackson.databind.ObjectMapper;
107
108 @RunWith(PowerMockRunner.class)
109 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, PortalConstants.class,
110                 EPCommonSystemProperties.class })
111 public class UserRolesCommonServiceImplTest {
112
113         @Mock
114         DataAccessService dataAccessService = new DataAccessServiceImpl();
115
116         @Mock
117         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
118
119         @Mock
120         ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
121
122         @Mock
123         ApplicationsRestClientService applicationsRestClientService;
124
125         @Mock
126         RoleServiceImpl roleServiceImpl = new RoleServiceImpl();
127
128         @Mock
129         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
130
131         @Mock
132         EPRoleServiceImpl epRoleServiceImpl = new EPRoleServiceImpl();
133
134         @Mock
135         RestTemplate template = new RestTemplate();
136
137         @Mock
138         SessionFactory sessionFactory;
139
140         @Mock
141         Session session;
142
143         @Mock
144         Transaction transaction;
145
146         @InjectMocks
147         UserRolesCommonServiceImpl userRolesCommonServiceImpl = new UserRolesCommonServiceImpl();
148
149         @Before
150         public void setup() {
151                 MockitoAnnotations.initMocks(this);
152                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
153                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
154         }
155
156         @After
157         public void after() {
158                 session.close();
159         }
160
161         MockEPUser mockUser = new MockEPUser();
162
163         public EPApp mockApp() {
164                 EPApp app = new EPApp();
165                 app.setName("Test");
166                 app.setImageUrl("test");
167                 app.setDescription("test");
168                 app.setNotes("test");
169                 app.setUrl("test");
170                 app.setId((long) 1);
171                 app.setAppRestEndpoint("test");
172                 app.setAlternateUrl("test");
173                 app.setName("test");
174                 app.setMlAppName("test");
175                 app.setMlAppAdminId("test");
176                 app.setUsername("test");
177                 app.setAppPassword("test");
178                 app.setOpen(false);
179                 app.setEnabled(false);
180                 app.setCentralAuth(true);
181                 app.setUebKey("test");
182                 app.setUebSecret("test");
183                 app.setUebTopicName("test");
184                 app.setAppType(1);
185                 return app;
186         }
187
188         @SuppressWarnings("unchecked")
189         @Test
190         public void getAppRolesForUserCentralizedForPortal() throws Exception {
191                 EPUser user = mockUser.mockEPUser();
192                 EPApp mockApp = mockApp();
193                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
194                 List<RoleInAppForUser> mockRoleInAppForUserList = getMockedRoleInAppUserList();
195                 List<CentralV2Role> mockCenV2Role = new ArrayList<>();
196         CentralV2Role cenV2Role = new CentralV2Role.CentralV2RoleBuilder().setId(1l).setCreated(null).setModified(null)
197                 .setCreatedId(null).setModifiedId(null).setRowNum(null).setName("test1").setActive(true)
198                 .setPriority(null).setRoleFunctions(new TreeSet<>()).setChildRoles(new TreeSet<>())
199                 .setParentRoles(new TreeSet<>()).createCentralV2Role();
200         CentralV2Role cenV2Role2 = new CentralV2Role.CentralV2RoleBuilder().setId(16l).setCreated(null)
201                 .setModified(null).setCreatedId(null).setModifiedId(null).setRowNum(null).setName("test2")
202                 .setActive(true).setPriority(null).setRoleFunctions(new TreeSet<>()).setChildRoles(new TreeSet<>())
203                 .setParentRoles(new TreeSet<>()).createCentralV2Role();
204                 mockCenV2Role.add(cenV2Role);
205                 mockCenV2Role.add(cenV2Role2);
206                 Mockito.when(externalAccessRolesServiceImpl.getRolesForApp(mockApp.getUebKey())).thenReturn(mockCenV2Role);
207                 List<EPUser> mockUserList = new ArrayList<>();
208                 mockUserList.add(user);
209                 Mockito.when((List<EPUser>) dataAccessService
210                                 .executeQuery("from EPUser where orgUserId='" + user.getOrgUserId() + "'", null))
211                                 .thenReturn(mockUserList);
212                 Mockito.when(userRolesCommonServiceImpl.getAppRolesForUser(1l, user.getOrgUserId(), true, user))
213                                 .thenReturn(mockRoleInAppForUserList);
214                 List<RoleInAppForUser> roleInAppForUser = userRolesCommonServiceImpl.getAppRolesForUser(1l, "test", true, user);
215                 assertEquals(roleInAppForUser, mockRoleInAppForUserList);
216         }
217
218         private List<RoleInAppForUser> getMockedRoleInAppUserList() {
219                 List<RoleInAppForUser> mockRoleInAppForUserList = new ArrayList<>();
220                 RoleInAppForUser mockRoleInAppForUser = new RoleInAppForUser();
221                 mockRoleInAppForUser.setIsApplied(false);
222                 mockRoleInAppForUser.setRoleId(1l);
223                 mockRoleInAppForUser.setRoleName("test1");
224                 RoleInAppForUser mockRoleInAppForUser2 = new RoleInAppForUser();
225                 mockRoleInAppForUser2.setIsApplied(false);
226                 mockRoleInAppForUser2.setRoleId(16l);
227                 mockRoleInAppForUser2.setRoleName("test2");
228                 mockRoleInAppForUserList.add(mockRoleInAppForUser);
229                 mockRoleInAppForUserList.add(mockRoleInAppForUser2);
230                 return mockRoleInAppForUserList;
231         }
232
233         @SuppressWarnings("unchecked")
234         @Test
235         public void checkTheProtectionAgainstSQLInjection() throws Exception {
236                 EPUser user = mockUser.mockEPUser();
237                 user.setId(1l);
238                 user.setOrgId(2l);
239                 Query epUserQuery = Mockito.mock(Query.class);
240                 List<EPUser> mockEPUserList = new ArrayList<>();
241                 mockEPUserList.add(user);
242
243                 Mockito.when(session.createQuery("from :name where orgUserId=:userId")).thenReturn(epUserQuery);
244                 Mockito.when(epUserQuery.setParameter("name",EPUser.class.getName())).thenReturn(epUserQuery);
245                 Mockito.when(epUserQuery.setParameter("userId",user.getOrgUserId() + "; select * from " + EPUser.class.getName() +";")).thenReturn(epUserQuery);
246                 userRolesCommonServiceImpl.createLocalUserIfNecessary(user.getOrgUserId(),true);
247
248                 Mockito.when(session.createQuery("from :name where orgUserId=:userId")).thenReturn(epUserQuery);
249                 Mockito.when(epUserQuery.setParameter("name",EPUser.class.getName())).thenReturn(epUserQuery);
250                 Mockito.when(epUserQuery.setParameter("userId",user.getOrgUserId())).thenReturn(epUserQuery);
251                 userRolesCommonServiceImpl.createLocalUserIfNecessary(user.getOrgUserId(),true);
252         }
253
254         @SuppressWarnings("unchecked")
255         @Test
256         public void getAppRolesForUserNonCentralizedForPortal() throws Exception {
257                 EPUser user = mockUser.mockEPUser();
258                 user.setId(1l);
259                 EPApp mockApp = mockApp();
260                 mockApp.setCentralAuth(false);
261                 EPRole mockEPRole = new EPRole();
262                 mockEPRole.setId(1l);
263                 mockEPRole.setName("test1");
264                 mockEPRole.setActive(true);
265                 SortedSet<EPUserApp> mockUserApps = new TreeSet<EPUserApp>();
266                 EPUserApp mockEPUserApp = new EPUserApp();
267                 mockEPUserApp.setApp(mockApp);
268                 mockEPUserApp.setRole(mockEPRole);
269                 mockEPUserApp.setUserId(1l);
270                 mockUserApps.add(mockEPUserApp);
271                 user.setEPUserApps(mockUserApps);
272                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
273                 List<RoleInAppForUser> mockRoleInAppForUserListNonCentralizedList = getMockedRoleInAppUserList();
274                 List<Role> mockRole = new ArrayList<>();
275                 Role role = new Role();
276                 role.setId(1l);
277                 role.setName("test1");
278                 role.setActive(true);
279                 Role role2 = new Role();
280                 role.setId(16l);
281                 role.setName("test2");
282                 role.setActive(true);
283                 mockRole.add(role);
284                 mockRole.add(role2);
285                 Mockito.when(roleServiceImpl.getAvailableRoles(user.getOrgUserId())).thenReturn(mockRole);
286                 List<EPUser> mockUserList = new ArrayList<>();
287                 mockUserList.add(user);
288                 Mockito.when((List<EPUser>) dataAccessService
289                                 .executeQuery("from EPUser where orgUserId='" + user.getOrgUserId() + "'", null))
290                                 .thenReturn(mockUserList);
291                 Mockito.when(userRolesCommonServiceImpl.getAppRolesForUser(1l, user.getOrgUserId(), true, user))
292                                 .thenReturn(mockRoleInAppForUserListNonCentralizedList);
293                 List<RoleInAppForUser> roleInAppForUserNonCentralized = userRolesCommonServiceImpl.getAppRolesForUser(1l,
294                                 user.getOrgUserId(), true, user);
295                 assertNull(roleInAppForUserNonCentralized);
296         }
297
298         @Test
299         public void getAppRolesForCentralizedPartnerAppTest() throws Exception {
300                 EPUser user = mockUser.mockEPUser();
301                 EPApp mockApp = mockApp();
302                 mockApp.setId(2l);
303                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
304                 List<RoleInAppForUser> mockRoleInAppForUserList = getMockedRoleInAppUserList();
305                 List<EPRole> mockEPRoleList = new ArrayList<>();
306                 EPRole mockEpRole = new EPRole();
307                 mockEpRole.setActive(true);
308                 mockEpRole.setAppId(2l);
309                 mockEpRole.setName("test1");
310                 mockEpRole.setAppRoleId(333l);
311                 mockEpRole.setId(1l);
312                 EPRole mockEpRole2 = new EPRole();
313                 mockEpRole2.setActive(true);
314                 mockEpRole2.setAppId(2l);
315                 mockEpRole2.setName("test2");
316                 mockEpRole2.setAppRoleId(444l);
317                 mockEpRole2.setId(16l);
318                 mockEPRoleList.add(mockEpRole);
319                 mockEPRoleList.add(mockEpRole2);
320                 final Map<String, Long> appParams = new HashMap<>();
321                 appParams.put("appId", 2l);
322                 Mockito.when(dataAccessService.executeNamedQuery("getActiveRolesOfApplication", appParams, null))
323                                 .thenReturn(mockEPRoleList);
324                 final Map<String, String> params = new HashMap<>();
325                 params.put("orgUserIdValue", "guestT");
326                 List<EPUser> mockEPUserList = new ArrayList<>();
327                 mockEPUserList.add(user);
328                 Mockito.when(dataAccessService.executeNamedQuery("epUserAppId", params, null)).thenReturn(mockEPUserList);
329                 final Map<String, Long> userParams = new HashMap<>();
330                 userParams.put("appId", mockApp.getId());
331                 userParams.put("userId", mockEPUserList.get(0).getId());
332                 List<EPUserAppCurrentRoles> epUserAppCurrentRolesList = new ArrayList<>();
333                 EPUserAppCurrentRoles epUserAppCurrentRoles = new EPUserAppCurrentRoles();
334                 epUserAppCurrentRoles.setRoleId(444l);
335                 epUserAppCurrentRoles.setRoleName("TestPartnerRole2");
336                 epUserAppCurrentRoles.setUserId(mockEPUserList.get(0).getId());
337                 epUserAppCurrentRolesList.add(epUserAppCurrentRoles);
338                 Mockito.when(dataAccessService.executeNamedQuery("getUserAppCurrentRoles", userParams, null))
339                                 .thenReturn(epUserAppCurrentRolesList);
340                 Mockito.when(userRolesCommonServiceImpl.getAppRolesForUser(2l, user.getOrgUserId(), true, user))
341                                 .thenReturn(mockRoleInAppForUserList);
342                 List<RoleInAppForUser> roleInAppForUser = userRolesCommonServiceImpl.getAppRolesForUser(2l, user.getOrgUserId(),
343                                 true, user);
344                 assertNotEquals(roleInAppForUser, mockRoleInAppForUserList);
345         }
346
347         @Test
348         public void getAppRolesForNonCentralizedPartnerAppTest() throws Exception {
349                 EPUser user = mockUser.mockEPUser();
350                 user.setId(2l);
351                 List<EPUser> mockEpUserList = new ArrayList<>();
352                 mockEpUserList.add(user);
353                 EPApp mockApp = mockApp();
354                 mockApp.setId(2l);
355                 mockApp.setCentralAuth(false);
356                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
357                 List<RoleInAppForUser> mockRoleInAppForUserList = new ArrayList<>();
358                 RoleInAppForUser mockRoleInAppForUser = new RoleInAppForUser();
359                 mockRoleInAppForUser.setIsApplied(false);
360                 mockRoleInAppForUser.setRoleId(333l);
361                 mockRoleInAppForUser.setRoleName("test1");
362                 RoleInAppForUser mockRoleInAppForUser2 = new RoleInAppForUser();
363                 mockRoleInAppForUser2.setIsApplied(false);
364                 mockRoleInAppForUser2.setRoleId(777l);
365                 mockRoleInAppForUser2.setRoleName("test2");
366                 RoleInAppForUser mockRoleInAppForUser3 = new RoleInAppForUser();
367                 mockRoleInAppForUser3.setIsApplied(false);
368                 mockRoleInAppForUser3.setRoleId(888l);
369                 mockRoleInAppForUser3.setRoleName("test5");
370                 mockRoleInAppForUserList.add(mockRoleInAppForUser);
371                 mockRoleInAppForUserList.add(mockRoleInAppForUser2);
372                 mockRoleInAppForUserList.add(mockRoleInAppForUser3);
373                 List<EcompRole> mockEcompRoleList = new ArrayList<>();
374                 EcompRole mockEcompRole = new EcompRole();
375                 mockEcompRole.setId(333l);
376                 mockEcompRole.setName("test1");
377                 EcompRole mockEcompRole2 = new EcompRole();
378                 mockEcompRole2.setId(777l);
379                 mockEcompRole2.setName("test2");
380                 EcompRole mockEcompRole3 = new EcompRole();
381                 mockEcompRole3.setId(888l);
382                 mockEcompRole3.setName("test5");
383                 mockEcompRoleList.add(mockEcompRole);
384                 mockEcompRoleList.add(mockEcompRole2);
385                 mockEcompRoleList.add(mockEcompRole3);
386                 EcompRole[] mockEcompRoleArray = mockEcompRoleList.toArray(new EcompRole[mockEcompRoleList.size()]);
387                 List<EPRole> mockEPRoleList = new ArrayList<>();
388                 EPRole mockEpRole = new EPRole();
389                 mockEpRole.setActive(true);
390                 mockEpRole.setAppId(2l);
391                 mockEpRole.setName("test1");
392                 mockEpRole.setAppRoleId(333l);
393                 mockEpRole.setId(16l);
394                 EPRole mockEpRole2 = new EPRole();
395                 mockEpRole2.setActive(true);
396                 mockEpRole2.setAppId(2l);
397                 mockEpRole2.setName("test3");
398                 mockEpRole2.setAppRoleId(555l);
399                 mockEpRole2.setId(15l);
400                 EPRole mockEpRole3 = new EPRole();
401                 mockEpRole3.setActive(true);
402                 mockEpRole3.setAppId(2l);
403                 mockEpRole3.setName("test4");
404                 mockEpRole3.setAppRoleId(888l);
405                 mockEpRole3.setId(17l);
406                 mockEPRoleList.add(mockEpRole);
407                 mockEPRoleList.add(mockEpRole2);
408                 mockEPRoleList.add(mockEpRole3);
409                 List<EPUserApp> mockUserRolesList = new ArrayList<>();
410                 EPUserApp mockEpUserApp = new EPUserApp();
411                 mockEpUserApp.setApp(mockApp);
412                 mockEpUserApp.setRole(mockEpRole2);
413                 mockEpUserApp.setUserId(user.getId());
414                 mockUserRolesList.add(mockEpUserApp);
415                 List<FunctionalMenuRole> mockFunctionalMenuRolesList = new ArrayList<>();
416                 FunctionalMenuRole mockFunctionalMenuRole = new FunctionalMenuRole();
417                 mockFunctionalMenuRole.setAppId((int) (long) mockApp.getId());
418                 mockFunctionalMenuRole.setRoleId((int) (long) 15l);
419                 mockFunctionalMenuRole.setMenuId(10l);
420                 mockFunctionalMenuRole.setId(10);
421                 mockFunctionalMenuRolesList.add(mockFunctionalMenuRole);
422                 List<FunctionalMenuItem> mockFunctionalMenuItemList = new ArrayList<>();
423                 FunctionalMenuItem mockFunctionalMenuItem = new FunctionalMenuItem();
424                 List<Integer> mockRolesList = new ArrayList<>();
425                 Integer role1 = 1;
426                 mockRolesList.add(role1);
427                 mockFunctionalMenuItem.setRestrictedApp(false);
428                 mockFunctionalMenuItem.setRoles(mockRolesList);
429                 mockFunctionalMenuItem.setUrl("http://test.com");
430                 mockFunctionalMenuItemList.add(mockFunctionalMenuItem);
431                 Query epRoleQuery = Mockito.mock(Query.class);
432                 Query epUserAppsQuery = Mockito.mock(Query.class);
433                 Query epFunctionalMenuQuery = Mockito.mock(Query.class);
434                 Query epFunctionalMenuQuery2 = Mockito.mock(Query.class);
435                 Query epFunctionalMenuItemQuery = Mockito.mock(Query.class);
436                 Query epUserListQuery = Mockito.mock(Query.class);
437                 Query epUserRolesListQuery = Mockito.mock(Query.class);
438                 Mockito.when(applicationsRestClientService.get(EcompRole[].class, mockApp.getId(), "/roles"))
439                                 .thenReturn(mockEcompRoleArray);
440                 // syncAppRolesTest
441
442                 Mockito.when(session.createQuery("from EPRole where appId = :appId"))
443                                 .thenReturn(epRoleQuery);
444
445                 Mockito.when(epRoleQuery.setParameter("appId",mockApp.getId())).thenReturn(epRoleQuery);
446
447                 Mockito.doReturn(mockEPRoleList).when(epRoleQuery).list();
448                 Mockito.when(session.createQuery("from EPUserApp where app.id=:appId and role_id=:roleId"))
449                                 .thenReturn(epUserAppsQuery);
450                 Mockito.when(epUserAppsQuery.setParameter("appId",mockApp.getId())).thenReturn(epUserAppsQuery);
451                 Mockito.when(epUserAppsQuery.setParameter("roleId",15l)).thenReturn(epUserAppsQuery);
452                 Mockito.doReturn(mockUserRolesList).when(epUserAppsQuery).list();
453
454                 Mockito.when(session.createQuery("from FunctionalMenuRole where roleId=:roleId"))
455                                 .thenReturn(epFunctionalMenuQuery);
456                 Mockito.when(epFunctionalMenuQuery.setParameter("roleId",15l)).thenReturn(epFunctionalMenuQuery);
457                 Mockito.doReturn(mockFunctionalMenuRolesList).when(epFunctionalMenuQuery).list();
458
459                 Mockito.when(session.createQuery("from FunctionalMenuRole where menuId=:menuId"))
460                                 .thenReturn(epFunctionalMenuQuery2);
461                 Mockito.when(epFunctionalMenuQuery2.setParameter(Matchers.anyString(),Matchers.anyLong())).thenReturn(epFunctionalMenuQuery2);
462                 Mockito.doReturn(mockFunctionalMenuRolesList).when(epFunctionalMenuQuery2).list();
463
464                 Mockito.when(session.createQuery("from FunctionalMenuItem where menuId=:menuId"))
465                                 .thenReturn(epFunctionalMenuItemQuery);
466                 Mockito.when(epFunctionalMenuItemQuery.setParameter(Matchers.anyString(),Matchers.anyLong())).thenReturn(epFunctionalMenuItemQuery);
467                 Mockito.doReturn(mockFunctionalMenuItemList).when(epFunctionalMenuItemQuery).list();
468                 List<EcompRole> mockEcompRoleList2 = new ArrayList<>();
469                 EcompRole mockUserAppRoles = new EcompRole();
470                 mockUserAppRoles.setId(333l);
471                 mockUserAppRoles.setName("test1");
472                 EcompRole mockUserAppRoles2 = new EcompRole();
473                 mockUserAppRoles2.setId(777l);
474                 mockUserAppRoles2.setName("test2");
475                 mockEcompRoleList2.add(mockUserAppRoles);
476                 mockEcompRoleList2.add(mockUserAppRoles2);
477                 EcompRole[] mockEcompRoleArray2 = mockEcompRoleList2.toArray(new EcompRole[mockEcompRoleList2.size()]);
478                 Mockito.when(applicationsRestClientService.get(EcompRole[].class, mockApp.getId(),
479                                 String.format("/user/%s/roles", user.getOrgUserId()))).thenReturn(mockEcompRoleArray2);
480                 
481
482                 Mockito.when(session.createQuery(
483                                 "from EPUser where orgUserId=:userId"))
484                                 .thenReturn(epUserListQuery);
485                 Mockito.when(epUserListQuery.setParameter("userId","guestT")).thenReturn(epUserListQuery);
486                 Mockito.doReturn(mockEpUserList).when(epUserListQuery).list();
487                 
488                 List<EPUserApp> mockUserRolesList2 = new ArrayList<>();
489                 EPUserApp mockEpUserAppRoles = new EPUserApp();
490                 mockEpUserAppRoles.setApp(mockApp);
491                 mockEpUserAppRoles.setRole(mockEpRole3);
492                 mockEpUserAppRoles.setUserId(user.getId());
493                 mockUserRolesList2.add(mockEpUserAppRoles);
494                 Mockito.when(session.createQuery(
495                                 "from EPUserApp where app.id=:appId and userId=:userId and role.active = 'Y'"))
496                                 .thenReturn(epUserRolesListQuery);
497                 
498                 Mockito.when(epUserRolesListQuery.setParameter("appId",2)).thenReturn(epUserRolesListQuery);
499                 Mockito.when(epUserRolesListQuery.setParameter("userId",2)).thenReturn(epUserRolesListQuery);
500
501                 Mockito.doReturn(mockUserRolesList2).when(epUserRolesListQuery).list();
502
503                 
504                 List<RoleInAppForUser> roleInAppForUser = userRolesCommonServiceImpl.getAppRolesForUser(2l, user.getOrgUserId(),
505                                 true, user);
506                 assertEquals(roleInAppForUser, mockRoleInAppForUserList);
507         }
508
509         @SuppressWarnings("unchecked")
510         @Test
511         public void setAppWithUserRoleStateForUserTestForCentralizedAppForPortal() throws Exception {
512                 PowerMockito.mockStatic(SystemProperties.class);
513                 PowerMockito.mockStatic(EcompPortalUtils.class);
514                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
515                 PowerMockito.mockStatic(PortalConstants.class);
516                 EPUser user = mockUser.mockEPUser();
517                 user.setId(2l);
518                 List<EPUser> mockEpUserList = new ArrayList<>();
519                 mockEpUserList.add(user);
520                 EPApp mockApp = mockApp();
521                 mockApp.setNameSpace("com.test.com");
522                 mockApp.setId(1l);
523                 mockApp.setCentralAuth(true);
524                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
525                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
526                 AppWithRolesForUser mockWithRolesForUser = new AppWithRolesForUser();
527                 List<RoleInAppForUser> mockRoleInAppForUserList = new ArrayList<>();
528                 RoleInAppForUser mockRoleInAppForUser = new RoleInAppForUser();
529                 mockRoleInAppForUser.setIsApplied(true);
530                 mockRoleInAppForUser.setRoleId(1l);
531                 mockRoleInAppForUser.setRoleName("test1");
532                 RoleInAppForUser mockRoleInAppForUser2 = new RoleInAppForUser();
533                 mockRoleInAppForUser2.setIsApplied(true);
534                 mockRoleInAppForUser2.setRoleId(1000l);
535                 mockRoleInAppForUser2.setRoleName("test3");
536                 mockRoleInAppForUserList.add(mockRoleInAppForUser);
537                 mockRoleInAppForUserList.add(mockRoleInAppForUser2);
538                 mockWithRolesForUser.setAppId(mockApp.getId());
539                 mockWithRolesForUser.setAppName(mockApp.getName());
540                 mockWithRolesForUser.setOrgUserId(user.getOrgUserId());
541                 mockWithRolesForUser.setAppRoles(mockRoleInAppForUserList);
542                 List<EPUserAppRolesRequest> mockEpRequestIdValList = new ArrayList<EPUserAppRolesRequest>();
543                 Set<EPUserAppRolesRequestDetail> mockEpUserAppRolesRequestDetailList = new TreeSet<>();
544                 EPUserAppRolesRequestDetail mockEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
545                 mockEpUserAppRolesRequestDetail.setId(2l);
546                 mockEpUserAppRolesRequestDetail.setReqType("P");
547                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
548                 EPUserAppRolesRequest mockEPUserAppRolesRequest = new EPUserAppRolesRequest();
549                 mockEPUserAppRolesRequest.setAppId(mockApp.getId());
550                 mockEPUserAppRolesRequest.setId(1l);
551                 mockEPUserAppRolesRequest.setRequestStatus("P");
552                 mockEPUserAppRolesRequest.setUserId(user.getId());
553                 mockEPUserAppRolesRequest.setEpRequestIdDetail(mockEpUserAppRolesRequestDetailList);
554                 mockEpRequestIdValList.add(mockEPUserAppRolesRequest);
555                 mockEpUserAppRolesRequestDetail.setEpRequestIdData(mockEPUserAppRolesRequest);
556                 mockEpUserAppRolesRequestDetailList.add(mockEpUserAppRolesRequestDetail);
557
558                 // Update Requests if any requests are pending
559                 final Map<String, Long> params = new HashMap<>();
560                 params.put("appId", mockApp.getId());
561                 params.put("userId", user.getId());
562                 Mockito.when((List<EPUserAppRolesRequest>) dataAccessService.executeNamedQuery("userAppRolesRequestList",
563                                 params, null)).thenReturn(mockEpRequestIdValList);
564                 mockEPUserAppRolesRequest.setUpdatedDate(new Date());
565                 mockEPUserAppRolesRequest.setRequestStatus("O");
566                 HashMap<String, Long> additionalUpdateParam = new HashMap<String, Long>();
567                 Mockito.doNothing().when(dataAccessService).saveDomainObject(mockEPUserAppRolesRequest, additionalUpdateParam);
568                 List<EPUserAppRolesRequestDetail> mockGetEpUserAppRolesRequestDetailList = new ArrayList<EPUserAppRolesRequestDetail>();
569                 EPUserAppRolesRequestDetail mockGetEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
570                 mockEpUserAppRolesRequestDetail.setId(2l);
571                 mockEpUserAppRolesRequestDetail.setReqType("P");
572                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
573                 mockGetEpUserAppRolesRequestDetailList.add(mockGetEpUserAppRolesRequestDetail);
574                 // Updates in External Auth System
575                 final Map<String, String> userParams = new HashMap<>();
576                 userParams.put("orgUserIdValue", user.getOrgUserId());
577                 Mockito.when((List<EPUser>) dataAccessService.executeNamedQuery("epUserAppId", userParams, null))
578                                 .thenReturn(mockEpUserList);
579                 PowerMockito
580                                 .when(EPCommonSystemProperties
581                                                 .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
582                                 .thenReturn(true);
583                 PowerMockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
584                                 .thenReturn("@test.com");
585                 HttpHeaders headers = new HttpHeaders();
586                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
587                 JSONObject mockJsonObject1 = new JSONObject();
588                 JSONObject mockJsonObject2 = new JSONObject();
589                 JSONObject mockJsonObject3 = new JSONObject();
590                 mockJsonObject1.put("name", "com.test.com.test1");
591                 mockJsonObject2.put("name", "com.test.com.test2");
592                 List<JSONObject> mockJson = new ArrayList<>();
593                 mockJson.add(mockJsonObject1);
594                 mockJson.add(mockJsonObject2);
595                 mockJsonObject3.put("role", mockJson);
596                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObject3.toString(), HttpStatus.OK);
597                 Mockito.when(externalAccessRolesServiceImpl.getUserRolesFromExtAuthSystem(Matchers.anyString(), Matchers.any(HttpEntity.class))).thenReturn(getResponse);
598                 EPRole mockEPRole = new EPRole();
599                 mockEPRole.setActive(true);
600                 mockEPRole.setAppId(null);
601                 mockEPRole.setAppRoleId(null);
602                 mockEPRole.setId(1l);
603                 mockEPRole.setName("test1");
604                 EPRole mockEPRole2 = new EPRole();
605                 mockEPRole2.setActive(true);
606                 mockEPRole2.setAppId(null);
607                 mockEPRole2.setAppRoleId(null);
608                 mockEPRole2.setId(16l);
609                 mockEPRole2.setName("test2");
610                 EPRole mockEPRole3 = new EPRole();
611                 mockEPRole3.setActive(true);
612                 mockEPRole3.setAppId(null);
613                 mockEPRole3.setAppRoleId(null);
614                 mockEPRole3.setId(1000l);
615                 mockEPRole3.setName("test3");
616                 Map<String, EPRole> mockEPRoleList = new HashMap<>();
617                 mockEPRoleList.put("test1", mockEPRole);
618                 mockEPRoleList.put("test2", mockEPRole2);
619                 mockEPRoleList.put("test3", mockEPRole3);
620                 Mockito.when(externalAccessRolesServiceImpl.getAppRoleNamesWithUnderscoreMap(mockApp)).thenReturn(mockEPRoleList);
621                 final Map<String, Long> params2 = new HashMap<>();
622                 params2.put("appId", mockApp.getId());
623                 params2.put("userId", user.getId());
624                 List<EcompUserAppRoles> mockEPuserAppList = getCurrentUserRoles(user, mockApp);
625                 Mockito.when(dataAccessService.executeNamedQuery("getUserAppExistingRoles", params2, null))
626                                 .thenReturn(mockEPuserAppList);
627                 final Map<String, Long> epDetailParams = new HashMap<>();
628                 epDetailParams.put("reqId", mockEPUserAppRolesRequest.getId());
629                 Mockito.when(dataAccessService.executeNamedQuery("userAppRolesRequestDetailList", epDetailParams, null))
630                                 .thenReturn(mockGetEpUserAppRolesRequestDetailList);
631
632                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
633                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
634                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
635
636                 ResponseEntity<String> deleteResponse = new ResponseEntity<>(HttpStatus.OK);
637                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
638                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(deleteResponse);
639
640                 // Updates in EP DB
641                 Query epsetAppWithUserRoleGetUserQuery = Mockito.mock(Query.class);
642                 Query epsetAppWithUserRoleGetUserAppsQuery = Mockito.mock(Query.class);
643                 Query epsetAppWithUserRoleGetRolesQuery = Mockito.mock(Query.class);
644                 SQLQuery epsetAppWithUserRoleUpdateEPRoleQuery = Mockito.mock(SQLQuery.class);
645                 Mockito.when(session.get(EPApp.class, mockApp.getId())).thenReturn(mockApp);
646                 Mockito.when(session
647                                 .createQuery("from " + EPUser.class.getName() + " where orgUserId='" + user.getOrgUserId() + "'"))
648                                 .thenReturn(epsetAppWithUserRoleGetUserQuery);
649                 Mockito.doReturn(mockEpUserList).when(epsetAppWithUserRoleGetUserQuery).list();
650                 List<EPUserApp> mockUserRolesList2 = new ArrayList<>();
651                 EPUserApp mockEpUserAppRoles = new EPUserApp();
652                 mockEpUserAppRoles.setApp(mockApp);
653                 mockEpUserAppRoles.setRole(mockEPRole);
654                 mockEpUserAppRoles.setUserId(user.getId());
655                 mockUserRolesList2.add(mockEpUserAppRoles);
656                 Mockito.when(session.createQuery(
657                                 "from org.onap.portalapp.portal.domain.EPUserApp where app.id=1 and role.active = 'Y' and userId=2"))
658                                 .thenReturn(epsetAppWithUserRoleGetUserAppsQuery);
659                 Mockito.doReturn(mockUserRolesList2).when(epsetAppWithUserRoleGetUserAppsQuery).list();
660                 List<EPRole> mockEPRoles = new ArrayList<>();
661                 mockEPRoles.add(mockEPRole2);
662                 mockEPRoles.add(mockEPRole3);
663                 Mockito.when(session.createQuery("from " + EPRole.class.getName() + " where appId is null and id != 1"))
664                                 .thenReturn(epsetAppWithUserRoleGetRolesQuery);
665                 Mockito.doReturn(mockEPRoles).when(epsetAppWithUserRoleGetRolesQuery).list();
666                 Mockito.when(session.createSQLQuery("update fn_role set app_id = null where app_id = 1 "))
667                                 .thenReturn(epsetAppWithUserRoleUpdateEPRoleQuery);
668                 ExternalRequestFieldsValidator actual = userRolesCommonServiceImpl.setAppWithUserRoleStateForUser(user, mockWithRolesForUser);
669                 assertFalse(actual.isResult());
670         }
671
672         private List<EcompUserAppRoles> getCurrentUserRoles(EPUser user, EPApp mockApp) {
673                 List<EcompUserAppRoles> mockEPuserAppList = new ArrayList<>();
674                 EcompUserAppRoles mockEcompUserAppRoles = new EcompUserAppRoles();
675                 mockEcompUserAppRoles.setAppId(String.valueOf(mockApp.getId()));
676                 mockEcompUserAppRoles.setRoleId(1l);
677                 mockEcompUserAppRoles.setRoleName("test1");
678                 mockEcompUserAppRoles.setUserId(user.getId());
679                 mockEPuserAppList.add(mockEcompUserAppRoles);
680                 return mockEPuserAppList;
681         }
682
683         @SuppressWarnings("unchecked")
684         @Test
685         public void setAppWithUserRoleStateForUserTestForNonCentralizedApp() throws Exception {
686                 PowerMockito.mockStatic(SystemProperties.class);
687                 PowerMockito.mockStatic(EcompPortalUtils.class);
688                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
689                 PowerMockito.mockStatic(PortalConstants.class);
690                 EPUser user = mockUser.mockEPUser();
691                 user.setId(2l);
692                 List<EPUser> mockEpUserList = new ArrayList<>();
693                 mockEpUserList.add(user);
694                 EPApp mockApp = mockApp();
695                 mockApp.setNameSpace("com.test.com");
696                 mockApp.setId(2l);
697                 mockApp.setCentralAuth(false);
698                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
699                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
700                 AppWithRolesForUser mockWithRolesForUser = new AppWithRolesForUser();
701                 List<RoleInAppForUser> mockRoleInAppForUserList = new ArrayList<>();
702                 RoleInAppForUser mockRoleInAppForUser = new RoleInAppForUser();
703                 mockRoleInAppForUser.setIsApplied(true);
704                 mockRoleInAppForUser.setRoleId(1l);
705                 mockRoleInAppForUser.setRoleName("test1");
706                 RoleInAppForUser mockRoleInAppForUser2 = new RoleInAppForUser();
707                 mockRoleInAppForUser2.setIsApplied(true);
708                 mockRoleInAppForUser2.setRoleId(1000l);
709                 mockRoleInAppForUser2.setRoleName("test3");
710                 mockRoleInAppForUserList.add(mockRoleInAppForUser);
711                 mockRoleInAppForUserList.add(mockRoleInAppForUser2);
712                 mockWithRolesForUser.setAppId(mockApp.getId());
713                 mockWithRolesForUser.setAppName(mockApp.getName());
714                 mockWithRolesForUser.setOrgUserId(user.getOrgUserId());
715                 mockWithRolesForUser.setAppRoles(mockRoleInAppForUserList);
716                 List<EPUserAppRolesRequest> mockEpRequestIdValList = new ArrayList<EPUserAppRolesRequest>();
717                 Set<EPUserAppRolesRequestDetail> mockEpUserAppRolesRequestDetailList = new TreeSet<>();
718                 EPUserAppRolesRequestDetail mockEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
719                 mockEpUserAppRolesRequestDetail.setId(2l);
720                 mockEpUserAppRolesRequestDetail.setReqType("P");
721                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
722                 EPUserAppRolesRequest mockEPUserAppRolesRequest = new EPUserAppRolesRequest();
723                 mockEPUserAppRolesRequest.setAppId(mockApp.getId());
724                 mockEPUserAppRolesRequest.setId(1l);
725                 mockEPUserAppRolesRequest.setRequestStatus("P");
726                 mockEPUserAppRolesRequest.setUserId(user.getId());
727                 mockEPUserAppRolesRequest.setEpRequestIdDetail(mockEpUserAppRolesRequestDetailList);
728                 mockEpRequestIdValList.add(mockEPUserAppRolesRequest);
729                 mockEpUserAppRolesRequestDetail.setEpRequestIdData(mockEPUserAppRolesRequest);
730                 mockEpUserAppRolesRequestDetailList.add(mockEpUserAppRolesRequestDetail);
731
732                 // Update Requests if any requests are pending
733                 final Map<String, Long> params = new HashMap<>();
734                 params.put("appId", mockApp.getId());
735                 params.put("userId", user.getId());
736                 Mockito.when((List<EPUserAppRolesRequest>) dataAccessService.executeNamedQuery("userAppRolesRequestList",
737                                 params, null)).thenReturn(mockEpRequestIdValList);
738                 mockEPUserAppRolesRequest.setUpdatedDate(new Date());
739                 mockEPUserAppRolesRequest.setRequestStatus("O");
740                 HashMap<String, Long> additionalUpdateParam = new HashMap<String, Long>();
741                 Mockito.doNothing().when(dataAccessService).saveDomainObject(mockEPUserAppRolesRequest, additionalUpdateParam);
742                 List<EPUserAppRolesRequestDetail> mockGetEpUserAppRolesRequestDetailList = new ArrayList<EPUserAppRolesRequestDetail>();
743                 EPUserAppRolesRequestDetail mockGetEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
744                 mockEpUserAppRolesRequestDetail.setId(2l);
745                 mockEpUserAppRolesRequestDetail.setReqType("P");
746                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
747                 mockGetEpUserAppRolesRequestDetailList.add(mockGetEpUserAppRolesRequestDetail);
748                 final Map<String, Long> epDetailParams = new HashMap<>();
749                 epDetailParams.put("reqId", mockEPUserAppRolesRequest.getId());
750
751                 final Map<String, String> userParams = new HashMap<>();
752                 userParams.put("orgUserIdValue", user.getOrgUserId());
753                 Mockito.when((List<EPUser>) dataAccessService.executeNamedQuery("epUserAppId", userParams, null))
754                                 .thenReturn(mockEpUserList);
755
756                 PowerMockito
757                                 .when(EPCommonSystemProperties
758                                                 .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
759                                 .thenReturn(true);
760                 PowerMockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
761                                 .thenReturn("@test.com");
762                 HttpHeaders headers = new HttpHeaders();
763                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
764                 EPRole mockEPRole = new EPRole();
765                 mockEPRole.setActive(true);
766                 mockEPRole.setAppId(2l);
767                 mockEPRole.setAppRoleId(null);
768                 mockEPRole.setId(10l);
769                 mockEPRole.setAppRoleId(1l);
770                 mockEPRole.setName("test1");
771                 EPRole mockEPRole2 = new EPRole();
772                 mockEPRole2.setActive(true);
773                 mockEPRole2.setAppId(2l);
774                 mockEPRole2.setAppRoleId(null);
775                 mockEPRole2.setId(160l);
776                 mockEPRole2.setName("test2");
777                 mockEPRole2.setAppRoleId(16l);
778                 EPRole mockEPRole3 = new EPRole();
779                 mockEPRole3.setActive(true);
780                 mockEPRole3.setAppId(2l);
781                 mockEPRole3.setAppRoleId(null);
782                 mockEPRole3.setId(1100l);
783                 mockEPRole3.setAppRoleId(100l);
784                 mockEPRole3.setName("test3");
785                 Mockito.when(applicationsRestClientService.get(EPUser.class, mockApp.getId(),
786                                 String.format("/user/%s", user.getOrgUserId()), true)).thenReturn(user);
787                 // Updates in EP DB
788                 Query epsetAppWithUserRoleNonCentralizedGetUserQuery = Mockito.mock(Query.class);
789                 Query epsetAppWithUserRoleNonCentralizedGetUserAppsQuery = Mockito.mock(Query.class);
790                 Query epsetAppWithUserRoleNonCentralizedGetRolesQuery = Mockito.mock(Query.class);
791                 Mockito.when(session.get(EPApp.class, mockApp.getId())).thenReturn(mockApp);
792                 Mockito.when(session
793                                 .createQuery("from " + EPUser.class.getName() + " where orgUserId='" + user.getOrgUserId() + "'"))
794                                 .thenReturn(epsetAppWithUserRoleNonCentralizedGetUserQuery);
795                 Mockito.doReturn(mockEpUserList).when(epsetAppWithUserRoleNonCentralizedGetUserQuery).list();
796                 List<EPUserApp> mockUserRolesList2 = new ArrayList<>();
797                 EPUserApp mockEpUserAppRoles = new EPUserApp();
798                 mockEpUserAppRoles.setApp(mockApp);
799                 mockEpUserAppRoles.setRole(mockEPRole);
800                 mockEpUserAppRoles.setUserId(user.getId());
801                 mockUserRolesList2.add(mockEpUserAppRoles);
802                 Mockito.when(session.createQuery(
803                                 "from org.onap.portalapp.portal.domain.EPUserApp where app.id=2 and role.active = 'Y' and userId=2"))
804                                 .thenReturn(epsetAppWithUserRoleNonCentralizedGetUserAppsQuery);
805                 Mockito.doReturn(mockUserRolesList2).when(epsetAppWithUserRoleNonCentralizedGetUserAppsQuery).list();
806                 List<EPRole> mockEPRoles = new ArrayList<>();
807                 mockEPRoles.add(mockEPRole2);
808                 mockEPRoles.add(mockEPRole3);
809                 Mockito.when(session.createQuery("from " + EPRole.class.getName() + " where appId=2"))
810                                 .thenReturn(epsetAppWithUserRoleNonCentralizedGetRolesQuery);
811                 Mockito.doReturn(mockEPRoles).when(epsetAppWithUserRoleNonCentralizedGetRolesQuery).list();
812                 ExternalRequestFieldsValidator expected = userRolesCommonServiceImpl.setAppWithUserRoleStateForUser(user, mockWithRolesForUser);
813                 assertEquals(expected.isResult(), false);
814         }
815
816         @SuppressWarnings("unchecked")
817         @Test
818         public void setExternalRequestUserAppRoleMerdianCentralizedAppTest() throws Exception {
819                 PowerMockito.mockStatic(SystemProperties.class);
820                 PowerMockito.mockStatic(EcompPortalUtils.class);
821                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
822                 PowerMockito.mockStatic(PortalConstants.class);
823                 EPUser user = mockUser.mockEPUser();
824                 user.setId(2l);
825                 List<EPUser> mockEpUserList = new ArrayList<>();
826                 mockEpUserList.add(user);
827                 EPApp mockApp = mockApp();
828                 mockApp.setNameSpace("com.test.com");
829                 mockApp.setId(1l);
830                 mockApp.setCentralAuth(true);
831                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
832                 ExternalSystemUser externalSystemUser = new ExternalSystemUser();
833                 List<ExternalSystemRoleApproval> mockExternalSystemRoleApprovalList = new ArrayList<>();
834                 ExternalSystemRoleApproval mockExternalSystemRoleApproval = new ExternalSystemRoleApproval();
835                 mockExternalSystemRoleApproval.setRoleName("test1");
836                 ExternalSystemRoleApproval mockExternalSystemRoleApproval2 = new ExternalSystemRoleApproval();
837                 mockExternalSystemRoleApproval2.setRoleName("test2");
838                 mockExternalSystemRoleApprovalList.add(mockExternalSystemRoleApproval);
839                 mockExternalSystemRoleApprovalList.add(mockExternalSystemRoleApproval2);
840                 externalSystemUser.setApplicationName(mockApp.getMlAppName());
841                 externalSystemUser.setLoginId(user.getOrgUserId());
842                 externalSystemUser.setRoles(mockExternalSystemRoleApprovalList);
843                 EPRole mockEPRole = new EPRole();
844                 mockEPRole.setActive(true);
845                 mockEPRole.setAppId(null);
846                 mockEPRole.setAppRoleId(null);
847                 mockEPRole.setId(1l);
848                 mockEPRole.setName("test1");
849                 EPRole mockEPRole2 = new EPRole();
850                 mockEPRole2.setActive(true);
851                 mockEPRole2.setAppId(null);
852                 mockEPRole2.setAppRoleId(null);
853                 mockEPRole2.setId(16l);
854                 mockEPRole2.setName("test2");
855                 EPRole mockEPRole3 = new EPRole();
856                 mockEPRole3.setActive(true);
857                 mockEPRole3.setAppId(null);
858                 mockEPRole3.setAppRoleId(null);
859                 mockEPRole3.setId(1000l);
860                 mockEPRole3.setName("test3");
861
862                 Mockito.when(epAppCommonServiceImpl.getAppDetail(mockApp.getMlAppName())).thenReturn(mockApp);
863                 final Map<String, String> userParams = new HashMap<>();
864                 userParams.put("orgUserIdValue", user.getOrgUserId());
865                 Mockito.when((List<EPUser>) dataAccessService.executeNamedQuery("epUserAppId", userParams, null))
866                                 .thenReturn(mockEpUserList);
867
868                 List<EPUserAppRolesRequest> mockMerdianEpRequestIdValList = new ArrayList<EPUserAppRolesRequest>();
869                 Set<EPUserAppRolesRequestDetail> mockEpUserAppRolesRequestDetailList = new TreeSet<>();
870                 EPUserAppRolesRequestDetail mockEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
871                 mockEpUserAppRolesRequestDetail.setId(2l);
872                 mockEpUserAppRolesRequestDetail.setReqType("P");
873                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
874                 EPUserAppRolesRequest mockEPUserAppRolesRequest = new EPUserAppRolesRequest();
875                 mockEPUserAppRolesRequest.setAppId(mockApp.getId());
876                 mockEPUserAppRolesRequest.setId(1l);
877                 mockEPUserAppRolesRequest.setRequestStatus("P");
878                 mockEPUserAppRolesRequest.setUserId(user.getId());
879                 mockEPUserAppRolesRequest.setEpRequestIdDetail(mockEpUserAppRolesRequestDetailList);
880                 mockMerdianEpRequestIdValList.add(mockEPUserAppRolesRequest);
881                 mockEpUserAppRolesRequestDetail.setEpRequestIdData(mockEPUserAppRolesRequest);
882                 mockEpUserAppRolesRequestDetailList.add(mockEpUserAppRolesRequestDetail);
883
884                 final Map<String, Long> params = new HashMap<>();
885                 params.put("appId", mockApp.getId());
886                 params.put("userId", user.getId());
887                 Mockito.when((List<EPUserAppRolesRequest>) dataAccessService.executeNamedQuery("userAppRolesRequestList",
888                                 params, null)).thenReturn(mockMerdianEpRequestIdValList);
889                 RoleInAppForUser mockRoleInAppForUser1 = new RoleInAppForUser();
890                 mockRoleInAppForUser1.setIsApplied(true);
891                 mockRoleInAppForUser1.setRoleId(1l);
892                 mockRoleInAppForUser1.setRoleName("test1");
893                 RoleInAppForUser mockRoleInAppForUser2 = new RoleInAppForUser();
894                 mockRoleInAppForUser2.setIsApplied(true);
895                 mockRoleInAppForUser2.setRoleId(16l);
896                 mockRoleInAppForUser2.setRoleName("test2");
897                 Mockito.when(epRoleServiceImpl.getAppRole(mockExternalSystemRoleApproval.getRoleName(), mockApp.getId()))
898                                 .thenReturn(mockEPRole);
899                 Mockito.when(epRoleServiceImpl.getAppRole(mockExternalSystemRoleApproval2.getRoleName(), mockApp.getId()))
900                                 .thenReturn(mockEPRole2);
901                 List<EcompUserAppRoles> mockEPuserAppList = getCurrentUserRoles(user, mockApp);
902                 final Map<String, Long> params2 = new HashMap<>();
903                 params2.put("userId", user.getId());
904                 params2.put("appId", mockApp.getId());
905                 Mockito.when(dataAccessService.executeNamedQuery("getUserAppExistingRoles", params2, null))
906                                 .thenReturn(mockEPuserAppList);
907                 // Updates in External Auth System
908                 List<EPUserAppRolesRequestDetail> mockGetEpUserAppRolesRequestDetailList = new ArrayList<EPUserAppRolesRequestDetail>();
909                 EPUserAppRolesRequestDetail mockGetEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
910                 mockEpUserAppRolesRequestDetail.setId(2l);
911                 mockEpUserAppRolesRequestDetail.setReqType("P");
912                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
913                 mockGetEpUserAppRolesRequestDetailList.add(mockGetEpUserAppRolesRequestDetail);
914                 final Map<String, String> userParams2 = new HashMap<>();
915                 userParams2.put("orgUserIdValue", user.getOrgUserId());
916                 Mockito.when((List<EPUser>) dataAccessService.executeNamedQuery("epUserAppId", userParams2, null))
917                                 .thenReturn(mockEpUserList);
918                 PowerMockito
919                                 .when(EPCommonSystemProperties
920                                                 .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
921                                 .thenReturn(true);
922                 PowerMockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
923                                 .thenReturn("@test.com");
924                 HttpHeaders headers = new HttpHeaders();
925                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
926                 JSONObject mockJsonObject1 = new JSONObject();
927                 JSONObject mockJsonObject2 = new JSONObject();
928                 JSONObject mockJsonObject3 = new JSONObject();
929                 mockJsonObject1.put("name", "com.test.com.test1");
930                 mockJsonObject2.put("name", "com.test.com.test2");
931                 List<JSONObject> mockJson = new ArrayList<>();
932                 mockJson.add(mockJsonObject1);
933                 mockJson.add(mockJsonObject2);
934                 mockJsonObject3.put("role", mockJson);
935                 ResponseEntity<String> getResponse = new ResponseEntity<>(mockJsonObject3.toString(), HttpStatus.OK);
936                 Mockito.when(externalAccessRolesServiceImpl.getUserRolesFromExtAuthSystem(Matchers.anyString(), Matchers.any(HttpEntity.class))).thenReturn(getResponse);
937                 Map<String, EPRole> mockEPRoleList = new HashMap<>();
938                 mockEPRoleList.put("test1", mockEPRole);
939                 mockEPRoleList.put("test2", mockEPRole2);
940                 mockEPRoleList.put("test3", mockEPRole3);
941                 Mockito.when(externalAccessRolesServiceImpl.getAppRoleNamesWithUnderscoreMap(mockApp)).thenReturn(mockEPRoleList);
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
946                 ResponseEntity<String> deleteResponse = new ResponseEntity<>(HttpStatus.OK);
947                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
948                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(deleteResponse);
949                 // Updated in EP DB
950                 Query epsetExternalRequestUserAppRoleGetUserQuery = Mockito.mock(Query.class);
951                 Query epsetExternalRequestUserAppRoleGetUserAppsQuery = Mockito.mock(Query.class);
952                 Query epsetExternalRequestUserAppRoleGetRolesQuery = Mockito.mock(Query.class);
953                 SQLQuery epsetAppWithUserRoleUpdateEPRoleQuery = Mockito.mock(SQLQuery.class);
954                 Mockito.when(session.get(EPApp.class, mockApp.getId())).thenReturn(mockApp);
955                 Mockito.when(session
956                                 .createQuery("from " + EPUser.class.getName() + " where orgUserId='" + user.getOrgUserId() + "'"))
957                                 .thenReturn(epsetExternalRequestUserAppRoleGetUserQuery);
958                 Mockito.doReturn(mockEpUserList).when(epsetExternalRequestUserAppRoleGetUserQuery).list();
959                 List<EPUserApp> mockUserRolesList2 = new ArrayList<>();
960                 EPUserApp mockEpUserAppRoles = new EPUserApp();
961                 mockEpUserAppRoles.setApp(mockApp);
962                 mockEpUserAppRoles.setRole(mockEPRole);
963                 mockEpUserAppRoles.setUserId(user.getId());
964                 mockUserRolesList2.add(mockEpUserAppRoles);
965                 Mockito.when(session.createQuery(
966                                 "from org.onap.portalapp.portal.domain.EPUserApp where app.id=1 and role.active = 'Y' and userId=2"))
967                                 .thenReturn(epsetExternalRequestUserAppRoleGetUserAppsQuery);
968                 Mockito.doReturn(mockUserRolesList2).when(epsetExternalRequestUserAppRoleGetUserAppsQuery).list();
969                 List<EPRole> mockEPRoles = new ArrayList<>();
970                 mockEPRoles.add(mockEPRole2);
971                 mockEPRoles.add(mockEPRole3);
972                 Mockito.when(session.createQuery("from org.onap.portalapp.portal.domain.EPRole where appId is null "))
973                                 .thenReturn(epsetExternalRequestUserAppRoleGetRolesQuery);
974                 Mockito.doReturn(mockEPRoles).when(epsetExternalRequestUserAppRoleGetRolesQuery).list();
975                 Mockito.when(session.createSQLQuery("update fn_role set app_id = null where app_id = 1 "))
976                                 .thenReturn(epsetAppWithUserRoleUpdateEPRoleQuery);
977
978                 ExternalRequestFieldsValidator mockExternalRequestFieldsValidator = new ExternalRequestFieldsValidator(true,
979                                 "Updated Successfully");
980                 ExternalRequestFieldsValidator externalRequestFieldsValidator = userRolesCommonServiceImpl
981                                 .setExternalRequestUserAppRole(externalSystemUser, "POST");
982                 assertTrue(mockExternalRequestFieldsValidator.equals(externalRequestFieldsValidator));
983         }
984
985         @SuppressWarnings("unchecked")
986         @Test
987         public void setExternalRequestUserAppRoleMerdianNonCentralizedAppTest() throws Exception {
988                 PowerMockito.mockStatic(SystemProperties.class);
989                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
990                 PowerMockito.mockStatic(PortalConstants.class);
991                 EPUser user = mockUser.mockEPUser();
992                 user.setId(2l);
993                 List<EPUser> mockEpUserList = new ArrayList<>();
994                 mockEpUserList.add(user);
995                 EPApp mockApp = mockApp();
996                 mockApp.setId(2l);
997                 mockApp.setEnabled(true);
998                 mockApp.setCentralAuth(false);
999                 ExternalSystemUser externalSystemUser = new ExternalSystemUser();
1000                 List<ExternalSystemRoleApproval> mockExternalSystemRoleApprovalList = new ArrayList<>();
1001                 ExternalSystemRoleApproval mockExternalSystemRoleApproval = new ExternalSystemRoleApproval();
1002                 mockExternalSystemRoleApproval.setRoleName("test1");
1003                 ExternalSystemRoleApproval mockExternalSystemRoleApproval2 = new ExternalSystemRoleApproval();
1004                 mockExternalSystemRoleApproval2.setRoleName("test2");
1005                 mockExternalSystemRoleApprovalList.add(mockExternalSystemRoleApproval);
1006                 mockExternalSystemRoleApprovalList.add(mockExternalSystemRoleApproval2);
1007                 externalSystemUser.setApplicationName(mockApp.getMlAppName());
1008                 externalSystemUser.setLoginId(user.getOrgUserId());
1009                 externalSystemUser.setRoles(mockExternalSystemRoleApprovalList);
1010
1011                 EPRole mockEPRole = new EPRole();
1012                 mockEPRole.setActive(true);
1013                 mockEPRole.setAppId(2l);
1014                 mockEPRole.setAppRoleId(1l);
1015                 mockEPRole.setId(1000l);
1016                 mockEPRole.setName("test1");
1017                 EPRole mockEPRole2 = new EPRole();
1018                 mockEPRole2.setActive(true);
1019                 mockEPRole2.setAppId(2l);
1020                 mockEPRole2.setAppRoleId(16l);
1021                 mockEPRole2.setId(160l);
1022                 mockEPRole2.setName("test2");
1023                 EPRole mockEPRole3 = new EPRole();
1024                 mockEPRole3.setActive(true);
1025                 mockEPRole3.setAppId(2l);
1026                 mockEPRole3.setAppRoleId(10l);
1027                 mockEPRole3.setId(100l);
1028                 mockEPRole3.setName("test3");
1029
1030                 Mockito.when(epAppCommonServiceImpl.getAppDetail(mockApp.getMlAppName())).thenReturn(mockApp);
1031                 final Map<String, String> userParams = new HashMap<>();
1032                 userParams.put("orgUserIdValue", user.getOrgUserId());
1033                 Mockito.when((List<EPUser>) dataAccessService.executeNamedQuery("epUserAppId", userParams, null))
1034                                 .thenReturn(mockEpUserList);
1035
1036                 List<EPUserAppRolesRequest> mockMerdianEpRequestIdValList = new ArrayList<EPUserAppRolesRequest>();
1037                 Set<EPUserAppRolesRequestDetail> mockEpUserAppRolesRequestDetailList = new TreeSet<>();
1038                 EPUserAppRolesRequestDetail mockEpUserAppRolesRequestDetail = new EPUserAppRolesRequestDetail();
1039                 mockEpUserAppRolesRequestDetail.setId(2l);
1040                 mockEpUserAppRolesRequestDetail.setReqType("P");
1041                 mockEpUserAppRolesRequestDetail.setReqRoleId(16l);
1042                 EPUserAppRolesRequest mockEPUserAppRolesRequest = new EPUserAppRolesRequest();
1043                 mockEPUserAppRolesRequest.setAppId(mockApp.getId());
1044                 mockEPUserAppRolesRequest.setId(1l);
1045                 mockEPUserAppRolesRequest.setRequestStatus("P");
1046                 mockEPUserAppRolesRequest.setUserId(user.getId());
1047                 mockEPUserAppRolesRequest.setEpRequestIdDetail(mockEpUserAppRolesRequestDetailList);
1048                 mockMerdianEpRequestIdValList.add(mockEPUserAppRolesRequest);
1049                 mockEpUserAppRolesRequestDetail.setEpRequestIdData(mockEPUserAppRolesRequest);
1050                 mockEpUserAppRolesRequestDetailList.add(mockEpUserAppRolesRequestDetail);
1051
1052                 final Map<String, Long> params = new HashMap<>();
1053                 params.put("appId", mockApp.getId());
1054                 params.put("userId", user.getId());
1055                 Mockito.when((List<EPUserAppRolesRequest>) dataAccessService.executeNamedQuery("userAppRolesRequestList",
1056                                 params, null)).thenReturn(mockMerdianEpRequestIdValList);
1057                 RoleInAppForUser mockRoleInAppForUser1 = new RoleInAppForUser();
1058                 mockRoleInAppForUser1.setIsApplied(true);
1059                 mockRoleInAppForUser1.setRoleId(1l);
1060                 mockRoleInAppForUser1.setRoleName("test1");
1061                 RoleInAppForUser mockRoleInAppForUser2 = new RoleInAppForUser();
1062                 mockRoleInAppForUser2.setIsApplied(true);
1063                 mockRoleInAppForUser2.setRoleId(16l);
1064                 mockRoleInAppForUser2.setRoleName("test2");
1065                 Mockito.when(epRoleServiceImpl.getAppRole(mockExternalSystemRoleApproval.getRoleName(), mockApp.getId()))
1066                                 .thenReturn(mockEPRole);
1067                 Mockito.when(epRoleServiceImpl.getAppRole(mockExternalSystemRoleApproval2.getRoleName(), mockApp.getId()))
1068                                 .thenReturn(mockEPRole2);
1069                 List<EcompUserAppRoles> mockEPuserAppList = new ArrayList<>();
1070                 EcompUserAppRoles mockEcompUserAppRoles = new EcompUserAppRoles();
1071                 mockEcompUserAppRoles.setAppId(String.valueOf(mockApp.getId()));
1072                 mockEcompUserAppRoles.setRoleId(100l);
1073                 mockEcompUserAppRoles.setRoleName("test1");
1074                 mockEcompUserAppRoles.setUserId(user.getId());
1075                 mockEPuserAppList.add(mockEcompUserAppRoles);
1076                 final Map<String, Long> params2 = new HashMap<>();
1077                 params2.put("userId", user.getId());
1078                 params2.put("appId", mockApp.getId());
1079                 Mockito.when(dataAccessService.executeNamedQuery("getUserAppExistingRoles", params2, null))
1080                                 .thenReturn(mockEPuserAppList);
1081                 List<EcompRole> mockEcompRoleList = new ArrayList<>();
1082                 EcompRole mockEcompRole = new EcompRole();
1083                 mockEcompRole.setId(1l);
1084                 mockEcompRole.setName("test1");
1085                 EcompRole mockEcompRole2 = new EcompRole();
1086                 mockEcompRole2.setId(16l);
1087                 mockEcompRole2.setName("test2");
1088                 mockEcompRoleList.add(mockEcompRole);
1089                 mockEcompRoleList.add(mockEcompRole2);
1090                 EcompRole[] mockEcompRoleArray = mockEcompRoleList.toArray(new EcompRole[mockEcompRoleList.size()]);
1091                 Mockito.when(applicationsRestClientService.get(EcompRole[].class, mockApp.getId(), "/roles"))
1092                                 .thenReturn(mockEcompRoleArray);
1093
1094                 // SyncAppRoles
1095                 List<EPUserApp> mockUserRolesList = new ArrayList<>();
1096                 EPUserApp mockEpUserApp = new EPUserApp();
1097                 mockEpUserApp.setApp(mockApp);
1098                 mockEpUserApp.setRole(mockEPRole2);
1099                 mockEpUserApp.setUserId(user.getId());
1100                 mockUserRolesList.add(mockEpUserApp);
1101                 List<FunctionalMenuRole> mockFunctionalMenuRolesList = new ArrayList<>();
1102                 FunctionalMenuRole mockFunctionalMenuRole = new FunctionalMenuRole();
1103                 mockFunctionalMenuRole.setAppId((int) (long) mockApp.getId());
1104                 mockFunctionalMenuRole.setRoleId((int) (long) 15l);
1105                 mockFunctionalMenuRole.setMenuId(10l);
1106                 mockFunctionalMenuRole.setId(10);
1107                 mockFunctionalMenuRolesList.add(mockFunctionalMenuRole);
1108                 List<FunctionalMenuItem> mockFunctionalMenuItemList = new ArrayList<>();
1109                 FunctionalMenuItem mockFunctionalMenuItem = new FunctionalMenuItem();
1110                 List<Integer> mockRolesList = new ArrayList<>();
1111                 Integer role1 = 1;
1112                 mockRolesList.add(role1);
1113                 mockFunctionalMenuItem.setRestrictedApp(false);
1114                 mockFunctionalMenuItem.setRoles(mockRolesList);
1115                 mockFunctionalMenuItem.setUrl("http://test.com");
1116                 mockFunctionalMenuItemList.add(mockFunctionalMenuItem);
1117                 Query epRoleQuery = Mockito.mock(Query.class);
1118                 Query epUserAppsQuery = Mockito.mock(Query.class);
1119                 Query epFunctionalMenuQuery = Mockito.mock(Query.class);
1120                 Query epFunctionalMenuQuery2 = Mockito.mock(Query.class);
1121                 Query epFunctionalMenuItemQuery = Mockito.mock(Query.class);
1122                 Mockito.when(applicationsRestClientService.get(EcompRole[].class, mockApp.getId(), "/roles"))
1123                                 .thenReturn(mockEcompRoleArray);
1124                 // syncAppRolesTest
1125                 Mockito.when(session.createQuery("from " + EPRole.class.getName() + " where appId=" + mockApp.getId()))
1126                                 .thenReturn(epRoleQuery);
1127                 Mockito.doReturn(mockEcompRoleList).when(epRoleQuery).list();
1128                 Mockito.when(session.createQuery(
1129                                 "from " + EPUserApp.class.getName() + " where app.id=" + mockApp.getId() + " and role_id=" + 100l))
1130                                 .thenReturn(epUserAppsQuery);
1131                 Mockito.doReturn(mockUserRolesList).when(epUserAppsQuery).list();
1132
1133                 Mockito.when(session.createQuery("from " + FunctionalMenuRole.class.getName() + " where roleId=" + 100l))
1134                                 .thenReturn(epFunctionalMenuQuery);
1135                 Mockito.doReturn(mockFunctionalMenuRolesList).when(epFunctionalMenuQuery).list();
1136
1137                 Mockito.when(session.createQuery("from " + FunctionalMenuRole.class.getName() + " where menuId=" + 10l))
1138                                 .thenReturn(epFunctionalMenuQuery2);
1139                 Mockito.doReturn(mockFunctionalMenuRolesList).when(epFunctionalMenuQuery2).list();
1140
1141                 Mockito.when(session.createQuery("from " + FunctionalMenuItem.class.getName() + " where menuId=" + 10l))
1142                                 .thenReturn(epFunctionalMenuItemQuery);
1143                 Mockito.doReturn(mockFunctionalMenuItemList).when(epFunctionalMenuItemQuery).list();
1144
1145                 Mockito.when(applicationsRestClientService.get(EPUser.class, mockApp.getId(),
1146                                 String.format("/user/%s", user.getOrgUserId()), true)).thenReturn(user);
1147                 // Updated in EP DB
1148                 Query epsetExternalRequestUserAppRoleGetUserQuery = Mockito.mock(Query.class);
1149                 Query epsetExternalRequestUserAppRoleGetUserAppsQuery = Mockito.mock(Query.class);
1150                 Query epsetExternalRequestUserAppRoleGetRolesQuery = Mockito.mock(Query.class);
1151                 Mockito.when(session.get(EPApp.class, mockApp.getId())).thenReturn(mockApp);
1152                 Mockito.when(session
1153                                 .createQuery("from " + EPUser.class.getName() + " where orgUserId='" + user.getOrgUserId() + "'"))
1154                                 .thenReturn(epsetExternalRequestUserAppRoleGetUserQuery);
1155                 Mockito.doReturn(mockEpUserList).when(epsetExternalRequestUserAppRoleGetUserQuery).list();
1156                 List<EPUserApp> mockUserRolesList2 = new ArrayList<>();
1157                 EPUserApp mockEpUserAppRoles = new EPUserApp();
1158                 mockEpUserAppRoles.setApp(mockApp);
1159                 mockEpUserAppRoles.setRole(mockEPRole);
1160                 mockEpUserAppRoles.setUserId(user.getId());
1161                 mockUserRolesList2.add(mockEpUserAppRoles);
1162                 Mockito.when(session.createQuery(
1163                                 "from org.onap.portalapp.portal.domain.EPUserApp where app.id=2 and role.active = 'Y' and userId=2"))
1164                                 .thenReturn(epsetExternalRequestUserAppRoleGetUserAppsQuery);
1165                 Mockito.doReturn(mockUserRolesList2).when(epsetExternalRequestUserAppRoleGetUserAppsQuery).list();
1166                 List<EPRole> mockEPRoles = new ArrayList<>();
1167                 mockEPRoles.add(mockEPRole2);
1168                 mockEPRoles.add(mockEPRole3);
1169                 Mockito.when(session.createQuery("from org.onap.portalapp.portal.domain.EPRole where appId=2"))
1170                                 .thenReturn(epsetExternalRequestUserAppRoleGetRolesQuery);
1171                 Mockito.doReturn(mockEPRoles).when(epsetExternalRequestUserAppRoleGetRolesQuery).list();
1172
1173                 ExternalRequestFieldsValidator mockExternalRequestFieldsValidator = new ExternalRequestFieldsValidator(true,
1174                                 "Updated Successfully");
1175                 ExternalRequestFieldsValidator externalRequestFieldsValidator = userRolesCommonServiceImpl
1176                                 .setExternalRequestUserAppRole(externalSystemUser, "POST");
1177                 assertFalse(mockExternalRequestFieldsValidator.equals(externalRequestFieldsValidator));
1178         }
1179
1180         @SuppressWarnings("unchecked")
1181         @Test
1182         public void getUsersFromAppEndpointCentralizedAppTest() throws HTTPException {
1183                 EPApp mockApp = mockApp();
1184                 mockApp.setId(1l);
1185                 mockApp.setEnabled(true);
1186                 mockApp.setCentralAuth(true);
1187                 EPUser user = mockUser.mockEPUser();
1188                 EPUser user2 = mockUser.mockEPUser();
1189                 user2.setActive(true);
1190                 user2.setOrgUserId("guestT2");
1191                 user2.setId(2l);
1192                 user2.setFirstName("Guest2");
1193                 user2.setLastName("Test2");
1194                 user.setId(1l);
1195                 user.setFirstName("Guest");
1196                 user.setLastName("Test");
1197                 EPRole mockEPRole1 = new EPRole();
1198                 mockEPRole1.setId(1l);
1199                 mockEPRole1.setName("test1");
1200                 mockEPRole1.setActive(true);
1201                 EPRole mockEPRole2 = new EPRole();
1202                 mockEPRole2.setId(16l);
1203                 mockEPRole2.setName("test2");
1204                 mockEPRole2.setActive(true);
1205                 SortedSet<EPUserApp> mockUserApps1 = new TreeSet<EPUserApp>();
1206                 EPUserApp mockEPUserApp1 = new EPUserApp();
1207                 mockEPUserApp1.setApp(mockApp);
1208                 mockEPUserApp1.setRole(mockEPRole1);
1209                 mockEPUserApp1.setUserId(1l);
1210                 mockUserApps1.add(mockEPUserApp1);
1211                 user.setEPUserApps(mockUserApps1);
1212                 SortedSet<EPUserApp> mockUserApps2 = new TreeSet<EPUserApp>();
1213                 EPUserApp mockEPUserApp2 = new EPUserApp();
1214                 mockEPUserApp2.setApp(mockApp);
1215                 mockEPUserApp2.setRole(mockEPRole2);
1216                 mockEPUserApp2.setUserId(2l);
1217                 mockUserApps2.add(mockEPUserApp2);
1218                 user2.setEPUserApps(mockUserApps2);
1219                 List<EPUser> mockEpUserList = new ArrayList<>();
1220                 mockEpUserList.add(user);
1221                 mockEpUserList.add(user2);
1222                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
1223                 List<UserApplicationRoles> mockUserApplicationRolesList = new ArrayList<>();
1224                 UserApplicationRoles mockUserApplicationRoles = new UserApplicationRoles();
1225                 List<RemoteRole> mockRemoteRoleList = new ArrayList<>();
1226                 RemoteRole mockRemoteRole = new RemoteRole();
1227                 mockRemoteRole.setId(1l);
1228                 mockRemoteRole.setName("test1");
1229                 mockRemoteRoleList.add(mockRemoteRole);
1230                 mockUserApplicationRoles.setAppId(mockApp.getId());
1231                 mockUserApplicationRoles.setFirstName("Guest");
1232                 mockUserApplicationRoles.setLastName("Test");
1233                 mockUserApplicationRoles.setOrgUserId("guestT");
1234                 mockUserApplicationRoles.setRoles(mockRemoteRoleList);
1235                 UserApplicationRoles mockUserApplicationRoles2 = new UserApplicationRoles();
1236                 List<RemoteRole> mockRemoteRoleList2 = new ArrayList<>();
1237                 RemoteRole mockRemoteRole2 = new RemoteRole();
1238                 mockRemoteRole2.setId(16l);
1239                 mockRemoteRole2.setName("test2");
1240                 mockRemoteRoleList2.add(mockRemoteRole2);
1241                 mockUserApplicationRoles2.setAppId(mockApp.getId());
1242                 mockUserApplicationRoles2.setFirstName("Guest2");
1243                 mockUserApplicationRoles2.setLastName("Test2");
1244                 mockUserApplicationRoles2.setOrgUserId("guestT2");
1245                 mockUserApplicationRoles2.setRoles(mockRemoteRoleList2);
1246                 mockUserApplicationRolesList.add(mockUserApplicationRoles);
1247                 mockUserApplicationRolesList.add(mockUserApplicationRoles2);
1248                 Mockito.when((List<EPUser>) dataAccessService.executeNamedQuery("getActiveUsers", null, null))
1249                                 .thenReturn(mockEpUserList);
1250                 assertEquals(userRolesCommonServiceImpl.getUsersFromAppEndpoint(1l).size(),
1251                                 mockUserApplicationRolesList.size());
1252         }
1253
1254         @Test
1255         public void getUsersFromAppEndpointNonCentralizedAppTest() throws HTTPException, JsonProcessingException {
1256                 EPApp mockApp = mockApp();
1257                 mockApp.setId(2l);
1258                 mockApp.setEnabled(true);
1259                 mockApp.setCentralAuth(false);
1260                 Mockito.when(epAppCommonServiceImpl.getApp(mockApp.getId())).thenReturn(mockApp);
1261                 List<UserApplicationRoles> mockUserApplicationRolesNonCentralizedList = new ArrayList<>();
1262                 UserApplicationRoles mockUserApplicationRoles = new UserApplicationRoles();
1263                 List<RemoteRole> mockRemoteRoleList = new ArrayList<>();
1264                 RemoteRole mockRemoteRole = new RemoteRole();
1265                 mockRemoteRole.setId(1l);
1266                 mockRemoteRole.setName("test1");
1267                 mockRemoteRoleList.add(mockRemoteRole);
1268                 mockUserApplicationRoles.setAppId(mockApp.getId());
1269                 mockUserApplicationRoles.setFirstName("Guest");
1270                 mockUserApplicationRoles.setLastName("Test");
1271                 mockUserApplicationRoles.setOrgUserId("guestT");
1272                 mockUserApplicationRoles.setRoles(mockRemoteRoleList);
1273                 UserApplicationRoles mockUserApplicationRoles2 = new UserApplicationRoles();
1274                 List<RemoteRole> mockRemoteRoleList2 = new ArrayList<>();
1275                 RemoteRole mockRemoteRole2 = new RemoteRole();
1276                 mockRemoteRole2.setId(16l);
1277                 mockRemoteRole2.setName("test2");
1278                 mockRemoteRoleList2.add(mockRemoteRole2);
1279                 mockUserApplicationRoles2.setAppId(mockApp.getId());
1280                 mockUserApplicationRoles2.setFirstName("Guest2");
1281                 mockUserApplicationRoles2.setLastName("Test2");
1282                 mockUserApplicationRoles2.setOrgUserId("guestT2");
1283                 mockUserApplicationRoles2.setRoles(mockRemoteRoleList2);
1284                 mockUserApplicationRolesNonCentralizedList.add(mockUserApplicationRoles);
1285                 mockUserApplicationRolesNonCentralizedList.add(mockUserApplicationRoles2);
1286                 RemoteUserWithRoles mockRemoteUserWithRoles1 = new RemoteUserWithRoles();
1287                 mockRemoteUserWithRoles1.setFirstName("Guest1");
1288                 mockRemoteUserWithRoles1.setLastName("Test1");
1289                 mockRemoteUserWithRoles1.setOrgUserId("guestT");
1290                 mockRemoteUserWithRoles1.setRoles(mockRemoteRoleList);
1291                 RemoteUserWithRoles mockRemoteUserWithRoles2 = new RemoteUserWithRoles();
1292                 mockRemoteUserWithRoles2.setFirstName("Guest2");
1293                 mockRemoteUserWithRoles2.setLastName("Test2");
1294                 mockRemoteUserWithRoles2.setOrgUserId("guestT");
1295                 mockRemoteUserWithRoles2.setRoles(mockRemoteRoleList2);
1296                 List<RemoteUserWithRoles> mockRemoteUserWithRolesList = new ArrayList<>();
1297                 mockRemoteUserWithRolesList.add(mockRemoteUserWithRoles1);
1298                 mockRemoteUserWithRolesList.add(mockRemoteUserWithRoles2);
1299                 ObjectMapper mapper = new ObjectMapper();
1300                 String mockGetRemoteUsersWithRoles = mapper.writeValueAsString(mockRemoteUserWithRolesList);
1301                 Mockito.when(applicationsRestClientService.getIncomingJsonString(mockApp.getId(), "/users"))
1302                                 .thenReturn(mockGetRemoteUsersWithRoles);
1303                 List<UserApplicationRoles> userApplicationRolesNonCentralizedList = userRolesCommonServiceImpl
1304                                 .getUsersFromAppEndpoint(2l);
1305                 assertEquals(mockUserApplicationRolesNonCentralizedList.size(), userApplicationRolesNonCentralizedList.size());
1306         }
1307
1308         @SuppressWarnings("unchecked")
1309         @Test
1310         public void putUserAppRolesRequestTest() {
1311                 EPApp mockApp = mockApp();
1312                 mockApp.setId(2l);
1313                 mockApp.setEnabled(true);
1314                 mockApp.setCentralAuth(false);
1315                 EPUser user = mockUser.mockEPUser();
1316                 AppWithRolesForUser appWithRolesForUser = new AppWithRolesForUser();
1317                 List<RoleInAppForUser> mockRoleInAppForUserList = new ArrayList<>();
1318                 RoleInAppForUser roleInAppForUser = new RoleInAppForUser();
1319                 roleInAppForUser.setIsApplied(true);
1320                 roleInAppForUser.setRoleId(1l);
1321                 roleInAppForUser.setRoleName("test1");
1322                 RoleInAppForUser roleInAppForUser2 = new RoleInAppForUser();
1323                 roleInAppForUser2.setIsApplied(true);
1324                 roleInAppForUser2.setRoleId(1000l);
1325                 roleInAppForUser2.setRoleName("test3");
1326                 mockRoleInAppForUserList.add(roleInAppForUser);
1327                 mockRoleInAppForUserList.add(roleInAppForUser2);
1328                 appWithRolesForUser.setAppId(mockApp.getId());
1329                 appWithRolesForUser.setAppName(mockApp.getName());
1330                 appWithRolesForUser.setOrgUserId(user.getOrgUserId());
1331                 appWithRolesForUser.setAppRoles(mockRoleInAppForUserList);
1332                 List<EPUserAppRoles> epUserAppRolesList = new ArrayList<>();
1333                 EPUserAppRoles appRole1 = new EPUserAppRoles();
1334                 appRole1.setAppId(mockApp.getId());
1335                 appRole1.setId(10l);
1336                 appRole1.setRoleId(roleInAppForUser.roleId);
1337                 epUserAppRolesList.add(appRole1);
1338                 List<EPUserAppRoles> epUserAppRolesList2 = new ArrayList<>();
1339                 EPUserAppRoles appRole2 = new EPUserAppRoles();
1340                 appRole2.setAppId(mockApp.getId());
1341                 appRole2.setId(11l);
1342                 appRole2.setRoleId(roleInAppForUser2.roleId);
1343                 epUserAppRolesList2.add(appRole2);
1344                 EPUserAppRolesRequest mockEpAppRolesRequestData = new EPUserAppRolesRequest();
1345                 Mockito.doNothing().when(dataAccessService).saveDomainObject(mockEpAppRolesRequestData, null);
1346                 final Map<String, Long> params = new HashMap<>();
1347                 params.put("appId", appWithRolesForUser.getAppId());
1348                 params.put("appRoleId", roleInAppForUser.roleId);
1349                 Mockito.when((List<EPUserAppRoles>) dataAccessService.executeNamedQuery("appRoles", params, null))
1350                                 .thenReturn(epUserAppRolesList);
1351                 params.put("appRoleId", roleInAppForUser2.roleId);
1352                 Mockito.when((List<EPUserAppRoles>) dataAccessService.executeNamedQuery("appRoles", params, null))
1353                                 .thenReturn(epUserAppRolesList2);
1354                 EPUserAppRolesRequestDetail mockEPAppRoleDetail = new EPUserAppRolesRequestDetail();
1355                 Mockito.doNothing().when(dataAccessService).saveDomainObject(mockEPAppRoleDetail, null);
1356                 FieldsValidator expected = new FieldsValidator();
1357                 expected.setHttpStatusCode(Long.valueOf(HttpServletResponse.SC_OK));
1358                 FieldsValidator actual = userRolesCommonServiceImpl.putUserAppRolesRequest(appWithRolesForUser, user);
1359                 assertEquals(expected, actual);
1360         }
1361
1362         @Test
1363         public void importRolesFromRemoteApplicationTest() throws HTTPException {
1364                 EPApp mockApp = mockApp();
1365                 mockApp.setId(2l);
1366                 mockApp.setEnabled(true);
1367                 mockApp.setCentralAuth(false);
1368                 List<EPRole> expected = new ArrayList<>();
1369                 EPRole epRole = new EPRole();
1370                 epRole.setAppId(mockApp.getId());
1371                 epRole.setActive(true);
1372                 epRole.setId(10l);
1373                 epRole.setName("test1");
1374                 EPRole epRole2 = new EPRole();
1375                 epRole2.setAppId(mockApp.getId());
1376                 epRole2.setActive(true);
1377                 epRole2.setId(11l);
1378                 epRole2.setName("test2");
1379                 expected.add(epRole);
1380                 expected.add(epRole2);
1381                 EPRole[] epRoleArray = expected.toArray(new EPRole[expected.size()]);
1382                 Mockito.when(applicationsRestClientService.get(EPRole[].class, mockApp.getId(), "/rolesFull"))
1383                                 .thenReturn(epRoleArray);
1384                 Mockito.when(epRoleServiceImpl.getRole(mockApp.getId(), epRole.getId())).thenReturn(epRole);
1385                 Mockito.when(epRoleServiceImpl.getRole(mockApp.getId(), epRole2.getId())).thenReturn(epRole2);
1386                 List<EPRole> actual = userRolesCommonServiceImpl.importRolesFromRemoteApplication(mockApp.getId());
1387                 assertEquals(expected.size(), actual.size());
1388         }
1389
1390         @SuppressWarnings("deprecation")
1391         @Test
1392         public void getCachedAppRolesForUserTest() {
1393                 EPApp mockApp = mockApp();
1394                 mockApp.setId(2l);
1395                 mockApp.setEnabled(true);
1396                 mockApp.setCentralAuth(false);
1397                 EPUser user = mockUser.mockEPUser();
1398                 List<EPUserApp> expected = new ArrayList<>();
1399                 EPUserApp epUserApp = new EPUserApp();
1400                 EPRole epRole = new EPRole();
1401                 epRole.setAppId(mockApp.getId());
1402                 epRole.setActive(true);
1403                 epRole.setId(10l);
1404                 epRole.setName("test1");
1405                 epUserApp.setApp(mockApp);
1406                 epUserApp.setRole(epRole);
1407                 epUserApp.setUserId(user.getId());
1408                 expected.add(epUserApp);
1409                 String filter = " where user_id = " + Long.toString(user.getId()) + " and app_id = "
1410                                 + Long.toString(mockApp.getId());
1411                 Mockito.when(dataAccessService.getList(EPUserApp.class, filter, null, null)).thenReturn(expected);
1412                 List<EPUserApp> actual = userRolesCommonServiceImpl.getCachedAppRolesForUser(mockApp.getId(), user.getId());
1413                 assertEquals(expected.size(), actual.size());
1414         }
1415
1416         @SuppressWarnings("unchecked")
1417         @Test
1418         public void getUserAppCatalogRolesTest() {
1419                 EPApp mockApp = mockApp();
1420                 mockApp.setId(2l);
1421                 mockApp.setEnabled(true);
1422                 mockApp.setCentralAuth(false);
1423                 EPUser user = mockUser.mockEPUser();
1424                 List<EPUserAppCatalogRoles> expected = new ArrayList<>();
1425                 EPUserAppCatalogRoles epUserAppCatalogRoles = new EPUserAppCatalogRoles();
1426                 epUserAppCatalogRoles.setAppId(mockApp.getId());
1427                 epUserAppCatalogRoles.setId(2l);
1428                 epUserAppCatalogRoles.setRequestedRoleId(10l);
1429                 epUserAppCatalogRoles.setRequestStatus("S");
1430                 epUserAppCatalogRoles.setRolename("test1");
1431                 expected.add(epUserAppCatalogRoles);
1432                 Map<String, String> params = new HashMap<>();
1433                 params.put("userid", user.getId().toString());
1434                 params.put("appName", mockApp.getName());
1435                 Mockito.when(
1436                                 (List<EPUserAppCatalogRoles>) dataAccessService.executeNamedQuery("userAppCatalogRoles", params, null))
1437                                 .thenReturn(expected);
1438                 List<EPUserAppCatalogRoles> actual = userRolesCommonServiceImpl.getUserAppCatalogRoles(user, mockApp.getName());
1439                 assertEquals(expected.size(), actual.size());
1440         }
1441
1442         @Test
1443         public void getExternalRequestAccessTest() {
1444                 ExternalSystemAccess expected = new ExternalSystemAccess("external_access_enable", false);
1445                 ExternalSystemAccess actual = userRolesCommonServiceImpl.getExternalRequestAccess();
1446                 assertEquals(expected, actual);
1447         }
1448
1449         @Test
1450         public void getEPUserAppListTest() {
1451                 EPApp mockApp = mockApp();
1452                 mockApp.setId(2l);
1453                 mockApp.setEnabled(true);
1454                 mockApp.setCentralAuth(false);
1455                 EPUser user = mockUser.mockEPUser();
1456                 List<EPUserApp> expected = new ArrayList<>();
1457                 EPUserApp epUserApp = new EPUserApp();
1458                 EPRole epRole = new EPRole();
1459                 epRole.setAppId(mockApp.getId());
1460                 epRole.setActive(true);
1461                 epRole.setId(10l);
1462                 epRole.setName("test1");
1463                 epUserApp.setApp(mockApp);
1464                 epUserApp.setRole(epRole);
1465                 epUserApp.setUserId(user.getId());
1466                 expected.add(epUserApp);
1467                 final Map<String, Long> params = new HashMap<>();
1468                 params.put("appId", mockApp.getId());
1469                 params.put("userId", user.getId());
1470                 params.put("roleId", epRole.getId());
1471                 Mockito.when(dataAccessService.executeNamedQuery("getUserRoleOnUserIdAndRoleIdAndAppId", params, null))
1472                                 .thenReturn(expected);
1473                 List<EPUserApp> actual = userRolesCommonServiceImpl.getEPUserAppList(mockApp.getId(), user.getId(),
1474                                 epRole.getId());
1475                 assertEquals(expected.size(), actual.size());
1476         }
1477
1478         @Test
1479         public void updateRemoteUserProfileTest() {
1480                 EPApp mockApp = mockApp();
1481                 EPApp mockApp2 = mockApp();
1482                 mockApp.setId(2l);
1483                 mockApp.setEnabled(true);
1484                 mockApp.setCentralAuth(false);
1485                 EPUser user = mockUser.mockEPUser();
1486                 List<EPApp> mockEpAppList = new ArrayList<>();
1487                 mockEpAppList.add(mockApp);
1488                 mockEpAppList.add(mockApp2);
1489                 Mockito.when(searchServiceImpl.searchUserByUserId(user.getOrgUserId())).thenReturn(user);
1490                 Mockito.when(epAppCommonServiceImpl.getUserRemoteApps(user.getId().toString())).thenReturn(mockEpAppList);
1491                 String expected = "success";
1492                 String actual = userRolesCommonServiceImpl.updateRemoteUserProfile(user.getOrgUserId(), mockApp.getId());
1493                 assertEquals(expected, actual);
1494         }
1495 }