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