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 / rest / VnfResourceRoaTest.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.rest;
18
19 import static org.junit.Assert.assertEquals;
20
21 import javax.servlet.http.HttpServletRequest;
22
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;
30
31 import mockit.Mock;
32 import mockit.MockUp;
33 import net.sf.json.JSONObject;
34
35 public class VnfResourceRoaTest {
36
37     private VnfResourceRoa vnfResourceRoa;
38
39     private VnfResourceMgr vnfResourceMgr;
40
41     @Before
42     public void setUp() {
43         vnfResourceRoa = new VnfResourceRoa();
44         vnfResourceMgr = new VnfResourceMgr();
45         vnfResourceRoa.setVnfResourceMgr(vnfResourceMgr);
46     }
47
48     @After
49     public void tearDown() {
50         vnfResourceRoa = null;
51         vnfResourceMgr = null;
52     }
53
54     @Test
55     public void testGrantVnfResByDataObjectNull() {
56         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {};
57         HttpServletRequest mockInstance = proxyStub.getMockInstance();
58         new MockUp<VnfmJsonUtil>() {
59
60             @Mock
61             public <T> T getJsonFromContexts(HttpServletRequest context) {
62                 return null;
63             }
64         };
65
66         String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
67
68         JSONObject restJson = new JSONObject();
69         restJson.put("retCode", Constant.REST_FAIL);
70         restJson.put("data", "Params error");
71         assertEquals(restJson.toString(), result);
72     }
73
74     @Test
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>() {
80
81             @SuppressWarnings("unchecked")
82             @Mock
83             public <T> T getJsonFromContexts(HttpServletRequest context) {
84                 return (T)dataObject;
85             }
86         };
87         new MockUp<JSONObject>() {
88
89             @Mock
90             public JSONObject getJSONObject(String key) {
91                 if(key == "grant") {
92                     return null;
93                 }
94                 return dataObject;
95             }
96         };
97
98         String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
99
100         JSONObject restJson = new JSONObject();
101         restJson.put("retCode", Constant.REST_FAIL);
102         restJson.put("data", "Grant param error");
103         assertEquals(restJson.toString(), result);
104     }
105
106     @Test
107     public void testGrantVnfRes() {
108         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
109
110             @Mock
111             public String getHeader(String name) {
112                 return "127.0.0.1";
113             }
114         };
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>() {
121
122             @SuppressWarnings("unchecked")
123             @Mock
124             public <T> T getJsonFromContexts(HttpServletRequest context) {
125                 return (T)dataObject;
126             }
127         };
128         new MockUp<VnfmUtil>() {
129
130             @Mock
131             public String getVnfmIdByIp(String ip) {
132                 return "vnfmId";
133             }
134         };
135
136         new MockUp<VnfResourceMgr>() {
137
138             @Mock
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;
146             }
147         };
148         String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
149
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);
156     }
157
158     @Test
159     public void testGrantVnfResByFail() {
160         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
161
162             @Mock
163             public String getHeader(String name) {
164                 return "127.0.0.1";
165             }
166         };
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>() {
173
174             @SuppressWarnings("unchecked")
175             @Mock
176             public <T> T getJsonFromContexts(HttpServletRequest context) {
177                 return (T)dataObject;
178             }
179         };
180         new MockUp<VnfmUtil>() {
181
182             @Mock
183             public String getVnfmIdByIp(String ip) {
184                 return "vnfmId";
185             }
186         };
187
188         new MockUp<VnfResourceMgr>() {
189
190             @Mock
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;
196             }
197         };
198         String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
199
200         JSONObject restJson = new JSONObject();
201         restJson.put("retCode", Constant.REST_FAIL);
202         restJson.put("data", "Fail!");
203         assertEquals(restJson.toString(), result);
204     }
205
206     @Test
207     public void testGrantVnfResByDataNull() {
208         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
209
210             @Mock
211             public String getHeader(String name) {
212                 return "127.0.0.1";
213             }
214         };
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>() {
221
222             @SuppressWarnings("unchecked")
223             @Mock
224             public <T> T getJsonFromContexts(HttpServletRequest context) {
225                 return (T)dataObject;
226             }
227         };
228         new MockUp<VnfmUtil>() {
229
230             @Mock
231             public String getVnfmIdByIp(String ip) {
232                 return "vnfmId";
233             }
234         };
235
236         new MockUp<VnfResourceMgr>() {
237
238             @Mock
239             public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
240                 JSONObject resultJson = new JSONObject();
241                 resultJson.put("retCode", Constant.REST_FAIL);
242                 return resultJson;
243             }
244         };
245         String result = vnfResourceRoa.grantVnfRes(mockInstance, "vnfId");
246
247         JSONObject restJson = new JSONObject();
248         restJson.put("retCode", Constant.REST_FAIL);
249         assertEquals(restJson.toString(), result);
250     }
251
252     @Test
253     public void testNotify() {
254         MockUp<HttpServletRequest> proxyStub = new MockUp<HttpServletRequest>() {
255
256             @Mock
257             public String getHeader(String name) {
258                 return "127.0.0.1";
259             }
260         };
261         HttpServletRequest mockInstance = proxyStub.getMockInstance();
262         String result = vnfResourceRoa.notify(mockInstance);
263
264         JSONObject restJson = new JSONObject();
265         restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
266         assertEquals(restJson.toString(), result);
267     }
268 }