SchedulerController class fix
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / SchedulerControllerTest.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.controller;
39
40 import static org.junit.Assert.*;
41
42 import java.util.HashSet;
43 import java.util.Set;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.apache.poi.ss.formula.functions.T;
49 import org.json.simple.JSONObject;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.InjectMocks;
54 import org.mockito.Matchers;
55 import org.mockito.Mock;
56 import org.mockito.Mockito;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalapp.portal.core.MockEPUser;
59 import org.onap.portalapp.portal.domain.EPUser;
60 import org.onap.portalapp.portal.framework.MockitoTestSuite;
61 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
62 import org.onap.portalapp.portal.scheduler.SchedulerRestInterface;
63 import org.onap.portalapp.portal.scheduler.restobjects.RestObject;
64 import org.onap.portalapp.portal.service.AdminRolesService;
65 import org.onap.portalapp.util.EPUserUtils;
66 import org.onap.portalsdk.core.util.SystemProperties;
67 import org.onap.portalsdk.core.web.support.UserUtils;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71 import org.springframework.http.HttpStatus;
72 import org.springframework.http.ResponseEntity;
73
74 @RunWith(PowerMockRunner.class)
75 @PrepareForTest({UserUtils.class,SystemProperties.class,SchedulerProperties.class,EPUserUtils.class})
76 public class SchedulerControllerTest {
77
78         @Mock
79         SchedulerRestInterface schedulerRestInterface;
80         
81         @Mock
82         AdminRolesService adminRolesService;
83
84         @InjectMocks
85         SchedulerController schedulerController;
86
87         @Before
88         public void setup() {
89                 MockitoAnnotations.initMocks(this);
90         }
91
92         MockEPUser mockUser = new MockEPUser();
93         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
94
95         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
96         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
97         NullPointerException nullPointerException = new NullPointerException();
98
99         @Test
100         public void  getTimeSlotsTest() throws Exception{
101                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
102                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
103                 Set<String> functions = new HashSet<>();
104                 functions.add("/get_time_slots/*");
105                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/get_time_slots/1");
106                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
107         PowerMockito.mockStatic(SystemProperties.class);
108         PowerMockito.mockStatic(EPUserUtils.class);
109         EPUser user = new EPUser();
110         user.setId((long) 1);
111         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
112         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
113                 schedulerController.getTimeSlots(mockedRequest, "12");
114                 
115         }
116         
117         @Test
118         public void  getTimeSlotsTestWithException1() throws Exception{
119                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
120                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
121                 Set<String> functions = new HashSet<>();
122                 functions.add("/get_time_slots/*");
123                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/get_time_slots/1");
124                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
125         PowerMockito.mockStatic(SystemProperties.class);
126         PowerMockito.mockStatic(EPUserUtils.class);
127         EPUser user = new EPUser();
128         user.setId((long) 1);
129         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
130         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
131                 RestObject<T> restObject=new RestObject<>();
132                 Mockito.doThrow(new NullPointerException()).when(schedulerRestInterface).Get(Matchers.any(),Matchers.any(),Matchers.any(),Matchers.any());
133                 schedulerController.getTimeSlots(mockedRequest, "12");
134                 
135         }
136         
137         
138         @Test
139         public void  getTimeSlotsTestWithexception() throws Exception{
140                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
141                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
142                 Set<String> functions = new HashSet<>();
143                 functions.add("/get_time_slots/*");
144                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/get_time_slots/1");
145                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
146         PowerMockito.mockStatic(SystemProperties.class);
147         PowerMockito.mockStatic(EPUserUtils.class);
148         EPUser user = new EPUser();
149         user.setId((long) 1);
150         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
151                 schedulerController.getTimeSlots(mockedRequest, null);
152                 
153         }
154         
155         @Test
156         public void postCreateNewVNFChangeTest() throws Exception{
157                 //String testJsonData="{\"domain\":\"ChangeManagement\",\"scheduleName\":\"VnfUpgrade/DWF\",\"userId\":\"su7376\",\"domainData\":[{\"WorkflowName\":\"HEAT Stack Software Update for vNFs\",\"CallbackUrl\":\"http://127.0.0.1:8989/scheduler/v1/loopbacktest/vid\",\"CallbackData\":\"testing\"}],\"schedulingInfo\":{\"normalDurationInSeconds\":60,\"additionalDurationInSeconds\":60,\"concurrencyLimit\":60,\"policyId\":\"SNIRO_CM_1707.Config_MS_Demo_TimeLimitAndVerticalTopology_zone_localTime.1.xml\",\"vnfDetails\":[{\"groupId\":\"group1\",\"node\":[\"satmo415vbc\",\"satmo455vbc\"]}]}}";
158                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
159                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
160                 Set<String> functions = new HashSet<>();
161                 functions.add("post_create_new_vnf_change");
162                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/post_create_new_vnf_change");
163                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
164         PowerMockito.mockStatic(SystemProperties.class);
165         PowerMockito.mockStatic(EPUserUtils.class);
166         EPUser user = new EPUser();
167         user.setId((long) 1);
168         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
169         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
170                 schedulerController.postCreateNewVNFChange(mockedRequest, jsonObject);
171         }
172         
173         @Test
174         public void  postCreateNewVNFChangeTestWithException1() throws Exception{
175                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
176                 RestObject<T> restObject=new RestObject<>();
177                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
178                 Set<String> functions = new HashSet<>();
179                 functions.add("post_create_new_vnf_change");
180                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/post_create_new_vnf_change");
181                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
182         PowerMockito.mockStatic(SystemProperties.class);
183         PowerMockito.mockStatic(EPUserUtils.class);
184         EPUser user = new EPUser();
185         user.setId((long) 1);
186         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
187         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
188                 Mockito.doThrow(new NullPointerException()).when(schedulerRestInterface).Post(Matchers.any(),Matchers.any(),Matchers.any(),Matchers.any());
189                 schedulerController.postCreateNewVNFChange(mockedRequest, jsonObject);
190                 
191         }
192         
193         
194         @Test
195         public void postCreateNewVNFChangeTestWithException() throws Exception{
196                 //String testJsonData="{\"domain\":\"ChangeManagement\",\"scheduleName\":\"VnfUpgrade/DWF\",\"userId\":\"su7376\",\"domainData\":[{\"WorkflowName\":\"HEAT Stack Software Update for vNFs\",\"CallbackUrl\":\"http://127.0.0.1:8989/scheduler/v1/loopbacktest/vid\",\"CallbackData\":\"testing\"}],\"schedulingInfo\":{\"normalDurationInSeconds\":60,\"additionalDurationInSeconds\":60,\"concurrencyLimit\":60,\"policyId\":\"SNIRO_CM_1707.Config_MS_Demo_TimeLimitAndVerticalTopology_zone_localTime.1.xml\",\"vnfDetails\":[{\"groupId\":\"group1\",\"node\":[\"satmo415vbc\",\"satmo455vbc\"]}]}}";
197                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
198                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
199                 Set<String> functions = new HashSet<>();
200                 functions.add("post_create_new_vnf_change");
201                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/post_create_new_vnf_change");
202                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
203         PowerMockito.mockStatic(SystemProperties.class);
204         PowerMockito.mockStatic(EPUserUtils.class);
205         EPUser user = new EPUser();
206         user.setId((long) 1);
207         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
208                 schedulerController.postCreateNewVNFChange(mockedRequest, null);
209         }
210         
211         @Test
212         public void postSubmitVnfChangeTimeslotsTest() throws Exception{
213                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
214                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
215                 Set<String> functions = new HashSet<>();
216                 functions.add("submit_vnf_change_timeslots");
217                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/submit_vnf_change_timeslots");
218                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
219         PowerMockito.mockStatic(SystemProperties.class);
220         PowerMockito.mockStatic(EPUserUtils.class);
221         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
222         EPUser user = new EPUser();
223         user.setId((long) 1);
224         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
225         PowerMockito.mockStatic(SystemProperties.class);
226                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SUBMIT_NEW_VNF_CHANGE)).thenReturn("/v1/ChangeManagement/schedules/{scheduleId}/approvals");
227                 schedulerController.postSubmitVnfChangeTimeslots(mockedRequest, jsonObject);
228         }
229         
230         @Test
231         public void  postSubmitVnfChangeTimeslotsTestWithException1() throws Exception{
232                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
233                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
234                 Set<String> functions = new HashSet<>();
235                 functions.add("submit_vnf_change_timeslots");
236                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/submit_vnf_change_timeslots");
237                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
238         PowerMockito.mockStatic(SystemProperties.class);
239         PowerMockito.mockStatic(EPUserUtils.class);
240         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
241         EPUser user = new EPUser();
242         user.setId((long) 1);
243         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
244                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SUBMIT_NEW_VNF_CHANGE)).thenReturn("/v1/ChangeManagement/schedules/{scheduleId}/approvals");
245                 ResponseEntity<String> res =    schedulerController.postSubmitVnfChangeTimeslots(mockedRequest, null);          
246         }
247         
248         @Test
249         public void postSubmitVnfChangeTimeslotsTestWithException() throws Exception{
250                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
251                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
252                 Set<String> functions = new HashSet<>();
253                 functions.add("submit_vnf_change_timeslots");
254                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/submit_vnf_change_timeslots");
255                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
256         PowerMockito.mockStatic(SystemProperties.class);
257         PowerMockito.mockStatic(EPUserUtils.class);
258         EPUser user = new EPUser();
259         user.setId((long) 1);
260         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
261                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SUBMIT_NEW_VNF_CHANGE)).thenReturn("/v1/ChangeManagement/schedules/{scheduleId}/approvals");
262                 ResponseEntity<String> res =    schedulerController.postSubmitVnfChangeTimeslots(mockedRequest, null);
263                 assertEquals(res.getStatusCode(), HttpStatus.UNAUTHORIZED);
264         }
265         
266         
267         @Test
268         public void getSchedulerConstantTestWithException() throws Exception{
269                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
270                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
271                 Set<String> functions = new HashSet<>();
272                 functions.add("get_scheduler_constant");
273                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/get_scheduler_constant");
274                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
275         PowerMockito.mockStatic(SystemProperties.class);
276         PowerMockito.mockStatic(EPUserUtils.class);
277         EPUser user = new EPUser();
278         user.setId((long) 1);
279         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
280         PowerMockito.mockStatic(SystemProperties.class);
281                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_CALLBACK_URL)).thenReturn("mockedRequest");
282                 schedulerController.getSchedulerConstant(mockedRequest, mockedResponse);
283         }
284         
285         @Test
286         public void getSchedulerConstantTest() throws Exception{
287                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
288                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
289                 Set<String> functions = new HashSet<>();
290                 functions.add("submit_vnf_change_timeslots");
291                 Mockito.when(mockedRequest.getRequestURI()).thenReturn("/portalApi/submit_vnf_change_timeslots");
292                 Mockito.when(adminRolesService.getAllAppsFunctionsOfUser(Matchers.anyString())).thenReturn(functions);
293         PowerMockito.mockStatic(SystemProperties.class);
294         PowerMockito.mockStatic(EPUserUtils.class);
295         Mockito.when(EPUserUtils.matchRoleFunctions(Matchers.anyString(), Matchers.anySet())).thenReturn(true);
296         EPUser user = new EPUser();
297         user.setId((long) 1);
298         Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
299         PowerMockito.mockStatic(SystemProperties.class);
300                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_CALLBACK_URL)).thenReturn("callbackUrl");
301                 schedulerController.getSchedulerConstant(mockedRequest, mockedResponse);
302         }
303         
304 }