74fb5da7467987fc9d2616d450eaa8c2c4339a8b
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / AdminRolesServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertNotNull;
43 import static org.junit.Assert.assertTrue;
44
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.SortedSet;
50
51 import org.hibernate.SQLQuery;
52 import org.hibernate.Session;
53 import org.hibernate.SessionFactory;
54 import org.hibernate.Transaction;
55 import org.json.simple.JSONObject;
56 import org.junit.After;
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.InjectMocks;
61 import org.mockito.Matchers;
62 import org.mockito.Mock;
63 import org.mockito.Mockito;
64 import org.mockito.MockitoAnnotations;
65 import org.onap.portalapp.portal.core.MockEPUser;
66 import org.onap.portalapp.portal.domain.EPApp;
67 import org.onap.portalapp.portal.domain.EPRole;
68 import org.onap.portalapp.portal.domain.EPUser;
69 import org.onap.portalapp.portal.domain.EPUserApp;
70 import org.onap.portalapp.portal.domain.UserRole;
71 import org.onap.portalapp.portal.transport.AppNameIdIsAdmin;
72 import org.onap.portalapp.portal.transport.AppsListWithAdminRole;
73 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
74 import org.onap.portalapp.portal.utils.EcompPortalUtils;
75 import org.onap.portalapp.portal.utils.PortalConstants;
76 import org.onap.portalsdk.core.service.DataAccessService;
77 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
78 import org.onap.portalsdk.core.util.SystemProperties;
79 import org.powermock.api.mockito.PowerMockito;
80 import org.powermock.core.classloader.annotations.PrepareForTest;
81 import org.powermock.modules.junit4.PowerMockRunner;
82 import org.springframework.http.HttpEntity;
83 import org.springframework.http.HttpMethod;
84 import org.springframework.http.HttpStatus;
85 import org.springframework.http.ResponseEntity;
86 import org.springframework.web.client.RestTemplate;
87
88 @RunWith(PowerMockRunner.class)
89 @PrepareForTest({ EcompPortalUtils.class, PortalConstants.class, SystemProperties.class,
90                 EPCommonSystemProperties.class })
91 public class AdminRolesServiceImplTest {
92
93         @Mock
94         DataAccessService dataAccessService = new DataAccessServiceImpl();
95
96         @Mock
97         EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
98
99         @Mock
100         SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
101
102         @Mock
103         SessionFactory sessionFactory;
104
105         @Mock
106         Session session;
107
108         @Mock
109         Transaction transaction;
110
111         @Mock
112         RestTemplate template = new RestTemplate();
113
114         @Before
115         public void setup() {
116                 MockitoAnnotations.initMocks(this);
117                 Mockito.when(sessionFactory.openSession()).thenReturn(session);
118                 Mockito.when(session.beginTransaction()).thenReturn(transaction);
119         }
120
121         @After
122         public void after() {
123                 session.close();
124         }
125
126         @InjectMocks
127         AdminRolesServiceImpl adminRolesServiceImpl = new AdminRolesServiceImpl();
128
129         private Long ACCOUNT_ADMIN_ROLE_ID = 999L;
130         
131         private Long ECOMP_APP_ID = 1L;
132         
133         public EPApp mockApp() {
134                 EPApp app = new EPApp();
135                 app.setName("Test");
136                 app.setImageUrl("test");
137                 app.setNameSpace("com.test.app");
138                 app.setCentralAuth(true);
139                 app.setDescription("test");
140                 app.setNotes("test");
141                 app.setUrl("test");
142                 app.setId((long) 1);
143                 app.setAppRestEndpoint("test");
144                 app.setAlternateUrl("test");
145                 app.setName("test");
146                 app.setMlAppName("test");
147                 app.setMlAppAdminId("test");
148                 app.setUsername("test");
149                 app.setAppPassword("test");
150                 app.setOpen(false);
151                 app.setEnabled(true);
152                 app.setUebKey("test");
153                 app.setUebSecret("test");
154                 app.setUebTopicName("test");
155                 app.setAppType(1);
156                 return app;
157         }
158
159         MockEPUser mockUser = new MockEPUser();
160
161         @SuppressWarnings("deprecation")
162         @Test
163         public void getAppsWithAdminRoleStateForUserTest() {
164                 EPUser user = mockUser.mockEPUser();
165                 EPApp app = mockApp();
166                 app.setId(1l);
167                 List<EPUser> users = new ArrayList<>();
168                 users.add(user);
169                 Map<String, String> userParams = new HashMap<>();
170                 userParams.put("org_user_id", user.getOrgUserId());
171                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)).thenReturn(users);
172                 List<EPUserApp> userAppList = new ArrayList<>();
173                 EPUserApp epUserApp = new EPUserApp();
174                 EPRole role = new EPRole();
175                 role.setActive(true);
176                 role.setId(1l);
177                 role.setName("test role");
178                 epUserApp.setApp(app);
179                 epUserApp.setRole(role);
180                 epUserApp.setUserId(1l);
181                 userAppList.add(epUserApp);
182                 Mockito.when(dataAccessService.getList(EPUserApp.class,
183                                 " where userId = " + user.getId() + " and role.id = " + 999, null, null)).thenReturn(userAppList);
184                 List<EPApp> appsList = new ArrayList<>();
185                 appsList.add(app);
186                 Mockito.when(dataAccessService.getList(EPApp.class,
187                                 "  where ( enabled = 'Y' or id = " + ECOMP_APP_ID + ")", null, null)).thenReturn(appsList);
188                 AppsListWithAdminRole  actual = adminRolesServiceImpl.getAppsWithAdminRoleStateForUser(user.getOrgUserId());
189                 assertNotNull(actual);
190         }
191         
192         @Test
193         public void getAppsWithAdminRoleStateForUserTestWithException() {
194                 EPUser user = mockUser.mockEPUser();
195                 EPApp app = mockApp();
196                 app.setId(1l);
197                 List<EPUser> users = new ArrayList<>();
198                 users.add(user);
199                 Map<String, String> userParams = new HashMap<>();
200                 userParams.put("org_user_id", user.getOrgUserId());
201                 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)).thenReturn(users);
202                 AppsListWithAdminRole  actual = adminRolesServiceImpl.getAppsWithAdminRoleStateForUser(user.getOrgUserId());
203
204                 
205         }
206
207         @SuppressWarnings({ "deprecation", "unchecked" })
208         @Test
209         public void setAppsWithAdminRoleStateForUserTest() {
210                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
211                 PowerMockito.mockStatic(EcompPortalUtils.class);
212                 PowerMockito.mockStatic(PortalConstants.class);
213                 PowerMockito.mockStatic(SystemProperties.class);
214                 EPUser user = mockUser.mockEPUser();
215                 EPApp app = mockApp();
216                 app.setId(1l);
217                 EPApp app2 = mockApp();
218                 app2.setName("app2");
219                 app2.setNameSpace("com.test.app2");
220                 app2.setId(2l);
221                 EPApp app3 = mockApp();
222                 app3.setName("app3");
223                 app3.setNameSpace("com.test.app3");
224                 app3.setId(3l);
225                 List<EPApp> apps = new ArrayList<>();
226                 apps.add(app);
227                 apps.add(app2);
228                 apps.add(app3);
229                 Mockito.when(epAppCommonServiceImpl.getAppsFullList()).thenReturn(apps);
230
231                 List<EPUser> localUserList = new ArrayList<>();
232                 localUserList.add(user);
233                 Mockito.when(
234                                 dataAccessService.getList(EPUser.class, " where org_user_id='" + user.getOrgUserId() + "'", null, null))
235                                 .thenReturn(localUserList);
236                 List<EPUserApp> oldAppsWhereUserIsAdmin = new ArrayList<EPUserApp>();
237                 EPUserApp epUserApp = new EPUserApp();
238                 EPRole role = new EPRole();
239                 role.setActive(true);
240                 role.setId(999l);
241                 role.setName("app5");
242                 epUserApp.setApp(app);
243                 epUserApp.setRole(role);
244                 epUserApp.setUserId(1l);
245                 oldAppsWhereUserIsAdmin.add(epUserApp);
246                 Mockito.when(dataAccessService.getList(EPUserApp.class,
247                                 " where userId = " + user.getId() + " and role.id = " + ACCOUNT_ADMIN_ROLE_ID, null,
248                                 null)).thenReturn(oldAppsWhereUserIsAdmin);
249                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
250                 EPApp app4 = mockApp();
251                 app4.setId(6l);
252                 app4.setName("app7");
253                 app4.setNameSpace("com.test.app7");
254                 List<EPApp> apps2 = new ArrayList<>();
255                 apps2.add(app);
256                 apps2.add(app2);
257                 apps2.add(app3);
258                 apps2.add(app4);
259                 Mockito.when(dataAccessService.executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps2);
260                 Mockito.when(
261                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
262                                 .thenReturn(true);
263                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
264                                 .thenReturn("@test.com");
265                 JSONObject getUserRoles = new JSONObject();
266                 ResponseEntity<String> getResponse = new ResponseEntity<>(getUserRoles.toString(), HttpStatus.OK);
267                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
268                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
269                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
270                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
271                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
272                 
273                 AppsListWithAdminRole newAppsListWithAdminRoles = new AppsListWithAdminRole();
274                 ArrayList<AppNameIdIsAdmin> appsRoles = new ArrayList<>();
275                 AppNameIdIsAdmin appNameIdIsAdmin = new AppNameIdIsAdmin();
276                 appNameIdIsAdmin.setAppName("app1");
277                 appNameIdIsAdmin.setId(2l);
278                 appNameIdIsAdmin.setIsAdmin(true);
279                 appNameIdIsAdmin.setRestrictedApp(false);
280                 AppNameIdIsAdmin appNameIdIsAdmin2 = new AppNameIdIsAdmin();
281                 appNameIdIsAdmin2.setAppName("app2");
282                 appNameIdIsAdmin2.setId(3l);
283                 appNameIdIsAdmin2.setIsAdmin(true);
284                 appNameIdIsAdmin2.setRestrictedApp(false);
285                 appsRoles.add(appNameIdIsAdmin);
286                 appsRoles.add(appNameIdIsAdmin2);
287                 newAppsListWithAdminRoles.setOrgUserId(user.getOrgUserId());
288                 newAppsListWithAdminRoles.setAppsRoles(appsRoles);
289                 Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin.id)).thenReturn(app2);
290                 Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin2.id)).thenReturn(app3);
291                 JSONObject getUserRoles2 = new JSONObject();
292                 JSONObject getUserRoles3 = new JSONObject();
293                 JSONObject getUserRoles4 = new JSONObject();
294                 JSONObject finalUserRoles = new JSONObject();
295                 getUserRoles2.put("role", "com.test.app3.Account_Administrator");
296                 getUserRoles3.put("role", "com.test.app3.admin");
297                 getUserRoles4.put("role", "com.test.app3.owner");
298                 List<JSONObject> userRoles =  new ArrayList<>();
299                 userRoles.add(getUserRoles2);
300                 userRoles.add(getUserRoles3);
301                 userRoles.add(getUserRoles4);
302                 finalUserRoles.put("userRole", userRoles);
303                 ResponseEntity<String> getResponse2 = new ResponseEntity<>(finalUserRoles.toString(), HttpStatus.OK);
304                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
305                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse2);
306                 boolean actual = adminRolesServiceImpl.setAppsWithAdminRoleStateForUser(newAppsListWithAdminRoles);
307                 assertTrue(actual);
308         }
309
310         @Test
311         public void isSuperAdminTest() {
312                 EPUser user = mockUser.mockEPUser();
313                 user.setId(1l);
314                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
315                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
316                 List<UserRole> userRoleList = new ArrayList<>();
317                 UserRole userRole = new UserRole();
318                 userRole.setFirstName("Hello");
319                 userRole.setLastName("World");
320                 userRole.setRoleId(1l);
321                 userRole.setRoleName("test");
322                 userRole.setUser_Id(1l);
323                 userRoleList.add(userRole);
324                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap()))
325                                 .thenReturn(userRoleList);
326                 boolean actual = adminRolesServiceImpl.isSuperAdmin(user);
327                 assertTrue(actual);
328         }
329
330         @Test
331         public void isSuperAdminExceptionTest() {
332                 EPUser user = mockUser.mockEPUser();
333                 user.setId(1l);
334                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
335                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
336                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeSQLQuery(Matchers.anyString(),
337                                 Matchers.any(), Matchers.anyMap());
338                 boolean actual = adminRolesServiceImpl.isSuperAdmin(user);
339                 assertFalse(actual);
340         }
341
342         @Test
343         public void isAccountAdminTest() {
344                 EPUser user = mockUser.mockEPUser();
345                 EPApp app = mockApp();
346                 app.setId(2l);
347                 SortedSet<EPUserApp> userApps = user.getEPUserApps();
348                 EPUserApp epUserApp = new EPUserApp();
349                 EPRole role = new EPRole();
350                 role.setActive(true);
351                 role.setId(999l);
352                 role.setName("test role");
353                 epUserApp.setApp(app);
354                 epUserApp.setRole(role);
355                 epUserApp.setUserId(1l);
356                 userApps.add(epUserApp);
357                 user.setUserApps(userApps);
358                 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
359                                 .thenReturn(user);
360                 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
361                 assertTrue(actual);
362         }
363
364         @Test
365         public void isAccountAdminExceptionTest() {
366                 EPUser user = mockUser.mockEPUser();
367                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
368                                 Matchers.anyLong(), Matchers.anyMap());
369                 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
370                 assertFalse(actual);
371         }
372
373         @Test
374         public void isUserTest() {
375                 EPUser user = mockUser.mockEPUser();
376                 EPApp app = mockApp();
377                 app.setId(2l);
378                 SortedSet<EPUserApp> userApps = user.getEPUserApps();
379                 EPUserApp epUserApp = new EPUserApp();
380                 EPRole role = new EPRole();
381                 role.setActive(true);
382                 role.setId(2l);
383                 role.setName("test role");
384                 epUserApp.setApp(app);
385                 epUserApp.setRole(role);
386                 epUserApp.setUserId(1l);
387                 userApps.add(epUserApp);
388                 user.setUserApps(userApps);
389                 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
390                                 .thenReturn(user);
391                 boolean actual = adminRolesServiceImpl.isUser(user);
392                 assertTrue(actual);
393         }
394
395         @Test
396         public void isUserExceptionTest() {
397                 EPUser user = mockUser.mockEPUser();
398                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
399                                 Matchers.anyLong(), Matchers.anyMap());
400                 boolean actual = adminRolesServiceImpl.isUser(user);
401                 assertFalse(actual);
402         }
403
404         @Test
405         public void getRolesByAppTest() {
406                 EPUser user = mockUser.mockEPUser();
407                 EPApp app = mockApp();
408                 List<EPRole> expected = new ArrayList<>();
409                 EPRole role = new EPRole();
410                 role.setActive(true);
411                 role.setId(1l);
412                 role.setName("test role");
413                 expected.add(role);
414                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap()))
415                                 .thenReturn(expected);
416                 List<EPRole> actual = adminRolesServiceImpl.getRolesByApp(user, app.getId());
417                 assertEquals(expected, actual);
418         }
419
420         @Test
421         public void isAccountAdminOfApplicationTest() {
422                 EPUser user = mockUser.mockEPUser();
423                 EPApp app = mockApp();
424                 SortedSet<EPUserApp> userApps = user.getEPUserApps();
425                 EPUserApp epUserApp = new EPUserApp();
426                 EPRole role = new EPRole();
427                 role.setActive(true);
428                 role.setId(999l);
429                 role.setName("test role");
430                 epUserApp.setApp(app);
431                 epUserApp.setRole(role);
432                 epUserApp.setUserId(1l);
433                 userApps.add(epUserApp);
434                 user.setUserApps(userApps);
435                 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
436                                 .thenReturn(user);
437                 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
438                 assertTrue(actual);
439         }
440
441         @Test
442         public void isAccountAdminOfApplicationExceptionTest() {
443                 EPUser user = mockUser.mockEPUser();
444                 EPApp app = mockApp();
445                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
446                                 Matchers.anyLong(), Matchers.anyMap());
447                 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
448                 assertFalse(actual);
449         }
450 }