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