Fix gvnfm juju compile problem
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / service / process / VnfMgrTest.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.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.process;
18
19 import static org.junit.Assert.*;
20
21 import java.util.Map;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.VnfmUtil;
26 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
27 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.inf.IResourceManager;
28 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.entity.JujuVnfmInfo;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.process.VnfMgr;
30 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 public class VnfMgrTest {
37
38     VnfMgr mgr;
39     @Before
40     public void setUp() {
41         mgr = new VnfMgr();
42         mgr.getJujuVnfmInfoMapper();
43         mgr.getResourceManager();
44
45     }
46
47     @Test
48     public void addVnfTestNullJson() {
49         JSONObject vnfObject = new JSONObject();
50         String vnfmId = "1234";
51         JSONObject resp = mgr.addVnf(vnfObject, vnfmId);
52         assertEquals(resp.get("retCode"), -1);
53     }
54
55     @Test
56     public void addVnfTestOk() {
57
58         new MockUp<VnfmUtil>() {
59
60             @Mock
61             public JSONObject getVnfmById(String vnfmId) {
62                 JSONObject vnfmObject = new JSONObject();
63                 vnfmObject.put("url", "http://localhost:8080");
64                 return vnfmObject;
65             }
66         };
67         new MockUp<VnfmRestfulUtil>() {
68
69             @Mock
70             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params,
71                     String domainTokens) {
72                 RestfulResponse resp = new RestfulResponse();
73                 resp.setStatus(200);
74                 return resp;
75             }
76         };
77         JSONObject vnfObject = new JSONObject();
78         vnfObject.put("vnfInstanceName", "test123");
79         vnfObject.put("vnfPackageId", "123");
80         String vnfmId = "1234";
81         JSONObject resp = mgr.addVnf(vnfObject, vnfmId);
82         assertEquals(resp.get("retCode"), -1);
83     }
84
85     @Test
86     public void addVnfTestNull() {
87
88         new MockUp<VnfmUtil>() {
89
90             @Mock
91             public JSONObject getVnfmById(String vnfmId) {
92                 JSONObject vnfmObject = new JSONObject();
93                 return vnfmObject;
94             }
95         };
96         JSONObject vnfObject = new JSONObject();
97         vnfObject.put("vnfInstanceName", "test123");
98         String vnfmId = "1234";
99         JSONObject resp = mgr.addVnf(vnfObject, vnfmId);
100         assertEquals(resp.get("retCode"), -1);
101     }
102
103     @Test
104     public void addVnfTestNullRes() {
105
106         new MockUp<VnfmUtil>() {
107
108             @Mock
109             public JSONObject getVnfmById(String vnfmId) {
110                 JSONObject vnfmObject = new JSONObject();
111                 vnfmObject.put("url", "http://localhost:8080");
112                 return vnfmObject;
113             }
114         };
115         new MockUp<VnfmRestfulUtil>() {
116
117             @Mock
118             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params,
119                     String domainTokens) {
120
121                 return null;
122             }
123         };
124         JSONObject vnfObject = new JSONObject();
125         vnfObject.put("vnfInstanceName", "test123");
126         String vnfmId = "1234";
127         JSONObject resp = mgr.addVnf(vnfObject, vnfmId);
128         assertEquals(resp.get("retCode"), -1);
129     }
130
131     @Test
132     public void deleteVnfTestNullJson() {
133
134         new MockUp<VnfmUtil>() {
135
136             @Mock
137             public JSONObject getVnfmById(String vnfmId) {
138                 return null;
139             }
140         };
141         JSONObject vnfObject = new JSONObject();
142         vnfObject.put("vnfInstanceName", "test123");
143         String vnfmId = "1234";
144         JSONObject resp = mgr.deleteVnf("vnfId", "vnfmId", vnfObject);
145         assertEquals(resp.get("retCode"), -1);
146     }
147
148     @Test
149     public void deleteVnfTestValidJson() {
150
151         new MockUp<VnfmUtil>() {
152
153             @Mock
154             public JSONObject getVnfmById(String vnfmId) {
155                 JSONObject obj = new JSONObject();
156                 obj.put("url", "http://localhost:8080");
157                 return obj;
158             }
159         };
160         new MockUp<VnfMgr>(){
161             @Mock
162             private JujuVnfmInfo findByVnfId(String vnfId){
163                 JujuVnfmInfo info = new JujuVnfmInfo();
164                 info.setVnfmId("1234");
165                 return info;
166             }
167         };
168         JSONObject vnfObject = new JSONObject();
169         vnfObject.put("vnfInstanceName", "test123");
170         String vnfmId = "1234";
171         JSONObject resp = mgr.deleteVnf("vnfId", "vnfmId", vnfObject);
172         assertEquals(resp.get("retCode"), -1);
173     }
174
175     @Test
176     public void deleteVnf2TestNormal() {
177
178         new MockUp<VnfmUtil>() {
179
180             @Mock
181             public JSONObject getVnfmById(String vnfmId) {
182                 JSONObject obj = new JSONObject();
183                 obj.put("url", "http://localhost:8080");
184                 return obj;
185             }
186         };
187         new MockUp<VnfMgr>(){
188             @Mock
189             private JujuVnfmInfo findByVnfId(String vnfId){
190                 JujuVnfmInfo info = new JujuVnfmInfo();
191                 info.setVnfmId("1234");
192                 return info;
193             }
194             @Mock
195             private void delJujuVnfmInfo(String vnfId){
196                 return;
197             }
198         };
199         new MockUp<VnfmRestfulUtil>(){
200             @Mock
201             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
202                 RestfulResponse resp = new RestfulResponse();
203                 resp.setStatus(204);
204                 return resp;
205             }
206         };
207         JSONObject vnfObject = new JSONObject();
208         vnfObject.put("vnfInstanceName", "test123");
209         String vnfmId = "1234";
210         JSONObject resp = mgr.deleteVnf("vnfId", "vnfmId", vnfObject);
211         assertEquals(resp.get("retCode"), 1);
212     }
213
214     @Test
215     public void getVnfTestNullResp() {
216         new MockUp<VnfmUtil>() {
217
218             @Mock
219             public JSONObject getVnfmById(String vnfmId) {
220                 return null;
221             }
222         };
223         JSONObject resp = mgr.getVnf("vnfId", "vnfmId");
224         assertEquals(resp.get("retCode"), -1);
225     }
226
227     @Test
228     public void getVnfTestValidJson() {
229         new MockUp<VnfmUtil>() {
230
231             @Mock
232             public JSONObject getVnfmById(String vnfmId) {
233                 JSONObject obj = new JSONObject();
234                 obj.put("url", "http://localhost:8080");
235                 return obj;
236             }
237         };
238         new MockUp<VnfMgr>(){
239             @Mock
240             private JujuVnfmInfo findByVnfId(String vnfId){
241                 JujuVnfmInfo info = new JujuVnfmInfo();
242                 info.setVnfmId("1234");
243                 return info;
244             }
245             @Mock
246             private void delJujuVnfmInfo(String vnfId){
247                 return;
248             }
249         };
250         new MockUp<VnfmRestfulUtil>(){
251             @Mock
252             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
253                 RestfulResponse resp = new RestfulResponse();
254                 resp.setStatus(201);
255                 JSONObject obj = new JSONObject();
256                 obj.put("data", new JSONObject());
257                 resp.setResponseJson(obj.toString());
258                 return resp;
259             }
260         };
261         JSONObject resp = mgr.getVnf("vnfId", "vnfmId");
262         assertEquals(resp.get("retCode"), -1);
263     }
264
265     @Test
266     public void getJobTestNullResp(){
267         new MockUp<VnfmUtil>() {
268             @Mock
269             public JSONObject getVnfmById(String vnfmId) {
270                 return null;
271             }
272         };
273         JSONObject resp = mgr.getJob("jobId", "vnfmId");
274         assertEquals(resp.get("retCode"), null);
275     }
276
277     @Test
278     public void getJobTestNullHttpResp(){
279         new MockUp<VnfmUtil>() {
280             @Mock
281             public JSONObject getVnfmById(String vnfmId) {
282                 JSONObject ret = new JSONObject();
283                 ret.put("url", "http://localhost:8080");
284                 return ret;
285             }
286         };
287         JSONObject resp = mgr.getJob("jobId", "vnfmId");
288         assertEquals(resp.get("retCode"), null);
289     }
290     @Test
291     public void getJobTestSuccessWithNullData(){
292         new MockUp<VnfmUtil>() {
293             @Mock
294             public JSONObject getVnfmById(String vnfmId) {
295                 JSONObject ret = new JSONObject();
296                 ret.put("url", "http://localhost:8080");
297                 return ret;
298             }
299         };
300         new MockUp<VnfmRestfulUtil>(){
301             @Mock
302             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
303                 RestfulResponse res = new RestfulResponse();
304                 res.setStatus(201);
305
306                 return res;
307             }
308         };
309         JSONObject resp = mgr.getJob("jobId", "vnfmId");
310         assertEquals(resp.get("retCode"), null);
311     }
312     @Test
313     public void getJobTestOkWithNullData(){
314         new MockUp<VnfmUtil>() {
315             @Mock
316             public JSONObject getVnfmById(String vnfmId) {
317                 JSONObject ret = new JSONObject();
318                 ret.put("url", "http://localhost:8080");
319                 return ret;
320             }
321         };
322         new MockUp<VnfmRestfulUtil>(){
323             @Mock
324             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
325                 RestfulResponse res = new RestfulResponse();
326                 res.setStatus(200);
327
328                 return res;
329             }
330         };
331         JSONObject resp = mgr.getJob("jobId", "vnfmId");
332         assertEquals(resp.get("retCode"), null);
333     }
334     @Test
335     public void getJobTestInternalError(){
336         new MockUp<VnfmUtil>() {
337             @Mock
338             public JSONObject getVnfmById(String vnfmId) {
339                 JSONObject ret = new JSONObject();
340                 ret.put("url", "http://localhost:8080");
341                 return ret;
342             }
343         };
344         new MockUp<VnfmRestfulUtil>(){
345             @Mock
346             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
347                 RestfulResponse res = new RestfulResponse();
348                 res.setStatus(500);
349
350                 return res;
351             }
352         };
353         JSONObject resp = mgr.getJob("jobId", "vnfmId");
354         assertEquals(resp.get("retCode"), null);
355     }
356     @Test
357     public void getJobTestNormal(){
358         new MockUp<VnfmUtil>() {
359             @Mock
360             public JSONObject getVnfmById(String vnfmId) {
361                 JSONObject ret = new JSONObject();
362                 ret.put("url", "http://localhost:8080");
363                 return ret;
364             }
365         };
366         new MockUp<VnfmRestfulUtil>(){
367             @Mock
368             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
369                 RestfulResponse res = new RestfulResponse();
370                 JSONObject jsonData = new JSONObject();
371                 jsonData.put("data", new JSONObject());
372                 res.setStatus(200);
373                 res.setResponseJson(jsonData.toString());
374                 return res;
375             }
376         };
377         JSONObject resp = mgr.getJob("jobId", "vnfmId");
378         assertEquals(resp.get("retCode"), null);
379     }
380     @Test
381     public void getJobTestNullData(){
382         new MockUp<VnfmUtil>() {
383             @Mock
384             public JSONObject getVnfmById(String vnfmId) {
385                 JSONObject ret = new JSONObject();
386                 ret.put("url", "http://localhost:8080");
387                 return ret;
388             }
389         };
390         new MockUp<VnfmRestfulUtil>(){
391             @Mock
392             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens) {
393                 RestfulResponse res = new RestfulResponse();
394                 JSONObject jsonData = new JSONObject();
395                 jsonData.put("data", null);
396                 res.setStatus(200);
397                 res.setResponseJson(jsonData.toString());
398                 return res;
399             }
400         };
401         JSONObject resp = mgr.getJob("jobId", "vnfmId");
402         assertEquals(resp.get("retCode"),null);
403     }
404
405 }