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.*;
42 import java.util.ArrayList;
43 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.InjectMocks;
52 import org.mockito.Matchers;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.portal.core.MockEPUser;
57 import org.onap.portalapp.portal.domain.EPRole;
58 import org.onap.portalapp.portal.framework.MockitoTestSuite;
59 import org.onap.portalapp.portal.service.EPRoleServiceImpl;
60 import org.onap.portalapp.portal.utils.PortalConstants;
61 import org.onap.portalsdk.core.domain.RoleFunction;
62 import org.onap.portalsdk.core.service.DataAccessService;
64 public class EPRoleServiceImplTest {
67 DataAccessService dataAccessService;
71 MockitoAnnotations.initMocks(this);
75 ExternalAccessRolesService externalAccessRolesService;
77 EPRoleServiceImpl ePRoleServiceImpl = new EPRoleServiceImpl();
79 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
81 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
82 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
83 NullPointerException nullPointerException = new NullPointerException();
84 MockEPUser mockUser = new MockEPUser();
87 public void getRoleFunctionsTest() {
88 List<RoleFunction> roleFunctionList = new ArrayList<>();
89 Mockito.when(dataAccessService.getList(RoleFunction.class, null)).thenReturn(roleFunctionList);
90 List<RoleFunction> expectedRoleFunctionList = ePRoleServiceImpl.getRoleFunctions();
91 assertEquals(roleFunctionList, expectedRoleFunctionList);
95 public void getAvailableChildRolesIfRoleIdIsNullTest() {
96 Long roleId = (long)123;
97 List<EPRole> roleList = new ArrayList<>();
98 EPRole epRole = new EPRole();
99 EPRole role = new EPRole();
100 EPRole role1 = new EPRole();
101 role.addChildRole(role1);
103 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
104 Mockito.when(dataAccessService.getDomainObject(EPRole.class, roleId, null)).thenReturn(epRole);
105 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(null);
106 assertEquals(roleList, expectedRoleList);
110 public void getAvailableChildRolesIfRoleIdTest() {
111 Long roleId = (long)123;
112 List<EPRole> roleList = new ArrayList<>();
113 EPRole epRole = new EPRole();
114 EPRole role = new EPRole();
115 EPRole role1 = new EPRole();
116 role.addChildRole(role1);
118 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
119 Mockito.when(dataAccessService.getDomainObject(EPRole.class, roleId, null)).thenReturn(epRole);
120 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(roleId);
121 assertEquals(roleList, expectedRoleList);
125 public void getRoleFunctionTest() {
126 RoleFunction roleFunction = new RoleFunction();
127 Mockito.when(dataAccessService.getDomainObject(RoleFunction.class, "test", null)).thenReturn(roleFunction);
128 RoleFunction expectedRoleFunction = ePRoleServiceImpl.getRoleFunction("test");
129 assertEquals(expectedRoleFunction, roleFunction);
133 public void saveRoleFunctionTest() {
134 EPRole role = new EPRole();
135 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
136 ePRoleServiceImpl.saveRole(role);
140 public void deleteRoleFunctionTest() {
141 RoleFunction roleFunction = new RoleFunction();
142 Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
143 ePRoleServiceImpl.deleteRoleFunction(roleFunction);
147 public void getRoleTest() {
149 Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
150 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
151 assertEquals(expectedRole, role);
155 public void getRoleIfappIdNullTest() {
156 assertNull(ePRoleServiceImpl.getRole(null, null));
161 public void getRoleIfappIdNotNullTest() {
162 List<EPRole> roles = new ArrayList<>();
163 EPRole role = new EPRole();
165 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
166 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
167 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
168 assertEquals(expectedRole, role);
173 public void getRoleIfListSizeIsMoreThan1Test() {
174 List<EPRole> roles = new ArrayList<>();
175 EPRole role = new EPRole();
176 EPRole role1 = new EPRole();
179 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
180 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
181 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
182 assertEquals(expectedRole, role);
187 public void getRoleIfListSizeIsEmptyTest() {
188 List<EPRole> roles = new ArrayList<>();
189 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
190 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
191 assertNull(ePRoleServiceImpl.getRole((long) 1, (long) 1));
196 public void saveRoleTest() {
197 EPRole role = new EPRole();
198 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
199 ePRoleServiceImpl.saveRole(role);
203 public void deleteRoleTest() {
204 EPRole role = new EPRole();
205 Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
206 ePRoleServiceImpl.deleteRole(role);
210 public void getAvailableRolesTest() {
211 List<EPRole> roleList = new ArrayList<>();
212 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
213 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableRoles();
214 assertEquals(expectedRoleList, roleList);
218 public void getAppRolesTest() {
219 final Map<String, String> portalParams = null;
220 List<EPRole> roleList = new ArrayList<>();
221 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null)).thenReturn(roleList);
222 Mockito.when(externalAccessRolesService.getPortalAppRoleInfo(Matchers.anyLong())).thenReturn(roleList);
223 assertNull(ePRoleServiceImpl.getAppRole("test", (long) 1));
227 @SuppressWarnings("unchecked")
229 public void getAppRolesIfNotPortalTest() {
230 final Map<String, String> params = null;
231 List<EPRole> roleList = new ArrayList<>();
232 EPRole role = new EPRole();
233 EPRole role1 = new EPRole();
236 Mockito.when((List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null))
237 .thenReturn(roleList);
238 List<EPRole> expectedRoleList = (List<EPRole>) ePRoleServiceImpl.getAppRole("test", (long) 10);
242 public void saveRoleFunction() {
243 RoleFunction domainRoleFunction = new RoleFunction();
244 Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
245 ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);