97659a7bb581cda30d669541bf7606182e74048d
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / vnfmdriver / controller / VnfmDriverControllerTest.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.controller;
18
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;
24
25 import javax.servlet.http.HttpServletResponse;
26
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;
54
55 public class VnfmDriverControllerTest {
56         @Mock
57         private VnfmDriverMgmrInf vnfmDriverMgmr;
58
59         @InjectMocks
60         private VnfmDriverController controller;
61
62         private MockMvc mockMvc;
63         
64         @Before
65         public void setUp() {
66                 MockitoAnnotations.initMocks(this);
67                 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
68         }
69
70         @Test
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\"}";
76                 
77                 when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse);
78                 
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).
84                                 content(jsonString))
85                 .andDo(print())
86                 .andExpect(status().isCreated())
87         .andReturn().getResponse().getContentAsString();
88                 
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"));
92         }
93         
94         @Test
95         public void testTerminateVnf() throws Exception {
96                 TerminateVnfResponse mockResponse = new TerminateVnfResponse();
97                 mockResponse.setJobId("job_002");
98                 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
99                 
100                 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
101                 
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).
107                                 content(jsonString))
108                                 .andDo(print())
109                                 .andExpect(status().isCreated())
110                                 .andReturn().getResponse().getContentAsString();
111                 
112                 JSONObject jsonObj = new JSONObject(responseString);
113                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
114         }
115         
116         @Test
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);
121                 
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).
127                                 content(jsonString))
128                                 .andDo(print())
129                                 .andExpect(status().isBadRequest())
130                                 .andReturn().getResponse().getErrorMessage();
131                 
132                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
133         }
134         
135         @Test
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);
140                 
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();
148                 
149                                 JSONObject jsonObj = new JSONObject(responseString);
150                                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
151         }
152         
153         @Test
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);
157                 
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))
163                                 .andDo(print())
164                                 .andExpect(status().isInternalServerError())
165                                 .andReturn().getResponse().getErrorMessage();
166                 
167                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
168         }
169         
170         @Test
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);
175                 
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();
183                 
184                                 JSONObject jsonObj = new JSONObject(responseString);
185                                 Assert.assertEquals("VnfdId is ", mockResponse.getVnfdId(), jsonObj.get("vnfdId"));
186         }
187         
188         @Test
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);
192                 
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))
198                                 .andDo(print())
199                                 .andExpect(status().isInternalServerError())
200                                 .andReturn().getResponse().getErrorMessage();
201                 
202                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
203         }
204         
205         @Test
206         public void testScaleVnf() throws Exception {
207                 ScaleVnfResponse mockResponse = new ScaleVnfResponse();
208                 mockResponse.setJobId("job_002");
209                 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
210                 
211                 when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
212                 
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).
218                                 content(jsonString))
219                                 .andDo(print())
220                                 .andExpect(status().isCreated())
221                                 .andReturn().getResponse().getContentAsString();
222                 
223                 JSONObject jsonObj = new JSONObject(responseString);
224                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
225         }
226         
227         @Test
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);
232                 
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).
238                                 content(jsonString))
239                                 .andDo(print())
240                                 .andExpect(status().isInternalServerError())
241                                 .andReturn().getResponse().getErrorMessage();
242                 
243                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
244         }
245         
246         @Test
247         public void testHealVnf() throws Exception {
248                 HealVnfResponse mockResponse = new HealVnfResponse();
249                 mockResponse.setJobId("job_002");
250                 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
251                 
252                 when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
253                 
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).
259                                 content(jsonString))
260                                 .andDo(print())
261                                 .andExpect(status().isCreated())
262                                 .andReturn().getResponse().getContentAsString();
263                 
264                 JSONObject jsonObj = new JSONObject(responseString);
265                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
266         }
267         
268         @Test
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);
273                 
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).
279                                 content(jsonString))
280                                 .andDo(print())
281                                 .andExpect(status().isInternalServerError())
282                                 .andReturn().getResponse().getErrorMessage();
283                 
284                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
285         }
286         
287         @Test
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\"}}";
292                 
293                 when(vnfmDriverMgmr.createSubscription(Mockito.any(CreateSubscriptionRequest.class))).thenReturn(mockResponse);
294                 
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).
300                                 content(jsonString))
301                                 .andDo(print())
302                                 .andExpect(status().isCreated())
303                                 .andReturn().getResponse().getContentAsString();
304                 
305                 JSONObject jsonObj = new JSONObject(responseString);
306                 Assert.assertEquals("jobId is ", mockResponse.getCallbackUri(), jsonObj.get("callbackUri"));
307         }
308
309 }