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 org.apache.http.HttpStatus;
26 import org.json.JSONObject;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
48 import org.springframework.http.MediaType;
49 import org.springframework.test.web.servlet.MockMvc;
50 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
52 public class VnfmDriverControllerTest {
54 private VnfmDriverMgmrInf vnfmDriverMgmr;
57 private VnfmDriverController controller;
59 private MockMvc mockMvc;
63 MockitoAnnotations.initMocks(this);
64 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
68 public void testInstantiateVnf() throws Exception {
69 InstantiateVnfResponse mockResponse = new InstantiateVnfResponse();
70 mockResponse.setJobId("job_001");
71 mockResponse.setVnfInstanceId("vnfInstanceId_001");
72 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
74 when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse);
76 String responseString = mockMvc.perform(
77 post("/api/nokiavnfmdriver/v1/vnfmId_001/vnfs").
78 characterEncoding("UTF-8").
79 accept(MediaType.APPLICATION_JSON).
80 contentType(MediaType.APPLICATION_JSON).
83 .andExpect(status().isCreated())
84 .andReturn().getResponse().getContentAsString();
86 JSONObject jsonObj = new JSONObject(responseString);
87 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
88 Assert.assertEquals("vnfInstanceId is ", mockResponse.getVnfInstanceId(), jsonObj.get("vnfInstanceId"));
92 public void testTerminateVnf() throws Exception {
93 TerminateVnfResponse mockResponse = new TerminateVnfResponse();
94 mockResponse.setJobId("job_002");
95 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
97 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
99 String responseString = mockMvc.perform(
100 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/terminate").
101 characterEncoding("UTF-8").
102 accept(MediaType.APPLICATION_JSON).
103 contentType(MediaType.APPLICATION_JSON).
106 .andExpect(status().isCreated())
107 .andReturn().getResponse().getContentAsString();
109 JSONObject jsonObj = new JSONObject(responseString);
110 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
114 public void testTerminateVnfException() throws Exception {
115 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
116 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_BAD_REQUEST, "vnfInstanceId is wrong");
117 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
119 String erroMsg = mockMvc.perform(
120 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/terminate").
121 characterEncoding("UTF-8").
122 accept(MediaType.APPLICATION_JSON).
123 contentType(MediaType.APPLICATION_JSON).
126 .andExpect(status().isBadRequest())
127 .andReturn().getResponse().getErrorMessage();
129 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
133 public void testGetOperStatus() throws Exception {
134 OperStatusVnfResponse mockResponse = new OperStatusVnfResponse();
135 mockResponse.setJobId("jobId_003");
136 when(vnfmDriverMgmr.getOperStatus(Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
138 String responseString = mockMvc.perform(
139 get("/api/nokiavnfmdriver/v1/vnfmId_002/jobs/jobId_001").
140 characterEncoding("UTF-8").
141 accept(MediaType.APPLICATION_JSON).
142 contentType(MediaType.APPLICATION_JSON))
143 .andExpect(status().isCreated())
144 .andReturn().getResponse().getContentAsString();
146 JSONObject jsonObj = new JSONObject(responseString);
147 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
151 public void testGetOperStatusException() throws Exception {
152 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
153 when(vnfmDriverMgmr.getOperStatus( Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
155 String erroMsg = mockMvc.perform(
156 get("/api/nokiavnfmdriver/v1/vnfmId_002/jobs/jobId_002").
157 characterEncoding("UTF-8").
158 accept(MediaType.APPLICATION_JSON).
159 contentType(MediaType.APPLICATION_JSON))
161 .andExpect(status().isInternalServerError())
162 .andReturn().getResponse().getErrorMessage();
164 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
168 public void testQueryVnf() throws Exception {
169 QueryVnfResponse mockResponse = new QueryVnfResponse();
170 mockResponse.setVnfdId("vnfdId_001");
171 when(vnfmDriverMgmr.queryVnf(Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
173 String responseString = mockMvc.perform(
174 get("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_001").
175 characterEncoding("UTF-8").
176 accept(MediaType.APPLICATION_JSON).
177 contentType(MediaType.APPLICATION_JSON))
178 .andExpect(status().isCreated())
179 .andReturn().getResponse().getContentAsString();
181 JSONObject jsonObj = new JSONObject(responseString);
182 Assert.assertEquals("VnfdId is ", mockResponse.getVnfdId(), jsonObj.get("vnfdId"));
186 public void testQueryVnfException() throws Exception {
187 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
188 when(vnfmDriverMgmr.queryVnf( Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
190 String erroMsg = mockMvc.perform(
191 get("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_001").
192 characterEncoding("UTF-8").
193 accept(MediaType.APPLICATION_JSON).
194 contentType(MediaType.APPLICATION_JSON))
196 .andExpect(status().isInternalServerError())
197 .andReturn().getResponse().getErrorMessage();
199 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
203 public void testScaleVnf() throws Exception {
204 ScaleVnfResponse mockResponse = new ScaleVnfResponse();
205 mockResponse.setJobId("job_002");
206 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
208 when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
210 String responseString = mockMvc.perform(
211 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_003/scale").
212 characterEncoding("UTF-8").
213 accept(MediaType.APPLICATION_JSON).
214 contentType(MediaType.APPLICATION_JSON).
217 .andExpect(status().isCreated())
218 .andReturn().getResponse().getContentAsString();
220 JSONObject jsonObj = new JSONObject(responseString);
221 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
225 public void testScaleVnfException() throws Exception {
226 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
227 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
228 when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
230 String erroMsg = mockMvc.perform(
231 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/scale").
232 characterEncoding("UTF-8").
233 accept(MediaType.APPLICATION_JSON).
234 contentType(MediaType.APPLICATION_JSON).
237 .andExpect(status().isInternalServerError())
238 .andReturn().getResponse().getErrorMessage();
240 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
243 public void testHealVnf() throws Exception {
244 HealVnfResponse mockResponse = new HealVnfResponse();
245 mockResponse.setJobId("job_002");
246 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
248 when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
250 String responseString = mockMvc.perform(
251 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_003/heal").
252 characterEncoding("UTF-8").
253 accept(MediaType.APPLICATION_JSON).
254 contentType(MediaType.APPLICATION_JSON).
257 .andExpect(status().isCreated())
258 .andReturn().getResponse().getContentAsString();
260 JSONObject jsonObj = new JSONObject(responseString);
261 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
265 public void testHealVnfException() throws Exception {
266 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
267 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
268 when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
270 String erroMsg = mockMvc.perform(
271 post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/heal").
272 characterEncoding("UTF-8").
273 accept(MediaType.APPLICATION_JSON).
274 contentType(MediaType.APPLICATION_JSON).
277 .andExpect(status().isInternalServerError())
278 .andReturn().getResponse().getErrorMessage();
280 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);