8216510b5f7b7f398f483761fc6f34b38c21b97d
[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 javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 import org.apache.poi.ss.formula.functions.T;
44 import org.json.simple.JSONObject;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.InjectMocks;
49 import org.mockito.Matchers;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.MockitoAnnotations;
53 import org.onap.portalapp.portal.controller.SchedulerController;
54 import org.onap.portalapp.portal.core.MockEPUser;
55 import org.onap.portalapp.portal.framework.MockitoTestSuite;
56 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
57 import org.onap.portalapp.portal.scheduler.SchedulerRestInterface;
58 import org.onap.portalapp.portal.scheduler.restobjects.RestObject;
59 import org.onap.portalsdk.core.util.SystemProperties;
60 import org.onap.portalsdk.core.web.support.UserUtils;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({UserUtils.class,SystemProperties.class,SchedulerProperties.class})
67
68 public class SchedulerControllerTest {
69
70         @Mock
71         SchedulerRestInterface schedulerRestInterface;
72         
73         
74
75         @InjectMocks
76         SchedulerController schedulerController = new SchedulerController();
77
78         @Before
79         public void setup() {
80                 MockitoAnnotations.initMocks(this);
81         }
82
83         MockEPUser mockUser = new MockEPUser();
84         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
85
86         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
87         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
88         NullPointerException nullPointerException = new NullPointerException();
89
90         @Test
91         public void  getTimeSlotsTest() throws Exception{
92                 schedulerController.getTimeSlots(mockedRequest, "12");
93                 
94         }
95         
96         @Test
97         public void  getTimeSlotsTestWithException1() throws Exception{
98                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
99                 RestObject<T> restObject=new RestObject<>();
100                 Mockito.doThrow(new NullPointerException()).when(schedulerRestInterface).Get(Matchers.any(),Matchers.any(),Matchers.any(),Matchers.any());
101                 schedulerController.getTimeSlots(mockedRequest, "12");
102                 
103         }
104         
105         
106         @Test
107         public void  getTimeSlotsTestWithexception() throws Exception{
108                 schedulerController.getTimeSlots(mockedRequest, null);
109                 
110         }
111         
112         @Test
113         public void postCreateNewVNFChangeTest() throws Exception{
114                 //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\"]}]}}";
115                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
116                 
117                 schedulerController.postCreateNewVNFChange(mockedRequest, jsonObject);
118         }
119         
120         @Test
121         public void  postCreateNewVNFChangeTestWithException1() throws Exception{
122                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
123                 RestObject<T> restObject=new RestObject<>();
124                 Mockito.doThrow(new NullPointerException()).when(schedulerRestInterface).Post(Matchers.any(),Matchers.any(),Matchers.any(),Matchers.any());
125                 schedulerController.postCreateNewVNFChange(mockedRequest, jsonObject);
126                 
127         }
128         
129         
130         @Test
131         public void postCreateNewVNFChangeTestWithException() throws Exception{
132                 //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\"]}]}}";
133                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
134                 
135                 schedulerController.postCreateNewVNFChange(mockedRequest, null);
136         }
137         
138         @Test
139         public void postSubmitVnfChangeTimeslotsTest() throws Exception{
140                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
141                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
142         PowerMockito.mockStatic(SystemProperties.class);
143                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SUBMIT_NEW_VNF_CHANGE)).thenReturn("/v1/ChangeManagement/schedules/{scheduleId}/approvals");
144                 schedulerController.postSubmitVnfChangeTimeslots(mockedRequest, jsonObject);
145         }
146         
147         @Test
148         public void  postSubmitVnfChangeTimeslotsTestWithException1() throws Exception{
149                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
150                 RestObject<T> restObject=new RestObject<>();
151                 Mockito.doThrow(new NullPointerException()).when(schedulerRestInterface).Post(Matchers.any(),Matchers.any(),Matchers.any(),Matchers.any());
152                 schedulerController.postSubmitVnfChangeTimeslots(mockedRequest, jsonObject);
153                 
154         }
155         
156         @Test
157         public void postSubmitVnfChangeTimeslotsTestWithException() throws Exception{
158                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
159                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
160         PowerMockito.mockStatic(SystemProperties.class);
161                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SUBMIT_NEW_VNF_CHANGE)).thenReturn("/v1/ChangeManagement/schedules/{scheduleId}/approvals");
162                 schedulerController.postSubmitVnfChangeTimeslots(mockedRequest, null);
163         }
164         
165         @Test
166         public void getSchedulerConstantTestWithException() throws Exception{
167                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
168                 Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
169         PowerMockito.mockStatic(SystemProperties.class);
170                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_CALLBACK_URL)).thenReturn("mockedRequest");
171                 schedulerController.getSchedulerConstant(mockedRequest, mockedResponse);
172         }
173         
174         @Test
175         public void getSchedulerConstantTest() throws Exception{
176                 JSONObject jsonObject =Mockito.mock(JSONObject.class);
177                 //Mockito.when(jsonObject.get("scheduleId")).thenReturn("12");
178         PowerMockito.mockStatic(SystemProperties.class);
179                 PowerMockito.when(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_CALLBACK_URL)).thenReturn("callbackUrl");
180                 schedulerController.getSchedulerConstant(mockedRequest, mockedResponse);
181         }
182         
183 }