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