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.onap.vfc.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.onap.vfc.nfvo.resmanagement.common.VimUtil;
31 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
32 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.impl.SitesImpl;
33 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
34 import org.onap.vfc.nfvo.resmanagement.service.rest.SitesRoa;
35 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
36 import org.springframework.mock.web.MockHttpServletRequest;
40 import net.sf.json.JSONArray;
41 import net.sf.json.JSONObject;
49 * @version NFVO 0.5 2016年8月16日
51 public class SitesRoaTest {
53 private SitesRoa sitesRoa;
57 sitesRoa = new SitesRoa();
58 sitesRoa.setSites(new SitesImpl());
62 public void testGetSites() throws ServiceException {
63 new MockUp<SitesImpl>() {
66 public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
67 return new ArrayList<SitesEntity>();
70 HttpServletRequest mock = new MockHttpServletRequest();
71 JSONObject result = sitesRoa.getSites(mock);
72 assertNotNull(result);
76 public void testGetSite() throws ServiceException {
77 new MockUp<SitesImpl>() {
80 public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
81 return new ArrayList<SitesEntity>();
84 HttpServletRequest mock = new MockHttpServletRequest();
85 JSONObject result = sitesRoa.getSite(mock, "id");
86 assertNotNull(result);
90 public void testAddSites() throws ServiceException {
91 new MockUp<RequestUtil>() {
94 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
95 return new JSONObject();
98 new MockUp<SitesImpl>() {
101 public int add(JSONObject jsonObject) throws ServiceException {
106 public void sendToMonitor(JSONObject jsonObject) throws ServiceException {
110 HttpServletRequest mock = new MockHttpServletRequest();
111 JSONObject result = sitesRoa.addSites(mock);
112 assertNotNull(result);
116 public void testAddSitesExceptions() throws ServiceException {
117 new MockUp<RequestUtil>() {
120 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
121 return new JSONObject();
124 new MockUp<SitesImpl>() {
127 public int add(JSONObject jsonObject) throws ServiceException {
128 throw new ServiceException();
131 JSONObject result = sitesRoa.addSites(null);
132 JSONObject expectedResult = new JSONObject();
133 expectedResult.put("msg", "");
134 assertEquals(expectedResult.toString(), result.toString());
138 public void testDeleteSites() throws ServiceException {
139 new MockUp<SitesImpl>() {
142 public int delete(String id) throws ServiceException {
146 JSONObject result = sitesRoa.deleteSites(null, "123");
147 JSONObject expectedResult = new JSONObject();
148 expectedResult.put("msg", "org.openo.nfvo.resmanage.common.del.success");
149 assertEquals(expectedResult.toString(), result.toString());
153 public void testDeleteSitesExceptions() throws ServiceException {
154 new MockUp<SitesImpl>() {
157 public int delete(String id) throws ServiceException {
158 throw new ServiceException();
161 JSONObject result = sitesRoa.deleteSites(null, "123");
162 JSONObject expectedResult = new JSONObject();
163 expectedResult.put("msg", "");
164 assertEquals(expectedResult.toString(), result.toString());
168 public void testUpdateSites() throws ServiceException {
169 new MockUp<RequestUtil>() {
172 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
173 return new JSONObject();
176 new MockUp<SitesImpl>() {
179 public int update(SitesEntity sitesEntity) throws ServiceException {
183 JSONObject result = sitesRoa.updateSites(null);
184 JSONObject expectedResult = new JSONObject();
185 expectedResult.put("msg", "org.openo.nfvo.resmanage.common.update.success");
186 assertEquals(expectedResult.toString(), result.toString());
190 public void testUpdateISitesExceptions() throws ServiceException {
191 new MockUp<RequestUtil>() {
194 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
195 return new JSONObject();
198 new MockUp<SitesImpl>() {
201 public int update(SitesEntity sitesEntity) throws ServiceException {
202 throw new ServiceException();
205 JSONObject result = sitesRoa.updateSites(null);
206 JSONObject expectedResult = new JSONObject();
207 expectedResult.put("msg", "");
208 assertEquals(expectedResult.toString(), result.toString());
212 public void testGrantResource() throws ServiceException {
213 new MockUp<RequestUtil>() {
216 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
217 return new JSONObject();
220 new MockUp<SitesImpl>() {
223 public int update(JSONObject jsonObject) throws ServiceException {
227 HttpServletRequest mock = new MockHttpServletRequest();
228 JSONObject result = sitesRoa.grantResource(mock);
229 assertNotNull(result);
233 public void testGrantResourceByException() throws ServiceException {
234 new MockUp<RequestUtil>() {
237 public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
238 return new JSONObject();
241 new MockUp<SitesImpl>() {
244 public int update(JSONObject jsonObject) throws ServiceException {
245 throw new ServiceException();
248 HttpServletRequest mock = new MockHttpServletRequest();
249 JSONObject result = sitesRoa.grantResource(mock);
250 assertNotNull(result);
254 public void testGetVims() throws ServiceException {
255 new MockUp<VimUtil>() {
258 public JSONArray getVims() {
259 return new JSONArray();
262 HttpServletRequest mock = new MockHttpServletRequest();
263 String result = sitesRoa.getVims(mock);
264 assertNotNull(result);
268 public void testGetVim() throws ServiceException {
269 new MockUp<VimUtil>() {
272 public JSONObject getVimById(String vimId) {
273 return new JSONObject();
276 HttpServletRequest mock = new MockHttpServletRequest();
277 String result = sitesRoa.getVim(mock, "id");
278 assertNotNull(result);