2a3c062e5ac4adc8f7dc0c367f34dce386ba281a
[vfc/nfvo/resmanagement.git] /
1 /*\r
2  * Copyright 2017 Huawei Technologies Co., Ltd.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.nfvo.resmanagement.service.rest;\r
18 \r
19 import static org.junit.Assert.assertNotNull;\r
20 \r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 import java.util.Map;\r
24 \r
25 import javax.servlet.http.HttpServletRequest;\r
26 \r
27 import org.junit.Before;\r
28 import org.junit.Test;\r
29 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
30 import org.openo.nfvo.resmanagement.common.util.request.RequestUtil;\r
31 import org.openo.nfvo.resmanagement.service.base.openstack.impl.LocationImpl;\r
32 import org.openo.nfvo.resmanagement.service.base.openstack.impl.SitesImpl;\r
33 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Location;\r
34 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Sites;\r
35 import org.openo.nfvo.resmanagement.service.entity.LocationEntity;\r
36 import org.openo.nfvo.resmanagement.service.entity.SitesEntity;\r
37 import org.springframework.mock.web.MockHttpServletRequest;\r
38 \r
39 import mockit.Mock;\r
40 import mockit.MockUp;\r
41 import net.sf.json.JSONObject;\r
42 \r
43 /**\r
44  * <br>\r
45  * <p>\r
46  * </p>\r
47  * \r
48  * @author\r
49  * @version NFVO 0.5 Feb 9, 2017\r
50  */\r
51 public class LocationRoaTest {\r
52 \r
53     private LocationRoa roa;\r
54 \r
55     private Location location;\r
56 \r
57     private Sites sites;\r
58 \r
59     @Before\r
60     public void setUp() {\r
61         roa = new LocationRoa();\r
62         location = new LocationImpl();\r
63         sites = new SitesImpl();\r
64         roa.setLocation(location);\r
65         roa.setSites(sites);\r
66     }\r
67 \r
68     @Test\r
69     public void testGetLocationsbase() throws ServiceException {\r
70         new MockUp<LocationImpl>() {\r
71 \r
72             @Mock\r
73             public List<LocationEntity> get(Map<String, Object> condition) throws ServiceException {\r
74                 return new ArrayList<LocationEntity>();\r
75             }\r
76         };\r
77         HttpServletRequest mock = new MockHttpServletRequest();\r
78         JSONObject result = roa.getLocationsbase(mock);\r
79         assertNotNull(result);\r
80     }\r
81 \r
82     @Test\r
83     public void testGetLocationbase() throws ServiceException {\r
84         new MockUp<LocationImpl>() {\r
85 \r
86             @Mock\r
87             public List<LocationEntity> get(Map<String, Object> condition) throws ServiceException {\r
88                 return new ArrayList<LocationEntity>();\r
89             }\r
90         };\r
91         HttpServletRequest mock = new MockHttpServletRequest();\r
92         JSONObject result = roa.getLocationbase(mock, "id");\r
93         assertNotNull(result);\r
94     }\r
95 \r
96     @Test\r
97     public void testGetCountry() throws ServiceException {\r
98         new MockUp<LocationImpl>() {\r
99 \r
100             @Mock\r
101             public List<String> getCountry() throws ServiceException {\r
102                 return new ArrayList<String>();\r
103             }\r
104         };\r
105         HttpServletRequest mock = new MockHttpServletRequest();\r
106         JSONObject result = roa.getCountry(mock);\r
107         assertNotNull(result);\r
108     }\r
109 \r
110     @Test\r
111     public void testGetLocationByCountry() throws ServiceException {\r
112         new MockUp<LocationImpl>() {\r
113 \r
114             @Mock\r
115             public List<String> getLocationByCountry(Map<String, Object> condition) throws ServiceException {\r
116                 return new ArrayList<String>();\r
117             }\r
118         };\r
119         HttpServletRequest mock = new MockHttpServletRequest();\r
120         JSONObject result = roa.getLocationByCountry(mock, "country");\r
121         assertNotNull(result);\r
122     }\r
123 \r
124     @Test\r
125     public void testGetLocation() throws ServiceException {\r
126         new MockUp<LocationImpl>() {\r
127 \r
128             @Mock\r
129             public List<LocationEntity> get(Map<String, Object> condition) throws ServiceException {\r
130                 return new ArrayList<LocationEntity>();\r
131             }\r
132 \r
133             @Mock\r
134             public List<JSONObject> getLocationInfo(List<LocationEntity> locationInfo) throws ServiceException {\r
135                 return new ArrayList<JSONObject>();\r
136             }\r
137         };\r
138         HttpServletRequest mock = new MockHttpServletRequest();\r
139         JSONObject result = roa.getLocation(mock, "locations");\r
140         assertNotNull(result);\r
141     }\r
142 \r
143     @Test\r
144     public void testAddLocation() throws ServiceException {\r
145         new MockUp<RequestUtil>() {\r
146 \r
147             @Mock\r
148             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
149                 return new JSONObject();\r
150             }\r
151         };\r
152         new MockUp<LocationImpl>() {\r
153 \r
154             @Mock\r
155             public int add(JSONObject jsonObject) throws ServiceException {\r
156                 return 1;\r
157             }\r
158         };\r
159         HttpServletRequest mock = new MockHttpServletRequest();\r
160         JSONObject result = roa.addLocation(mock);\r
161         assertNotNull(result);\r
162     }\r
163 \r
164     @Test\r
165     public void testAddLocationByException() throws ServiceException {\r
166         new MockUp<RequestUtil>() {\r
167 \r
168             @Mock\r
169             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
170                 return new JSONObject();\r
171             }\r
172         };\r
173         new MockUp<LocationImpl>() {\r
174 \r
175             @Mock\r
176             public int add(JSONObject jsonObject) throws ServiceException {\r
177                 throw new ServiceException();\r
178             }\r
179         };\r
180         HttpServletRequest mock = new MockHttpServletRequest();\r
181         JSONObject result = roa.addLocation(mock);\r
182         assertNotNull(result);\r
183     }\r
184 \r
185     @Test\r
186     public void testDeleteLocationbase() throws ServiceException {\r
187         new MockUp<LocationImpl>() {\r
188 \r
189             @Mock\r
190             public int delete(String location) throws ServiceException {\r
191                 return 1;\r
192             }\r
193         };\r
194         HttpServletRequest mock = new MockHttpServletRequest();\r
195         JSONObject result = roa.deleteLocationbase(mock, "locations");\r
196         assertNotNull(result);\r
197     }\r
198 \r
199     @Test\r
200     public void testDeleteLocationbaseByException() throws ServiceException {\r
201         new MockUp<LocationImpl>() {\r
202 \r
203             @Mock\r
204             public int delete(String location) throws ServiceException {\r
205                 throw new ServiceException();\r
206             }\r
207         };\r
208         HttpServletRequest mock = new MockHttpServletRequest();\r
209         JSONObject result = roa.deleteLocationbase(mock, "locations");\r
210         assertNotNull(result);\r
211     }\r
212 \r
213     @Test\r
214     public void testDeleteLocation() throws ServiceException {\r
215         new MockUp<RequestUtil>() {\r
216 \r
217             @Mock\r
218             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
219                 JSONObject object = new JSONObject();\r
220                 object.put("location", "location");\r
221                 object.put("id", "id");\r
222                 return object;\r
223             }\r
224         };\r
225         new MockUp<SitesImpl>() {\r
226 \r
227             @Mock\r
228             public SitesEntity get(Map<String, Object> condition) throws ServiceException {\r
229                 return null;\r
230             }\r
231         };\r
232         new MockUp<LocationImpl>() {\r
233 \r
234             @Mock\r
235             public int delete(String location) throws ServiceException {\r
236                 return 1;\r
237             }\r
238         };\r
239         HttpServletRequest mock = new MockHttpServletRequest();\r
240         JSONObject result = roa.deleteLocation(mock);\r
241         assertNotNull(result);\r
242     }\r
243 \r
244     @Test\r
245     public void testDeleteLocationByException() throws ServiceException {\r
246         new MockUp<RequestUtil>() {\r
247 \r
248             @Mock\r
249             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
250                 JSONObject object = new JSONObject();\r
251                 object.put("location", "location");\r
252                 object.put("id", "id");\r
253                 return object;\r
254             }\r
255         };\r
256         new MockUp<SitesImpl>() {\r
257 \r
258             @Mock\r
259             public SitesEntity get(Map<String, Object> condition) throws ServiceException {\r
260                 return null;\r
261             }\r
262         };\r
263         new MockUp<LocationImpl>() {\r
264 \r
265             @Mock\r
266             public int delete(String location) throws ServiceException {\r
267                 throw new ServiceException();\r
268             }\r
269         };\r
270         HttpServletRequest mock = new MockHttpServletRequest();\r
271         JSONObject result = roa.deleteLocation(mock);\r
272         assertNotNull(result);\r
273     }\r
274 \r
275     @Test\r
276     public void testDeleteLocationBySitesEntity() throws ServiceException {\r
277         new MockUp<RequestUtil>() {\r
278 \r
279             @Mock\r
280             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
281                 JSONObject object = new JSONObject();\r
282                 object.put("location", "location");\r
283                 object.put("id", "id");\r
284                 return object;\r
285             }\r
286         };\r
287         new MockUp<SitesImpl>() {\r
288 \r
289             @Mock\r
290             public SitesEntity get(Map<String, Object> condition) throws ServiceException {\r
291                 return new SitesEntity();\r
292             }\r
293         };\r
294         HttpServletRequest mock = new MockHttpServletRequest();\r
295         JSONObject result = roa.deleteLocation(mock);\r
296         assertNotNull(result);\r
297     }\r
298 \r
299     @Test\r
300     public void testUpdateLocation() throws ServiceException {\r
301         new MockUp<RequestUtil>() {\r
302 \r
303             @Mock\r
304             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
305                 JSONObject object = new JSONObject();\r
306                 object.put("location", "location");\r
307                 return object;\r
308             }\r
309         };\r
310         new MockUp<SitesImpl>() {\r
311 \r
312             @Mock\r
313             public SitesEntity get(Map<String, Object> condition) throws ServiceException {\r
314                 return null;\r
315             }\r
316         };\r
317         new MockUp<LocationImpl>() {\r
318 \r
319             @Mock\r
320             public int update(JSONObject jsonObject) throws ServiceException {\r
321                 return 1;\r
322             }\r
323         };\r
324         HttpServletRequest mock = new MockHttpServletRequest();\r
325         JSONObject result = roa.updateLocation(mock);\r
326         assertNotNull(result);\r
327     }\r
328 \r
329     @Test\r
330     public void testUpdateLocationByException() throws ServiceException {\r
331         new MockUp<RequestUtil>() {\r
332 \r
333             @Mock\r
334             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
335                 JSONObject object = new JSONObject();\r
336                 object.put("location", "location");\r
337                 return object;\r
338             }\r
339         };\r
340         new MockUp<SitesImpl>() {\r
341 \r
342             @Mock\r
343             public SitesEntity get(Map<String, Object> condition) throws ServiceException {\r
344                 return null;\r
345             }\r
346         };\r
347         new MockUp<LocationImpl>() {\r
348 \r
349             @Mock\r
350             public int update(JSONObject jsonObject) throws ServiceException {\r
351                 throw new ServiceException();\r
352             }\r
353         };\r
354         HttpServletRequest mock = new MockHttpServletRequest();\r
355         JSONObject result = roa.updateLocation(mock);\r
356         assertNotNull(result);\r
357     }\r
358 \r
359     @Test\r
360     public void testUpdateLocationBySitesEntity() throws ServiceException {\r
361         new MockUp<RequestUtil>() {\r
362 \r
363             @Mock\r
364             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
365                 JSONObject object = new JSONObject();\r
366                 object.put("location", "location");\r
367                 return object;\r
368             }\r
369         };\r
370         new MockUp<SitesImpl>() {\r
371 \r
372             @Mock\r
373             public SitesEntity get(Map<String, Object> condition) throws ServiceException {\r
374                 return new SitesEntity();\r
375             }\r
376         };\r
377         new MockUp<LocationImpl>() {\r
378 \r
379             @Mock\r
380             public int update(JSONObject jsonObject) throws ServiceException {\r
381                 return 1;\r
382             }\r
383         };\r
384         HttpServletRequest mock = new MockHttpServletRequest();\r
385         JSONObject result = roa.updateLocation(mock);\r
386         assertNotNull(result);\r
387     }\r
388 \r
389 }\r