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