summaryrefslogtreecommitdiffstats
path: root/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/vnfmdriver/controller/VnfmDriverControllerTest.java
blob: 103b394b1aef692ffb98d08dbbf21f81a0e23b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.controller;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

public class VnfmDriverControllerTest {
	@Mock
	private VnfmDriverMgmrInf vnfmDriverMgmr;

	@InjectMocks
	private VnfmDriverController controller;

	private MockMvc mockMvc;
	
	@Before
	public void setUp() {
		MockitoAnnotations.initMocks(this);
		mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
	}

	@Test
	public void testInstantiateVnf() throws Exception {
		InstantiateVnfResponse mockResponse = new InstantiateVnfResponse();
		mockResponse.setJobId("job_001");
		mockResponse.setVnfInstanceId("vnfInstanceId_001");
		String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
		
		when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_001/vnfs").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
		.andDo(print())
		.andExpect(status().isCreated())
        .andReturn().getResponse().getContentAsString();
		
		JSONObject jsonObj = new JSONObject(responseString);
		Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
		Assert.assertEquals("vnfInstanceId is ", mockResponse.getVnfInstanceId(), jsonObj.get("vnfInstanceId"));
	}
	
	@Test
	public void testTerminateVnf() throws Exception {
		TerminateVnfResponse mockResponse = new TerminateVnfResponse();
		mockResponse.setJobId("job_002");
		String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
		
		when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/terminate").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isCreated())
				.andReturn().getResponse().getContentAsString();
		
		JSONObject jsonObj = new JSONObject(responseString);
		Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
	}
	
	@Test
	public void testTerminateVnfException() throws Exception {
		String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
		VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_BAD_REQUEST, "vnfInstanceId is wrong");
		when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
		
		String erroMsg = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/terminate").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isBadRequest())
				.andReturn().getResponse().getErrorMessage();
		
		Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
	}
	
	@Test
	public void testGetOperStatus() throws Exception {
		OperStatusVnfResponse mockResponse = new OperStatusVnfResponse();
		mockResponse.setJobId("jobId_003");
		when(vnfmDriverMgmr.getOperStatus(Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				get("/api/nokiavnfmdriver/v1/vnfmId_002/jobs/jobId_001").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON))
				.andExpect(status().isCreated())
				.andReturn().getResponse().getContentAsString();
		
				JSONObject jsonObj = new JSONObject(responseString);
				Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
	}
	
	@Test
	public void testGetOperStatusException() throws Exception {
		VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
		when(vnfmDriverMgmr.getOperStatus( Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
		
		String erroMsg = mockMvc.perform(
				get("/api/nokiavnfmdriver/v1/vnfmId_002/jobs/jobId_002").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON))
				.andDo(print())
				.andExpect(status().isInternalServerError())
				.andReturn().getResponse().getErrorMessage();
		
		Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
	}
	
	@Test
	public void testQueryVnf() throws Exception {
		QueryVnfResponse mockResponse = new QueryVnfResponse();
		mockResponse.setVnfdId("vnfdId_001");
		when(vnfmDriverMgmr.queryVnf(Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				get("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_001").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON))
				.andExpect(status().isCreated())
				.andReturn().getResponse().getContentAsString();
		
				JSONObject jsonObj = new JSONObject(responseString);
				Assert.assertEquals("VnfdId is ", mockResponse.getVnfdId(), jsonObj.get("vnfdId"));
	}
	
	@Test
	public void testQueryVnfException() throws Exception {
		VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
		when(vnfmDriverMgmr.queryVnf( Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
		
		String erroMsg = mockMvc.perform(
				get("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_001").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON))
				.andDo(print())
				.andExpect(status().isInternalServerError())
				.andReturn().getResponse().getErrorMessage();
		
		Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
	}
	
	@Test
	public void testScaleVnf() throws Exception {
		ScaleVnfResponse mockResponse = new ScaleVnfResponse();
		mockResponse.setJobId("job_002");
		String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
		
		when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_003/scale").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isCreated())
				.andReturn().getResponse().getContentAsString();
		
		JSONObject jsonObj = new JSONObject(responseString);
		Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
	}
	
	@Test
	public void testScaleVnfException() throws Exception {
		String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
		VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
		when(vnfmDriverMgmr.scaleVnf(Mockito.any(ScaleVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
		
		String erroMsg = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/scale").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isInternalServerError())
				.andReturn().getResponse().getErrorMessage();
		
		Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
	}
	
	@Test
	public void testHealVnf() throws Exception {
		HealVnfResponse mockResponse = new HealVnfResponse();
		mockResponse.setJobId("job_002");
		String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_003\",\"vnfPackageId\":\"1\"}";
		
		when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_003/heal").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isCreated())
				.andReturn().getResponse().getContentAsString();
		
		JSONObject jsonObj = new JSONObject(responseString);
		Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId"));
	}
	
	@Test
	public void testHealVnfException() throws Exception {
		String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
		VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_INTERNAL_SERVER_ERROR, CommonConstants.HTTP_ERROR_DESC_500);
		when(vnfmDriverMgmr.healVnf(Mockito.any(HealVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception);
		
		String erroMsg = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/vnfmId_002/vnfs/vnfInstanceId_002/heal").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isInternalServerError())
				.andReturn().getResponse().getErrorMessage();
		
		Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg);
	}
	
	@Test
	public void testCreateSubscripiton() throws Exception {
		CreateSubscriptionResponse mockResponse = new CreateSubscriptionResponse();
		mockResponse.setCallbackUri("callbackUri");
		String jsonString = "{\"callbackUri\":\"callbackUri\"," + "\"authentication\":{\"userName\":\"userName\",\"password\":\"password\",\"clientName\":\"clientName\"}}";
		
		when(vnfmDriverMgmr.createSubscription(Mockito.any(CreateSubscriptionRequest.class))).thenReturn(mockResponse);
		
		String responseString = mockMvc.perform(
				post("/api/nokiavnfmdriver/v1/createSubscription").
				characterEncoding("UTF-8").
				accept(MediaType.APPLICATION_JSON).
				contentType(MediaType.APPLICATION_JSON).
				content(jsonString))
				.andDo(print())
				.andExpect(status().isCreated())
				.andReturn().getResponse().getContentAsString();
		
		JSONObject jsonObj = new JSONObject(responseString);
		Assert.assertEquals("jobId is ", mockResponse.getCallbackUri(), jsonObj.get("callbackUri"));
	}

}