95986457717964b0a0982eac13d84278c13f9798
[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.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;
61
62 public class EPRoleServiceImplTest {
63
64         @Mock
65         DataAccessService dataAccessService;
66
67         @Before
68         public void setup() {
69                 MockitoAnnotations.initMocks(this);
70         }
71
72         @InjectMocks
73         EPRoleServiceImpl ePRoleServiceImpl = new EPRoleServiceImpl();
74
75         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
76
77         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
78         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
79         NullPointerException nullPointerException = new NullPointerException();
80         MockEPUser mockUser = new MockEPUser();
81
82         @Test
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);
88         }
89
90         @Test
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);
98                 roleList.add(role);
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);
103         }
104
105         @Test
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);
113                 roleList.add(role);
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);
118         }
119         
120         @Test
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);
126         }
127
128         @Test
129         public void saveRoleFunctionTest() {
130                 EPRole role = new EPRole();
131                 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
132                 ePRoleServiceImpl.saveRole(role);
133         }
134
135         @Test
136         public void deleteRoleFunctionTest() {
137                 RoleFunction roleFunction = new RoleFunction();
138                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
139                 ePRoleServiceImpl.deleteRoleFunction(roleFunction);
140         }
141
142         @Test
143         public void getRoleTest() {
144                 EPRole role = null;
145                 Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
146                 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
147                 assertEquals(expectedRole, role);
148         }
149
150         @Test
151         public void getRoleIfappIdNullTest() {
152                 assertNull(ePRoleServiceImpl.getRole(null, null));
153
154         }
155
156         @Test
157         public void getRoleIfappIdNotNullTest() {
158                 List<EPRole> roles = new ArrayList<>();
159                 EPRole role = new EPRole();
160                 roles.add(role);
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);
165
166         }
167
168         @Test
169         public void getRoleIfListSizeIsMoreThan1Test() {
170                 List<EPRole> roles = new ArrayList<>();
171                 EPRole role = new EPRole();
172                 EPRole role1 = new EPRole();
173                 roles.add(role);
174                 roles.add(role1);
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);
179
180         }
181
182         @Test
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));
188
189         }
190
191         @Test
192         public void saveRoleTest() {
193                 EPRole role = new EPRole();
194                 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
195                 ePRoleServiceImpl.saveRole(role);
196         }
197
198         @Test
199         public void deleteRoleTest() {
200                 EPRole role = new EPRole();
201                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
202                 ePRoleServiceImpl.deleteRole(role);
203         }
204
205         @Test
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);
211         }
212
213         @Test
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));
219
220         }
221
222         @SuppressWarnings("unchecked")
223         @Test
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();
229                 roleList.add(role);
230                 roleList.add(role1);
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);
235
236         }
237
238         @Test
239         public void saveRoleFunction() {
240                 RoleFunction domainRoleFunction = new RoleFunction();
241                 Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
242                 ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);
243         }
244 }