74dfe994accea38cbd6ce86f9119348b823547b6
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.nfvo.resmanagement.service.base.openstack.impl;
18
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
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;
34
35 import mockit.Mock;
36 import mockit.MockUp;
37 import net.sf.json.JSONObject;
38
39 public class LocationImplTest {
40
41     private LocationImpl locationImpl;
42
43     @Before
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);
52
53     }
54
55     @Test
56     public void testLocationImpl() throws ServiceException {
57         new MockUp<LocationBusinessImpl>() {
58
59             @Mock
60             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
61                 List<LocationEntity> list = new ArrayList<>();
62                 LocationEntity localEntity = new LocationEntity();
63
64                 list.add(localEntity);
65                 return null;
66             }
67         };
68         new MockUp<LocationDaoImpl>() {
69
70             @Mock
71             public int addLocation(LocationEntity locationEntity) {
72                 return 1;
73             }
74         };
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);
83     }
84
85     @Test
86     public void testLocationImplBranch() throws ServiceException {
87         new MockUp<LocationBusinessImpl>() {
88
89             @Mock
90             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
91                 List<LocationEntity> list = new ArrayList<>();
92
93                 // list.add(localEntity);
94                 return list;
95             }
96         };
97         new MockUp<LocationDaoImpl>() {
98
99             @Mock
100             public int addLocation(LocationEntity locationEntity) {
101                 return 1;
102             }
103         };
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);
112     }
113
114     @Test
115     public void testLocationImplBranch1() throws ServiceException {
116         new MockUp<LocationBusinessImpl>() {
117
118             @Mock
119             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
120                 List<LocationEntity> list = new ArrayList<>();
121                 LocationEntity localEntity = new LocationEntity();
122
123                 list.add(localEntity);
124                 return null;
125             }
126         };
127         new MockUp<LocationDaoImpl>() {
128
129             @Mock
130             public int addLocation(LocationEntity locationEntity) {
131                 return 1;
132             }
133         };
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);
142     }
143
144     @Test(expected = ServiceException.class)
145     public void testLocationImplException() throws ServiceException {
146         new MockUp<LocationBusinessImpl>() {
147
148             @Mock
149             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
150                 List<LocationEntity> list = new ArrayList<>();
151                 LocationEntity localEntity = new LocationEntity();
152
153                 list.add(localEntity);
154                 return list;
155             }
156         };
157
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);
166     }
167
168     @Test(expected = ServiceException.class)
169     public void testLocationImplException1() throws ServiceException {
170
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);
179
180     }
181
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);
192     }
193
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);
204     }
205
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);
216     }
217
218     @Test(expected = ServiceException.class)
219     public void testLocationImplException5() throws ServiceException {
220         new MockUp<LocationBusinessImpl>() {
221
222             @Mock
223             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
224                 List<LocationEntity> list = new ArrayList<>();
225                 LocationEntity localEntity = new LocationEntity();
226
227                 list.add(localEntity);
228                 return null;
229             }
230         };
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);
239     }
240
241     @Test(expected = ServiceException.class)
242     public void testLocationImplException6() throws ServiceException {
243         new MockUp<LocationBusinessImpl>() {
244
245             @Mock
246             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
247                 List<LocationEntity> list = new ArrayList<>();
248                 LocationEntity localEntity = new LocationEntity();
249
250                 list.add(localEntity);
251                 return null;
252             }
253         };
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);
262     }
263
264     @Test(expected = ServiceException.class)
265     public void testLocationImplException7() throws ServiceException {
266         new MockUp<LocationBusinessImpl>() {
267
268             @Mock
269             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
270                 List<LocationEntity> list = new ArrayList<>();
271                 LocationEntity localEntity = new LocationEntity();
272
273                 list.add(localEntity);
274                 return null;
275             }
276         };
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);
285     }
286
287     @Test
288     public void testUpdate() throws ServiceException {
289         new MockUp<LocationDaoImpl>() {
290
291             @Mock
292             public LocationEntity getLocation(String id) {
293                 LocationEntity localEntity = new LocationEntity();
294                 localEntity.setCountry("country");
295                 localEntity.setLocation("location");
296
297                 return localEntity;
298             }
299         };
300         new MockUp<LocationDaoImpl>() {
301
302             @Mock
303             public int updateLocationSelective(LocationEntity locationEntity) {
304                 return 1;
305             }
306         };
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);
315     }
316
317     @Test(expected = ServiceException.class)
318     public void testUpdateException() throws ServiceException {
319
320         new MockUp<LocationDaoImpl>() {
321
322             @Mock
323             public int updateLocationSelective(LocationEntity locationEntity) {
324                 return 1;
325             }
326         };
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);
335     }
336
337     @Test(expected = ServiceException.class)
338     public void testUpdateException1() throws ServiceException {
339         new MockUp<LocationDaoImpl>() {
340
341             @Mock
342             public LocationEntity getLocation(String id) {
343                 LocationEntity localEntity = new LocationEntity();
344                 localEntity.setCountry("countryNew");
345                 localEntity.setLocation("location");
346
347                 return localEntity;
348             }
349         };
350
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);
359     }
360
361     @Test(expected = ServiceException.class)
362     public void testUpdateException2() throws ServiceException {
363         new MockUp<LocationDaoImpl>() {
364
365             @Mock
366             public LocationEntity getLocation(String id) {
367                 LocationEntity localEntity = new LocationEntity();
368                 localEntity.setCountry("country");
369                 localEntity.setLocation("locationNew");
370
371                 return localEntity;
372             }
373         };
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);
382     }
383
384     @Test
385     public void testDelete() throws ServiceException {
386
387         new MockUp<LocationDaoImpl>() {
388
389             @Mock
390             public int deleteLocation(String id) {
391                 return 1;
392             }
393         };
394         assertTrue(locationImpl.delete("location") == 1);
395     }
396
397     @Test(expected = ServiceException.class)
398     public void testDeleteException() throws ServiceException {
399
400         locationImpl.delete("");
401     }
402
403     @Test
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();
414
415         assertTrue(locationImpl.computingSite(total, used) != null);
416     }
417
418     @Test
419     public void testGetLocation() throws ServiceException {
420         new MockUp<LocationDaoImpl>() {
421
422             @Mock
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);
429                 return list;
430             }
431         };
432         assertTrue(locationImpl.getLocation(new HashMap<>()) != null);
433     }
434
435     @Test
436     public void testGetLocationBranch() throws ServiceException {
437         new MockUp<LocationDaoImpl>() {
438
439             @Mock
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");
445                 return list;
446             }
447         };
448         locationImpl.getLocation(new HashMap<>());
449     }
450
451     @Test
452     public void testGetLocationInfo() throws ServiceException {
453         new MockUp<SitesBusinessImpl>() {
454
455             @Mock
456             public List<SitesEntity> getSites(Map<String, Object> condition) {
457                 List<SitesEntity> siteList = new ArrayList<>();
458                 SitesEntity site = new SitesEntity();
459
460                 siteList.add(site);
461                 return siteList;
462             }
463         };
464
465         new MockUp<JSONObject>() {
466
467             @Mock
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");
473                 return total;
474             }
475         };
476         List<LocationEntity> locationInfo = new ArrayList<>();
477         LocationEntity localEntity = new LocationEntity();
478         locationInfo.add(localEntity);
479         locationImpl.getLocationInfo(locationInfo);
480     }
481
482     @Test
483     public void testGet() throws ServiceException {
484         new MockUp<LocationBusinessImpl>() {
485
486             @Mock
487             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
488                 List<LocationEntity> list = new ArrayList<>();
489                 LocationEntity localEntity = new LocationEntity();
490
491                 list.add(localEntity);
492                 return null;
493             }
494         };
495         locationImpl.get(new HashMap<>());
496         assertTrue(locationImpl.get("id") != null);
497     }
498
499 }