2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.controller;
19 import static org.mockito.Mockito.when;
20 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
23 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25 import javax.servlet.http.HttpServletResponse;
27 import org.apache.http.HttpStatus;
28 import org.json.JSONObject;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionRequest;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionResponse;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
48 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
49 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
50 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
51 import org.springframework.http.MediaType;
52 import org.springframework.test.web.servlet.MockMvc;
53 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
55 public class VnfmDriverControllerTest {
57 private VnfmDriverMgmrInf vnfmDriverMgmr;
60 private VnfmDriverController controller;
62 private MockMvc mockMvc;
66 MockitoAnnotations.initMocks(this);
67 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
71 public void testInstantiateVnf() throws Exception {
72 InstantiateVnfResponse mockResponse = new InstantiateVnfResponse();
73 mockResponse.setJobId("job_001");
74 mockResponse.setVnfInstanceId("vnfInstanceId_001");
75 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
77 when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse);
79 String responseString = mockMvc.perform(
80 post("/api/nokiavnfmdriver/v1/vnfmId_001/vnfs").
81 characterEncoding("UTF-8").
82 accept(MediaType.APPLICATION_JSON).
83 contentType(MediaType.APPLICATION_JSON).
86 .andExpect(status().isCreated())
87 .andReturn().getResponse().getContentAsString();
89 JSONObject jsonObj = new JSONObject(responseString);
90 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
91 Assert.assertEquals("vnfInstanceId is ", mockResponse.getVnfInstanceId(), jsonObj.get("vnfInstanceId"));
95 public void testTerminateVnf() throws Exception {
96 TerminateVnfResponse mockResponse = new TerminateVnfResponse();
97 mockResponse.setJobId("job_002");
98 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
100 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
102 String responseString = mockMvc.perform(
103 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/terminate").
104 characterEncoding("UTF-8").
105 accept(MediaType.APPLICATION_JSON).
106 contentType(MediaType.APPLICATION_JSON).
109 .andExpect(status().isCreated())
110 .andReturn().getResponse().getContentAsString();
112 JSONObject jsonObj = new JSONObject(responseString);
113 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
117 public void testTerminateVnfException() throws Exception {
118 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
119 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_BAD_REQUEST, "vnfInstanceId is wrong");
120 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
122 String erroMsg = mockMvc.perform(
123 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/terminate").
124 characterEncoding("UTF-8").
125 accept(MediaType.APPLICATION_JSON).
126 contentType(MediaType.APPLICATION_JSON).
129 .andExpect(status().isBadRequest())
130 .andReturn().getResponse().getErrorMessage();
132 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
136 public void testGetOperStatus() throws Exception {
137 OperStatusVnfResponse mockResponse = new OperStatusVnfResponse();
138 mockResponse.setJobId("jobId_003");
139 when(vnfmDriverMgmr.getOperStatus(Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
141 String responseString = mockMvc.perform(
142 get("/api/nokiavnfmdriver/v1/vnfmId_002/jobs/jobId_001").
143 characterEncoding("UTF-8").
144 accept(MediaType.APPLICATION_JSON).
145 contentType(MediaType.APPLICATION_JSON))
146 .andExpect(status().isCreated())
147 .andReturn().getResponse().getContentAsString();
149 JSONObject jsonObj = new JSONObject(responseString);
150 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
154 public void testGetOperStatusException() throws Exception {
155 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
156 when(vnfmDriverMgmr.getOperStatus( Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
158 String erroMsg = mockMvc.perform(
159 get("/api/nokiavnfmdriver/v1/vnfmId_002/jobs/jobId_002").
160 characterEncoding("UTF-8").
161 accept(MediaType.APPLICATION_JSON).
162 contentType(MediaType.APPLICATION_JSON))
164 .andExpect(status().isInternalServerError())
165 .andReturn().getResponse().getErrorMessage();
167 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
171 public void testQueryVnf() throws Exception {
172 QueryVnfResponse mockResponse = new QueryVnfResponse();
173 mockResponse.setVnfdId("vnfdId_001");
174 when(vnfmDriverMgmr.queryVnf(Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
176 String responseString = mockMvc.perform(
177 get("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_001").
178 characterEncoding("UTF-8").
179 accept(MediaType.APPLICATION_JSON).
180 contentType(MediaType.APPLICATION_JSON))
181 .andExpect(status().isCreated())
182 .andReturn().getResponse().getContentAsString();
184 JSONObject jsonObj = new JSONObject(responseString);
185 Assert.assertEquals("VnfdId is ", mockResponse.getVnfdId(), jsonObj.get("vnfdId"));
189 public void testQueryVnfException() throws Exception {
190 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
191 when(vnfmDriverMgmr.queryVnf( Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
193 String erroMsg = mockMvc.perform(
194 get("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_001").
195 characterEncoding("UTF-8").
196 accept(MediaType.APPLICATION_JSON).
197 contentType(MediaType.APPLICATION_JSON))
199 .andExpect(status().isInternalServerError())
200 .andReturn().getResponse().getErrorMessage();
202 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
206 public void testScaleVnf() throws Exception {
207 ScaleVnfResponse mockResponse = new ScaleVnfResponse();
208 mockResponse.setJobId("job_002");
209 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
211 when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
213 String responseString = mockMvc.perform(
214 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_003/scale").
215 characterEncoding("UTF-8").
216 accept(MediaType.APPLICATION_JSON).
217 contentType(MediaType.APPLICATION_JSON).
220 .andExpect(status().isCreated())
221 .andReturn().getResponse().getContentAsString();
223 JSONObject jsonObj = new JSONObject(responseString);
224 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
228 public void testScaleVnfException() throws Exception {
229 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
230 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
231 when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
233 String erroMsg = mockMvc.perform(
234 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/scale").
235 characterEncoding("UTF-8").
236 accept(MediaType.APPLICATION_JSON).
237 contentType(MediaType.APPLICATION_JSON).
240 .andExpect(status().isInternalServerError())
241 .andReturn().getResponse().getErrorMessage();
243 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
247 public void testHealVnf() throws Exception {
248 HealVnfResponse mockResponse = new HealVnfResponse();
249 mockResponse.setJobId("job_002");
250 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
252 when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
254 String responseString = mockMvc.perform(
255 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_003/heal").
256 characterEncoding("UTF-8").
257 accept(MediaType.APPLICATION_JSON).
258 contentType(MediaType.APPLICATION_JSON).
261 .andExpect(status().isCreated())
262 .andReturn().getResponse().getContentAsString();
264 JSONObject jsonObj = new JSONObject(responseString);
265 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
269 public void testHealVnfException() throws Exception {
270 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
271 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
272 when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
274 String erroMsg = mockMvc.perform(
275 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/heal").
276 characterEncoding("UTF-8").
277 accept(MediaType.APPLICATION_JSON).
278 contentType(MediaType.APPLICATION_JSON).
281 .andExpect(status().isInternalServerError())
282 .andReturn().getResponse().getErrorMessage();
284 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
288 public void testCreateSubscripiton() throws Exception {
289 CreateSubscriptionResponse mockResponse = new CreateSubscriptionResponse();
290 mockResponse.setCallbackUri("callbackUri");
291 String jsonString = "{\"callbackUri\":\"callbackUri\",\"callbackUri\":\"callbackUrl\"," + "\"authentication\":{\"userName\":\"userName\",\"password\":\"password\",\"clientName\":\"clientName\"}}";
293 when(vnfmDriverMgmr.createSubscription(Mockito.any(CreateSubscriptionRequest.class))).thenReturn(mockResponse);
295 String responseString = mockMvc.perform(
296 post("/api/nokiavnfmdriver/v1/createSubscripiton").
297 characterEncoding("UTF-8").
298 accept(MediaType.APPLICATION_JSON).
299 contentType(MediaType.APPLICATION_JSON).
302 .andExpect(status().isCreated())
303 .andReturn().getResponse().getContentAsString();
305 JSONObject jsonObj = new JSONObject(responseString);
306 Assert.assertEquals("jobId is ", mockResponse.getCallbackUri(), jsonObj.get("callbackUri"));