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.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.openo.baseservice.remoteservice.exception.ServiceException;
29 import org.openo.nfvo.resmanagement.service.business.impl.LocationBusinessImpl;
30 import org.openo.nfvo.resmanagement.service.business.impl.SitesBusinessImpl;
31 import org.openo.nfvo.resmanagement.service.dao.impl.LocationDaoImpl;
32 import org.openo.nfvo.resmanagement.service.entity.LocationEntity;
33 import org.openo.nfvo.resmanagement.service.entity.SitesEntity;
37 import net.sf.json.JSONObject;
39 public class LocationImplTest {
41 private LocationImpl locationImpl;
44 public void setUp() throws ServiceException {
45 locationImpl = new LocationImpl();
46 LocationBusinessImpl locationBusinessImpl = new LocationBusinessImpl();
47 locationBusinessImpl.setLocationDao(new LocationDaoImpl());
48 SitesImpl sitesImpl = new SitesImpl();
49 sitesImpl.setSitesBusiness(new SitesBusinessImpl());
50 locationImpl.setSites(sitesImpl);
51 locationImpl.setLocationBusiness(locationBusinessImpl);
56 public void testLocationImpl() throws ServiceException {
57 new MockUp<LocationBusinessImpl>() {
60 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
61 List<LocationEntity> list = new ArrayList<>();
62 LocationEntity localEntity = new LocationEntity();
64 list.add(localEntity);
68 new MockUp<LocationDaoImpl>() {
71 public int addLocation(LocationEntity locationEntity) {
75 JSONObject jsonObject = new JSONObject();
76 jsonObject.put("id", "id");
77 jsonObject.put("country", "country");
78 jsonObject.put("location", "location");
79 jsonObject.put("latitude", "3");
80 jsonObject.put("longitude", "12");
81 jsonObject.put("description", "description");
82 assertTrue(locationImpl.add(jsonObject) == 1);
86 public void testLocationImplBranch() throws ServiceException {
87 new MockUp<LocationBusinessImpl>() {
90 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
91 List<LocationEntity> list = new ArrayList<>();
93 // list.add(localEntity);
97 new MockUp<LocationDaoImpl>() {
100 public int addLocation(LocationEntity locationEntity) {
104 JSONObject jsonObject = new JSONObject();
105 jsonObject.put("id", "");
106 jsonObject.put("country", "country");
107 jsonObject.put("location", "location");
108 jsonObject.put("latitude", "90");
109 jsonObject.put("longitude", "180");
110 jsonObject.put("description", "description");
111 assertTrue(locationImpl.add(jsonObject) == 1);
115 public void testLocationImplBranch1() throws ServiceException {
116 new MockUp<LocationBusinessImpl>() {
119 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
120 List<LocationEntity> list = new ArrayList<>();
121 LocationEntity localEntity = new LocationEntity();
123 list.add(localEntity);
127 new MockUp<LocationDaoImpl>() {
130 public int addLocation(LocationEntity locationEntity) {
134 JSONObject jsonObject = new JSONObject();
135 jsonObject.put("id", null);
136 jsonObject.put("country", "country");
137 jsonObject.put("location", "location");
138 jsonObject.put("latitude", "3");
139 jsonObject.put("longitude", "12");
140 jsonObject.put("description", "description");
141 assertTrue(locationImpl.add(jsonObject) == 1);
144 @Test(expected = ServiceException.class)
145 public void testLocationImplException() throws ServiceException {
146 new MockUp<LocationBusinessImpl>() {
149 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
150 List<LocationEntity> list = new ArrayList<>();
151 LocationEntity localEntity = new LocationEntity();
153 list.add(localEntity);
158 JSONObject jsonObject = new JSONObject();
159 jsonObject.put("id", "2");
160 jsonObject.put("country", "country");
161 jsonObject.put("location", "location");
162 jsonObject.put("latitude", "3");
163 jsonObject.put("longitude", "12");
164 jsonObject.put("description", "description");
165 locationImpl.add(jsonObject);
168 @Test(expected = ServiceException.class)
169 public void testLocationImplException1() throws ServiceException {
171 JSONObject jsonObject = new JSONObject();
172 jsonObject.put("id", null);
173 jsonObject.put("country", "");
174 jsonObject.put("location", "location");
175 jsonObject.put("latitude", "81");
176 jsonObject.put("longitude", "12");
177 jsonObject.put("description", "description");
178 locationImpl.add(jsonObject);
182 @Test(expected = ServiceException.class)
183 public void testLocationImplException2() throws ServiceException {
184 JSONObject jsonObject = new JSONObject();
185 jsonObject.put("id", null);
186 jsonObject.put("country", "country");
187 jsonObject.put("location", "");
188 jsonObject.put("latitude", "81");
189 jsonObject.put("longitude", "12");
190 jsonObject.put("description", "description");
191 locationImpl.add(jsonObject);
194 @Test(expected = ServiceException.class)
195 public void testLocationImplException3() throws ServiceException {
196 JSONObject jsonObject = new JSONObject();
197 jsonObject.put("id", null);
198 jsonObject.put("country", "country");
199 jsonObject.put("location", "location");
200 jsonObject.put("latitude", "");
201 jsonObject.put("longitude", "12");
202 jsonObject.put("description", "description");
203 locationImpl.add(jsonObject);
206 @Test(expected = ServiceException.class)
207 public void testLocationImplException4() throws ServiceException {
208 JSONObject jsonObject = new JSONObject();
209 jsonObject.put("id", null);
210 jsonObject.put("country", "country");
211 jsonObject.put("location", "location");
212 jsonObject.put("latitude", "latitude");
213 jsonObject.put("longitude", "");
214 jsonObject.put("description", "description");
215 locationImpl.add(jsonObject);
218 @Test(expected = ServiceException.class)
219 public void testLocationImplException5() throws ServiceException {
220 new MockUp<LocationBusinessImpl>() {
223 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
224 List<LocationEntity> list = new ArrayList<>();
225 LocationEntity localEntity = new LocationEntity();
227 list.add(localEntity);
231 JSONObject jsonObject = new JSONObject();
232 jsonObject.put("id", "id");
233 jsonObject.put("country", "country");
234 jsonObject.put("location", "location");
235 jsonObject.put("latitude", "95");
236 jsonObject.put("longitude", "185");
237 jsonObject.put("description", "description");
238 locationImpl.add(jsonObject);
241 @Test(expected = ServiceException.class)
242 public void testLocationImplException6() throws ServiceException {
243 new MockUp<LocationBusinessImpl>() {
246 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
247 List<LocationEntity> list = new ArrayList<>();
248 LocationEntity localEntity = new LocationEntity();
250 list.add(localEntity);
254 JSONObject jsonObject = new JSONObject();
255 jsonObject.put("id", "id");
256 jsonObject.put("country", "country");
257 jsonObject.put("location", "location");
258 jsonObject.put("latitude", "80");
259 jsonObject.put("longitude", "185");
260 jsonObject.put("description", "description");
261 locationImpl.add(jsonObject);
264 @Test(expected = ServiceException.class)
265 public void testLocationImplException7() throws ServiceException {
266 new MockUp<LocationBusinessImpl>() {
269 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
270 List<LocationEntity> list = new ArrayList<>();
271 LocationEntity localEntity = new LocationEntity();
273 list.add(localEntity);
277 JSONObject jsonObject = new JSONObject();
278 jsonObject.put("id", "id");
279 jsonObject.put("country", "country");
280 jsonObject.put("location", "location");
281 jsonObject.put("latitude", "95");
282 jsonObject.put("longitude", "175");
283 jsonObject.put("description", "description");
284 locationImpl.add(jsonObject);
288 public void testUpdate() throws ServiceException {
289 new MockUp<LocationDaoImpl>() {
292 public LocationEntity getLocation(String id) {
293 LocationEntity localEntity = new LocationEntity();
294 localEntity.setCountry("country");
295 localEntity.setLocation("location");
300 new MockUp<LocationDaoImpl>() {
303 public int updateLocationSelective(LocationEntity locationEntity) {
307 JSONObject jsonObject = new JSONObject();
308 jsonObject.put("id", "id");
309 jsonObject.put("country", "country");
310 jsonObject.put("location", "location");
311 jsonObject.put("latitude", "14");
312 jsonObject.put("longitude", "12");
313 jsonObject.put("description", "description");
314 locationImpl.update(jsonObject);
317 @Test(expected = ServiceException.class)
318 public void testUpdateException() throws ServiceException {
320 new MockUp<LocationDaoImpl>() {
323 public int updateLocationSelective(LocationEntity locationEntity) {
327 JSONObject jsonObject = new JSONObject();
328 jsonObject.put("id", "id");
329 jsonObject.put("country", "country");
330 jsonObject.put("location", "location");
331 jsonObject.put("latitude", "91");
332 jsonObject.put("longitude", "12");
333 jsonObject.put("description", "description");
334 locationImpl.update(jsonObject);
337 @Test(expected = ServiceException.class)
338 public void testUpdateException1() throws ServiceException {
339 new MockUp<LocationDaoImpl>() {
342 public LocationEntity getLocation(String id) {
343 LocationEntity localEntity = new LocationEntity();
344 localEntity.setCountry("countryNew");
345 localEntity.setLocation("location");
351 JSONObject jsonObject = new JSONObject();
352 jsonObject.put("id", "id");
353 jsonObject.put("country", "country");
354 jsonObject.put("location", "location");
355 jsonObject.put("latitude", "10");
356 jsonObject.put("longitude", "12");
357 jsonObject.put("description", "description");
358 locationImpl.update(jsonObject);
361 @Test(expected = ServiceException.class)
362 public void testUpdateException2() throws ServiceException {
363 new MockUp<LocationDaoImpl>() {
366 public LocationEntity getLocation(String id) {
367 LocationEntity localEntity = new LocationEntity();
368 localEntity.setCountry("country");
369 localEntity.setLocation("locationNew");
374 JSONObject jsonObject = new JSONObject();
375 jsonObject.put("id", "id");
376 jsonObject.put("country", "country");
377 jsonObject.put("location", "location");
378 jsonObject.put("latitude", "10");
379 jsonObject.put("longitude", "12");
380 jsonObject.put("description", "description");
381 locationImpl.update(jsonObject);
385 public void testDelete() throws ServiceException {
387 new MockUp<LocationDaoImpl>() {
390 public int deleteLocation(String id) {
394 assertTrue(locationImpl.delete("location") == 1);
397 @Test(expected = ServiceException.class)
398 public void testDeleteException() throws ServiceException {
400 locationImpl.delete("");
404 public void testComputeSite() {
405 JSONObject total = new JSONObject();
406 total.put("vcpus", "12");
407 total.put("memory", "23");
408 total.put("disk", "23");
409 JSONObject used = new JSONObject();
410 used.put("vcpus", "12");
411 used.put("memory", "23");
412 used.put("disk", "23");
413 locationImpl.getLocationBusiness();
415 assertTrue(locationImpl.computingSite(total, used) != null);
419 public void testGetLocation() throws ServiceException {
420 new MockUp<LocationDaoImpl>() {
423 public List<LocationEntity> getLocations(Map<String, Object> condition) {
424 List<LocationEntity> list = new ArrayList<>();
425 LocationEntity localEntity = new LocationEntity();
426 localEntity.setCountry("country");
427 localEntity.setLocation("locationNew");
428 list.add(localEntity);
432 assertTrue(locationImpl.getLocation(new HashMap<>()) != null);
436 public void testGetLocationBranch() throws ServiceException {
437 new MockUp<LocationDaoImpl>() {
440 public List<LocationEntity> getLocations(Map<String, Object> condition) {
441 List<LocationEntity> list = new ArrayList<>();
442 LocationEntity localEntity = new LocationEntity();
443 localEntity.setCountry("country");
444 localEntity.setLocation("locationNew");
448 locationImpl.getLocation(new HashMap<>());
452 public void testGetLocationInfo() throws ServiceException {
453 new MockUp<SitesBusinessImpl>() {
456 public List<SitesEntity> getSites(Map<String, Object> condition) {
457 List<SitesEntity> siteList = new ArrayList<>();
458 SitesEntity site = new SitesEntity();
465 new MockUp<JSONObject>() {
468 public JSONObject getJSONObject(String key) {
469 JSONObject total = new JSONObject();
470 total.put("vcpus", "12");
471 total.put("memory", "23");
472 total.put("disk", "23");
476 List<LocationEntity> locationInfo = new ArrayList<>();
477 LocationEntity localEntity = new LocationEntity();
478 locationInfo.add(localEntity);
479 locationImpl.getLocationInfo(locationInfo);
483 public void testGet() throws ServiceException {
484 new MockUp<LocationBusinessImpl>() {
487 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
488 List<LocationEntity> list = new ArrayList<>();
489 LocationEntity localEntity = new LocationEntity();
491 list.add(localEntity);
495 locationImpl.get(new HashMap<>());
496 assertTrue(locationImpl.get("id") != null);