2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import static org.junit.Assert.*;
42 import java.util.HashSet;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
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;
74 @RunWith(PowerMockRunner.class)
75 @PrepareForTest({UserUtils.class,SystemProperties.class,SchedulerProperties.class,EPUserUtils.class})
76 public class SchedulerControllerTest {
79 SchedulerRestInterface schedulerRestInterface;
82 AdminRolesService adminRolesService;
85 SchedulerController schedulerController;
89 MockitoAnnotations.initMocks(this);
92 MockEPUser mockUser = new MockEPUser();
93 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
95 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
96 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
97 NullPointerException nullPointerException = new NullPointerException();
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");
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");
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);
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);
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);
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);
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);
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);
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);
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);
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);