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.PortImpl;
\r
32 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Port;
\r
33 import org.openo.nfvo.resmanagement.service.entity.PortEntity;
\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 PortRoaTest {
\r
50 private PortRoa roa;
\r
55 public void setUp() {
\r
56 roa = new PortRoa();
\r
57 port = new PortImpl();
\r
62 public void testGetPorts() throws ServiceException {
\r
63 new MockUp<PortImpl>() {
\r
66 public List<PortEntity> getList(Map<String, Object> condition) throws ServiceException {
\r
67 return new ArrayList<PortEntity>();
\r
70 HttpServletRequest mock = new MockHttpServletRequest();
\r
71 JSONObject result = roa.getPorts(mock);
\r
72 assertNotNull(result);
\r
76 public void testGetPort() throws ServiceException {
\r
77 new MockUp<PortImpl>() {
\r
80 public List<PortEntity> getList(Map<String, Object> condition) throws ServiceException {
\r
81 return new ArrayList<PortEntity>();
\r
84 HttpServletRequest mock = new MockHttpServletRequest();
\r
85 JSONObject result = roa.getPort(mock, "id");
\r
86 assertNotNull(result);
\r
90 public void testAddPort() throws ServiceException {
\r
91 new MockUp<RequestUtil>() {
\r
94 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
95 return new JSONObject();
\r
98 new MockUp<PortEntity>() {
\r
101 public PortEntity toEntity(JSONObject jsonObject) {
\r
102 return new PortEntity();
\r
105 new MockUp<PortImpl>() {
\r
108 public int add(PortEntity portEntity) throws ServiceException {
\r
112 HttpServletRequest mock = new MockHttpServletRequest();
\r
113 JSONObject result = roa.addPort(mock);
\r
114 assertNotNull(result);
\r
118 public void testAddPortByException() throws ServiceException {
\r
119 new MockUp<RequestUtil>() {
\r
122 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
123 return new JSONObject();
\r
126 new MockUp<PortEntity>() {
\r
129 public PortEntity toEntity(JSONObject jsonObject) {
\r
130 return new PortEntity();
\r
133 new MockUp<PortImpl>() {
\r
136 public int add(PortEntity portEntity) throws ServiceException {
\r
137 throw new ServiceException();
\r
140 HttpServletRequest mock = new MockHttpServletRequest();
\r
141 JSONObject result = roa.addPort(mock);
\r
142 assertNotNull(result);
\r
146 public void testDeletePort() throws ServiceException {
\r
147 new MockUp<PortImpl>() {
\r
150 public int delete(String id) throws ServiceException {
\r
154 HttpServletRequest mock = new MockHttpServletRequest();
\r
155 JSONObject result = roa.deletePort(mock, "id");
\r
156 assertNotNull(result);
\r
160 public void testDeletePortByException() throws ServiceException {
\r
161 new MockUp<PortImpl>() {
\r
164 public int delete(String id) throws ServiceException {
\r
165 throw new ServiceException();
\r
168 HttpServletRequest mock = new MockHttpServletRequest();
\r
169 JSONObject result = roa.deletePort(mock, "id");
\r
170 assertNotNull(result);
\r
174 public void testUpdatePort() throws ServiceException {
\r
175 new MockUp<RequestUtil>() {
\r
178 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
179 return new JSONObject();
\r
182 new MockUp<PortImpl>() {
\r
185 public int update(JSONObject jsonObject) throws ServiceException {
\r
189 HttpServletRequest mock = new MockHttpServletRequest();
\r
190 JSONObject result = roa.updatePort(mock);
\r
191 assertNotNull(result);
\r
195 public void testUpdatePortByException() throws ServiceException {
\r
196 new MockUp<RequestUtil>() {
\r
199 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
200 return new JSONObject();
\r
203 new MockUp<PortImpl>() {
\r
206 public int update(JSONObject jsonObject) throws ServiceException {
\r
207 throw new ServiceException();
\r
210 HttpServletRequest mock = new MockHttpServletRequest();
\r
211 JSONObject result = roa.updatePort(mock);
\r
212 assertNotNull(result);
\r