Add test case for VnfRoa.java
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / service / rest / VnfRoaTest.java
1 /*
2  * Copyright 2016 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.rest;
18
19 import static org.junit.Assert.assertNotNull;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.StringUtil;
27 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.restclient.ServiceException;
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.springframework.mock.web.MockHttpServletResponse;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 public class VnfRoaTest {
37
38     VnfRoa roa = new VnfRoa();
39
40     @Before
41     public void setUp() {
42         roa.setVnfMgr(new VnfMgr());
43     }
44
45     @Test
46     public void addVnfTestNull() throws ServiceException {
47         new MockUp<StringUtil>() {
48
49             @Mock
50             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
51                 return null;
52             }
53         };
54         HttpServletRequest context = null;
55         HttpServletResponse resp = new MockHttpServletResponse();
56         String vnfmId = "1234";
57         String res = roa.addVnf(context, resp, vnfmId);
58         assertNotNull(res);
59     }
60
61     @Test
62     public void addVnfTest() throws ServiceException {
63         new MockUp<StringUtil>() {
64
65             @Mock
66             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
67                 String vnfJsonStr = "{}";
68                 return (T)JSONObject.fromObject(vnfJsonStr);
69             }
70         };
71         new MockUp<VnfMgr>() {
72
73             @Mock
74             public JSONObject addVnf(JSONObject vnfObject, String vnfmId) {
75                 JSONObject result = new JSONObject();
76                 result.put("retCode", -1);
77                 return result;
78             }
79         };
80         HttpServletRequest context = null;
81         HttpServletResponse resp = new MockHttpServletResponse();
82         String vnfmId = "1234";
83         String res = roa.addVnf(context, resp, vnfmId);
84         assertNotNull(res);
85     }
86
87     @Test
88     public void addVnfTestSucce() throws ServiceException {
89         new MockUp<StringUtil>() {
90
91             @Mock
92             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
93                 String vnfJsonStr = "{}";
94                 return (T)JSONObject.fromObject(vnfJsonStr);
95             }
96         };
97         new MockUp<VnfMgr>() {
98
99             @Mock
100             public JSONObject addVnf(JSONObject vnfObject, String vnfmId) {
101                 JSONObject result = new JSONObject();
102                 result.put("retCode", 1);
103                 JSONObject data = new JSONObject();
104                 data.put("result", "success");
105                 result.put("data", data);
106                 return result;
107             }
108         };
109         HttpServletRequest context = null;
110         HttpServletResponse resp = new MockHttpServletResponse();
111         String vnfmId = "1234";
112         String res = roa.addVnf(context, resp, vnfmId);
113         assertNotNull(res);
114     }
115
116     @Test
117     public void delVnfTestNull() throws ServiceException {
118         new MockUp<StringUtil>() {
119
120             @Mock
121             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
122                 return null;
123             }
124         };
125         new MockUp<VnfMgr>() {
126
127             @Mock
128             private JujuVnfmInfo findByVnfId(String vnfId) {
129                 JujuVnfmInfo info = new JujuVnfmInfo();
130                 info.setVnfmId("1234");
131                 return info;
132             }
133         };
134         HttpServletRequest context = null;
135         HttpServletResponse resp = new MockHttpServletResponse();
136         String vnfmId = "1234";
137         String res = roa.delVnf("vnfmId", resp, "vnfInstanceId", context);
138         assertNotNull(res);
139     }
140
141     @Test
142     public void delVnf2TestNull() throws ServiceException {
143         new MockUp<StringUtil>() {
144
145             @Mock
146             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
147                 return null;
148             }
149         };
150         HttpServletRequest context = null;
151         HttpServletResponse resp = new MockHttpServletResponse();
152         String vnfmId = "1234";
153         String res = roa.delVnf(null, resp, "vnfInstanceId", context);
154         assertNotNull(res);
155     }
156
157     @Test
158     public void delVnf3TestNull() throws ServiceException {
159         new MockUp<StringUtil>() {
160
161             @Mock
162             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
163                 return null;
164             }
165         };
166         HttpServletRequest context = null;
167         HttpServletResponse resp = new MockHttpServletResponse();
168         String vnfmId = "1234";
169         String res = roa.delVnf(vnfmId, resp, null, context);
170         assertNotNull(res);
171     }
172
173     @Test
174     public void delVnfSuccess() throws ServiceException {
175         new MockUp<StringUtil>() {
176
177             @Mock
178             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
179                 return null;
180             }
181         };
182         new MockUp<VnfMgr>() {
183
184             @Mock
185             public JSONObject deleteVnf(String vnfId, String vnfmId, JSONObject vnfObject) {
186                 JSONObject result = new JSONObject();
187                 result.put("retCode", 1);
188                 JSONObject data = new JSONObject();
189                 data.put("result", "success");
190                 result.put("data", data);
191                 return result;
192             }
193         };
194         HttpServletRequest context = null;
195         HttpServletResponse resp = new MockHttpServletResponse();
196         String vnfmId = "1234";
197         String res = roa.delVnf(vnfmId, resp, "vnfInstanceId", context);
198         assertNotNull(res);
199     }
200
201     @Test
202     public void delVnfFail() throws ServiceException {
203         new MockUp<StringUtil>() {
204
205             @Mock
206             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
207                 return null;
208             }
209         };
210         new MockUp<VnfMgr>() {
211
212             @Mock
213             public JSONObject deleteVnf(String vnfId, String vnfmId, JSONObject vnfObject) {
214                 JSONObject result = new JSONObject();
215                 result.put("retCode", -1);
216                 return result;
217             }
218         };
219         HttpServletRequest context = null;
220         HttpServletResponse resp = new MockHttpServletResponse();
221         String vnfmId = "1234";
222         String res = roa.delVnf(null, resp, "vnfInstanceId", context);
223         assertNotNull(res);
224     }
225
226     @Test
227     public void getVnf1TestNull() throws ServiceException {
228         new MockUp<StringUtil>() {
229
230             @Mock
231             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
232                 return null;
233             }
234         };
235         new MockUp<VnfMgr>() {
236
237             @Mock
238             private JujuVnfmInfo findByVnfId(String vnfId) {
239                 JujuVnfmInfo info = new JujuVnfmInfo();
240                 info.setVnfmId("1234");
241                 return info;
242             }
243         };
244         HttpServletRequest context = null;
245         HttpServletResponse resp = new MockHttpServletResponse();
246         String vnfmId = "1234";
247         String res = roa.getVnf(vnfmId, resp, "vnfInstanceId", context);
248         assertNotNull(res);
249     }
250
251     @Test
252     public void getVnfSuccess() throws ServiceException {
253         new MockUp<StringUtil>() {
254
255             @Mock
256             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
257                 return null;
258             }
259         };
260         new MockUp<VnfMgr>() {
261
262             @Mock
263             public JSONObject getVnf(String vnfId, String vnfmId) {
264                 JSONObject result = new JSONObject();
265                 result.put("retCode", 1);
266                 JSONObject data = new JSONObject();
267                 data.put("result", "success");
268                 result.put("data", data);
269                 return result;
270             }
271         };
272         HttpServletRequest context = null;
273         HttpServletResponse resp = new MockHttpServletResponse();
274         String vnfmId = "1234";
275         String res = roa.getVnf(vnfmId, resp, "vnfInstanceId", context);
276         assertNotNull(res);
277     }
278
279     @Test
280     public void getVnfFail() throws ServiceException {
281         new MockUp<StringUtil>() {
282
283             @Mock
284             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
285                 return null;
286             }
287         };
288         new MockUp<VnfMgr>() {
289
290             @Mock
291             public JSONObject getVnf(String vnfId, String vnfmId) {
292                 JSONObject result = new JSONObject();
293                 result.put("retCode", -1);
294                 return result;
295             }
296         };
297         HttpServletRequest context = null;
298         HttpServletResponse resp = new MockHttpServletResponse();
299         String vnfmId = "1234";
300         String res = roa.getVnf(vnfmId, resp, "vnfInstanceId", context);
301         assertNotNull(res);
302     }
303
304     @Test
305     public void getVnf2TestNull() throws ServiceException {
306         new MockUp<StringUtil>() {
307
308             @Mock
309             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
310                 return null;
311             }
312         };
313         HttpServletRequest context = null;
314         HttpServletResponse resp = new MockHttpServletResponse();
315         String vnfmId = "1234";
316         String res = roa.getVnf(null, resp, "vnfInstanceId", context);
317         assertNotNull(res);
318     }
319
320     @Test
321     public void getVnf3TestNull() throws ServiceException {
322         new MockUp<StringUtil>() {
323
324             @Mock
325             public <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
326                 return null;
327             }
328         };
329         HttpServletRequest context = null;
330         HttpServletResponse resp = new MockHttpServletResponse();
331         String vnfmId = "1234";
332         String res = roa.getVnf(vnfmId, resp, null, context);
333         assertNotNull(res);
334     }
335
336     @Test
337     public void getJobTestNull() throws ServiceException {
338         HttpServletResponse resp = new MockHttpServletResponse();
339         String res = roa.getJob("jobId", "vnfmId", resp, "responseId");
340         assertNotNull(res);
341     }
342
343     @Test
344     public void getJobTest2Null() throws ServiceException {
345         HttpServletResponse resp = new MockHttpServletResponse();
346         String res = roa.getJob(null, "vnfmId", resp, "responseId");
347         assertNotNull(res);
348     }
349
350     @Test
351     public void getJobTest3Null() throws ServiceException {
352         HttpServletResponse resp = new MockHttpServletResponse();
353         String res = roa.getJob("jobId", null, resp, "responseId");
354         assertNotNull(res);
355     }
356
357     @Test
358     public void getJobTestNormal() throws ServiceException {
359         new MockUp<VnfMgr>() {
360
361             @Mock
362             public JSONObject getJob(String jobId, String vnfmId) {
363                 JSONObject obj = new JSONObject();
364                 JSONObject dataObj = new JSONObject();
365                 dataObj.put("id", "1234");
366                 dataObj.put("status", "Success");
367                 obj.put("data", dataObj);
368                 obj.put("retCode", 1);
369                 return obj;
370             }
371
372         };
373         HttpServletResponse resp = new MockHttpServletResponse();
374         String res = roa.getJob("jobId", "vnfmId", resp, "responseId");
375         assertNotNull(res);
376     }
377
378 }