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.onap.vfc.nfvo.vnfm.svnfm.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.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;
 
  34 import net.sf.json.JSONObject;
 
  42  * @version VFC 1.0 Jan 24, 2017
 
  44 public class VnfmAdapter2DriverManagerTest {
 
  46     VnfmAdapter2DriverManager manager = new VnfmAdapter2DriverManager();
 
  48     Map<String, String> vim = new HashMap<String, String>();
 
  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");
 
  62     public void registerDriverTestNullResp() {
 
  63         new MockUp<VNFRestfulUtil>() {
 
  66             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
  67                 RestfulResponse response = new RestfulResponse();
 
  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."));
 
  81     public void registerDriverCreateSuccess() {
 
  82         new MockUp<VNFRestfulUtil>() {
 
  85             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
  86                 RestfulResponse response = new RestfulResponse();
 
  87                 response.setStatus(201);
 
  89                 String vimStr = toJson(vim);
 
  90                 response.setResponseJson(vimStr);
 
  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);
 
 103     public void registerDriverOkSuccess() {
 
 104         new MockUp<VNFRestfulUtil>() {
 
 107             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 108                 RestfulResponse response = new RestfulResponse();
 
 109                 response.setStatus(200);
 
 111                 String vimStr = toJson(vim);
 
 112                 response.setResponseJson(vimStr);
 
 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);
 
 125     public void registerDriverTestInvalidParams() {
 
 126         new MockUp<VNFRestfulUtil>() {
 
 129             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 130                 RestfulResponse response = new RestfulResponse();
 
 131                 response.setStatus(415);
 
 133                 String vimStr = toJson(vim);
 
 134                 response.setResponseJson(vimStr);
 
 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."));
 
 147     public void registerDriverTestInternalError() {
 
 148         new MockUp<VNFRestfulUtil>() {
 
 151             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 152                 RestfulResponse response = new RestfulResponse();
 
 153                 response.setStatus(500);
 
 155                 String vimStr = toJson(vim);
 
 156                 response.setResponseJson(vimStr);
 
 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."));
 
 169     public void unregisterDriverTestNullResp() {
 
 170         new MockUp<VNFRestfulUtil>() {
 
 173             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 174                 RestfulResponse response = new RestfulResponse();
 
 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."));
 
 188     public void unregisterDriverDeleteSuccess() {
 
 189         new MockUp<VNFRestfulUtil>() {
 
 192             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 193                 RestfulResponse response = new RestfulResponse();
 
 194                 response.setStatus(204);
 
 196                 String vimStr = toJson(vim);
 
 197                 response.setResponseJson(vimStr);
 
 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);
 
 210     public void unregisterDriverResourceNotFound() {
 
 211         new MockUp<VNFRestfulUtil>() {
 
 214             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 215                 RestfulResponse response = new RestfulResponse();
 
 216                 response.setStatus(404);
 
 218                 String vimStr = toJson(vim);
 
 219                 response.setResponseJson(vimStr);
 
 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."));
 
 232     public void unregisterDriverTestInvalidParams() {
 
 233         new MockUp<VNFRestfulUtil>() {
 
 236             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 237                 RestfulResponse response = new RestfulResponse();
 
 238                 response.setStatus(415);
 
 240                 String vimStr = toJson(vim);
 
 241                 response.setResponseJson(vimStr);
 
 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."));
 
 254     public void unregisterDriverTestInternalError() {
 
 255         new MockUp<VNFRestfulUtil>() {
 
 258             public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
 
 259                 RestfulResponse response = new RestfulResponse();
 
 260                 response.setStatus(500);
 
 262                 String vimStr = toJson(vim);
 
 263                 response.setResponseJson(vimStr);
 
 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."));
 
 275     public static String toJson(Map o) {
 
 277             return JsonUtil.marshal(o);
 
 278         } catch(IOException e) {