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.openo.nfvo.vnfmadapter.service.rest;
19 import static org.junit.Assert.assertEquals;
21 import javax.servlet.http.HttpServletRequest;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openo.nfvo.vnfmadapter.common.VnfmJsonUtil;
27 import org.openo.nfvo.vnfmadapter.common.VnfmUtil;
28 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
29 import org.openo.nfvo.vnfmadapter.service.process.VnfResourceMgr;
33 import net.sf.json.JSONObject;
35 public class VnfResourceRoaTest {
37 private VnfResourceRoa vnfResourceRoa;
39 private VnfResourceMgr vnfResourceMgr;
43 vnfResourceRoa = new VnfResourceRoa();
44 vnfResourceMgr = new VnfResourceMgr();
45 vnfResourceRoa.setVnfResourceMgr(vnfResourceMgr);
49 public void tearDown() {
50 vnfResourceRoa = null;
51 vnfResourceMgr = null;
55 public void testGrantVnfResByDataObjectNull() {
56 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
57 HttpServletRequest mockInstance = proxyStub.getMockInstance();
58 new MockUp<VnfmJsonUtil>() {
61 public <T> T getJsonFromContexts(HttpServletRequest context) {
66 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
68 JSONObject restJson = new JSONObject();
69 restJson.put("retCode", Constant.REST_FAIL);
70 restJson.put("data", "Params error");
71 assertEquals(restJson.toString(), result);
75 public void testGrantVnfResByGrantObjNull() {
76 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
77 HttpServletRequest mockInstance = proxyStub.getMockInstance();
78 final JSONObject dataObject = new JSONObject();
79 new MockUp<VnfmJsonUtil>() {
81 @SuppressWarnings("unchecked")
83 public <T> T getJsonFromContexts(HttpServletRequest context) {
87 new MockUp<JSONObject>() {
90 public JSONObject getJSONObject(String key) {
98 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
100 JSONObject restJson = new JSONObject();
101 restJson.put("retCode", Constant.REST_FAIL);
102 restJson.put("data", "Grant param error");
103 assertEquals(restJson.toString(), result);
107 public void testGrantVnfRes() {
108 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
111 public String getHeader(String name) {
115 HttpServletRequest mockInstance = proxyStub.getMockInstance();
116 final JSONObject dataObject = new JSONObject();
117 JSONObject grant = new JSONObject();
118 grant.put("project_id", "project_id");
119 dataObject.put("grant", grant);
120 new MockUp<VnfmJsonUtil>() {
122 @SuppressWarnings("unchecked")
124 public <T> T getJsonFromContexts(HttpServletRequest context) {
125 return (T)dataObject;
128 new MockUp<VnfmUtil>() {
131 public String getVnfmIdByIp(String ip) {
136 new MockUp<VnfResourceMgr>() {
139 public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
140 JSONObject resultJson = new JSONObject();
141 resultJson.put("retCode", Constant.REST_SUCCESS);
142 JSONObject data = new JSONObject();
143 data.put("data", "success");
144 resultJson.put("data", data);
148 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
150 JSONObject restJson = new JSONObject();
151 restJson.put("retCode", Constant.REST_SUCCESS);
152 JSONObject data = new JSONObject();
153 data.put("data", "success");
154 restJson.put("data", data);
155 assertEquals(restJson.toString(), result);
159 public void testGrantVnfResByFail() {
160 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
163 public String getHeader(String name) {
167 HttpServletRequest mockInstance = proxyStub.getMockInstance();
168 final JSONObject dataObject = new JSONObject();
169 JSONObject grant = new JSONObject();
170 grant.put("project_id", "project_id");
171 dataObject.put("grant", grant);
172 new MockUp<VnfmJsonUtil>() {
174 @SuppressWarnings("unchecked")
176 public <T> T getJsonFromContexts(HttpServletRequest context) {
177 return (T)dataObject;
180 new MockUp<VnfmUtil>() {
183 public String getVnfmIdByIp(String ip) {
188 new MockUp<VnfResourceMgr>() {
191 public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
192 JSONObject resultJson = new JSONObject();
193 resultJson.put("retCode", Constant.REST_FAIL);
194 resultJson.put("data", "Fail!");
198 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
200 JSONObject restJson = new JSONObject();
201 restJson.put("retCode", Constant.REST_FAIL);
202 restJson.put("data", "Fail!");
203 assertEquals(restJson.toString(), result);
207 public void testGrantVnfResByDataNull() {
208 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
211 public String getHeader(String name) {
215 HttpServletRequest mockInstance = proxyStub.getMockInstance();
216 final JSONObject dataObject = new JSONObject();
217 JSONObject grant = new JSONObject();
218 grant.put("project_id", "project_id");
219 dataObject.put("grant", grant);
220 new MockUp<VnfmJsonUtil>() {
222 @SuppressWarnings("unchecked")
224 public <T> T getJsonFromContexts(HttpServletRequest context) {
225 return (T)dataObject;
228 new MockUp<VnfmUtil>() {
231 public String getVnfmIdByIp(String ip) {
236 new MockUp<VnfResourceMgr>() {
239 public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
240 JSONObject resultJson = new JSONObject();
241 resultJson.put("retCode", Constant.REST_FAIL);
245 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
247 JSONObject restJson = new JSONObject();
248 restJson.put("retCode", Constant.REST_FAIL);
249 assertEquals(restJson.toString(), result);
253 public void testNotify() {
254 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
257 public String getHeader(String name) {
261 HttpServletRequest mockInstance = proxyStub.getMockInstance();
262 String result = vnfResourceRoa.notify(mockInstance);
264 JSONObject restJson = new JSONObject();
265 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
266 assertEquals(restJson.toString(), result);