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.assertNotNull;
21 import java.io.IOException;
22 import java.lang.reflect.Method;
24 import javax.servlet.ServletInputStream;
25 import javax.servlet.http.HttpServletRequest;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
35 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.VnfResourceMgr;
37 import net.sf.json.JSONObject;
38 @RunWith(MockitoJUnitRunner.class)
39 public class VnfResourceRoaTest {
41 private VnfResourceRoa vnfResourceRoa;
43 private VnfResourceMgr vnfResourceMgr;
46 HttpServletRequest context;
49 ServletInputStream servletInputStream;
53 vnfResourceRoa = new VnfResourceRoa();
54 vnfResourceMgr = new VnfResourceMgr();
55 vnfResourceRoa.setVnfResourceMgr(vnfResourceMgr);
59 public void tearDown() {
60 vnfResourceRoa = null;
61 vnfResourceMgr = null;
65 public void testGrantVnfResByDataObjectNull() throws Exception {
66 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
67 String result = vnfResourceRoa.grantVnfRes(context, "vnfId");
68 JSONObject restJson = new JSONObject();
69 restJson.put("retCode", Constant.REST_FAIL);
70 restJson.put("data", "Params error");
71 assertNotNull(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) {
84 // return (T)dataObject;
87 // new MockUp<JSONObject>() {
90 // public JSONObject getJSONObject(String key) {
91 // if(key == "grant") {
98 String result = vnfResourceRoa.grantVnfRes(null, "vnfId");
100 JSONObject restJson = new JSONObject();
101 restJson.put("retCode", Constant.REST_FAIL);
102 restJson.put("data", "Grant param error");
103 assertNotNull(result);
107 public void testGrantVnfRes() {
108 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
111 // public String getHeader(String name) {
112 // return "127.0.0.1";
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);
145 // return resultJson;
148 String result = vnfResourceRoa.grantVnfRes(null, "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 assertNotNull(result);
159 public void testGrantVnfResByFail() {
160 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
163 // public String getHeader(String name) {
164 // return "127.0.0.1";
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!");
195 // return resultJson;
199 String result = vnfResourceRoa.grantVnfRes(null, "vnfId");
201 JSONObject restJson = new JSONObject();
202 restJson.put("retCode", Constant.REST_FAIL);
203 restJson.put("data", "Fail!");
204 assertNotNull(result);
208 public void testGrantVnfResByDataNull() {
209 //HttpServletRequest mockInstance = proxyStub.getMockInstance();
210 final JSONObject dataObject = new JSONObject();
211 JSONObject grant = new JSONObject();
212 grant.put("project_id", "project_id");
213 dataObject.put("grant", grant);
214 // new MockUp<VnfmJsonUtil>() {
216 // @SuppressWarnings("unchecked")
218 // public <T> T getJsonFromContexts(HttpServletRequest context) {
219 // return (T)dataObject;
222 // new MockUp<VnfmUtil>() {
225 // public String getVnfmIdByIp(String ip) {
230 // new MockUp<VnfResourceMgr>() {
233 // public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
234 // JSONObject resultJson = new JSONObject();
235 // resultJson.put("retCode", Constant.REST_FAIL);
236 // return resultJson;
239 String result = vnfResourceRoa.grantVnfRes(null, "vnfId");
241 JSONObject restJson = new JSONObject();
242 restJson.put("retCode", Constant.REST_FAIL);
243 assertNotNull(result);
247 public void testNotify() throws IOException {
248 // MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
251 // public String getHeader(String name) {
252 // return "127.0.0.1";
255 // HttpServletRequest mockInstance = proxyStub.getMockInstance();
257 Mockito.when(context.getInputStream()).thenReturn(servletInputStream);
258 String result = vnfResourceRoa.notify(context);
260 JSONObject restJson = new JSONObject();
261 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
262 assertNotNull(result);
267 public void callLcmNotify() throws Exception{
268 VnfResourceRoa vnfRoa = new VnfResourceRoa();
269 JSONObject json = new JSONObject();
270 Method m = VnfResourceRoa.class.getDeclaredMethod("callLcmNotify", new Class[] {JSONObject.class});
271 m.setAccessible(true);
272 m.invoke(vnfRoa, json);