6222005fcd99fd95197e5f3d455ea6b324f1b1b2
[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.base.openstack.impl;
18
19 import static org.junit.Assert.*;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.junit.Test;
26 import org.openo.baseservice.remoteservice.exception.ServiceException;
27 import org.openo.nfvo.resmanagement.service.business.impl.NetworkBusinessImpl;
28 import org.openo.nfvo.resmanagement.service.dao.impl.NetworkDaoImpl;
29 import org.openo.nfvo.resmanagement.service.dao.inf.NetworkDao;
30 import org.openo.nfvo.resmanagement.service.entity.NetworkEntity;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 public class NetworkImplTest {
37
38
39     @Test
40     public void testAddBranch() throws ServiceException {
41         new MockUp<NetworkDaoImpl>() {
42
43             @Mock
44             public NetworkEntity getNetwork(String id) {
45                 return null;
46             }
47
48             @Mock
49             public int addNetwork(NetworkEntity networkEntity) {
50                 return 1;
51             }
52         };
53         NetworkImpl networkImpl = new NetworkImpl();
54         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
55         NetworkDao networkDao = new NetworkDaoImpl();
56         networkBusiness.setNetworkDao(networkDao);
57         networkImpl.setNetworkBusiness(networkBusiness);
58         JSONObject json = new JSONObject();
59         json.put("id", "id");
60         json.put("name", "name");
61         json.put("status", "status");
62         json.put("tenant_id", "tenant_id");
63         json.put("vimId", "vimId");
64         json.put("vimName", "vimName");
65         json.put("provider:physical_network", "provider:physical_network");
66         json.put("provider:network_type", "provider:network_type");
67         json.put("provider:segmentation_id", "provider:segmentation_id");
68         assertTrue(networkImpl.add(json) == 1);
69     }
70
71     @Test
72     public void testAddBranch1() throws ServiceException {
73         new MockUp<NetworkDaoImpl>() {
74
75             @Mock
76             public NetworkEntity getNetwork(String id) {
77                 return null;
78             }
79
80             @Mock
81             public int addNetwork(NetworkEntity networkEntity) {
82                 return 1;
83             }
84         };
85         NetworkImpl networkImpl = new NetworkImpl();
86         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
87         NetworkDao networkDao = new NetworkDaoImpl();
88         networkBusiness.setNetworkDao(networkDao);
89         networkImpl.setNetworkBusiness(networkBusiness);
90         JSONObject json = new JSONObject();
91         json.put("id", "");
92         json.put("name", "name");
93         json.put("status", "status");
94         json.put("tenant_id", "tenant_id");
95         json.put("vimId", "vimId");
96         json.put("vimName", "vimName");
97         json.put("provider:physical_network", "provider:physical_network");
98         json.put("provider:network_type", "provider:network_type");
99         json.put("provider:segmentation_id", "provider:segmentation_id");
100         assertTrue(networkImpl.add(json) == 1);
101     }
102
103     @Test(expected = ServiceException.class)
104     public void testAddBranch2() throws ServiceException {
105          new MockUp<NetworkDaoImpl>() {
106          @Mock
107          public NetworkEntity getNetwork(String id) {
108              return null;
109          }
110
111          @Mock
112          public int addNetwork(NetworkEntity networkEntity) {
113              return 1;
114          }
115      };
116         NetworkImpl networkImpl = new NetworkImpl();
117         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
118         NetworkDao networkDao = new NetworkDaoImpl();
119         networkBusiness.setNetworkDao(networkDao);
120         networkImpl.setNetworkBusiness(networkBusiness);
121         NetworkEntity entity = null;
122         networkImpl.add(entity);
123
124     }
125
126
127     @Test
128     public void testDelete() throws ServiceException {
129         new MockUp<NetworkDaoImpl>() {
130
131             @Mock
132             public int deleteNetwork(String id) {
133                 return 1;
134             }
135         };
136         NetworkImpl networkImpl = new NetworkImpl();
137         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
138         NetworkDao networkDao = new NetworkDaoImpl();
139         networkBusiness.setNetworkDao(networkDao);
140         networkImpl.setNetworkBusiness(networkBusiness);
141         assertTrue(networkImpl.delete("id") == 1);
142     }
143
144     @Test(expected = ServiceException.class)
145     public void testDelete1() throws ServiceException {
146
147         NetworkImpl networkImpl = new NetworkImpl();
148         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
149         NetworkDao networkDao = new NetworkDaoImpl();
150         networkBusiness.setNetworkDao(networkDao);
151         networkImpl.setNetworkBusiness(networkBusiness);
152         networkImpl.delete("");
153     }
154
155     @Test(expected = ServiceException.class)
156     public void testDeleteVimByIdException() throws ServiceException {
157
158         NetworkImpl networkImpl = new NetworkImpl();
159         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
160         NetworkDao networkDao = new NetworkDaoImpl();
161         networkBusiness.setNetworkDao(networkDao);
162         networkImpl.setNetworkBusiness(networkBusiness);
163     networkImpl.deleteResByVimId("");
164     }
165
166     @Test
167     public void testDeleteVimById() throws ServiceException {
168         new MockUp<NetworkDaoImpl>() {
169
170             @Mock
171             public int deleteNetworkByVimId(String vimId) {
172                 return 1;
173             }
174         };
175         NetworkImpl networkImpl = new NetworkImpl();
176         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
177         NetworkDao networkDao = new NetworkDaoImpl();
178         networkBusiness.setNetworkDao(networkDao);
179         networkImpl.setNetworkBusiness(networkBusiness);
180         assertTrue(networkImpl.deleteResByVimId("vimId") == 1);
181     }
182
183
184     @Test
185     public void testUpdate() throws ServiceException {
186         new MockUp<NetworkDaoImpl>() {
187
188             @Mock
189             public int updateNetworkSelective(NetworkEntity networkEntity) {
190                 return 1;
191             }
192         };
193         NetworkImpl networkImpl = new NetworkImpl();
194         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
195         NetworkDao networkDao = new NetworkDaoImpl();
196         networkBusiness.setNetworkDao(networkDao);
197         networkImpl.setNetworkBusiness(networkBusiness);
198
199         JSONObject json = new JSONObject();
200         json.put("id", "");
201         json.put("name", "name");
202         json.put("status", "status");
203         json.put("tenant_id", "tenant_id");
204         json.put("vimId", "vimId");
205         json.put("vimName", "vimName");
206         json.put("provider:physical_network", "provider:physical_network");
207         json.put("provider:network_type", "provider:network_type");
208         json.put("provider:segmentation_id", "provider:segmentation_id");
209         assertTrue(networkImpl.update(json)==1);
210     }
211
212
213
214     @Test
215     public void testUpdateVimById() throws ServiceException {
216         new MockUp<NetworkDaoImpl>() {
217
218             @Mock
219             public int updateNetworkByVimId(NetworkEntity NetworkEntity) {
220                 return 1;
221             }
222         };
223         NetworkImpl networkImpl = new NetworkImpl();
224         NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
225         NetworkDao networkDao = new NetworkDaoImpl();
226         networkBusiness.setNetworkDao(networkDao);
227         networkImpl.setNetworkBusiness(networkBusiness);
228         JSONObject json = new JSONObject();
229         json.put("id", "");
230         json.put("name", "name");
231         json.put("status", "status");
232         json.put("tenant_id", "tenant_id");
233         json.put("vimId", "vimId");
234         json.put("vimName", "vimName");
235         json.put("provider:physical_network", "provider:physical_network");
236         json.put("provider:network_type", "provider:network_type");
237         json.put("provider:segmentation_id", "provider:segmentation_id");
238         assertTrue(networkImpl.updateStatusByVimId(json)==1);
239
240     }
241
242     @Test
243     public void testgetList() throws ServiceException {
244         Map<String, Object> condition = new HashMap<>();
245         NetworkImpl networkImpl = new NetworkImpl();
246         networkImpl.setNetworkBusiness(new NetworkBusinessImpl());
247         new MockUp<NetworkBusinessImpl>() {
248
249             @Mock
250             public List<NetworkEntity> getNetworks(Map<String, Object> condition) {
251                 return null;
252             }
253         };
254         List<NetworkEntity> result = networkImpl.getList(condition);
255         List<NetworkEntity> exceptedResult = null;
256         assertEquals(exceptedResult, result);
257     }
258     @Test
259     public void testadd() throws ServiceException {
260         NetworkImpl networkImpl = new NetworkImpl();
261         networkImpl.setNetworkBusiness(new NetworkBusinessImpl());
262         NetworkEntity networkEntity =new NetworkEntity();
263         networkEntity.setId("1");
264         new MockUp<NetworkBusinessImpl>() {
265
266             @Mock
267             public int addNetwork(NetworkEntity networkEntity ) {
268                 return 1;
269             }
270         };
271          int result = networkImpl.add(networkEntity);
272             int exceptedResult = 1;
273             assertEquals(exceptedResult, result);
274         }
275     }