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