2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
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;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.SortedSet;
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;
88 @RunWith(PowerMockRunner.class)
89 @PrepareForTest({ EcompPortalUtils.class, PortalConstants.class, SystemProperties.class,
90 EPCommonSystemProperties.class })
91 public class AdminRolesServiceImplTest {
94 DataAccessService dataAccessService = new DataAccessServiceImpl();
97 EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
100 SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
103 SessionFactory sessionFactory;
109 Transaction transaction;
112 RestTemplate template = new RestTemplate();
115 public void setup() {
116 MockitoAnnotations.initMocks(this);
117 Mockito.when(sessionFactory.openSession()).thenReturn(session);
118 Mockito.when(session.beginTransaction()).thenReturn(transaction);
122 public void after() {
127 AdminRolesServiceImpl adminRolesServiceImpl = new AdminRolesServiceImpl();
129 private Long ACCOUNT_ADMIN_ROLE_ID = 999L;
131 private Long ECOMP_APP_ID = 1L;
133 public EPApp mockApp() {
134 EPApp app = new EPApp();
136 app.setImageUrl("test");
137 app.setNameSpace("com.test.app");
138 app.setCentralAuth(true);
139 app.setDescription("test");
140 app.setNotes("test");
143 app.setAppRestEndpoint("test");
144 app.setAlternateUrl("test");
146 app.setMlAppName("test");
147 app.setMlAppAdminId("test");
148 app.setUsername("test");
149 app.setAppPassword("test");
151 app.setEnabled(true);
152 app.setUebKey("test");
153 app.setUebSecret("test");
154 app.setUebTopicName("test");
159 MockEPUser mockUser = new MockEPUser();
161 @SuppressWarnings("deprecation")
163 public void getAppsWithAdminRoleStateForUserTest() {
164 EPUser user = mockUser.mockEPUser();
165 EPApp app = mockApp();
167 List<EPUser> users = new ArrayList<>();
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);
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<>();
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);
193 public void getAppsWithAdminRoleStateForUserTestWithException() {
194 EPUser user = mockUser.mockEPUser();
195 EPApp app = mockApp();
197 List<EPUser> users = new ArrayList<>();
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());
207 @SuppressWarnings({ "deprecation", "unchecked" })
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();
217 EPApp app2 = mockApp();
218 app2.setName("app2");
219 app2.setNameSpace("com.test.app2");
221 EPApp app3 = mockApp();
222 app3.setName("app3");
223 app3.setNameSpace("com.test.app3");
225 List<EPApp> apps = new ArrayList<>();
229 Mockito.when(epAppCommonServiceImpl.getAppsFullList()).thenReturn(apps);
231 List<EPUser> localUserList = new ArrayList<>();
232 localUserList.add(user);
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);
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();
252 app4.setName("app7");
253 app4.setNameSpace("com.test.app7");
254 List<EPApp> apps2 = new ArrayList<>();
259 Mockito.when(dataAccessService.executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps2);
261 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
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);
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);
311 public void isSuperAdminTest() {
312 EPUser user = mockUser.mockEPUser();
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);
331 public void isSuperAdminExceptionTest() {
332 EPUser user = mockUser.mockEPUser();
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);
343 public void isAccountAdminTest() {
344 EPUser user = mockUser.mockEPUser();
345 EPApp app = mockApp();
347 SortedSet<EPUserApp> userApps = user.getEPUserApps();
348 EPUserApp epUserApp = new EPUserApp();
349 EPRole role = new EPRole();
350 role.setActive(true);
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()))
360 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
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);
374 public void isUserTest() {
375 EPUser user = mockUser.mockEPUser();
376 EPApp app = mockApp();
378 SortedSet<EPUserApp> userApps = user.getEPUserApps();
379 EPUserApp epUserApp = new EPUserApp();
380 EPRole role = new EPRole();
381 role.setActive(true);
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()))
391 boolean actual = adminRolesServiceImpl.isUser(user);
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);
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);
412 role.setName("test 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);
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);
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()))
437 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
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);