2 * Copyright 2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.vnfmadapter.service.adapter.impl;
19 import static org.junit.Assert.assertTrue;
21 import java.io.IOException;
22 import java.util.HashMap;
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;
33 import net.sf.json.JSONObject;
41 * @version NFVO 0.5 Jan 24, 2017
43 public class VnfmAdapter2DriverManagerTest {
45 VnfmAdapter2DriverManager manager = new VnfmAdapter2DriverManager();
47 Map<String, String> vim = new HashMap<String, String>();
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");
61 public void registerDriverTestNullResp() {
62 new MockUp<VNFRestfulUtil>() {
65 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
66 RestfulResponse response = new RestfulResponse();
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."));
80 public void registerDriverCreateSuccess() {
81 new MockUp<VNFRestfulUtil>() {
84 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
85 RestfulResponse response = new RestfulResponse();
86 response.setStatus(201);
88 String vimStr = toJson(vim);
89 response.setResponseJson(vimStr);
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);
102 public void registerDriverOkSuccess() {
103 new MockUp<VNFRestfulUtil>() {
106 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
107 RestfulResponse response = new RestfulResponse();
108 response.setStatus(200);
110 String vimStr = toJson(vim);
111 response.setResponseJson(vimStr);
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);
124 public void registerDriverTestInvalidParams() {
125 new MockUp<VNFRestfulUtil>() {
128 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
129 RestfulResponse response = new RestfulResponse();
130 response.setStatus(415);
132 String vimStr = toJson(vim);
133 response.setResponseJson(vimStr);
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."));
146 public void registerDriverTestInternalError() {
147 new MockUp<VNFRestfulUtil>() {
150 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
151 RestfulResponse response = new RestfulResponse();
152 response.setStatus(500);
154 String vimStr = toJson(vim);
155 response.setResponseJson(vimStr);
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."));
168 public void unregisterDriverTestNullResp() {
169 new MockUp<VNFRestfulUtil>() {
172 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
173 RestfulResponse response = new RestfulResponse();
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."));
187 public void unregisterDriverDeleteSuccess() {
188 new MockUp<VNFRestfulUtil>() {
191 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
192 RestfulResponse response = new RestfulResponse();
193 response.setStatus(204);
195 String vimStr = toJson(vim);
196 response.setResponseJson(vimStr);
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);
209 public void unregisterDriverResourceNotFound() {
210 new MockUp<VNFRestfulUtil>() {
213 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
214 RestfulResponse response = new RestfulResponse();
215 response.setStatus(404);
217 String vimStr = toJson(vim);
218 response.setResponseJson(vimStr);
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."));
231 public void unregisterDriverTestInvalidParams() {
232 new MockUp<VNFRestfulUtil>() {
235 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
236 RestfulResponse response = new RestfulResponse();
237 response.setStatus(415);
239 String vimStr = toJson(vim);
240 response.setResponseJson(vimStr);
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."));
253 public void unregisterDriverTestInternalError() {
254 new MockUp<VNFRestfulUtil>() {
257 public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
258 RestfulResponse response = new RestfulResponse();
259 response.setStatus(500);
261 String vimStr = toJson(vim);
262 response.setResponseJson(vimStr);
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."));
274 public static String toJson(Map o) {
276 return JsonUtil.marshal(o);
277 } catch(IOException e) {