Change: add OPEN-O seed code for VF-C
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / openo / nfvo / vnfmadapter / service / process / VnfResourceMgrTest.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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.openo.nfvo.vnfmadapter.service.process;
18
19 import static org.junit.Assert.assertEquals;
20
21 import org.junit.Test;
22 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
23 import org.openo.nfvo.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
24 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
25
26 import mockit.Mock;
27 import mockit.MockUp;
28 import net.sf.json.JSONObject;
29
30 /**
31  * <br/>
32  * <p>
33  * </p>
34  *
35  * @author
36  * @version NFVO 0.5 Aug 9, 2016
37  */
38 public class VnfResourceMgrTest {
39
40     @Test
41     public void testgrantVnfResource() {
42
43         String data =
44                 "{\"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\"}";
45         JSONObject vnfObj = JSONObject.fromObject(data);
46         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
47         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
48
49         JSONObject retJson = new JSONObject();
50         retJson.put("retCode", Constant.REST_FAIL);
51         retJson.put("errorMsg", "params parse exception");
52         assertEquals(retJson, result);
53     }
54
55     @Test
56     public void testgrantVnfResourceSuccess() {
57         new MockUp<VnfmRestfulUtil>() {
58
59             @Mock
60             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
61                 RestfulResponse rsp = new RestfulResponse();
62                 rsp.setStatus(200);
63                 rsp.setResponseJson(new JSONObject().toString());
64                 return rsp;
65             }
66         };
67
68         JSONObject vnfObj = new JSONObject();
69         String data =
70                 "{\"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\": []}";
71         vnfObj = JSONObject.fromObject(data);
72         VnfResourceMgr grantTest = new VnfResourceMgr();
73         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
74
75         JSONObject retJson = new JSONObject();
76         assertEquals(retJson, result);
77     }
78
79     @Test
80     public void testgrantVnfResourceSuccess1() {
81         new MockUp<VnfmRestfulUtil>() {
82
83             @Mock
84             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
85                 RestfulResponse rsp = new RestfulResponse();
86                 rsp.setStatus(200);
87                 rsp.setResponseJson(new JSONObject().toString());
88                 return rsp;
89             }
90         };
91
92         JSONObject vnfObj = new JSONObject();
93         String data =
94                 "{\"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\": []}";
95         vnfObj = JSONObject.fromObject(data);
96         VnfResourceMgr grantTest = new VnfResourceMgr();
97         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
98
99         JSONObject retJson = new JSONObject();
100         assertEquals(retJson, result);
101     }
102
103     @Test
104     public void testgrantVnfResourceSuccess2() {
105         new MockUp<VnfmRestfulUtil>() {
106
107             @Mock
108             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
109                 RestfulResponse rsp = new RestfulResponse();
110                 rsp.setStatus(200);
111                 rsp.setResponseJson(new JSONObject().toString());
112                 return rsp;
113             }
114         };
115
116         JSONObject vnfObj = new JSONObject();
117         String data =
118                 "{\"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\": []}";
119         vnfObj = JSONObject.fromObject(data);
120         VnfResourceMgr grantTest = new VnfResourceMgr();
121         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
122
123         JSONObject retJson = new JSONObject();
124         assertEquals(retJson, result);
125     }
126
127     @Test
128     public void testgrantVnfResourceSuccess3() {
129         new MockUp<VnfmRestfulUtil>() {
130
131             @Mock
132             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
133                 RestfulResponse rsp = new RestfulResponse();
134                 rsp.setStatus(200);
135                 rsp.setResponseJson(new JSONObject().toString());
136                 return rsp;
137             }
138         };
139
140         JSONObject vnfObj = new JSONObject();
141         String data =
142                 "{\"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\": []}";
143         vnfObj = JSONObject.fromObject(data);
144         VnfResourceMgr grantTest = new VnfResourceMgr();
145         JSONObject result = grantTest.grantVnfResource(vnfObj, "1111", "abc");
146
147         JSONObject retJson = new JSONObject();
148         assertEquals(retJson, result);
149     }
150
151     @Test
152     public void testgrantVnfResourceByResMapNull() {
153
154         String data =
155                 "{\"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\"}";
156         JSONObject vnfObj = JSONObject.fromObject(data);
157         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
158         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
159
160         JSONObject retJson = new JSONObject();
161         retJson.put("retCode", Constant.REST_FAIL);
162         retJson.put("errorMsg", "resource params error");
163         assertEquals(retJson, result);
164     }
165
166     @Test
167     public void testgrantVnfResourceByTypeIsEmpty() {
168         String data =
169                 "{\"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\"}";
170         JSONObject vnfObj = JSONObject.fromObject(data);
171         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
172         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
173
174         JSONObject retJson = new JSONObject();
175         retJson.put("retCode", Constant.REST_FAIL);
176         retJson.put("errorMsg", "basic params error");
177         assertEquals(retJson, result);
178     }
179
180     @Test
181     public void testgrantVnfResourceByVnfNameIsEmpty() {
182         String data =
183                 "{\"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\"}";
184         JSONObject vnfObj = JSONObject.fromObject(data);
185         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
186         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
187
188         JSONObject retJson = new JSONObject();
189         retJson.put("retCode", Constant.REST_FAIL);
190         retJson.put("errorMsg", "basic params error");
191         assertEquals(retJson, result);
192     }
193
194     @Test
195     public void testgrantVnfResourceByVnfIdIsEmpty() {
196         String data =
197                 "{\"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\"}";
198         JSONObject vnfObj = JSONObject.fromObject(data);
199         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
200         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "", "vnfmId");
201
202         JSONObject retJson = new JSONObject();
203         retJson.put("retCode", Constant.REST_FAIL);
204         retJson.put("errorMsg", "basic params error");
205         assertEquals(retJson, result);
206     }
207
208     @Test
209     public void testgrantVnfResourceByRequestTypeIsEmpty() {
210         String data =
211                 "{\"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\"}";
212         JSONObject vnfObj = JSONObject.fromObject(data);
213         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
214         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
215
216         JSONObject retJson = new JSONObject();
217         retJson.put("retCode", Constant.REST_FAIL);
218         retJson.put("errorMsg", "basic params error");
219         assertEquals(retJson, result);
220     }
221
222     @Test
223     public void testgrantVnfResourceByJSONException() {
224         String data =
225                 "{\"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\"}";
226         JSONObject vnfObj = JSONObject.fromObject(data);
227         VnfResourceMgr vnfResourceMgr = new VnfResourceMgr();
228         JSONObject result = vnfResourceMgr.grantVnfResource(vnfObj, "vnfId", "vnfmId");
229
230         JSONObject retJson = new JSONObject();
231         retJson.put("retCode", Constant.REST_FAIL);
232         retJson.put("errorMsg", "params parse exception");
233         assertEquals(retJson, result);
234     }
235 }