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