2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.openecomp.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.openecomp.portalapp.portal.domain.EPRole;
56 import org.openecomp.portalapp.portal.service.EPRoleServiceImpl;
57 import org.openecomp.portalapp.portal.core.MockEPUser;
58 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
59 import org.openecomp.portalsdk.core.domain.RoleFunction;
60 import org.openecomp.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 List<EPRole> roleList = new ArrayList<>();
93 EPRole role = new EPRole();
94 EPRole role1 = new EPRole();
95 role.addChildRole(role1);
97 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
98 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(null);
99 assertEquals(roleList, expectedRoleList);
103 // public void getAvailableChildRolesIfRoleIdNotNullTest()
105 // List<EPRole> roleList = new ArrayList<>();
106 // EPRole role = new EPRole();
107 // EPRole role1= new EPRole();
108 // role.addChildRole(role1);
109 // roleList.add(role);
110 // Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1,
111 // null)).thenReturn(role);
112 // Mockito.when(dataAccessService.getList(EPRole.class,
113 // null)).thenReturn(roleList);
115 // List<EPRole> expectedRoleList =
116 // ePRoleServiceImpl.getAvailableChildRoles((long) 1);
117 // System.out.println(expectedRoleList);
118 // assertEquals(roleList,expectedRoleList);
122 public void getRoleFunctionTest() {
123 RoleFunction roleFunction = new RoleFunction();
124 Mockito.when(dataAccessService.getDomainObject(RoleFunction.class, "test", null)).thenReturn(roleFunction);
125 RoleFunction expectedRoleFunction = ePRoleServiceImpl.getRoleFunction("test");
126 assertEquals(expectedRoleFunction, roleFunction);
130 public void saveRoleFunctionTest() {
131 EPRole role = new EPRole();
132 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
133 ePRoleServiceImpl.saveRole(role);
137 public void deleteRoleFunctionTest() {
138 RoleFunction roleFunction = new RoleFunction();
139 Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
140 ePRoleServiceImpl.deleteRoleFunction(roleFunction);
144 public void getRoleTest() {
146 Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
147 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
148 assertEquals(expectedRole, role);
152 public void getRoleIfappIdNullTest() {
153 assertNull(ePRoleServiceImpl.getRole(null, null));
158 public void getRoleIfappIdNotNullTest() {
159 List<EPRole> roles = new ArrayList<>();
160 EPRole role = new EPRole();
162 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
163 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
164 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
165 assertEquals(expectedRole, role);
170 public void getRoleIfListSizeIsMoreThan1Test() {
171 List<EPRole> roles = new ArrayList<>();
172 EPRole role = new EPRole();
173 EPRole role1 = new EPRole();
176 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
177 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
178 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1, (long) 1);
179 assertEquals(expectedRole, role);
184 public void getRoleIfListSizeIsEmptyTest() {
185 List<EPRole> roles = new ArrayList<>();
186 String sql = "SELECT * FROM fn_role where APP_ID = 1 AND APP_ROLE_ID = 1";
187 Mockito.when(dataAccessService.executeSQLQuery(sql, EPRole.class, null)).thenReturn(roles);
188 assertNull(ePRoleServiceImpl.getRole((long) 1, (long) 1));
193 public void saveRoleTest() {
194 EPRole role = new EPRole();
195 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
196 ePRoleServiceImpl.saveRole(role);
200 public void deleteRoleTest() {
201 EPRole role = new EPRole();
202 Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
203 ePRoleServiceImpl.deleteRole(role);
207 public void getAvailableRolesTest() {
208 List<EPRole> roleList = new ArrayList<>();
209 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
210 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableRoles();
211 assertEquals(expectedRoleList, roleList);
215 public void getAppRolesTest() {
216 final Map<String, String> portalParams = null;
217 List<EPRole> roleList = new ArrayList<>();
218 Mockito.when(dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null)).thenReturn(roleList);
219 assertNull(ePRoleServiceImpl.getAppRole("test", (long) 1));
223 @SuppressWarnings("unchecked")
225 public void getAppRolesIfNotPortalTest() {
226 final Map<String, String> params = null;
227 List<EPRole> roleList = new ArrayList<>();
228 EPRole role = new EPRole();
229 EPRole role1 = new EPRole();
232 Mockito.when((List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null))
233 .thenReturn(roleList);
234 List<EPRole> expectedRoleList = (List<EPRole>) ePRoleServiceImpl.getAppRole("test", (long) 10);
235 System.out.println(expectedRoleList);
240 public void saveRoleFunction() {
241 RoleFunction domainRoleFunction = new RoleFunction();
242 Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
243 ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);