Add Unit Tests.
[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 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;
51
52 public class VnfmDriverControllerTest {
53         @Mock
54         private VnfmDriverMgmrInf vnfmDriverMgmr;
55
56         @InjectMocks
57         private VnfmDriverController controller;
58
59         private MockMvc mockMvc;
60         
61         @Before
62         public void setUp() {
63                 MockitoAnnotations.initMocks(this);
64                 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
65         }
66
67         @Test
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\"}";
73                 
74                 when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse);
75                 
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).
81                                 content(jsonString))
82                 .andDo(print())
83                 .andExpect(status().isCreated())
84         .andReturn().getResponse().getContentAsString();
85                 
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"));
89         }
90         
91         @Test
92         public void testTerminateVnf() throws Exception {
93                 TerminateVnfResponse mockResponse = new TerminateVnfResponse();
94                 mockResponse.setJobId("job_002");
95                 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
96                 
97                 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
98                 
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).
104                                 content(jsonString))
105                                 .andDo(print())
106                                 .andExpect(status().isCreated())
107                                 .andReturn().getResponse().getContentAsString();
108                 
109                 JSONObject jsonObj = new JSONObject(responseString);
110                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
111         }
112         
113         @Test
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);
118                 
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).
124                                 content(jsonString))
125                                 .andDo(print())
126                                 .andExpect(status().isBadRequest())
127                                 .andReturn().getResponse().getErrorMessage();
128                 
129                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
130         }
131         
132         @Test
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);
137                 
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();
145                 
146                                 JSONObject jsonObj = new JSONObject(responseString);
147                                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
148         }
149         
150         @Test
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);
154                 
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))
160                                 .andDo(print())
161                                 .andExpect(status().isInternalServerError())
162                                 .andReturn().getResponse().getErrorMessage();
163                 
164                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
165         }
166         
167         @Test
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);
172                 
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();
180                 
181                                 JSONObject jsonObj = new JSONObject(responseString);
182                                 Assert.assertEquals("VnfdId is ", mockResponse.getVnfdId(), jsonObj.get("vnfdId"));
183         }
184         
185         @Test
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);
189                 
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))
195                                 .andDo(print())
196                                 .andExpect(status().isInternalServerError())
197                                 .andReturn().getResponse().getErrorMessage();
198                 
199                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
200         }
201         
202         @Test
203         public void testScaleVnf() throws Exception {
204                 ScaleVnfResponse mockResponse = new ScaleVnfResponse();
205                 mockResponse.setJobId("job_002");
206                 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
207                 
208                 when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
209                 
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).
215                                 content(jsonString))
216                                 .andDo(print())
217                                 .andExpect(status().isCreated())
218                                 .andReturn().getResponse().getContentAsString();
219                 
220                 JSONObject jsonObj = new JSONObject(responseString);
221                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
222         }
223         
224         @Test
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);
229                 
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).
235                                 content(jsonString))
236                                 .andDo(print())
237                                 .andExpect(status().isInternalServerError())
238                                 .andReturn().getResponse().getErrorMessage();
239                 
240                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
241         }
242         @Test
243         public void testHealVnf() throws Exception {
244                 HealVnfResponse mockResponse = new HealVnfResponse();
245                 mockResponse.setJobId("job_002");
246                 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
247                 
248                 when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
249                 
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).
255                                 content(jsonString))
256                                 .andDo(print())
257                                 .andExpect(status().isCreated())
258                                 .andReturn().getResponse().getContentAsString();
259                 
260                 JSONObject jsonObj = new JSONObject(responseString);
261                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
262         }
263         
264         @Test
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);
269                 
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).
275                                 content(jsonString))
276                                 .andDo(print())
277                                 .andExpect(status().isInternalServerError())
278                                 .andReturn().getResponse().getErrorMessage();
279                 
280                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
281         }
282
283 }