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