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.res.service.base.openstack.impl;
19 import static org.junit.Assert.assertTrue;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.vfc.nfvo.res.service.base.openstack.impl.LocationImpl;
29 import org.onap.vfc.nfvo.res.service.base.openstack.impl.SitesImpl;
30 import org.onap.vfc.nfvo.res.service.business.impl.LocationBusinessImpl;
31 import org.onap.vfc.nfvo.res.service.business.impl.SitesBusinessImpl;
32 import org.onap.vfc.nfvo.res.service.dao.impl.LocationDaoImpl;
33 import org.onap.vfc.nfvo.res.service.entity.LocationEntity;
34 import org.onap.vfc.nfvo.res.service.entity.SitesEntity;
35 import org.openo.baseservice.remoteservice.exception.ServiceException;
39 import net.sf.json.JSONObject;
41 public class LocationImplTest {
43 private LocationImpl locationImpl;
46 public void setUp() throws ServiceException {
47 locationImpl = new LocationImpl();
48 LocationBusinessImpl locationBusinessImpl = new LocationBusinessImpl();
49 locationBusinessImpl.setLocationDao(new LocationDaoImpl());
50 SitesImpl sitesImpl = new SitesImpl();
51 sitesImpl.setSitesBusiness(new SitesBusinessImpl());
52 locationImpl.setSites(sitesImpl);
53 locationImpl.setLocationBusiness(locationBusinessImpl);
58 public void testLocationImpl() throws ServiceException {
59 new MockUp<LocationBusinessImpl>() {
62 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
63 List<LocationEntity> list = new ArrayList<>();
64 LocationEntity localEntity = new LocationEntity();
66 list.add(localEntity);
70 new MockUp<LocationDaoImpl>() {
73 public int addLocation(LocationEntity locationEntity) {
77 JSONObject jsonObject = new JSONObject();
78 jsonObject.put("id", "id");
79 jsonObject.put("country", "country");
80 jsonObject.put("location", "location");
81 jsonObject.put("latitude", "3");
82 jsonObject.put("longitude", "12");
83 jsonObject.put("description", "description");
84 assertTrue(locationImpl.add(jsonObject) == 1);
88 public void testLocationImplBranch() throws ServiceException {
89 new MockUp<LocationBusinessImpl>() {
92 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
93 List<LocationEntity> list = new ArrayList<>();
95 // list.add(localEntity);
99 new MockUp<LocationDaoImpl>() {
102 public int addLocation(LocationEntity locationEntity) {
106 JSONObject jsonObject = new JSONObject();
107 jsonObject.put("id", "");
108 jsonObject.put("country", "country");
109 jsonObject.put("location", "location");
110 jsonObject.put("latitude", "90");
111 jsonObject.put("longitude", "180");
112 jsonObject.put("description", "description");
113 assertTrue(locationImpl.add(jsonObject) == 1);
117 public void testLocationImplBranch1() throws ServiceException {
118 new MockUp<LocationBusinessImpl>() {
121 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
122 List<LocationEntity> list = new ArrayList<>();
123 LocationEntity localEntity = new LocationEntity();
125 list.add(localEntity);
129 new MockUp<LocationDaoImpl>() {
132 public int addLocation(LocationEntity locationEntity) {
136 JSONObject jsonObject = new JSONObject();
137 jsonObject.put("id", null);
138 jsonObject.put("country", "country");
139 jsonObject.put("location", "location");
140 jsonObject.put("latitude", "3");
141 jsonObject.put("longitude", "12");
142 jsonObject.put("description", "description");
143 assertTrue(locationImpl.add(jsonObject) == 1);
146 @Test(expected = ServiceException.class)
147 public void testLocationImplException() throws ServiceException {
148 new MockUp<LocationBusinessImpl>() {
151 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
152 List<LocationEntity> list = new ArrayList<>();
153 LocationEntity localEntity = new LocationEntity();
155 list.add(localEntity);
160 JSONObject jsonObject = new JSONObject();
161 jsonObject.put("id", "2");
162 jsonObject.put("country", "country");
163 jsonObject.put("location", "location");
164 jsonObject.put("latitude", "3");
165 jsonObject.put("longitude", "12");
166 jsonObject.put("description", "description");
167 locationImpl.add(jsonObject);
170 @Test(expected = ServiceException.class)
171 public void testLocationImplException1() throws ServiceException {
173 JSONObject jsonObject = new JSONObject();
174 jsonObject.put("id", null);
175 jsonObject.put("country", "");
176 jsonObject.put("location", "location");
177 jsonObject.put("latitude", "81");
178 jsonObject.put("longitude", "12");
179 jsonObject.put("description", "description");
180 locationImpl.add(jsonObject);
184 @Test(expected = ServiceException.class)
185 public void testLocationImplException2() throws ServiceException {
186 JSONObject jsonObject = new JSONObject();
187 jsonObject.put("id", null);
188 jsonObject.put("country", "country");
189 jsonObject.put("location", "");
190 jsonObject.put("latitude", "81");
191 jsonObject.put("longitude", "12");
192 jsonObject.put("description", "description");
193 locationImpl.add(jsonObject);
196 @Test(expected = ServiceException.class)
197 public void testLocationImplException3() throws ServiceException {
198 JSONObject jsonObject = new JSONObject();
199 jsonObject.put("id", null);
200 jsonObject.put("country", "country");
201 jsonObject.put("location", "location");
202 jsonObject.put("latitude", "");
203 jsonObject.put("longitude", "12");
204 jsonObject.put("description", "description");
205 locationImpl.add(jsonObject);
208 @Test(expected = ServiceException.class)
209 public void testLocationImplException4() throws ServiceException {
210 JSONObject jsonObject = new JSONObject();
211 jsonObject.put("id", null);
212 jsonObject.put("country", "country");
213 jsonObject.put("location", "location");
214 jsonObject.put("latitude", "latitude");
215 jsonObject.put("longitude", "");
216 jsonObject.put("description", "description");
217 locationImpl.add(jsonObject);
220 @Test(expected = ServiceException.class)
221 public void testLocationImplException5() throws ServiceException {
222 new MockUp<LocationBusinessImpl>() {
225 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
226 List<LocationEntity> list = new ArrayList<>();
227 LocationEntity localEntity = new LocationEntity();
229 list.add(localEntity);
233 JSONObject jsonObject = new JSONObject();
234 jsonObject.put("id", "id");
235 jsonObject.put("country", "country");
236 jsonObject.put("location", "location");
237 jsonObject.put("latitude", "95");
238 jsonObject.put("longitude", "185");
239 jsonObject.put("description", "description");
240 locationImpl.add(jsonObject);
243 @Test(expected = ServiceException.class)
244 public void testLocationImplException6() throws ServiceException {
245 new MockUp<LocationBusinessImpl>() {
248 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
249 List<LocationEntity> list = new ArrayList<>();
250 LocationEntity localEntity = new LocationEntity();
252 list.add(localEntity);
256 JSONObject jsonObject = new JSONObject();
257 jsonObject.put("id", "id");
258 jsonObject.put("country", "country");
259 jsonObject.put("location", "location");
260 jsonObject.put("latitude", "80");
261 jsonObject.put("longitude", "185");
262 jsonObject.put("description", "description");
263 locationImpl.add(jsonObject);
266 @Test(expected = ServiceException.class)
267 public void testLocationImplException7() throws ServiceException {
268 new MockUp<LocationBusinessImpl>() {
271 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
272 List<LocationEntity> list = new ArrayList<>();
273 LocationEntity localEntity = new LocationEntity();
275 list.add(localEntity);
279 JSONObject jsonObject = new JSONObject();
280 jsonObject.put("id", "id");
281 jsonObject.put("country", "country");
282 jsonObject.put("location", "location");
283 jsonObject.put("latitude", "95");
284 jsonObject.put("longitude", "175");
285 jsonObject.put("description", "description");
286 locationImpl.add(jsonObject);
290 public void testUpdate() throws ServiceException {
291 new MockUp<LocationDaoImpl>() {
294 public LocationEntity getLocation(String id) {
295 LocationEntity localEntity = new LocationEntity();
296 localEntity.setCountry("country");
297 localEntity.setLocation("location");
302 new MockUp<LocationDaoImpl>() {
305 public int updateLocationSelective(LocationEntity locationEntity) {
309 JSONObject jsonObject = new JSONObject();
310 jsonObject.put("id", "id");
311 jsonObject.put("country", "country");
312 jsonObject.put("location", "location");
313 jsonObject.put("latitude", "14");
314 jsonObject.put("longitude", "12");
315 jsonObject.put("description", "description");
316 locationImpl.update(jsonObject);
319 @Test(expected = ServiceException.class)
320 public void testUpdateException() throws ServiceException {
322 new MockUp<LocationDaoImpl>() {
325 public int updateLocationSelective(LocationEntity locationEntity) {
329 JSONObject jsonObject = new JSONObject();
330 jsonObject.put("id", "id");
331 jsonObject.put("country", "country");
332 jsonObject.put("location", "location");
333 jsonObject.put("latitude", "91");
334 jsonObject.put("longitude", "12");
335 jsonObject.put("description", "description");
336 locationImpl.update(jsonObject);
339 @Test(expected = ServiceException.class)
340 public void testUpdateException1() throws ServiceException {
341 new MockUp<LocationDaoImpl>() {
344 public LocationEntity getLocation(String id) {
345 LocationEntity localEntity = new LocationEntity();
346 localEntity.setCountry("countryNew");
347 localEntity.setLocation("location");
353 JSONObject jsonObject = new JSONObject();
354 jsonObject.put("id", "id");
355 jsonObject.put("country", "country");
356 jsonObject.put("location", "location");
357 jsonObject.put("latitude", "10");
358 jsonObject.put("longitude", "12");
359 jsonObject.put("description", "description");
360 locationImpl.update(jsonObject);
363 @Test(expected = ServiceException.class)
364 public void testUpdateException2() throws ServiceException {
365 new MockUp<LocationDaoImpl>() {
368 public LocationEntity getLocation(String id) {
369 LocationEntity localEntity = new LocationEntity();
370 localEntity.setCountry("country");
371 localEntity.setLocation("locationNew");
376 JSONObject jsonObject = new JSONObject();
377 jsonObject.put("id", "id");
378 jsonObject.put("country", "country");
379 jsonObject.put("location", "location");
380 jsonObject.put("latitude", "10");
381 jsonObject.put("longitude", "12");
382 jsonObject.put("description", "description");
383 locationImpl.update(jsonObject);
387 public void testDelete() throws ServiceException {
389 new MockUp<LocationDaoImpl>() {
392 public int deleteLocation(String id) {
396 assertTrue(locationImpl.delete("location") == 1);
399 @Test(expected = ServiceException.class)
400 public void testDeleteException() throws ServiceException {
402 locationImpl.delete("");
406 public void testComputeSite() {
407 JSONObject total = new JSONObject();
408 total.put("vcpus", "12");
409 total.put("memory", "23");
410 total.put("disk", "23");
411 JSONObject used = new JSONObject();
412 used.put("vcpus", "12");
413 used.put("memory", "23");
414 used.put("disk", "23");
415 locationImpl.getLocationBusiness();
417 assertTrue(locationImpl.computingSite(total, used) != null);
421 public void testGetLocation() throws ServiceException {
422 new MockUp<LocationDaoImpl>() {
425 public List<LocationEntity> getLocations(Map<String, Object> condition) {
426 List<LocationEntity> list = new ArrayList<>();
427 LocationEntity localEntity = new LocationEntity();
428 localEntity.setCountry("country");
429 localEntity.setLocation("locationNew");
430 list.add(localEntity);
434 assertTrue(locationImpl.getLocation(new HashMap<>()) != null);
438 public void testGetLocationBranch() throws ServiceException {
439 new MockUp<LocationDaoImpl>() {
442 public List<LocationEntity> getLocations(Map<String, Object> condition) {
443 List<LocationEntity> list = new ArrayList<>();
444 LocationEntity localEntity = new LocationEntity();
445 localEntity.setCountry("country");
446 localEntity.setLocation("locationNew");
450 locationImpl.getLocation(new HashMap<>());
454 public void testGetLocationInfo() throws ServiceException {
455 new MockUp<SitesBusinessImpl>() {
458 public List<SitesEntity> getSites(Map<String, Object> condition) {
459 List<SitesEntity> siteList = new ArrayList<>();
460 SitesEntity site = new SitesEntity();
467 new MockUp<JSONObject>() {
470 public JSONObject getJSONObject(String key) {
471 JSONObject total = new JSONObject();
472 total.put("vcpus", "12");
473 total.put("memory", "23");
474 total.put("disk", "23");
478 List<LocationEntity> locationInfo = new ArrayList<>();
479 LocationEntity localEntity = new LocationEntity();
480 locationInfo.add(localEntity);
481 locationImpl.getLocationInfo(locationInfo);
485 public void testGet() throws ServiceException {
486 new MockUp<LocationBusinessImpl>() {
489 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
490 List<LocationEntity> list = new ArrayList<>();
491 LocationEntity localEntity = new LocationEntity();
493 list.add(localEntity);
497 locationImpl.get(new HashMap<>());
498 assertTrue(locationImpl.get("id") != null);