2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.resmanagement.service.base.openstack.impl;
19 import static org.junit.Assert.*;
21 import java.util.HashMap;
22 import java.util.List;
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;
34 import net.sf.json.JSONObject;
36 public class NetworkImplTest {
40 public void testAddBranch() throws ServiceException {
41 new MockUp<NetworkDaoImpl>() {
44 public NetworkEntity getNetwork(String id) {
49 public int addNetwork(NetworkEntity networkEntity) {
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();
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);
72 public void testAddBranch1() throws ServiceException {
73 new MockUp<NetworkDaoImpl>() {
76 public NetworkEntity getNetwork(String id) {
81 public int addNetwork(NetworkEntity networkEntity) {
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();
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);
103 @Test(expected = ServiceException.class)
104 public void testAddBranch2() throws ServiceException {
105 new MockUp<NetworkDaoImpl>() {
107 public NetworkEntity getNetwork(String id) {
112 public int addNetwork(NetworkEntity networkEntity) {
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);
128 public void testDelete() throws ServiceException {
129 new MockUp<NetworkDaoImpl>() {
132 public int deleteNetwork(String id) {
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);
144 @Test(expected = ServiceException.class)
145 public void testDelete1() throws ServiceException {
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("");
155 @Test(expected = ServiceException.class)
156 public void testDeleteVimByIdException() throws ServiceException {
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("");
167 public void testDeleteVimById() throws ServiceException {
168 new MockUp<NetworkDaoImpl>() {
171 public int deleteNetworkByVimId(String vimId) {
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);
185 public void testUpdate() throws ServiceException {
186 new MockUp<NetworkDaoImpl>() {
189 public int updateNetworkSelective(NetworkEntity networkEntity) {
193 NetworkImpl networkImpl = new NetworkImpl();
194 NetworkBusinessImpl networkBusiness = new NetworkBusinessImpl();
195 NetworkDao networkDao = new NetworkDaoImpl();
196 networkBusiness.setNetworkDao(networkDao);
197 networkImpl.setNetworkBusiness(networkBusiness);
199 JSONObject json = new JSONObject();
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);
215 public void testUpdateVimById() throws ServiceException {
216 new MockUp<NetworkDaoImpl>() {
219 public int updateNetworkByVimId(NetworkEntity NetworkEntity) {
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();
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);
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>() {
250 public List<NetworkEntity> getNetworks(Map<String, Object> condition) {
254 List<NetworkEntity> result = networkImpl.getList(condition);
255 List<NetworkEntity> exceptedResult = null;
256 assertEquals(exceptedResult, result);
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>() {
267 public int addNetwork(NetworkEntity networkEntity ) {
271 int result = networkImpl.add(networkEntity);
272 int exceptedResult = 1;
273 assertEquals(exceptedResult, result);