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.process;
 
  19 import static org.junit.Assert.assertEquals;
 
  21 import org.junit.Test;
 
  22 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
 
  23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
 
  24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.VnfResourceMgr;
 
  25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
 
  29 import net.sf.json.JSONObject;
 
  37  * @version VFC 1.0 Aug 9, 2016
 
  39 public class VnfResourceMgrTest {
 
  42     public void testgrantVnfResource() {
 
  45                 "{\"type\": \"instantiation\",\"operation_right\": \"increase\",\"vnf_name\": \"vnf_name\",\"vm_list\": [{\"vm_flavor\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
  46         JSONObject vnfObj = JSONObject.fromObject(data);
 
  47         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
  48         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
 
  50         JSONObject retJson = new JSONObject();
 
  51         retJson.put("retCode", Constant.REST_FAIL);
 
  52         retJson.put("errorMsg", "params parse exception");
 
  53         assertEquals(retJson, result);
 
  57     public void testgrantVnfResourceSuccess() {
 
  58         new MockUp<VnfmRestfulUtil>() {
 
  61             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
 
  62                 RestfulResponse rsp = new RestfulResponse();
 
  64                 rsp.setResponseJson(new JSONObject().toString());
 
  69         JSONObject vnfObj = new JSONObject();
 
  71                 "{\"type\": \"instantiation\",\"location\": \"RomaRespool\",\"version\":\"V500R001C20\",\"vendor\": \"V500R001C20B001\",\"tenant\": \"admin\",\"vnf_id\":\"NE=1304\",\"vnfm_id\": \"9a49755d-8050-4369-9e7d-0a0855097585\",\"project_id\":\"\",\"operation_right\": \"increase\",\"vnf_name\": \"wangyuans4\",\"vim_id\":\"\",\"dr_location\": \"RomaRespool\",\"nfvo_id\": \"\",\"vnf_type\": \"vNGFW\",\"plan_id\":\"V4\",\"plan_name\": \"V4\",\"vnfd_id\": \"vNGFW\",\"vm_list\": [{\"vm_flavor\":{\"location\": \"\",\"priority\": \"1\",\"storage\": [{\"vol_type\":\"local_volume\",\"vol_size\": \"200\"},{\"storage_type\": \"local_image\",\"disk_size\":\"80\"}],\"vdu_name\": \"OMU\",\"num_cpus\": \"4\",\"mem_size\": \"8192\"},\"init_number\":\"1\"}],\"ex_vim_list\": []}";
 
  72         vnfObj = JSONObject.fromObject(data);
 
  73         VnfResourceMgr grantTest = new VnfResourceMgr();
 
  74         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
 
  76         JSONObject retJson = new JSONObject();
 
  77         assertEquals(retJson, result);
 
  81     public void testgrantVnfResourceSuccess1() {
 
  82         new MockUp<VnfmRestfulUtil>() {
 
  85             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
 
  86                 RestfulResponse rsp = new RestfulResponse();
 
  88                 rsp.setResponseJson(new JSONObject().toString());
 
  93         JSONObject vnfObj = new JSONObject();
 
  95                 "{\"type\": \"instantiation\",\"location\": \"RomaRespool\",\"version\":\"V500R001C20\",\"vendor\": \"V500R001C20B001\",\"tenant\": \"admin\",\"vnf_id\":\"NE=1304\",\"vnfm_id\": \"9a49755d-8050-4369-9e7d-0a0855097585\",\"project_id\":\"\",\"operation_right\": \"decrease\",\"vnf_name\": \"wangyuans4\",\"vim_id\":\"\",\"dr_location\": \"RomaRespool\",\"nfvo_id\": \"\",\"vnf_type\": \"vNGFW\",\"plan_id\":\"V4\",\"plan_name\": \"V4\",\"vnfd_id\": \"vNGFW\",\"vm_list\": [{\"vm_flavor\":{\"location\": \"\",\"priority\": \"1\",\"storage\": [{\"vol_type\":\"local_volume\",\"vol_size\": \"200\"},{\"storage_type\": \"local_image\",\"disk_size\":\"80\"}],\"vdu_name\": \"OMU\",\"num_cpus\": \"4\",\"mem_size\": \"8192\"},\"init_number\":\"1\"}],\"ex_vim_list\": []}";
 
  96         vnfObj = JSONObject.fromObject(data);
 
  97         VnfResourceMgr grantTest = new VnfResourceMgr();
 
  98         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
 
 100         JSONObject retJson = new JSONObject();
 
 101         assertEquals(retJson, result);
 
 105     public void testgrantVnfResourceSuccess2() {
 
 106         new MockUp<VnfmRestfulUtil>() {
 
 109             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
 
 110                 RestfulResponse rsp = new RestfulResponse();
 
 112                 rsp.setResponseJson(new JSONObject().toString());
 
 117         JSONObject vnfObj = new JSONObject();
 
 119                 "{\"type\": \"scale\",\"location\": \"RomaRespool\",\"version\":\"V500R001C20\",\"vendor\": \"V500R001C20B001\",\"tenant\": \"admin\",\"vnf_id\":\"NE=1304\",\"vnfm_id\": \"9a49755d-8050-4369-9e7d-0a0855097585\",\"project_id\":\"\",\"operation_right\": \"decrease\",\"vnf_name\": \"wangyuans4\",\"vim_id\":\"\",\"dr_location\": \"RomaRespool\",\"nfvo_id\": \"\",\"vnf_type\": \"vNGFW\",\"plan_id\":\"V4\",\"plan_name\": \"V4\",\"vnfd_id\": \"vNGFW\",\"vm_list\": [{\"vm_flavor\":{\"location\": \"\",\"priority\": \"1\",\"storage\": [{\"vol_type\":\"local_volume\",\"vol_size\": \"200\"},{\"storage_type\": \"local_image\",\"disk_size\":\"80\"}],\"vdu_name\": \"OMU\",\"num_cpus\": \"4\",\"mem_size\": \"8192\"},\"init_number\":\"1\"}],\"ex_vim_list\": []}";
 
 120         vnfObj = JSONObject.fromObject(data);
 
 121         VnfResourceMgr grantTest = new VnfResourceMgr();
 
 122         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
 
 124         JSONObject retJson = new JSONObject();
 
 125         assertEquals(retJson, result);
 
 129     public void testgrantVnfResourceSuccess3() {
 
 130         new MockUp<VnfmRestfulUtil>() {
 
 133             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
 
 134                 RestfulResponse rsp = new RestfulResponse();
 
 136                 rsp.setResponseJson(new JSONObject().toString());
 
 141         JSONObject vnfObj = new JSONObject();
 
 143                 "{\"type\": \"scale\",\"location\": \"RomaRespool\",\"version\":\"V500R001C20\",\"vendor\": \"V500R001C20B001\",\"tenant\": \"admin\",\"vnf_id\":\"NE=1304\",\"vnfm_id\": \"9a49755d-8050-4369-9e7d-0a0855097585\",\"project_id\":\"\",\"operation_right\": \"increase\",\"vnf_name\": \"wangyuans4\",\"vim_id\":\"\",\"dr_location\": \"RomaRespool\",\"nfvo_id\": \"\",\"vnf_type\": \"vNGFW\",\"plan_id\":\"V4\",\"plan_name\": \"V4\",\"vnfd_id\": \"vNGFW\",\"vm_list\": [{\"vm_flavor\":{\"location\": \"\",\"priority\": \"1\",\"storage\": [{\"vol_type\":\"local_volume\",\"vol_size\": \"200\"},{\"storage_type\": \"local_image\",\"disk_size\":\"80\"}],\"vdu_name\": \"OMU\",\"num_cpus\": \"4\",\"mem_size\": \"8192\"},\"init_number\":\"1\"}],\"ex_vim_list\": []}";
 
 144         vnfObj = JSONObject.fromObject(data);
 
 145         VnfResourceMgr grantTest = new VnfResourceMgr();
 
 146         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
 
 148         JSONObject retJson = new JSONObject();
 
 149         assertEquals(retJson, result);
 
 153     public void testgrantVnfResourceByResMapNull() {
 
 156                 "{\"type\": \"instantiation\",\"operation_right\": \"increase\",\"vnf_name\": \"vnf_name\",\"vm_list\": [{\"vm_flavors\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
 157         JSONObject vnfObj = JSONObject.fromObject(data);
 
 158         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
 159         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
 
 161         JSONObject retJson = new JSONObject();
 
 162         retJson.put("retCode", Constant.REST_FAIL);
 
 163         retJson.put("errorMsg", "resource params error");
 
 164         assertEquals(retJson, result);
 
 168     public void testgrantVnfResourceByTypeIsEmpty() {
 
 170                 "{\"type\": \"\",\"operation_right\": \"increase\",\"vnf_name\": \"vnf_name\",\"vm_list\": [{\"vm_flavor\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
 171         JSONObject vnfObj = JSONObject.fromObject(data);
 
 172         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
 173         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
 
 175         JSONObject retJson = new JSONObject();
 
 176         retJson.put("retCode", Constant.REST_FAIL);
 
 177         retJson.put("errorMsg", "basic params error");
 
 178         assertEquals(retJson, result);
 
 182     public void testgrantVnfResourceByVnfNameIsEmpty() {
 
 184                 "{\"type\": \"instantiation\",\"operation_right\": \"increase\",\"vnf_name\": \"\",\"vm_list\": [{\"vm_flavor\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
 185         JSONObject vnfObj = JSONObject.fromObject(data);
 
 186         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
 187         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
 
 189         JSONObject retJson = new JSONObject();
 
 190         retJson.put("retCode", Constant.REST_FAIL);
 
 191         retJson.put("errorMsg", "basic params error");
 
 192         assertEquals(retJson, result);
 
 196     public void testgrantVnfResourceByVnfIdIsEmpty() {
 
 198                 "{\"type\": \"instantiation\",\"operation_right\": \"increase\",\"vnf_name\": \"vnf_name\",\"vm_list\": [{\"vm_flavor\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
 199         JSONObject vnfObj = JSONObject.fromObject(data);
 
 200         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
 201         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "", "vnfmId");
 
 203         JSONObject retJson = new JSONObject();
 
 204         retJson.put("retCode", Constant.REST_FAIL);
 
 205         retJson.put("errorMsg", "basic params error");
 
 206         assertEquals(retJson, result);
 
 210     public void testgrantVnfResourceByRequestTypeIsEmpty() {
 
 212                 "{\"type\": \"instantiation\",\"operation_right\": \"\",\"vnf_name\": \"vnf_name\",\"vm_list\": [{\"vm_flavor\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
 213         JSONObject vnfObj = JSONObject.fromObject(data);
 
 214         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
 215         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
 
 217         JSONObject retJson = new JSONObject();
 
 218         retJson.put("retCode", Constant.REST_FAIL);
 
 219         retJson.put("errorMsg", "basic params error");
 
 220         assertEquals(retJson, result);
 
 224     public void testgrantVnfResourceByJSONException() {
 
 226                 "{\"operation_right\": \"increase\",\"vnf_name\": \"vnf_name\",\"vm_list\": [{\"vm_flavor\": {\"storage\": [{\"vol_type\": \"local_volume\",\"vol_size\": \"2\",\"storage_type\": \"local_image\",\"disk_size\": \"100\"}],\"num_cpus\": \"6\",\"mem_size\": \"8\"},\"init_number\": \"1\"}],\"version\": \"version\",\"template_id\": \"template_id\",\"template_name\": \"template_name\",\"plan_id\": \"plan_id\",\"plan_name\": \"plan_name\",\"project_id\": \"project_id\",\"project_name\": \"project_name\",\"creator\": \"creator\",\"status\": \"status\",\"tenant\": \"tenant\",\"parent_tenant\": \"parent_tenant\",\"vnfd_id\": \"vnfd_id\",\"location\": \"location\",\"dr_location\": \"dr_location\",\"nfvo_id\": \"nfvo_id\"}";
 
 227         JSONObject vnfObj = JSONObject.fromObject(data);
 
 228         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
 
 229         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
 
 231         JSONObject retJson = new JSONObject();
 
 232         retJson.put("retCode", Constant.REST_FAIL);
 
 233         retJson.put("errorMsg", "params parse exception");
 
 234         assertEquals(retJson, result);