d9beb4d14c6fafb87b220ecdb25a670250cab163
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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         @SuppressWarnings({ "deprecation", "unchecked" })
193         @Test
194         public void setAppsWithAdminRoleStateForUserTest() {
195                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
196                 PowerMockito.mockStatic(EcompPortalUtils.class);
197                 PowerMockito.mockStatic(PortalConstants.class);
198                 PowerMockito.mockStatic(SystemProperties.class);
199                 EPUser user = mockUser.mockEPUser();
200                 EPApp app = mockApp();
201                 app.setId(1l);
202                 EPApp app2 = mockApp();
203                 app2.setName("app2");
204                 app2.setNameSpace("com.test.app2");
205                 app2.setId(2l);
206                 EPApp app3 = mockApp();
207                 app3.setName("app3");
208                 app3.setNameSpace("com.test.app3");
209                 app3.setId(3l);
210                 List<EPApp> apps = new ArrayList<>();
211                 apps.add(app);
212                 apps.add(app2);
213                 apps.add(app3);
214                 Mockito.when(epAppCommonServiceImpl.getAppsFullList()).thenReturn(apps);
215
216                 List<EPUser> localUserList = new ArrayList<>();
217                 localUserList.add(user);
218                 Mockito.when(
219                                 dataAccessService.getList(EPUser.class, " where org_user_id='" + user.getOrgUserId() + "'", null, null))
220                                 .thenReturn(localUserList);
221                 List<EPUserApp> oldAppsWhereUserIsAdmin = new ArrayList<EPUserApp>();
222                 EPUserApp epUserApp = new EPUserApp();
223                 EPRole role = new EPRole();
224                 role.setActive(true);
225                 role.setId(999l);
226                 role.setName("app5");
227                 epUserApp.setApp(app);
228                 epUserApp.setRole(role);
229                 epUserApp.setUserId(1l);
230                 oldAppsWhereUserIsAdmin.add(epUserApp);
231                 Mockito.when(dataAccessService.getList(EPUserApp.class,
232                                 " where userId = " + user.getId() + " and role.id = " + ACCOUNT_ADMIN_ROLE_ID, null,
233                                 null)).thenReturn(oldAppsWhereUserIsAdmin);
234                 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
235                 EPApp app4 = mockApp();
236                 app4.setId(6l);
237                 app4.setName("app7");
238                 app4.setNameSpace("com.test.app7");
239                 List<EPApp> apps2 = new ArrayList<>();
240                 apps2.add(app);
241                 apps2.add(app2);
242                 apps2.add(app3);
243                 apps2.add(app4);
244                 Mockito.when(dataAccessService.executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps2);
245                 Mockito.when(
246                                 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
247                                 .thenReturn(true);
248                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
249                                 .thenReturn("@test.com");
250                 JSONObject getUserRoles = new JSONObject();
251                 ResponseEntity<String> getResponse = new ResponseEntity<>(getUserRoles.toString(), HttpStatus.OK);
252                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
253                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
254                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
255                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
256                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
257                 
258                 AppsListWithAdminRole newAppsListWithAdminRoles = new AppsListWithAdminRole();
259                 ArrayList<AppNameIdIsAdmin> appsRoles = new ArrayList<>();
260                 AppNameIdIsAdmin appNameIdIsAdmin = new AppNameIdIsAdmin();
261                 appNameIdIsAdmin.setAppName("app1");
262                 appNameIdIsAdmin.setId(2l);
263                 appNameIdIsAdmin.setIsAdmin(true);
264                 appNameIdIsAdmin.setRestrictedApp(false);
265                 AppNameIdIsAdmin appNameIdIsAdmin2 = new AppNameIdIsAdmin();
266                 appNameIdIsAdmin2.setAppName("app2");
267                 appNameIdIsAdmin2.setId(3l);
268                 appNameIdIsAdmin2.setIsAdmin(true);
269                 appNameIdIsAdmin2.setRestrictedApp(false);
270                 appsRoles.add(appNameIdIsAdmin);
271                 appsRoles.add(appNameIdIsAdmin2);
272                 newAppsListWithAdminRoles.setOrgUserId(user.getOrgUserId());
273                 newAppsListWithAdminRoles.setAppsRoles(appsRoles);
274                 Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin.id)).thenReturn(app2);
275                 Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin2.id)).thenReturn(app3);
276                 JSONObject getUserRoles2 = new JSONObject();
277                 JSONObject getUserRoles3 = new JSONObject();
278                 JSONObject getUserRoles4 = new JSONObject();
279                 JSONObject finalUserRoles = new JSONObject();
280                 getUserRoles2.put("role", "com.test.app3.Account_Administrator");
281                 getUserRoles3.put("role", "com.test.app3.admin");
282                 getUserRoles4.put("role", "com.test.app3.owner");
283                 List<JSONObject> userRoles =  new ArrayList<>();
284                 userRoles.add(getUserRoles2);
285                 userRoles.add(getUserRoles3);
286                 userRoles.add(getUserRoles4);
287                 finalUserRoles.put("userRole", userRoles);
288                 ResponseEntity<String> getResponse2 = new ResponseEntity<>(finalUserRoles.toString(), HttpStatus.OK);
289                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
290                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse2);
291                 boolean actual = adminRolesServiceImpl.setAppsWithAdminRoleStateForUser(newAppsListWithAdminRoles);
292                 assertTrue(actual);
293         }
294
295         @Test
296         public void isSuperAdminTest() {
297                 EPUser user = mockUser.mockEPUser();
298                 user.setId(1l);
299                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
300                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
301                 List<UserRole> userRoleList = new ArrayList<>();
302                 UserRole userRole = new UserRole();
303                 userRole.setFirstName("Hello");
304                 userRole.setLastName("World");
305                 userRole.setRoleId(1l);
306                 userRole.setRoleName("test");
307                 userRole.setUser_Id(1l);
308                 userRoleList.add(userRole);
309                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap()))
310                                 .thenReturn(userRoleList);
311                 boolean actual = adminRolesServiceImpl.isSuperAdmin(user);
312                 assertTrue(actual);
313         }
314
315         @Test
316         public void isSuperAdminExceptionTest() {
317                 EPUser user = mockUser.mockEPUser();
318                 user.setId(1l);
319                 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
320                 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
321                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeSQLQuery(Matchers.anyString(),
322                                 Matchers.any(), Matchers.anyMap());
323                 boolean actual = adminRolesServiceImpl.isSuperAdmin(user);
324                 assertFalse(actual);
325         }
326
327         @Test
328         public void isAccountAdminTest() {
329                 EPUser user = mockUser.mockEPUser();
330                 EPApp app = mockApp();
331                 app.setId(2l);
332                 SortedSet<EPUserApp> userApps = user.getEPUserApps();
333                 EPUserApp epUserApp = new EPUserApp();
334                 EPRole role = new EPRole();
335                 role.setActive(true);
336                 role.setId(999l);
337                 role.setName("test role");
338                 epUserApp.setApp(app);
339                 epUserApp.setRole(role);
340                 epUserApp.setUserId(1l);
341                 userApps.add(epUserApp);
342                 user.setUserApps(userApps);
343                 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
344                                 .thenReturn(user);
345                 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
346                 assertTrue(actual);
347         }
348
349         @Test
350         public void isAccountAdminExceptionTest() {
351                 EPUser user = mockUser.mockEPUser();
352                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
353                                 Matchers.anyLong(), Matchers.anyMap());
354                 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
355                 assertFalse(actual);
356         }
357
358         @Test
359         public void isUserTest() {
360                 EPUser user = mockUser.mockEPUser();
361                 EPApp app = mockApp();
362                 app.setId(2l);
363                 SortedSet<EPUserApp> userApps = user.getEPUserApps();
364                 EPUserApp epUserApp = new EPUserApp();
365                 EPRole role = new EPRole();
366                 role.setActive(true);
367                 role.setId(2l);
368                 role.setName("test role");
369                 epUserApp.setApp(app);
370                 epUserApp.setRole(role);
371                 epUserApp.setUserId(1l);
372                 userApps.add(epUserApp);
373                 user.setUserApps(userApps);
374                 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
375                                 .thenReturn(user);
376                 boolean actual = adminRolesServiceImpl.isUser(user);
377                 assertTrue(actual);
378         }
379
380         @Test
381         public void isUserExceptionTest() {
382                 EPUser user = mockUser.mockEPUser();
383                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
384                                 Matchers.anyLong(), Matchers.anyMap());
385                 boolean actual = adminRolesServiceImpl.isUser(user);
386                 assertFalse(actual);
387         }
388
389         @Test
390         public void getRolesByAppTest() {
391                 EPUser user = mockUser.mockEPUser();
392                 EPApp app = mockApp();
393                 List<EPRole> expected = new ArrayList<>();
394                 EPRole role = new EPRole();
395                 role.setActive(true);
396                 role.setId(1l);
397                 role.setName("test role");
398                 expected.add(role);
399                 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap()))
400                                 .thenReturn(expected);
401                 List<EPRole> actual = adminRolesServiceImpl.getRolesByApp(user, app.getId());
402                 assertEquals(expected, actual);
403         }
404
405         @Test
406         public void isAccountAdminOfApplicationTest() {
407                 EPUser user = mockUser.mockEPUser();
408                 EPApp app = mockApp();
409                 SortedSet<EPUserApp> userApps = user.getEPUserApps();
410                 EPUserApp epUserApp = new EPUserApp();
411                 EPRole role = new EPRole();
412                 role.setActive(true);
413                 role.setId(999l);
414                 role.setName("test role");
415                 epUserApp.setApp(app);
416                 epUserApp.setRole(role);
417                 epUserApp.setUserId(1l);
418                 userApps.add(epUserApp);
419                 user.setUserApps(userApps);
420                 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
421                                 .thenReturn(user);
422                 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
423                 assertTrue(actual);
424         }
425
426         @Test
427         public void isAccountAdminOfApplicationExceptionTest() {
428                 EPUser user = mockUser.mockEPUser();
429                 EPApp app = mockApp();
430                 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
431                                 Matchers.anyLong(), Matchers.anyMap());
432                 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
433                 assertFalse(actual);
434         }
435 }