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.onap.vfc.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.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
\r
30 import org.onap.vfc.nfvo.resmanagement.service.entity.VmEntity;
\r
31 import org.onap.vfc.nfvo.resmanagement.service.group.impl.VmServiceImpl;
\r
32 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VmService;
\r
33 import org.onap.vfc.nfvo.resmanagement.service.rest.VmRoa;
\r
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
\r
35 import org.springframework.mock.web.MockHttpServletRequest;
\r
38 import mockit.MockUp;
\r
39 import net.sf.json.JSONObject;
\r
47 * @version NFVO 0.5 Feb 9, 2017
\r
49 public class VmRoaTest {
\r
53 private VmService vmService;
\r
56 public void setUp() {
\r
58 vmService = new VmServiceImpl();
\r
59 roa.setVmService(vmService);
\r
63 public void testGetVms() throws ServiceException {
\r
64 new MockUp<VmServiceImpl>() {
\r
67 public List<VmEntity> getList(Map<String, Object> map) throws ServiceException {
\r
68 return new ArrayList<VmEntity>();
\r
71 HttpServletRequest mock = new MockHttpServletRequest();
\r
72 JSONObject result = roa.getVms(mock);
\r
73 assertNotNull(result);
\r
77 public void testGetVm() throws ServiceException {
\r
78 new MockUp<VmServiceImpl>() {
\r
81 public List<VmEntity> getList(Map<String, Object> map) throws ServiceException {
\r
82 return new ArrayList<VmEntity>();
\r
85 HttpServletRequest mock = new MockHttpServletRequest();
\r
86 JSONObject result = roa.getVm(mock, "id");
\r
87 assertNotNull(result);
\r
91 public void testAddVm() throws ServiceException {
\r
92 new MockUp<RequestUtil>() {
\r
95 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
96 return new JSONObject();
\r
99 new MockUp<VmEntity>() {
\r
102 public VmEntity toEntity(JSONObject jsonObject) {
\r
103 return new VmEntity();
\r
106 new MockUp<VmServiceImpl>() {
\r
109 public JSONObject addVm(VmEntity vmEntity) throws ServiceException {
\r
110 return new JSONObject();
\r
113 HttpServletRequest mock = new MockHttpServletRequest();
\r
114 JSONObject result = roa.addVm(mock);
\r
115 assertNotNull(result);
\r
119 public void testAddVmByException() throws ServiceException {
\r
120 new MockUp<RequestUtil>() {
\r
123 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
124 return new JSONObject();
\r
127 new MockUp<VmEntity>() {
\r
130 public VmEntity toEntity(JSONObject jsonObject) {
\r
131 return new VmEntity();
\r
134 new MockUp<VmServiceImpl>() {
\r
137 public JSONObject addVm(VmEntity vmEntity) throws ServiceException {
\r
138 throw new ServiceException();
\r
141 HttpServletRequest mock = new MockHttpServletRequest();
\r
142 JSONObject result = roa.addVm(mock);
\r
143 assertNotNull(result);
\r
146 @Test(expected = ServiceException.class)
\r
147 public void testAddVmByNull() throws ServiceException {
\r
148 new MockUp<RequestUtil>() {
\r
151 public JSONObject getJsonRequestBody(HttpServletRequest context) {
\r
155 HttpServletRequest mock = new MockHttpServletRequest();
\r
160 public void testDeleteVm() throws ServiceException {
\r
161 new MockUp<VmServiceImpl>() {
\r
164 public int delete(String id) throws ServiceException {
\r
168 HttpServletRequest mock = new MockHttpServletRequest();
\r
169 JSONObject result = roa.deleteVm(mock, "id");
\r
170 assertNotNull(result);
\r
174 public void testDeleteVmByException() throws ServiceException {
\r
175 new MockUp<VmServiceImpl>() {
\r
178 public int delete(String id) throws ServiceException {
\r
179 throw new ServiceException();
\r
182 HttpServletRequest mock = new MockHttpServletRequest();
\r
183 JSONObject result = roa.deleteVm(mock, "id");
\r
184 assertNotNull(result);
\r
187 @Test(expected = ServiceException.class)
\r
188 public void testDeleteVmByNull() throws ServiceException {
\r
189 HttpServletRequest mock = new MockHttpServletRequest();
\r
190 JSONObject result = roa.deleteVm(mock, null);
\r
191 assertNotNull(result);
\r