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;
21 import javax.servlet.http.HttpServletRequest;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmJsonUtil;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmUtil;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.VnfResourceMgr;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.rest.VnfResourceRoa;
34 import net.sf.json.JSONObject;
36 public class VnfResourceRoaTest {
38 private VnfResourceRoa vnfResourceRoa;
40 private VnfResourceMgr vnfResourceMgr;
44 vnfResourceRoa = new VnfResourceRoa();
45 vnfResourceMgr = new VnfResourceMgr();
46 vnfResourceRoa.setVnfResourceMgr(vnfResourceMgr);
50 public void tearDown() {
51 vnfResourceRoa = null;
52 vnfResourceMgr = null;
56 public void testGrantVnfResByDataObjectNull() {
57 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
58 HttpServletRequest mockInstance = proxyStub.getMockInstance();
59 new MockUp<VnfmJsonUtil>() {
62 public <T> T getJsonFromContexts(HttpServletRequest context) {
67 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
69 JSONObject restJson = new JSONObject();
70 restJson.put("retCode", Constant.REST_FAIL);
71 restJson.put("data", "Params error");
72 assertEquals(restJson.toString(), result);
76 public void testGrantVnfResByGrantObjNull() {
77 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
78 HttpServletRequest mockInstance = proxyStub.getMockInstance();
79 final JSONObject dataObject = new JSONObject();
80 new MockUp<VnfmJsonUtil>() {
82 @SuppressWarnings("unchecked")
84 public <T> T getJsonFromContexts(HttpServletRequest context) {
88 new MockUp<JSONObject>() {
91 public JSONObject getJSONObject(String key) {
99 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
101 JSONObject restJson = new JSONObject();
102 restJson.put("retCode", Constant.REST_FAIL);
103 restJson.put("data", "Grant param error");
104 assertEquals(restJson.toString(), result);
108 public void testGrantVnfRes() {
109 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
112 public String getHeader(String name) {
116 HttpServletRequest mockInstance = proxyStub.getMockInstance();
117 final JSONObject dataObject = new JSONObject();
118 JSONObject grant = new JSONObject();
119 grant.put("project_id", "project_id");
120 dataObject.put("grant", grant);
121 new MockUp<VnfmJsonUtil>() {
123 @SuppressWarnings("unchecked")
125 public <T> T getJsonFromContexts(HttpServletRequest context) {
126 return (T)dataObject;
129 new MockUp<VnfmUtil>() {
132 public String getVnfmIdByIp(String ip) {
137 new MockUp<VnfResourceMgr>() {
140 public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
141 JSONObject resultJson = new JSONObject();
142 resultJson.put("retCode", Constant.REST_SUCCESS);
143 JSONObject data = new JSONObject();
144 data.put("data", "success");
145 resultJson.put("data", data);
149 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
151 JSONObject restJson = new JSONObject();
152 restJson.put("retCode", Constant.REST_SUCCESS);
153 JSONObject data = new JSONObject();
154 data.put("data", "success");
155 restJson.put("data", data);
156 assertEquals(restJson.toString(), result);
160 public void testGrantVnfResByFail() {
161 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
164 public String getHeader(String name) {
168 HttpServletRequest mockInstance = proxyStub.getMockInstance();
169 final JSONObject dataObject = new JSONObject();
170 JSONObject grant = new JSONObject();
171 grant.put("project_id", "project_id");
172 dataObject.put("grant", grant);
173 new MockUp<VnfmJsonUtil>() {
175 @SuppressWarnings("unchecked")
177 public <T> T getJsonFromContexts(HttpServletRequest context) {
178 return (T)dataObject;
181 new MockUp<VnfmUtil>() {
184 public String getVnfmIdByIp(String ip) {
189 new MockUp<VnfResourceMgr>() {
192 public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
193 JSONObject resultJson = new JSONObject();
194 resultJson.put("retCode", Constant.REST_FAIL);
195 resultJson.put("data", "Fail!");
199 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
201 JSONObject restJson = new JSONObject();
202 restJson.put("retCode", Constant.REST_FAIL);
203 restJson.put("data", "Fail!");
204 assertEquals(restJson.toString(), result);
208 public void testGrantVnfResByDataNull() {
209 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
212 public String getHeader(String name) {
216 HttpServletRequest mockInstance = proxyStub.getMockInstance();
217 final JSONObject dataObject = new JSONObject();
218 JSONObject grant = new JSONObject();
219 grant.put("project_id", "project_id");
220 dataObject.put("grant", grant);
221 new MockUp<VnfmJsonUtil>() {
223 @SuppressWarnings("unchecked")
225 public <T> T getJsonFromContexts(HttpServletRequest context) {
226 return (T)dataObject;
229 new MockUp<VnfmUtil>() {
232 public String getVnfmIdByIp(String ip) {
237 new MockUp<VnfResourceMgr>() {
240 public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
241 JSONObject resultJson = new JSONObject();
242 resultJson.put("retCode", Constant.REST_FAIL);
246 String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
248 JSONObject restJson = new JSONObject();
249 restJson.put("retCode", Constant.REST_FAIL);
250 assertEquals(restJson.toString(), result);
254 public void testNotify() {
255 MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
258 public String getHeader(String name) {
262 HttpServletRequest mockInstance = proxyStub.getMockInstance();
263 String result = vnfResourceRoa.notify(mockInstance);
265 JSONObject restJson = new JSONObject();
266 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
267 assertEquals(restJson.toString(), result);