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.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.core.MockEPUser;
56 import org.onap.portalapp.portal.domain.EPRole;
57 import org.onap.portalapp.portal.framework.MockitoTestSuite;
58 import org.onap.portalapp.portal.service.EPRoleServiceImpl;
59 import org.onap.portalsdk.core.domain.RoleFunction;
60 import org.onap.portalsdk.core.service.DataAccessService;
62 public class EPRoleServiceImplTest {
65 DataAccessService dataAccessService;
69 MockitoAnnotations.initMocks(this);
73 EPRoleServiceImpl ePRoleServiceImpl = new EPRoleServiceImpl();
75 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
77 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
78 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
79 NullPointerException nullPointerException = new NullPointerException();
80 MockEPUser mockUser = new MockEPUser();
83 public void getRoleFunctionsTest() {
84 List<RoleFunction> roleFunctionList = new ArrayList<>();
85 Mockito.when(dataAccessService.getList(RoleFunction.class, null)).thenReturn(roleFunctionList);
86 List<RoleFunction> expectedRoleFunctionList = ePRoleServiceImpl.getRoleFunctions();
87 assertEquals(roleFunctionList, expectedRoleFunctionList);
91 public void getAvailableChildRolesIfRoleIdIsNullTest() {
92 Long roleId = (long)123;
93 List<EPRole> roleList = new ArrayList<>();
94 EPRole epRole = new EPRole();
95 EPRole role = new EPRole();
96 EPRole role1 = new EPRole();
97 role.addChildRole(role1);
99 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
100 Mockito.when(dataAccessService.getDomainObject(EPRole.class, roleId, null)).thenReturn(epRole);
101 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(null);
102 assertEquals(roleList, expectedRoleList);
106 public void getAvailableChildRolesIfRoleIdTest() {
107 Long roleId = (long)123;
108 List<EPRole> roleList = new ArrayList<>();
109 EPRole epRole = new EPRole();
110 EPRole role = new EPRole();
111 EPRole role1 = new EPRole();
112 role.addChildRole(role1);
114 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
115 Mockito.when(dataAccessService.getDomainObject(EPRole.class, roleId, null)).thenReturn(epRole);
116 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(roleId);
117 assertEquals(roleList, expectedRoleList);
121 public void getRoleFunctionTest() {
122 RoleFunction roleFunction = new RoleFunction();
123 Mockito.when(dataAccessService.getDomainObject(RoleFunction.class, "test", null)).thenReturn(roleFunction);
124 RoleFunction expectedRoleFunction = ePRoleServiceImpl.getRoleFunction("test");
125 assertEquals(expectedRoleFunction, roleFunction);
129 public void saveRoleFunctionTest() {
130 EPRole role = new EPRole();
131 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
132 ePRoleServiceImpl.saveRole(role);
136 public void deleteRoleFunctionTest() {
137 RoleFunction roleFunction = new RoleFunction();
138 Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
139 ePRoleServiceImpl.deleteRoleFunction(roleFunction);
143 public void getRoleTest() {
145 Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
146 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
147 assertEquals(expectedRole, role);
151 public void getRoleIfappIdNullTest() {
152 assertNull(ePRoleServiceImpl.getRole(null, null));
157 public void getRoleIfappIdNotNullTest() {
158 List<EPRole> roles = new ArrayList<>();
159 EPRole role = new EPRole();
161 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
162 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
163 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
164 assertEquals(expectedRole, role);
169 public void getRoleIfListSizeIsMoreThan1Test() {
170 List<EPRole> roles = new ArrayList<>();
171 EPRole role = new EPRole();
172 EPRole role1 = new EPRole();
175 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
176 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
177 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
178 assertEquals(expectedRole, role);
183 public void getRoleIfListSizeIsEmptyTest() {
184 List<EPRole> roles = new ArrayList<>();
185 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
186 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
187 assertNull(ePRoleServiceImpl.getRole((long) 1, (long) 1));
192 public void saveRoleTest() {
193 EPRole role = new EPRole();
194 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
195 ePRoleServiceImpl.saveRole(role);
199 public void deleteRoleTest() {
200 EPRole role = new EPRole();
201 Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
202 ePRoleServiceImpl.deleteRole(role);
206 public void getAvailableRolesTest() {
207 List<EPRole> roleList = new ArrayList<>();
208 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
209 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableRoles();
210 assertEquals(expectedRoleList, roleList);
214 public void getAppRolesTest() {
215 final Map<String, String> portalParams = null;
216 List<EPRole> roleList = new ArrayList<>();
217 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null)).thenReturn(roleList);
218 assertNull(ePRoleServiceImpl.getAppRole("test", (long) 1));
222 @SuppressWarnings("unchecked")
224 public void getAppRolesIfNotPortalTest() {
225 final Map<String, String> params = null;
226 List<EPRole> roleList = new ArrayList<>();
227 EPRole role = new EPRole();
228 EPRole role1 = new EPRole();
231 Mockito.when((List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null))
232 .thenReturn(roleList);
233 List<EPRole> expectedRoleList = (List<EPRole>) ePRoleServiceImpl.getAppRole("test", (long) 10);
234 System.out.println(expectedRoleList);
239 public void saveRoleFunction() {
240 RoleFunction domainRoleFunction = new RoleFunction();
241 Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
242 ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);