Merge "Junit coverage increased Issue-ID: VFC-1726"
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / adapter / impl / VnfmAdapter2DriverManagerTest.java
1 /*
2  * Copyright 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.svnfm.vnfmadapter.service.adapter.impl;
18
19 import static org.junit.Assert.assertTrue;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VNFRestfulUtil;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.VnfmAdapter2DriverManager;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.testutils.JsonUtil;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 /**
37  * <br>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version VFC 1.0 Jan 24, 2017
43  */
44 public class VnfmAdapter2DriverManagerTest {
45
46     VnfmAdapter2DriverManager manager = new VnfmAdapter2DriverManager();
47
48     Map<String, String> vim = new HashMap<String, String>();
49
50     @Before
51     public void setUp() {
52         vim.put("vimId", "123");
53         vim.put("name", "123");
54         vim.put("url", "123");
55         vim.put("userName", "123");
56         vim.put("password", "123");
57         vim.put("type", "123");
58         vim.put("version", "123");
59     }
60
61     @Test
62     public void registerDriverTestNullResp() {
63 //        new MockUp<VNFRestfulUtil>() {
64 //
65 //            @Mock
66 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
67 //                RestfulResponse response = new RestfulResponse();
68 //
69 //                return null;
70 //            }
71 //        };
72         Map<String, String> paramsMap = new HashMap<>();
73         paramsMap.put("url", "/test/api");
74         paramsMap.put("path", "http://localhost:8080");
75         paramsMap.put("methodType", "get");
76         JSONObject obj = manager.registerDriver(paramsMap, new JSONObject());
77         assertTrue(obj.get("reason").equals("RestfulResponse is null."));
78     }
79
80     @Test
81     public void registerDriverCreateSuccess() {
82 //        new MockUp<VNFRestfulUtil>() {
83 //
84 //            @Mock
85 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
86 //                RestfulResponse response = new RestfulResponse();
87 //                response.setStatus(201);
88 //
89 //                String vimStr = toJson(vim);
90 //                response.setResponseJson(vimStr);
91 //                return response;
92 //            }
93 //        };
94         Map<String, String> paramsMap = new HashMap<>();
95         paramsMap.put("url", "/test/api");
96         paramsMap.put("path", "http://localhost:8080");
97         paramsMap.put("methodType", "get");
98         JSONObject obj = manager.registerDriver(paramsMap, new JSONObject());
99         assertTrue(Integer.valueOf(obj.get("retCode").toString()) == 201);
100     }
101
102     @Test
103     public void registerDriverOkSuccess() {
104 //        new MockUp<VNFRestfulUtil>() {
105 //
106 //            @Mock
107 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
108 //                RestfulResponse response = new RestfulResponse();
109 //                response.setStatus(200);
110 //
111 //                String vimStr = toJson(vim);
112 //                response.setResponseJson(vimStr);
113 //                return response;
114 //            }
115 //        };
116         Map<String, String> paramsMap = new HashMap<>();
117         paramsMap.put("url", "/test/api");
118         paramsMap.put("path", "http://localhost:8080");
119         paramsMap.put("methodType", "get");
120         JSONObject obj = manager.registerDriver(paramsMap, new JSONObject());
121         assertTrue(Integer.valueOf(obj.get("retCode").toString()) == -1);
122     }
123
124     @Test
125     public void registerDriverTestInvalidParams() {
126 //        new MockUp<VNFRestfulUtil>() {
127 //
128 //            @Mock
129 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
130 //                RestfulResponse response = new RestfulResponse();
131 //                response.setStatus(415);
132 //
133 //                String vimStr = toJson(vim);
134 //                response.setResponseJson(vimStr);
135 //                return response;
136 //            }
137 //        };
138         Map<String, String> paramsMap = new HashMap<>();
139         paramsMap.put("url", "/test/api");
140         paramsMap.put("path", "http://localhost:8080");
141         paramsMap.put("methodType", "get");
142         JSONObject obj = manager.registerDriver(paramsMap, new JSONObject());
143         assertTrue(obj.get("reason").equals("DriverManager return fail,invalid parameters."));
144     }
145
146     @Test
147     public void registerDriverTestInternalError() {
148 //        new MockUp<VNFRestfulUtil>() {
149 //
150 //            @Mock
151 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
152 //                RestfulResponse response = new RestfulResponse();
153 //                response.setStatus(500);
154 //
155 //                String vimStr = toJson(vim);
156 //                response.setResponseJson(vimStr);
157 //                return response;
158 //            }
159 //        };
160         Map<String, String> paramsMap = new HashMap<>();
161         paramsMap.put("url", "/test/api");
162         paramsMap.put("path", "http://localhost:8080");
163         paramsMap.put("methodType", "get");
164         JSONObject obj = manager.registerDriver(paramsMap, new JSONObject());
165         assertTrue(obj.get("reason").equals("DriverManager return fail,internal system error."));
166     }
167
168     @Test
169     public void unregisterDriverTestNullResp() {
170 //        new MockUp<VNFRestfulUtil>() {
171 //
172 //            @Mock
173 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
174 //                RestfulResponse response = new RestfulResponse();
175 //
176 //                return null;
177 //            }
178 //        };
179         Map<String, String> paramsMap = new HashMap<>();
180         paramsMap.put("url", "/test/api");
181         paramsMap.put("path", "http://localhost:8080");
182         paramsMap.put("methodType", "get");
183         JSONObject obj = manager.unregisterDriver(paramsMap);
184         assertTrue(obj.get("reason").equals("RestfulResponse is null."));
185     }
186
187     @Test
188     public void unregisterDriverDeleteSuccess() {
189 //        new MockUp<VNFRestfulUtil>() {
190 //
191 //            @Mock
192 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
193 //                RestfulResponse response = new RestfulResponse();
194 //                response.setStatus(204);
195 //
196 //                String vimStr = toJson(vim);
197 //                response.setResponseJson(vimStr);
198 //                return response;
199 //            }
200 //        };
201         Map<String, String> paramsMap = new HashMap<>();
202         paramsMap.put("url", "/test/api");
203         paramsMap.put("path", "http://localhost:8080");
204         paramsMap.put("methodType", "get");
205         JSONObject obj = manager.unregisterDriver(paramsMap);
206         assertTrue(Integer.valueOf(obj.get("retCode").toString()) == 204);
207     }
208
209     @Test
210     public void unregisterDriverResourceNotFound() {
211 //        new MockUp<VNFRestfulUtil>() {
212 //
213 //            @Mock
214 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
215 //                RestfulResponse response = new RestfulResponse();
216 //                response.setStatus(404);
217 //
218 //                String vimStr = toJson(vim);
219 //                response.setResponseJson(vimStr);
220 //                return response;
221 //            }
222 //        };
223         Map<String, String> paramsMap = new HashMap<>();
224         paramsMap.put("url", "/test/api");
225         paramsMap.put("path", "http://localhost:8080");
226         paramsMap.put("methodType", "get");
227         JSONObject obj = manager.unregisterDriver(paramsMap);
228         assertTrue(obj.get("reason").equals("DriverManager return fail,can't find the service instance."));
229     }
230
231     @Test
232     public void unregisterDriverTestInvalidParams() {
233 //        new MockUp<VNFRestfulUtil>() {
234 //
235 //            @Mock
236 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
237 //                RestfulResponse response = new RestfulResponse();
238 //                response.setStatus(415);
239 //
240 //                String vimStr = toJson(vim);
241 //                response.setResponseJson(vimStr);
242 //                return response;
243 //            }
244 //        };
245         Map<String, String> paramsMap = new HashMap<>();
246         paramsMap.put("url", "/test/api");
247         paramsMap.put("path", "http://localhost:8080");
248         paramsMap.put("methodType", "get");
249         JSONObject obj = manager.unregisterDriver(paramsMap);
250         assertTrue(obj.get("reason").equals("DriverManager return fail,invalid parameters."));
251     }
252
253     @Test
254     public void unregisterDriverTestInternalError() {
255 //        new MockUp<VNFRestfulUtil>() {
256 //
257 //            @Mock
258 //            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
259 //                RestfulResponse response = new RestfulResponse();
260 //                response.setStatus(500);
261 //
262 //                String vimStr = toJson(vim);
263 //                response.setResponseJson(vimStr);
264 //                return response;
265 //            }
266 //        };
267         Map<String, String> paramsMap = new HashMap<>();
268         paramsMap.put("url", "/test/api");
269         paramsMap.put("path", "http://localhost:8080");
270         paramsMap.put("methodType", "get");
271         JSONObject obj = manager.unregisterDriver(paramsMap);
272         assertTrue(obj.get("reason").equals("DriverManager return fail,internal system error."));
273     }
274
275     public static String toJson(Map o) {
276         try {
277             return JsonUtil.marshal(o);
278         } catch(IOException e) {
279             return "";
280         }
281     }
282 }