2 * Copyright 2017 Huawei Technologies Co., Ltd.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.nfvo.resmanagement.service.rest;
\r
19 import static org.junit.Assert.assertNotNull;
\r
21 import java.util.ArrayList;
\r
22 import java.util.List;
\r
23 import java.util.Map;
\r
25 import javax.servlet.http.HttpServletRequest;
\r
27 import org.junit.Before;
\r
28 import org.junit.Test;
\r
29 import org.openo.baseservice.remoteservice.exception.ServiceException;
\r
30 import org.openo.nfvo.resmanagement.common.util.request.RequestUtil;
\r
31 import org.openo.nfvo.resmanagement.service.base.openstack.impl.HostImpl;
\r
32 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Host;
\r
33 import org.openo.nfvo.resmanagement.service.entity.HostEntity;
\r
34 import org.springframework.mock.web.MockHttpServletRequest;
\r
37 import mockit.MockUp;
\r
38 import net.sf.json.JSONObject;
\r
46 * @version NFVO 0.5 Feb 9, 2017
\r
48 public class HostRoaTest {
\r
50 private HostRoa roa;
\r
55 public void setUp() {
\r
56 roa = new HostRoa();
\r
57 host = new HostImpl();
\r
62 public void testGetHosts() throws ServiceException {
\r
63 new MockUp<HostImpl>() {
\r
66 public List<HostEntity> getList(Map<String, Object> condition) throws ServiceException {
\r
67 return new ArrayList<HostEntity>();
\r
70 HttpServletRequest mock = new MockHttpServletRequest();
\r
71 JSONObject result = roa.getHosts(mock);
\r
72 assertNotNull(result);
\r
76 public void testGetHost() throws ServiceException {
\r
77 new MockUp<HostImpl>() {
\r
80 public List<HostEntity> getList(Map<String, Object> condition) throws ServiceException {
\r
81 return new ArrayList<HostEntity>();
\r
84 HttpServletRequest mock = new MockHttpServletRequest();
\r
85 JSONObject result = roa.getHost(mock, "id");
\r
86 assertNotNull(result);
\r
90 public void testAddHost() throws ServiceException {
\r
91 new MockUp<RequestUtil>() {
\r
94 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
95 return new JSONObject();
\r
98 new MockUp<HostImpl>() {
\r
101 public int add(JSONObject jsonObject) throws ServiceException {
\r
105 HttpServletRequest mock = new MockHttpServletRequest();
\r
106 JSONObject result = roa.addHost(mock);
\r
107 assertNotNull(result);
\r
111 public void testAddHostByException() throws ServiceException {
\r
112 new MockUp<RequestUtil>() {
\r
115 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
116 return new JSONObject();
\r
119 new MockUp<HostImpl>() {
\r
122 public int add(JSONObject jsonObject) throws ServiceException {
\r
123 throw new ServiceException();
\r
126 HttpServletRequest mock = new MockHttpServletRequest();
\r
127 JSONObject result = roa.addHost(mock);
\r
128 assertNotNull(result);
\r
132 public void testDeleteHost() throws ServiceException {
\r
133 new MockUp<HostImpl>() {
\r
136 public int delete(String id) throws ServiceException {
\r
140 HttpServletRequest mock = new MockHttpServletRequest();
\r
141 JSONObject result = roa.deleteHost(mock, "id");
\r
142 assertNotNull(result);
\r
146 public void testDeleteHostByException() throws ServiceException {
\r
147 new MockUp<HostImpl>() {
\r
150 public int delete(String id) throws ServiceException {
\r
151 throw new ServiceException();
\r
154 HttpServletRequest mock = new MockHttpServletRequest();
\r
155 JSONObject result = roa.deleteHost(mock, "id");
\r
156 assertNotNull(result);
\r
160 public void testUpdateHost() throws ServiceException {
\r
161 new MockUp<RequestUtil>() {
\r
164 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
165 return new JSONObject();
\r
168 new MockUp<HostImpl>() {
\r
171 public int update(JSONObject jsonObject) throws ServiceException {
\r
175 HttpServletRequest mock = new MockHttpServletRequest();
\r
176 JSONObject result = roa.updateHost(mock);
\r
177 assertNotNull(result);
\r
181 public void testUpdateHostByException() throws ServiceException {
\r
182 new MockUp<RequestUtil>() {
\r
185 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
186 return new JSONObject();
\r
189 new MockUp<HostImpl>() {
\r
192 public int update(JSONObject jsonObject) throws ServiceException {
\r
193 throw new ServiceException();
\r
196 HttpServletRequest mock = new MockHttpServletRequest();
\r
197 JSONObject result = roa.updateHost(mock);
\r
198 assertNotNull(result);
\r