42e55921a6029741ca598029dc8b8ec0ce3aa75e
[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.resmanagement.service.rest;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
31 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
32 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.impl.SitesImpl;
33 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
34 import org.onap.vfc.nfvo.resmanagement.service.rest.SitesRoa;
35 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
36 import org.springframework.mock.web.MockHttpServletRequest;
37
38 import mockit.Mock;
39 import mockit.MockUp;
40 import net.sf.json.JSONArray;
41 import net.sf.json.JSONObject;
42
43 /**
44  * <br/>
45  * <p>
46  * </p>
47  *
48  * @author
49  * @version NFVO 0.5 2016年8月16日
50  */
51 public class SitesRoaTest {
52
53     private SitesRoa sitesRoa;
54
55     @Before
56     public void setUp() {
57         sitesRoa = new SitesRoa();
58         sitesRoa.setSites(new SitesImpl());
59     }
60
61     @Test
62     public void testGetSites() throws ServiceException {
63         new MockUp<SitesImpl>() {
64
65             @Mock
66             public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
67                 return new ArrayList<SitesEntity>();
68             }
69         };
70         HttpServletRequest mock = new MockHttpServletRequest();
71         JSONObject result = sitesRoa.getSites(mock);
72         assertNotNull(result);
73     }
74
75     @Test
76     public void testGetSite() throws ServiceException {
77         new MockUp<SitesImpl>() {
78
79             @Mock
80             public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
81                 return new ArrayList<SitesEntity>();
82             }
83         };
84         HttpServletRequest mock = new MockHttpServletRequest();
85         JSONObject result = sitesRoa.getSite(mock, "id");
86         assertNotNull(result);
87     }
88
89     @Test
90     public void testAddSites() throws ServiceException {
91         new MockUp<RequestUtil>() {
92
93             @Mock
94             public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
95                 return new JSONObject();
96             }
97         };
98         new MockUp<SitesImpl>() {
99
100             @Mock
101             public int add(JSONObject jsonObject) throws ServiceException {
102                 return 1;
103             }
104
105             @Mock
106             public void sendToMonitor(JSONObject jsonObject) throws ServiceException {
107
108             }
109         };
110         HttpServletRequest mock = new MockHttpServletRequest();
111         JSONObject result = sitesRoa.addSites(mock);
112         assertNotNull(result);
113     }
114
115     @Test
116     public void testAddSitesExceptions() throws ServiceException {
117         new MockUp<RequestUtil>() {
118
119             @Mock
120             public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
121                 return new JSONObject();
122             }
123         };
124         new MockUp<SitesImpl>() {
125
126             @Mock
127             public int add(JSONObject jsonObject) throws ServiceException {
128                 throw new ServiceException();
129             }
130         };
131         JSONObject result = sitesRoa.addSites(null);
132         JSONObject expectedResult = new JSONObject();
133         expectedResult.put("msg", "");
134         assertEquals(expectedResult.toString(), result.toString());
135     }
136
137     @Test
138     public void testDeleteSites() throws ServiceException {
139         new MockUp<SitesImpl>() {
140
141             @Mock
142             public int delete(String id) throws ServiceException {
143                 return 1;
144             }
145         };
146         JSONObject result = sitesRoa.deleteSites(null, "123");
147         JSONObject expectedResult = new JSONObject();
148         expectedResult.put("msg", "org.openo.nfvo.resmanage.common.del.success");
149         assertEquals(expectedResult.toString(), result.toString());
150     }
151
152     @Test
153     public void testDeleteSitesExceptions() throws ServiceException {
154         new MockUp<SitesImpl>() {
155
156             @Mock
157             public int delete(String id) throws ServiceException {
158                 throw new ServiceException();
159             }
160         };
161         JSONObject result = sitesRoa.deleteSites(null, "123");
162         JSONObject expectedResult = new JSONObject();
163         expectedResult.put("msg", "");
164         assertEquals(expectedResult.toString(), result.toString());
165     }
166
167     @Test
168     public void testUpdateSites() throws ServiceException {
169         new MockUp<RequestUtil>() {
170
171             @Mock
172             public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
173                 return new JSONObject();
174             }
175         };
176         new MockUp<SitesImpl>() {
177
178             @Mock
179             public int update(SitesEntity sitesEntity) throws ServiceException {
180                 return 1;
181             }
182         };
183         JSONObject result = sitesRoa.updateSites(null);
184         JSONObject expectedResult = new JSONObject();
185         expectedResult.put("msg", "org.openo.nfvo.resmanage.common.update.success");
186         assertEquals(expectedResult.toString(), result.toString());
187     }
188
189     @Test
190     public void testUpdateISitesExceptions() throws ServiceException {
191         new MockUp<RequestUtil>() {
192
193             @Mock
194             public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
195                 return new JSONObject();
196             }
197         };
198         new MockUp<SitesImpl>() {
199
200             @Mock
201             public int update(SitesEntity sitesEntity) throws ServiceException {
202                 throw new ServiceException();
203             }
204         };
205         JSONObject result = sitesRoa.updateSites(null);
206         JSONObject expectedResult = new JSONObject();
207         expectedResult.put("msg", "");
208         assertEquals(expectedResult.toString(), result.toString());
209     }
210
211     @Test
212     public void testGrantResource() throws ServiceException {
213         new MockUp<RequestUtil>() {
214
215             @Mock
216             public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
217                 return new JSONObject();
218             }
219         };
220         new MockUp<SitesImpl>() {
221
222             @Mock
223             public int update(JSONObject jsonObject) throws ServiceException {
224                 return 1;
225             }
226         };
227         HttpServletRequest mock = new MockHttpServletRequest();
228         JSONObject result = sitesRoa.grantResource(mock);
229         assertNotNull(result);
230     }
231
232     @Test
233     public void testGrantResourceByException() throws ServiceException {
234         new MockUp<RequestUtil>() {
235
236             @Mock
237             public JSONObject getAllJsonRequestBody(HttpServletRequest context) {
238                 return new JSONObject();
239             }
240         };
241         new MockUp<SitesImpl>() {
242
243             @Mock
244             public int update(JSONObject jsonObject) throws ServiceException {
245                 throw new ServiceException();
246             }
247         };
248         HttpServletRequest mock = new MockHttpServletRequest();
249         JSONObject result = sitesRoa.grantResource(mock);
250         assertNotNull(result);
251     }
252
253     @Test
254     public void testGetVims() throws ServiceException {
255         new MockUp<VimUtil>() {
256
257             @Mock
258             public JSONArray getVims() {
259                 return new JSONArray();
260             }
261         };
262         HttpServletRequest mock = new MockHttpServletRequest();
263         String result = sitesRoa.getVims(mock);
264         assertNotNull(result);
265     }
266
267     @Test
268     public void testGetVim() throws ServiceException {
269         new MockUp<VimUtil>() {
270
271             @Mock
272             public JSONObject getVimById(String vimId) {
273                 return new JSONObject();
274             }
275         };
276         HttpServletRequest mock = new MockHttpServletRequest();
277         String result = sitesRoa.getVim(mock, "id");
278         assertNotNull(result);
279     }
280 }