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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.assertTrue;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.SortedSet;
50 import org.hibernate.SQLQuery;
51 import org.hibernate.Session;
52 import org.hibernate.SessionFactory;
53 import org.hibernate.Transaction;
54 import org.json.simple.JSONObject;
55 import org.junit.After;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.InjectMocks;
60 import org.mockito.Matchers;
61 import org.mockito.Mock;
62 import org.mockito.Mockito;
63 import org.mockito.MockitoAnnotations;
64 import org.onap.portalapp.portal.core.MockEPUser;
65 import org.onap.portalapp.portal.domain.EPApp;
66 import org.onap.portalapp.portal.domain.EPRole;
67 import org.onap.portalapp.portal.domain.EPUser;
68 import org.onap.portalapp.portal.domain.EPUserApp;
69 import org.onap.portalapp.portal.domain.UserRole;
70 import org.onap.portalapp.portal.transport.AppNameIdIsAdmin;
71 import org.onap.portalapp.portal.transport.AppsListWithAdminRole;
72 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
73 import org.onap.portalapp.portal.utils.EcompPortalUtils;
74 import org.onap.portalapp.portal.utils.PortalConstants;
75 import org.onap.portalsdk.core.service.DataAccessService;
76 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
77 import org.onap.portalsdk.core.util.SystemProperties;
78 import org.powermock.api.mockito.PowerMockito;
79 import org.powermock.core.classloader.annotations.PrepareForTest;
80 import org.powermock.modules.junit4.PowerMockRunner;
81 import org.springframework.http.HttpEntity;
82 import org.springframework.http.HttpMethod;
83 import org.springframework.http.HttpStatus;
84 import org.springframework.http.ResponseEntity;
85 import org.springframework.web.client.RestTemplate;
87 @RunWith(PowerMockRunner.class)
88 @PrepareForTest({ EcompPortalUtils.class, PortalConstants.class, SystemProperties.class,
89 EPCommonSystemProperties.class })
90 public class AdminRolesServiceImplTest {
93 DataAccessService dataAccessService = new DataAccessServiceImpl();
96 EPAppCommonServiceImpl epAppCommonServiceImpl = new EPAppCommonServiceImpl();
99 SearchServiceImpl searchServiceImpl = new SearchServiceImpl();
102 SessionFactory sessionFactory;
108 Transaction transaction;
111 RestTemplate template = new RestTemplate();
114 public void setup() {
115 MockitoAnnotations.initMocks(this);
116 Mockito.when(sessionFactory.openSession()).thenReturn(session);
117 Mockito.when(session.beginTransaction()).thenReturn(transaction);
121 public void after() {
126 AdminRolesServiceImpl adminRolesServiceImpl = new AdminRolesServiceImpl();
128 private Long ACCOUNT_ADMIN_ROLE_ID = 999L;
130 public EPApp mockApp() {
131 EPApp app = new EPApp();
133 app.setImageUrl("test");
134 app.setNameSpace("com.test.app");
135 app.setCentralAuth(true);
136 app.setDescription("test");
137 app.setNotes("test");
140 app.setAppRestEndpoint("test");
141 app.setAlternateUrl("test");
143 app.setMlAppName("test");
144 app.setMlAppAdminId("test");
145 app.setUsername("test");
146 app.setAppPassword("test");
148 app.setEnabled(true);
149 app.setUebKey("test");
150 app.setUebSecret("test");
151 app.setUebTopicName("test");
156 MockEPUser mockUser = new MockEPUser();
158 @SuppressWarnings("deprecation")
160 public void getAppsWithAdminRoleStateForUserTest() {
161 EPUser user = mockUser.mockEPUser();
162 EPApp app = mockApp();
164 List<EPUser> users = new ArrayList<>();
166 Map<String, String> userParams = new HashMap<>();
167 userParams.put("org_user_id", user.getOrgUserId());
168 Mockito.when(dataAccessService.executeNamedQuery("getEPUserByOrgUserId", userParams, null)).thenReturn(users);
169 List<EPUserApp> userAppList = new ArrayList<>();
170 EPUserApp epUserApp = new EPUserApp();
171 EPRole role = new EPRole();
172 role.setActive(true);
174 role.setName("test role");
175 epUserApp.setApp(app);
176 epUserApp.setRole(role);
177 epUserApp.setUserId(1l);
178 userAppList.add(epUserApp);
179 Mockito.when(dataAccessService.getList(EPUserApp.class,
180 " where userId = " + user.getId() + " and role.id = " + 999, null, null)).thenReturn(userAppList);
181 adminRolesServiceImpl.getAppsWithAdminRoleStateForUser(user.getOrgUserId());
184 @SuppressWarnings({ "deprecation", "unchecked" })
186 public void setAppsWithAdminRoleStateForUserTest() {
187 PowerMockito.mockStatic(EPCommonSystemProperties.class);
188 PowerMockito.mockStatic(EcompPortalUtils.class);
189 PowerMockito.mockStatic(PortalConstants.class);
190 PowerMockito.mockStatic(SystemProperties.class);
191 EPUser user = mockUser.mockEPUser();
192 EPApp app = mockApp();
194 EPApp app2 = mockApp();
195 app2.setName("app2");
196 app2.setNameSpace("com.test.app2");
198 EPApp app3 = mockApp();
199 app3.setName("app3");
200 app3.setNameSpace("com.test.app3");
202 List<EPApp> apps = new ArrayList<>();
206 Mockito.when(epAppCommonServiceImpl.getAppsFullList()).thenReturn(apps);
208 List<EPUser> localUserList = new ArrayList<>();
209 localUserList.add(user);
211 dataAccessService.getList(EPUser.class, " where org_user_id='" + user.getOrgUserId() + "'", null, null))
212 .thenReturn(localUserList);
213 List<EPUserApp> oldAppsWhereUserIsAdmin = new ArrayList<EPUserApp>();
214 EPUserApp epUserApp = new EPUserApp();
215 EPRole role = new EPRole();
216 role.setActive(true);
218 role.setName("app5");
219 epUserApp.setApp(app);
220 epUserApp.setRole(role);
221 epUserApp.setUserId(1l);
222 oldAppsWhereUserIsAdmin.add(epUserApp);
223 Mockito.when(dataAccessService.getList(EPUserApp.class,
224 " where userId = " + user.getId() + " and role.id = " + ACCOUNT_ADMIN_ROLE_ID, null,
225 null)).thenReturn(oldAppsWhereUserIsAdmin);
226 Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
227 EPApp app4 = mockApp();
229 app4.setName("app7");
230 app4.setNameSpace("com.test.app7");
231 List<EPApp> apps2 = new ArrayList<>();
236 Mockito.when(dataAccessService.executeNamedQuery("getCentralizedApps", null, null)).thenReturn(apps2);
238 EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
240 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN))
241 .thenReturn("@test.com");
242 JSONObject getUserRoles = new JSONObject();
243 ResponseEntity<String> getResponse = new ResponseEntity<>(getUserRoles.toString(), HttpStatus.OK);
244 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
245 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse);
246 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
247 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
248 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
250 AppsListWithAdminRole newAppsListWithAdminRoles = new AppsListWithAdminRole();
251 ArrayList<AppNameIdIsAdmin> appsRoles = new ArrayList<>();
252 AppNameIdIsAdmin appNameIdIsAdmin = new AppNameIdIsAdmin();
253 appNameIdIsAdmin.setAppName("app1");
254 appNameIdIsAdmin.setId(2l);
255 appNameIdIsAdmin.setIsAdmin(true);
256 appNameIdIsAdmin.setRestrictedApp(false);
257 AppNameIdIsAdmin appNameIdIsAdmin2 = new AppNameIdIsAdmin();
258 appNameIdIsAdmin2.setAppName("app2");
259 appNameIdIsAdmin2.setId(3l);
260 appNameIdIsAdmin2.setIsAdmin(true);
261 appNameIdIsAdmin2.setRestrictedApp(false);
262 appsRoles.add(appNameIdIsAdmin);
263 appsRoles.add(appNameIdIsAdmin2);
264 newAppsListWithAdminRoles.setOrgUserId(user.getOrgUserId());
265 newAppsListWithAdminRoles.setAppsRoles(appsRoles);
266 Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin.id)).thenReturn(app2);
267 Mockito.when((EPApp) session.get(EPApp.class, appNameIdIsAdmin2.id)).thenReturn(app3);
268 JSONObject getUserRoles2 = new JSONObject();
269 JSONObject getUserRoles3 = new JSONObject();
270 JSONObject getUserRoles4 = new JSONObject();
271 JSONObject finalUserRoles = new JSONObject();
272 getUserRoles2.put("role", "com.test.app3.Account_Administrator");
273 getUserRoles3.put("role", "com.test.app3.admin");
274 getUserRoles4.put("role", "com.test.app3.owner");
275 List<JSONObject> userRoles = new ArrayList<>();
276 userRoles.add(getUserRoles2);
277 userRoles.add(getUserRoles3);
278 userRoles.add(getUserRoles4);
279 finalUserRoles.put("userRole", userRoles);
280 ResponseEntity<String> getResponse2 = new ResponseEntity<>(finalUserRoles.toString(), HttpStatus.OK);
281 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
282 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(getResponse2);
283 boolean actual = adminRolesServiceImpl.setAppsWithAdminRoleStateForUser(newAppsListWithAdminRoles);
288 public void isSuperAdminTest() {
289 EPUser user = mockUser.mockEPUser();
291 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
292 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
293 List<UserRole> userRoleList = new ArrayList<>();
294 UserRole userRole = new UserRole();
295 userRole.setFirstName("Hello");
296 userRole.setLastName("World");
297 userRole.setRoleId(1l);
298 userRole.setRoleName("test");
299 userRole.setUser_Id(1l);
300 userRoleList.add(userRole);
301 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap()))
302 .thenReturn(userRoleList);
303 boolean actual = adminRolesServiceImpl.isSuperAdmin(user);
308 public void isSuperAdminExceptionTest() {
309 EPUser user = mockUser.mockEPUser();
311 SQLQuery SqlQuery = Mockito.mock(SQLQuery.class);
312 Mockito.when(session.createSQLQuery(Matchers.anyString())).thenReturn(SqlQuery);
313 Mockito.doThrow(new NullPointerException()).when(dataAccessService).executeSQLQuery(Matchers.anyString(),
314 Matchers.any(), Matchers.anyMap());
315 boolean actual = adminRolesServiceImpl.isSuperAdmin(user);
320 public void isAccountAdminTest() {
321 EPUser user = mockUser.mockEPUser();
322 EPApp app = mockApp();
324 SortedSet<EPUserApp> userApps = user.getEPUserApps();
325 EPUserApp epUserApp = new EPUserApp();
326 EPRole role = new EPRole();
327 role.setActive(true);
329 role.setName("test role");
330 epUserApp.setApp(app);
331 epUserApp.setRole(role);
332 epUserApp.setUserId(1l);
333 userApps.add(epUserApp);
334 user.setUserApps(userApps);
335 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
337 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
342 public void isAccountAdminExceptionTest() {
343 EPUser user = mockUser.mockEPUser();
344 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
345 Matchers.anyLong(), Matchers.anyMap());
346 boolean actual = adminRolesServiceImpl.isAccountAdmin(user);
351 public void isUserTest() {
352 EPUser user = mockUser.mockEPUser();
353 EPApp app = mockApp();
355 SortedSet<EPUserApp> userApps = user.getEPUserApps();
356 EPUserApp epUserApp = new EPUserApp();
357 EPRole role = new EPRole();
358 role.setActive(true);
360 role.setName("test role");
361 epUserApp.setApp(app);
362 epUserApp.setRole(role);
363 epUserApp.setUserId(1l);
364 userApps.add(epUserApp);
365 user.setUserApps(userApps);
366 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
368 boolean actual = adminRolesServiceImpl.isUser(user);
373 public void isUserExceptionTest() {
374 EPUser user = mockUser.mockEPUser();
375 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
376 Matchers.anyLong(), Matchers.anyMap());
377 boolean actual = adminRolesServiceImpl.isUser(user);
382 public void getRolesByAppTest() {
383 EPUser user = mockUser.mockEPUser();
384 EPApp app = mockApp();
385 List<EPRole> expected = new ArrayList<>();
386 EPRole role = new EPRole();
387 role.setActive(true);
389 role.setName("test role");
391 Mockito.when(dataAccessService.executeSQLQuery(Matchers.anyString(), Matchers.any(), Matchers.anyMap()))
392 .thenReturn(expected);
393 List<EPRole> actual = adminRolesServiceImpl.getRolesByApp(user, app.getId());
394 assertEquals(expected, actual);
398 public void isAccountAdminOfApplicationTest() {
399 EPUser user = mockUser.mockEPUser();
400 EPApp app = mockApp();
401 SortedSet<EPUserApp> userApps = user.getEPUserApps();
402 EPUserApp epUserApp = new EPUserApp();
403 EPRole role = new EPRole();
404 role.setActive(true);
406 role.setName("test role");
407 epUserApp.setApp(app);
408 epUserApp.setRole(role);
409 epUserApp.setUserId(1l);
410 userApps.add(epUserApp);
411 user.setUserApps(userApps);
412 Mockito.when((EPUser) dataAccessService.getDomainObject(Matchers.any(), Matchers.anyLong(), Matchers.anyMap()))
414 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);
419 public void isAccountAdminOfApplicationExceptionTest() {
420 EPUser user = mockUser.mockEPUser();
421 EPApp app = mockApp();
422 Mockito.doThrow(new NullPointerException()).when(dataAccessService).getDomainObject(Matchers.any(),
423 Matchers.anyLong(), Matchers.anyMap());
424 boolean actual = adminRolesServiceImpl.isAccountAdminOfApplication(user, app);