Merge "Portal Spring Boot Development"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / EPRoleServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.*;
41
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Map;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
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;
63
64 public class EPRoleServiceImplTest {
65
66         @Mock
67         DataAccessService dataAccessService;
68
69         @Before
70         public void setup() {
71                 MockitoAnnotations.initMocks(this);
72         }
73
74         @Mock
75         ExternalAccessRolesService externalAccessRolesService;
76         @InjectMocks
77         EPRoleServiceImpl ePRoleServiceImpl = new EPRoleServiceImpl();
78
79         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
80
81         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
82         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
83         NullPointerException nullPointerException = new NullPointerException();
84         MockEPUser mockUser = new MockEPUser();
85
86         @Test
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);
92         }
93
94         @Test
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);
102                 roleList.add(role);
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);
107         }
108
109         @Test
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);
117                 roleList.add(role);
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);
122         }
123         
124         @Test
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);
130         }
131
132         @Test
133         public void saveRoleFunctionTest() {
134                 EPRole role = new EPRole();
135                 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
136                 ePRoleServiceImpl.saveRole(role);
137         }
138
139         @Test
140         public void deleteRoleFunctionTest() {
141                 RoleFunction roleFunction = new RoleFunction();
142                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
143                 ePRoleServiceImpl.deleteRoleFunction(roleFunction);
144         }
145
146         @Test
147         public void getRoleTest() {
148                 EPRole role = null;
149                 Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
150                 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
151                 assertEquals(expectedRole, role);
152         }
153
154         @Test
155         public void getRoleIfappIdNullTest() {
156                 assertNull(ePRoleServiceImpl.getRole(null, null));
157
158         }
159
160         @Test
161         public void getRoleIfappIdNotNullTest() {
162                 List<EPRole> roles = new ArrayList<>();
163                 EPRole role = new EPRole();
164                 roles.add(role);
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);
169
170         }
171
172         @Test
173         public void getRoleIfListSizeIsMoreThan1Test() {
174                 List<EPRole> roles = new ArrayList<>();
175                 EPRole role = new EPRole();
176                 EPRole role1 = new EPRole();
177                 roles.add(role);
178                 roles.add(role1);
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);
183
184         }
185
186         @Test
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));
192
193         }
194
195         @Test
196         public void saveRoleTest() {
197                 EPRole role = new EPRole();
198                 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
199                 ePRoleServiceImpl.saveRole(role);
200         }
201
202         @Test
203         public void deleteRoleTest() {
204                 EPRole role = new EPRole();
205                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
206                 ePRoleServiceImpl.deleteRole(role);
207         }
208
209         @Test
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);
215         }
216
217         @Test
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));
224
225         }
226
227         @SuppressWarnings("unchecked")
228         @Test
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();
234                 roleList.add(role);
235                 roleList.add(role1);
236                 Mockito.when((List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null))
237                                 .thenReturn(roleList);
238                 List<EPRole> expectedRoleList = (List<EPRole>) ePRoleServiceImpl.getAppRole("test", (long) 10);
239         }
240
241         @Test
242         public void saveRoleFunction() {
243                 RoleFunction domainRoleFunction = new RoleFunction();
244                 Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
245                 ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);
246         }
247 }