Create seed code of svnfm vnfmdriver
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / test / java / com / nokia / vfcadaptor / 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 com.nokia.vfcadaptor.vnfmdriver.controller;
18
19
20 import static org.mockito.Mockito.when;
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.springframework.http.MediaType;
35 import org.springframework.test.web.servlet.MockMvc;
36 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
37
38 import com.nokia.vfcadaptor.exception.VnfmDriverException;
39 import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfRequest;
40 import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfResponse;
41 import com.nokia.vfcadaptor.vnfmdriver.bo.TerminateVnfRequest;
42 import com.nokia.vfcadaptor.vnfmdriver.bo.TerminateVnfResponse;
43 import com.nokia.vfcadaptor.vnfmdriver.inf.VnfmDriverMgmrInf;
44
45 //@RunWith(SpringJUnit4ClassRunner.class)
46 //@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/mvc-servlet.xml"})
47 //@WebAppConfiguration(value = "src/main/webapp")
48 public class VnfmDriverControllerTest {
49
50         @Mock
51         private VnfmDriverMgmrInf vnfmDriverMgmr;
52
53         @InjectMocks
54         private VnfmDriverController controller;
55
56         private MockMvc mockMvc;
57         
58         @Before
59         public void setUp() {
60                 MockitoAnnotations.initMocks(this);
61                 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
62         }
63
64         @Test
65         public void testInstantiateVnf() throws Exception
66         {
67                 InstantiateVnfResponse mockResponse = new InstantiateVnfResponse();
68                 mockResponse.setJobId("job_001");
69                 mockResponse.setVnfInstanceId("vnfInstanceId_001");
70                 String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
71                 when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse);
72                 
73                 String responseString = mockMvc.perform(
74                                 post("/nokiavnfm/v1/vnfmId_001/vnfs").
75                                 characterEncoding("UTF-8").
76                                 accept(MediaType.APPLICATION_JSON).
77                                 contentType(MediaType.APPLICATION_JSON).
78                                 content(jsonString))
79                 .andDo(print())
80                 .andExpect(status().isCreated())
81         .andReturn().getResponse().getContentAsString();
82                 
83                 JSONObject jsonObj = new JSONObject(responseString);
84                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
85                 Assert.assertEquals("vnfInstanceId is ", mockResponse.getVnfInstanceId(), jsonObj.get("vnfInstanceId"));
86         }
87         
88         @Test
89         public void testTerminateVnfSuccess() throws Exception
90         {
91                 TerminateVnfResponse mockResponse = new TerminateVnfResponse();
92                 mockResponse.setJobId("job_002");
93                 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_001\"}";
94                 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
95                 
96                 String responseString = mockMvc.perform(
97                                 post("/nokiavnfm/v1/vnfmId_001/vnfs/vnfInstanceId_001/terminate").
98                                 characterEncoding("UTF-8").
99                                 accept(MediaType.APPLICATION_JSON).
100                                 contentType(MediaType.APPLICATION_JSON).
101                                 content(jsonString))
102                                 .andDo(print())
103                                 .andExpect(status().isCreated())
104                                 .andReturn().getResponse().getContentAsString();
105                 
106                 JSONObject jsonObj = new JSONObject(responseString);
107                 Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
108         }
109         @Test
110         public void testTerminateVnfException() throws Exception
111         {
112                 TerminateVnfResponse mockResponse = new TerminateVnfResponse();
113                 mockResponse.setJobId("job_002");
114                 String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_001\"}";
115                 VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_BAD_REQUEST, "vnfInstanceId is wrong");
116                 when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
117                 
118                 String erroMsg = mockMvc.perform(
119                                 post("/nokiavnfm/v1/vnfmId_001/vnfs/vnfInstanceId_001/terminate").
120                                 characterEncoding("UTF-8").
121                                 accept(MediaType.APPLICATION_JSON).
122                                 contentType(MediaType.APPLICATION_JSON).
123                                 content(jsonString))
124                                 .andDo(print())
125                                 .andExpect(status().isBadRequest())
126                                 .andReturn().getResponse().getErrorMessage()
127                                 ;
128                 Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
129         }
130
131 }