3d36011ada4964bf20db87a4951cd99cd313ec2e
[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.assertTrue;
20
21 import org.junit.Test;
22 import org.openo.baseservice.remoteservice.exception.ServiceException;
23 import org.openo.nfvo.resmanagement.common.util.JsonUtil;
24 import org.openo.nfvo.resmanagement.service.business.impl.PortBusinessImpl;
25 import org.openo.nfvo.resmanagement.service.dao.impl.PortDaoImpl;
26 import org.openo.nfvo.resmanagement.service.entity.PortEntity;
27
28 import mockit.Mock;
29 import mockit.MockUp;
30 import net.sf.json.JSONObject;
31
32 public class PortImplTest {
33
34     @Test
35     public void testAddBranch() throws ServiceException {
36         new MockUp<PortDaoImpl>() {
37
38             @Mock
39             public PortEntity getPort(String id) {
40                 return null;
41             }
42
43             @Mock
44             public int addPort(PortEntity portEntity) {
45                 return 1;
46             }
47         };
48         PortImpl portImpl = new PortImpl();
49         PortBusinessImpl portBusiness = new PortBusinessImpl();
50         portBusiness.setPortDao(new PortDaoImpl());
51         portImpl.setPortBusiness(portBusiness);
52         JSONObject json = new JSONObject();
53         json.put("id", "");
54         json.put("name", "name");
55         json.put("status", "status");
56         json.put("tenant_id", "tenant_id");
57         json.put("vimId", "vimId");
58         json.put("vimName", "vimName");
59         json.put("network_id", "network_id");
60         assertTrue(portImpl.add(json) == 1);
61     }
62
63     @Test(expected = ServiceException.class)
64     public void testAddBranch1() throws ServiceException {
65
66         PortImpl portImpl = new PortImpl();
67         PortBusinessImpl portBusiness = new PortBusinessImpl();
68         portBusiness.setPortDao(new PortDaoImpl());
69         portImpl.setPortBusiness(portBusiness);
70         PortEntity portEntity = null;
71         portImpl.add(portEntity);
72     }
73
74     @Test(expected = ServiceException.class)
75     public void testUpdateException() throws ServiceException {
76
77         PortImpl portImpl = new PortImpl();
78         PortBusinessImpl portBusiness = new PortBusinessImpl();
79         portBusiness.setPortDao(new PortDaoImpl());
80         portImpl.setPortBusiness(portBusiness);
81         PortEntity portEntity = null;
82         portImpl.update(portEntity);
83     }
84
85     @Test
86     public void testUpdate() throws ServiceException {
87         new MockUp<PortDaoImpl>() {
88
89             @Mock
90             public int updatePortSelective(PortEntity portEntity) {
91                 return 1;
92             }
93
94         };
95         PortImpl portImpl = new PortImpl();
96         PortBusinessImpl portBusiness = new PortBusinessImpl();
97         portBusiness.setPortDao(new PortDaoImpl());
98         portImpl.setPortBusiness(portBusiness);
99         assertTrue(portImpl.update(new JSONObject()) == 1);
100     }
101
102     @Test
103     public void testdelete() throws ServiceException {
104         new MockUp<PortDaoImpl>() {
105
106             @Mock
107             public int deletePort(String id) {
108                 return 1;
109             }
110
111         };
112         PortImpl portImpl = new PortImpl();
113         PortBusinessImpl portBusiness = new PortBusinessImpl();
114         portBusiness.setPortDao(new PortDaoImpl());
115         portImpl.setPortBusiness(portBusiness);
116
117     }
118
119     @Test
120     public void testDeleteResByVimId() throws ServiceException {
121         new MockUp<PortDaoImpl>() {
122
123             @Mock
124             public int deletePortByVimId(String vimId) {
125                 return 1;
126             }
127
128         };
129         PortImpl portImpl = new PortImpl();
130         PortBusinessImpl portBusiness = new PortBusinessImpl();
131         portBusiness.setPortDao(new PortDaoImpl());
132         portImpl.setPortBusiness(portBusiness);
133         assertTrue(portImpl.deleteResByVimId("vimId") == 1);
134     }
135
136     @Test(expected = ServiceException.class)
137     public void testDeleteResByVimIdException() throws ServiceException {
138         PortImpl portImpl = new PortImpl();
139         PortBusinessImpl portBusiness = new PortBusinessImpl();
140         portBusiness.setPortDao(new PortDaoImpl());
141         portImpl.setPortBusiness(portBusiness);
142         portImpl.deleteResByVimId("");
143     }
144
145     @Test(expected = ServiceException.class)
146     public void testDelete() throws ServiceException {
147         PortImpl portImpl = new PortImpl();
148         PortBusinessImpl portBusiness = new PortBusinessImpl();
149         portBusiness.setPortDao(new PortDaoImpl());
150         portImpl.setPortBusiness(portBusiness);
151         portImpl.delete("");
152     }
153 }