2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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.vnfm.svnfm.vnfmadapter.service.rest;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
22 import java.io.IOException;
23 import java.lang.reflect.Method;
25 import javax.servlet.ServletInputStream;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Matchers;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.ServiceException;
38 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
39 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.VnfMgr;
42 import net.sf.json.JSONObject;
43 @RunWith(MockitoJUnitRunner.class)
44 public class VnfRoaTest {
46 private VnfRoa vnfRoa;
48 private VnfMgr vnfMgr;
51 HttpServletRequest context;
54 HttpServletResponse resp;
57 ServletInputStream servletInputStream;
64 vnfRoa = new VnfRoa();
65 vnfMgr = new VnfMgr();
66 vnfRoa.setVnfMgr(vnfMgr);
70 public void tearDown() {
76 public void testAddVnf() throws ServiceException, IOException {
77 final JSONObject restJson = new JSONObject();
78 restJson.put("retCode", Constant.REST_SUCCESS);
79 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
80 // HttpServletRequest mockInstance = proxyStub.getMockInstance();
82 // MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
83 // HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
85 // new MockUp<VnfMgr>() {
88 // public JSONObject addVnf(JSONObject subJsonObject, String vnfmId) {
89 // JSONObject retJson = new JSONObject();
90 // retJson.put("id", "123");
91 // restJson.put("data", retJson);
95 // new MockUp<VnfmJsonUtil>() {
97 // @SuppressWarnings("unchecked")
99 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
100 // return (T)restJson;
103 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
104 String result = vnfRoa.addVnf(context, resp, "vnfmId");
105 JSONObject retJson = new JSONObject();
106 retJson.put("id", "123");
107 assertEquals(retJson.toString(), result);
112 public void testAddVnfFail() throws ServiceException {
113 final JSONObject restJson = new JSONObject();
114 restJson.put("retCode", Constant.REST_FAIL);
115 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
116 HttpServletRequest mockInstance = proxyStub.getMockInstance();
118 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
119 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
121 // new MockUp<VnfMgr>() {
124 // public JSONObject addVnf(JSONObject subJsonObject, String vnfmId) {
128 // new MockUp<VnfmJsonUtil>() {
130 // @SuppressWarnings("unchecked")
132 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
133 // return (T)restJson;
137 String result = vnfRoa.addVnf(mockInstance, mockResInstance, "vnfmId");
139 assertEquals(restJson.toString(), result);
144 public void testAddVnfBySubJsonObjectNull() throws ServiceException {
145 final JSONObject restJson = new JSONObject();
146 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
147 HttpServletRequest mockInstance = proxyStub.getMockInstance();
149 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
150 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
152 // new MockUp<VnfmJsonUtil>() {
155 // public <T> T getJsonFromContexts(HttpServletRequest context) {
160 String result = vnfRoa.addVnf(mockInstance, mockResInstance, "vnfmId");
162 assertEquals(restJson.toString(), result);
167 public void testDelVnf() throws ServiceException, IOException {
168 final JSONObject restJson = new JSONObject();
169 restJson.put("retCode", Constant.REST_SUCCESS);
170 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
171 // HttpServletRequest mockInstance = proxyStub.getMockInstance();
173 // MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
174 // HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
176 // new MockUp<VnfMgr>() {
179 // public JSONObject deleteVnf(String vnfId, String vnfmId, JSONObject vnfObject) {
180 // JSONObject retJson = new JSONObject();
181 // retJson.put("id", "123");
182 // restJson.put("data", retJson);
187 // new MockUp<VnfmJsonUtil>() {
189 // @SuppressWarnings("unchecked")
191 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
192 // return (T)restJson;
195 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
196 String result = vnfRoa.delVnf("vnfmId", resp, "vnfId", context);
197 JSONObject retJson = new JSONObject();
198 retJson.put("id", "123");
199 assertEquals(retJson.toString(), result);
203 public void testDelVnfByVnfIdIsEmpty() throws ServiceException {
204 final JSONObject restJson = new JSONObject();
205 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
206 HttpServletRequest mockInstance = proxyStub.getMockInstance();
208 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
209 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
211 // new MockUp<VnfmJsonUtil>() {
213 // @SuppressWarnings("unchecked")
215 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
216 // return (T)restJson;
220 String result = vnfRoa.delVnf("vnfmId", mockResInstance, "", mockInstance);
222 assertEquals(restJson.toString(), result);
226 public void testDelVnfByVnfmIdIsEmpty() throws ServiceException {
227 final JSONObject restJson = new JSONObject();
228 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
229 HttpServletRequest mockInstance = proxyStub.getMockInstance();
231 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
232 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
234 // new MockUp<VnfmJsonUtil>() {
236 // @SuppressWarnings("unchecked")
238 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
239 // return (T)restJson;
243 String result = vnfRoa.delVnf("", mockResInstance, "vnfId", mockInstance);
245 assertEquals(restJson.toString(), result);
249 public void testDelVnfByVnfIdVnfmIdEmpty() throws ServiceException {
250 final JSONObject restJson = new JSONObject();
251 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
252 HttpServletRequest mockInstance = proxyStub.getMockInstance();
254 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
255 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
257 // new MockUp<VnfmJsonUtil>() {
259 // @SuppressWarnings("unchecked")
261 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
262 // return (T)restJson;
266 String result = vnfRoa.delVnf("", mockResInstance, "", mockInstance);
268 assertEquals(restJson.toString(), result);
272 public void testDelVnfFail() throws ServiceException {
273 final JSONObject restJson = new JSONObject();
274 restJson.put("retCode", Constant.REST_FAIL);
275 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
276 HttpServletRequest mockInstance = proxyStub.getMockInstance();
278 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
279 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
281 // new MockUp<VnfMgr>() {
284 // public JSONObject deleteVnf(String vnfId, String vnfmId, JSONObject vnfObject) {
289 // new MockUp<VnfmJsonUtil>() {
291 // @SuppressWarnings("unchecked")
293 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
294 // return (T)restJson;
298 String result = vnfRoa.delVnf("vnfmId", mockResInstance, "vnfId", mockInstance);
299 assertEquals(restJson.toString(), result);
303 public void testGetVnfByVnfIdIsEmpty() throws IOException, ServiceException {
304 final JSONObject restJson = new JSONObject();
305 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
306 HttpServletRequest mockInstance = proxyStub.getMockInstance();
308 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
309 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
311 // new MockUp<VnfmJsonUtil>() {
313 // @SuppressWarnings("unchecked")
315 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
316 // return (T)restJson;
320 String result = vnfRoa.getVnf("vnfmId", mockResInstance, "");
322 assertEquals(restJson.toString(), result);
326 public void testGetVnfByVnfmIdIsEmpty() throws IOException, ServiceException {
327 final JSONObject restJson = new JSONObject();
328 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
329 HttpServletRequest mockInstance = proxyStub.getMockInstance();
331 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
332 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
334 // new MockUp<VnfmJsonUtil>() {
336 // @SuppressWarnings("unchecked")
338 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
339 // return (T)restJson;
343 String result = vnfRoa.getVnf("", mockResInstance, "vnfId");
345 assertEquals(restJson.toString(), result);
349 public void testGetVnfFail() throws IOException, ServiceException {
350 final JSONObject restJson = new JSONObject();
351 restJson.put("retCode", Constant.REST_FAIL);
352 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
353 HttpServletRequest mockInstance = proxyStub.getMockInstance();
355 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
356 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
358 // new MockUp<VnfMgr>() {
361 // public JSONObject getVnf(String vnfId, String vnfmId) {
366 // new MockUp<VnfmJsonUtil>() {
368 // @SuppressWarnings("unchecked")
370 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
371 // return (T)restJson;
375 String result = vnfRoa.getVnf("vnfmId", mockResInstance, "vnfId");
376 assertEquals(restJson.toString(), result);
380 public void testGetVnf() throws IOException, ServiceException {
381 final JSONObject restJson = new JSONObject();
382 restJson.put("retCode", Constant.REST_SUCCESS);
383 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
384 HttpServletRequest mockInstance = proxyStub.getMockInstance();
386 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
387 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
389 // new MockUp<VnfMgr>() {
392 // public JSONObject getVnf(String vnfId, String vnfmId) {
393 // JSONObject retJson = new JSONObject();
394 // JSONObject basicInfoJson = new JSONObject();
395 // basicInfoJson.put("vnfInstanceId", "123");
396 // basicInfoJson.put("vnfInstanceName", "1");
397 // basicInfoJson.put("vnfInstanceDescription", "vFW");
398 // basicInfoJson.put("vnfdId", "1");
399 // basicInfoJson.put("vnfdPackageId", "vFW");
400 // basicInfoJson.put("version", "vFW");
401 // basicInfoJson.put("vnfProvider", "hw");
402 // basicInfoJson.put("vnfType", "fw");
403 // basicInfoJson.put("vnfStatus", "active");
404 // retJson.put("vnfInfo", basicInfoJson);
405 // retJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
410 // new MockUp<VnfmJsonUtil>() {
412 // @SuppressWarnings("unchecked")
414 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
415 // return (T)restJson;
419 String result = vnfRoa.getVnf("vnfmId", mockResInstance, "vnfId");
420 JSONObject basicInfoJson = new JSONObject();
421 JSONObject retJson = new JSONObject();
422 basicInfoJson.put("vnfInstanceId", "123");
423 basicInfoJson.put("vnfInstanceName", "1");
424 basicInfoJson.put("vnfInstanceDescription", "vFW");
425 basicInfoJson.put("vnfdId", "1");
426 basicInfoJson.put("vnfdPackageId", "vFW");
427 basicInfoJson.put("version", "vFW");
428 basicInfoJson.put("vnfProvider", "hw");
429 basicInfoJson.put("vnfType", "fw");
430 basicInfoJson.put("vnfStatus", "active");
431 retJson.put("vnfInfo", basicInfoJson);
432 assertEquals(retJson.toString(), result);
436 public void testGetJobByJobIdNull() throws ServiceException {
437 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
438 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
440 String result = vnfRoa.getJob(null, null, mockResInstance, "1111");
441 assertEquals("{}", result);
445 public void testGetJobByVnfmIdNull() throws ServiceException {
446 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
447 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
449 String result = vnfRoa.getJob("123", null, mockResInstance, "1111");
450 assertEquals("{}", result);
454 public void testGetJobByVnfMgrFail() throws ServiceException {
455 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
456 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
457 // new MockUp<VnfMgr>() {
460 // public JSONObject getJob(String jobId, String vnfmId) {
461 // JSONObject restJson = new JSONObject();
462 // restJson.put(Constant.RETCODE, Constant.REST_FAIL);
466 String result = vnfRoa.getJob("123", "1234", mockResInstance, "1111");
467 assertEquals("{\"retCode\":-1}", result);
471 public void testGetJob() throws ServiceException {
472 // new MockUp<VnfMgr>() {
475 // public JSONObject getJob(String jobId, String vnfmId) {
476 // JSONObject restJson = new JSONObject();
477 // JSONArray data = new JSONArray();
478 // JSONObject obj = new JSONObject();
479 // obj.put("id", "11111");
480 // obj.put("status", "Active");
482 // restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
483 // restJson.put("data", data);
488 String result = vnfRoa.getJob("123", "1234", null, "1111");
489 assertNotNull(result);
493 public void testHealVnf() throws ServiceException, IOException {
494 final JSONObject restJson = new JSONObject();
495 restJson.put("retCode", Constant.REST_SUCCESS);
496 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
497 // HttpServletRequest mockInstance = proxyStub.getMockInstance();
499 // MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
500 // HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
502 // new MockUp<VnfMgr>() {
505 // public JSONObject healVnf(JSONObject jsonObject, String vnfInstanceId, String vnfmId) {
506 // JSONObject retJson = new JSONObject();
507 // retJson.put("id", "123");
508 // restJson.put("data", retJson);
512 // new MockUp<VnfmJsonUtil>() {
514 // @SuppressWarnings("unchecked")
516 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
517 // return (T)restJson;
520 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
522 String result = vnfRoa.healVnf(context, resp, "id", "id");
523 assertNotNull(result);
528 public void testScaleVnf() throws ServiceException, IOException {
529 final JSONObject restJson = new JSONObject();
530 restJson.put("retCode", Constant.REST_SUCCESS);
531 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
532 // HttpServletRequest mockInstance = proxyStub.getMockInstance();
534 // MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
535 // HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
537 // new MockUp<VnfMgr>() {
540 // public JSONObject scaleVNF(JSONObject vnfObject, String vnfmId, String vnfInstanceId) {
541 // JSONObject retJson = new JSONObject();
542 // retJson.put("id", "123");
543 // restJson.put("data", retJson);
547 // new MockUp<VnfmJsonUtil>() {
549 // @SuppressWarnings("unchecked")
551 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
552 // return (T)restJson;
556 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
557 String result = vnfRoa.scaleVnf(context, resp, "id", "id");
559 JSONObject retJson = new JSONObject();
560 retJson.put("id", "123");
561 assertEquals(retJson.toString(), result);
566 public void testScaleVnfFail() throws ServiceException {
567 final JSONObject restJson = new JSONObject();
568 restJson.put("retCode", Constant.REST_FAIL);
569 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
570 HttpServletRequest mockInstance = proxyStub.getMockInstance();
572 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
573 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
575 // new MockUp<VnfMgr>() {
578 // public JSONObject scaleVNF(JSONObject vnfObject, String vnfmId, String vnfInstanceId) {
579 // JSONObject retJson = new JSONObject();
580 // retJson.put("id", "123");
581 // restJson.put("data", retJson);
585 // new MockUp<VnfmJsonUtil>() {
587 // @SuppressWarnings("unchecked")
589 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
590 // return (T)restJson;
593 String result = vnfRoa.scaleVnf(mockInstance, mockResInstance, "id", "id");
595 assertEquals(restJson.toString(), result);
600 public void testScaleVnfFail2() throws ServiceException {
601 final JSONObject restJson = new JSONObject();
602 restJson.put("retCode", Constant.REST_FAIL);
603 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
604 HttpServletRequest mockInstance = proxyStub.getMockInstance();
606 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
607 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
609 // new MockUp<VnfMgr>() {
612 // public JSONObject scaleVNF(JSONObject vnfObject, String vnfmId, String vnfInstanceId) {
613 // JSONObject retJson = new JSONObject();
614 // retJson.put("id", "123");
615 // restJson.put("data", retJson);
619 // new MockUp<VnfmJsonUtil>() {
621 // @SuppressWarnings("unchecked")
623 // public <T> T getJsonFromContexts(HttpServletRequest VNFreq) {
627 String result = vnfRoa.scaleVnf(mockInstance, mockResInstance, "id", "id");
629 assertNotNull(result);
634 public void testGetVnfmById() throws ServiceException {
635 // new MockUp<VnfmUtil>() {
638 // public JSONObject getVnfmById(String vnfmId) {
639 // JSONObject json = new JSONObject();
640 // json.put("vnfm", "1234");
644 String result = vnfRoa.getVnfmById("1234", null);
645 assertNotNull(result);
649 public void testGetJobFromVnfm() throws ServiceException {
650 // new MockUp<VnfMgr>() {
653 // public JSONObject getJobFromVnfm(String jobId, String vnfmId) {
654 // JSONObject json = new JSONObject();
655 // json.put("retCode", "1");
660 // public String transferToLcm(JSONObject restJson) {
664 String result = vnfRoa.getJobFromVnfm("jobId", "vnfmId", null, "responseId");
665 assertNotNull(result);
669 public void testGetJobFromVnfmFail() throws ServiceException {
671 // new MockUp<VnfMgr>() {
674 // public JSONObject getJobFromVnfm(String jobId, String vnfmId) {
675 // JSONObject json = new JSONObject();
676 // json.put("retCode", "-1");
681 MockUp<HttpServletResponse> proxyResStub = new MockUp<HttpServletResponse>() {};
682 HttpServletResponse mockResInstance = proxyResStub.getMockInstance();
683 String result = vnfRoa.getJobFromVnfm("jobId", "vnfmId", mockResInstance, "responseId");
684 assertNotNull(result);
688 public void getHealJobBody() throws Exception {
689 VnfRoa vnfRoa = new VnfRoa();
690 Method m = VnfRoa.class.getDeclaredMethod("getHealJobBody",new Class[] {String.class});
691 m.setAccessible(true);
692 assertNotNull(m.invoke(vnfRoa, "jobId"));
696 public void getJobBody() throws Exception {
697 JSONObject restJson = new JSONObject();
698 JSONObject data = new JSONObject();
699 data.put(Constant.STATUS, Constant.STATUS);
700 restJson.put("data", new JSONObject[] {data});
701 VnfRoa vnfRoa = new VnfRoa();
702 Method m = VnfRoa.class.getDeclaredMethod("getJobBody",new Class[] {JSONObject.class,String.class});
703 m.setAccessible(true);
704 assertNotNull(m.invoke(vnfRoa, restJson,"jobId"));
708 public void getVms() {
709 VnfRoa vnfRoa = new VnfRoa();
710 Mockito.when(vnfMgr1.getVmsFromVnfm(Matchers.anyString(), Matchers.any())).thenReturn(new JSONObject());
711 vnfRoa.getVms("Vmf", resp);