2 * Copyright 2016-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.resmanagement.service.rest;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
22 import java.util.ArrayList;
23 import java.util.List;
26 import javax.servlet.http.HttpServletRequest;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.openo.baseservice.remoteservice.exception.ServiceException;
31 import org.openo.nfvo.resmanagement.common.VimUtil;
32 import org.openo.nfvo.resmanagement.common.util.request.RequestUtil;
33 import org.openo.nfvo.resmanagement.service.base.openstack.impl.SitesImpl;
34 import org.openo.nfvo.resmanagement.service.entity.SitesEntity;
35 import org.springframework.mock.web.MockHttpServletRequest;
39 import net.sf.json.JSONArray;
40 import net.sf.json.JSONObject;
48 * @version NFVO 0.5 2016年8月16日
50 public class SitesRoaTest {
52 private SitesRoa sitesRoa;
56 sitesRoa = new SitesRoa();
57 sitesRoa.setSites(new SitesImpl());
61 public void testGetSites() throws ServiceException {
62 new MockUp<SitesImpl>() {
65 public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
66 return new ArrayList<SitesEntity>();
69 HttpServletRequest mock = new MockHttpServletRequest();
70 JSONObject result = sitesRoa.getSites(mock);
71 assertNotNull(result);
75 public void testGetSite() throws ServiceException {
76 new MockUp<SitesImpl>() {
79 public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
80 return new ArrayList<SitesEntity>();
83 HttpServletRequest mock = new MockHttpServletRequest();
84 JSONObject result = sitesRoa.getSite(mock, "id");
85 assertNotNull(result);
89 public void testAddSites() throws ServiceException {
90 new MockUp<RequestUtil>() {
93 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
94 return new JSONObject();
97 new MockUp<SitesImpl>() {
100 public int add(JSONObject jsonObject) throws ServiceException {
105 public void sendToMonitor(JSONObject jsonObject) throws ServiceException {
109 HttpServletRequest mock = new MockHttpServletRequest();
110 JSONObject result = sitesRoa.addSites(mock);
111 assertNotNull(result);
115 public void testAddSitesExceptions() throws ServiceException {
116 new MockUp<RequestUtil>() {
119 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
120 return new JSONObject();
123 new MockUp<SitesImpl>() {
126 public int add(JSONObject jsonObject) throws ServiceException {
127 throw new ServiceException();
130 JSONObject result = sitesRoa.addSites(null);
131 JSONObject expectedResult = new JSONObject();
132 expectedResult.put("msg", "");
133 assertEquals(expectedResult.toString(), result.toString());
137 public void testDeleteSites() throws ServiceException {
138 new MockUp<SitesImpl>() {
141 public int delete(String id) throws ServiceException {
145 JSONObject result = sitesRoa.deleteSites(null, "123");
146 JSONObject expectedResult = new JSONObject();
147 expectedResult.put("msg", "org.openo.nfvo.resmanage.common.del.success");
148 assertEquals(expectedResult.toString(), result.toString());
152 public void testDeleteSitesExceptions() throws ServiceException {
153 new MockUp<SitesImpl>() {
156 public int delete(String id) throws ServiceException {
157 throw new ServiceException();
160 JSONObject result = sitesRoa.deleteSites(null, "123");
161 JSONObject expectedResult = new JSONObject();
162 expectedResult.put("msg", "");
163 assertEquals(expectedResult.toString(), result.toString());
167 public void testUpdateSites() throws ServiceException {
168 new MockUp<RequestUtil>() {
171 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
172 return new JSONObject();
175 new MockUp<SitesImpl>() {
178 public int update(SitesEntity sitesEntity) throws ServiceException {
182 JSONObject result = sitesRoa.updateSites(null);
183 JSONObject expectedResult = new JSONObject();
184 expectedResult.put("msg", "org.openo.nfvo.resmanage.common.update.success");
185 assertEquals(expectedResult.toString(), result.toString());
189 public void testUpdateISitesExceptions() throws ServiceException {
190 new MockUp<RequestUtil>() {
193 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
194 return new JSONObject();
197 new MockUp<SitesImpl>() {
200 public int update(SitesEntity sitesEntity) throws ServiceException {
201 throw new ServiceException();
204 JSONObject result = sitesRoa.updateSites(null);
205 JSONObject expectedResult = new JSONObject();
206 expectedResult.put("msg", "");
207 assertEquals(expectedResult.toString(), result.toString());
211 public void testGrantResource() throws ServiceException {
212 new MockUp<RequestUtil>() {
215 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
216 return new JSONObject();
219 new MockUp<SitesImpl>() {
222 public int update(JSONObject jsonObject) throws ServiceException {
226 HttpServletRequest mock = new MockHttpServletRequest();
227 JSONObject result = sitesRoa.grantResource(mock);
228 assertNotNull(result);
232 public void testGrantResourceByException() throws ServiceException {
233 new MockUp<RequestUtil>() {
236 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
237 return new JSONObject();
240 new MockUp<SitesImpl>() {
243 public int update(JSONObject jsonObject) throws ServiceException {
244 throw new ServiceException();
247 HttpServletRequest mock = new MockHttpServletRequest();
248 JSONObject result = sitesRoa.grantResource(mock);
249 assertNotNull(result);
253 public void testGetVims() throws ServiceException {
254 new MockUp<VimUtil>() {
257 public JSONArray getVims() {
258 return new JSONArray();
261 HttpServletRequest mock = new MockHttpServletRequest();
262 String result = sitesRoa.getVims(mock);
263 assertNotNull(result);
267 public void testGetVim() throws ServiceException {
268 new MockUp<VimUtil>() {
271 public JSONObject getVimById(String vimId) {
272 return new JSONObject();
275 HttpServletRequest mock = new MockHttpServletRequest();
276 String result = sitesRoa.getVim(mock, "id");
277 assertNotNull(result);