Replace ecomp references
[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                 List<EPRole> roleList = new ArrayList<>();
93                 EPRole role = new EPRole();
94                 EPRole role1 = new EPRole();
95                 role.addChildRole(role1);
96                 roleList.add(role);
97                 Mockito.when(dataAccessService.getList(EPRole.class, null)).thenReturn(roleList);
98                 List<EPRole> expectedRoleList = ePRoleServiceImpl.getAvailableChildRoles(null);
99                 assertEquals(roleList, expectedRoleList);
100         }
101
102         // @Test
103         // public void getAvailableChildRolesIfRoleIdNotNullTest()
104         // {
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);
114         //
115         // List<EPRole> expectedRoleList =
116         // ePRoleServiceImpl.getAvailableChildRoles((long) 1);
117         // System.out.println(expectedRoleList);
118         // assertEquals(roleList,expectedRoleList);
119         // }
120         //
121         @Test
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);
127         }
128
129         @Test
130         public void saveRoleFunctionTest() {
131                 EPRole role = new EPRole();
132                 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
133                 ePRoleServiceImpl.saveRole(role);
134         }
135
136         @Test
137         public void deleteRoleFunctionTest() {
138                 RoleFunction roleFunction = new RoleFunction();
139                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(roleFunction, null);
140                 ePRoleServiceImpl.deleteRoleFunction(roleFunction);
141         }
142
143         @Test
144         public void getRoleTest() {
145                 EPRole role = null;
146                 Mockito.when(dataAccessService.getDomainObject(EPRole.class, 1, null)).thenReturn(role);
147                 EPRole expectedRole = ePRoleServiceImpl.getRole((long) 1);
148                 assertEquals(expectedRole, role);
149         }
150
151         @Test
152         public void getRoleIfappIdNullTest() {
153                 assertNull(ePRoleServiceImpl.getRole(null, null));
154
155         }
156
157         @Test
158         public void getRoleIfappIdNotNullTest() {
159                 List<EPRole> roles = new ArrayList<>();
160                 EPRole role = new EPRole();
161                 roles.add(role);
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);
166
167         }
168
169         @Test
170         public void getRoleIfListSizeIsMoreThan1Test() {
171                 List<EPRole> roles = new ArrayList<>();
172                 EPRole role = new EPRole();
173                 EPRole role1 = new EPRole();
174                 roles.add(role);
175                 roles.add(role1);
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);
180
181         }
182
183         @Test
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));
189
190         }
191
192         @Test
193         public void saveRoleTest() {
194                 EPRole role = new EPRole();
195                 Mockito.doNothing().when(dataAccessService).saveDomainObject(role, null);
196                 ePRoleServiceImpl.saveRole(role);
197         }
198
199         @Test
200         public void deleteRoleTest() {
201                 EPRole role = new EPRole();
202                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(role, null);
203                 ePRoleServiceImpl.deleteRole(role);
204         }
205
206         @Test
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);
212         }
213
214         @Test
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));
220
221         }
222
223         @SuppressWarnings("unchecked")
224         @Test
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();
230                 roleList.add(role);
231                 roleList.add(role1);
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);
236
237         }
238
239         @Test
240         public void saveRoleFunction() {
241                 RoleFunction domainRoleFunction = new RoleFunction();
242                 Mockito.doNothing().when(dataAccessService).saveDomainObject(domainRoleFunction, null);
243                 ePRoleServiceImpl.saveRoleFunction(domainRoleFunction);
244         }
245 }