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