4bf8763322aa55b7e2e6cd0e02eae287d21492b2
[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.onap.vfc.nfvo.res.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.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;
36
37 import mockit.Mock;
38 import mockit.MockUp;
39 import net.sf.json.JSONObject;
40
41 public class LocationImplTest {
42
43     private LocationImpl locationImpl;
44
45     @Before
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);
54
55     }
56
57     @Test
58     public void testLocationImpl() throws ServiceException {
59         new MockUp<LocationBusinessImpl>() {
60
61             @Mock
62             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
63                 List<LocationEntity> list = new ArrayList<>();
64                 LocationEntity localEntity = new LocationEntity();
65
66                 list.add(localEntity);
67                 return null;
68             }
69         };
70         new MockUp<LocationDaoImpl>() {
71
72             @Mock
73             public int addLocation(LocationEntity locationEntity) {
74                 return 1;
75             }
76         };
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);
85     }
86
87     @Test
88     public void testLocationImplBranch() throws ServiceException {
89         new MockUp<LocationBusinessImpl>() {
90
91             @Mock
92             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
93                 List<LocationEntity> list = new ArrayList<>();
94
95                 // list.add(localEntity);
96                 return list;
97             }
98         };
99         new MockUp<LocationDaoImpl>() {
100
101             @Mock
102             public int addLocation(LocationEntity locationEntity) {
103                 return 1;
104             }
105         };
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);
114     }
115
116     @Test
117     public void testLocationImplBranch1() throws ServiceException {
118         new MockUp<LocationBusinessImpl>() {
119
120             @Mock
121             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
122                 List<LocationEntity> list = new ArrayList<>();
123                 LocationEntity localEntity = new LocationEntity();
124
125                 list.add(localEntity);
126                 return null;
127             }
128         };
129         new MockUp<LocationDaoImpl>() {
130
131             @Mock
132             public int addLocation(LocationEntity locationEntity) {
133                 return 1;
134             }
135         };
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);
144     }
145
146     @Test(expected = ServiceException.class)
147     public void testLocationImplException() throws ServiceException {
148         new MockUp<LocationBusinessImpl>() {
149
150             @Mock
151             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
152                 List<LocationEntity> list = new ArrayList<>();
153                 LocationEntity localEntity = new LocationEntity();
154
155                 list.add(localEntity);
156                 return list;
157             }
158         };
159
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);
168     }
169
170     @Test(expected = ServiceException.class)
171     public void testLocationImplException1() throws ServiceException {
172
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);
181
182     }
183
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);
194     }
195
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);
206     }
207
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);
218     }
219
220     @Test(expected = ServiceException.class)
221     public void testLocationImplException5() throws ServiceException {
222         new MockUp<LocationBusinessImpl>() {
223
224             @Mock
225             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
226                 List<LocationEntity> list = new ArrayList<>();
227                 LocationEntity localEntity = new LocationEntity();
228
229                 list.add(localEntity);
230                 return null;
231             }
232         };
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);
241     }
242
243     @Test(expected = ServiceException.class)
244     public void testLocationImplException6() throws ServiceException {
245         new MockUp<LocationBusinessImpl>() {
246
247             @Mock
248             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
249                 List<LocationEntity> list = new ArrayList<>();
250                 LocationEntity localEntity = new LocationEntity();
251
252                 list.add(localEntity);
253                 return null;
254             }
255         };
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);
264     }
265
266     @Test(expected = ServiceException.class)
267     public void testLocationImplException7() throws ServiceException {
268         new MockUp<LocationBusinessImpl>() {
269
270             @Mock
271             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
272                 List<LocationEntity> list = new ArrayList<>();
273                 LocationEntity localEntity = new LocationEntity();
274
275                 list.add(localEntity);
276                 return null;
277             }
278         };
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);
287     }
288
289     @Test
290     public void testUpdate() throws ServiceException {
291         new MockUp<LocationDaoImpl>() {
292
293             @Mock
294             public LocationEntity getLocation(String id) {
295                 LocationEntity localEntity = new LocationEntity();
296                 localEntity.setCountry("country");
297                 localEntity.setLocation("location");
298
299                 return localEntity;
300             }
301         };
302         new MockUp<LocationDaoImpl>() {
303
304             @Mock
305             public int updateLocationSelective(LocationEntity locationEntity) {
306                 return 1;
307             }
308         };
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);
317     }
318
319     @Test(expected = ServiceException.class)
320     public void testUpdateException() throws ServiceException {
321
322         new MockUp<LocationDaoImpl>() {
323
324             @Mock
325             public int updateLocationSelective(LocationEntity locationEntity) {
326                 return 1;
327             }
328         };
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);
337     }
338
339     @Test(expected = ServiceException.class)
340     public void testUpdateException1() throws ServiceException {
341         new MockUp<LocationDaoImpl>() {
342
343             @Mock
344             public LocationEntity getLocation(String id) {
345                 LocationEntity localEntity = new LocationEntity();
346                 localEntity.setCountry("countryNew");
347                 localEntity.setLocation("location");
348
349                 return localEntity;
350             }
351         };
352
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);
361     }
362
363     @Test(expected = ServiceException.class)
364     public void testUpdateException2() throws ServiceException {
365         new MockUp<LocationDaoImpl>() {
366
367             @Mock
368             public LocationEntity getLocation(String id) {
369                 LocationEntity localEntity = new LocationEntity();
370                 localEntity.setCountry("country");
371                 localEntity.setLocation("locationNew");
372
373                 return localEntity;
374             }
375         };
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);
384     }
385
386     @Test
387     public void testDelete() throws ServiceException {
388
389         new MockUp<LocationDaoImpl>() {
390
391             @Mock
392             public int deleteLocation(String id) {
393                 return 1;
394             }
395         };
396         assertTrue(locationImpl.delete("location") == 1);
397     }
398
399     @Test(expected = ServiceException.class)
400     public void testDeleteException() throws ServiceException {
401
402         locationImpl.delete("");
403     }
404
405     @Test
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();
416
417         assertTrue(locationImpl.computingSite(total, used) != null);
418     }
419
420     @Test
421     public void testGetLocation() throws ServiceException {
422         new MockUp<LocationDaoImpl>() {
423
424             @Mock
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);
431                 return list;
432             }
433         };
434         assertTrue(locationImpl.getLocation(new HashMap<>()) != null);
435     }
436
437     @Test
438     public void testGetLocationBranch() throws ServiceException {
439         new MockUp<LocationDaoImpl>() {
440
441             @Mock
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");
447                 return list;
448             }
449         };
450         locationImpl.getLocation(new HashMap<>());
451     }
452
453     @Test
454     public void testGetLocationInfo() throws ServiceException {
455         new MockUp<SitesBusinessImpl>() {
456
457             @Mock
458             public List<SitesEntity> getSites(Map<String, Object> condition) {
459                 List<SitesEntity> siteList = new ArrayList<>();
460                 SitesEntity site = new SitesEntity();
461
462                 siteList.add(site);
463                 return siteList;
464             }
465         };
466
467         new MockUp<JSONObject>() {
468
469             @Mock
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");
475                 return total;
476             }
477         };
478         List<LocationEntity> locationInfo = new ArrayList<>();
479         LocationEntity localEntity = new LocationEntity();
480         locationInfo.add(localEntity);
481         locationImpl.getLocationInfo(locationInfo);
482     }
483
484     @Test
485     public void testGet() throws ServiceException {
486         new MockUp<LocationBusinessImpl>() {
487
488             @Mock
489             public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
490                 List<LocationEntity> list = new ArrayList<>();
491                 LocationEntity localEntity = new LocationEntity();
492
493                 list.add(localEntity);
494                 return null;
495             }
496         };
497         locationImpl.get(new HashMap<>());
498         assertTrue(locationImpl.get("id") != null);
499     }
500
501 }