827a56da71b79071aaac3cdcfde852c94330db2f
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 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 package org.onap.vfc.nfvo.res.service.base.openstack.impl;
17
18 import static org.junit.Assert.*;
19
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.junit.Test;
25 import org.onap.vfc.nfvo.res.service.base.openstack.impl.HostImpl;
26 import org.onap.vfc.nfvo.res.service.business.impl.HostBusinessImpl;
27 import org.onap.vfc.nfvo.res.service.dao.impl.HostDaoImpl;
28 import org.onap.vfc.nfvo.res.service.entity.HostEntity;
29 import org.openo.baseservice.remoteservice.exception.ServiceException;
30
31 import mockit.Mock;
32 import mockit.MockUp;
33 import net.sf.json.JSONObject;
34
35 public class HostImplTest {
36
37     @Test
38     public void testdeleteHostByVimId() throws ServiceException {
39         new MockUp<HostDaoImpl>() {
40
41             @Mock
42             public int deleteHostByVimId(String vimId) {
43                 return 1;
44             }
45
46         };
47         HostImpl hostImpl = new HostImpl();
48         HostBusinessImpl hostBusiness = new HostBusinessImpl();
49         hostBusiness.setHostDao(new HostDaoImpl());
50         hostImpl.setHostBusiness(hostBusiness);
51
52         assertTrue(hostImpl.deleteResByVimId("vimId") == 1);
53     }
54     @Test
55     public void testupdateStatusByVimId() throws ServiceException {
56         HostImpl hostImpl = new HostImpl();
57         hostImpl.setHostBusiness(new HostBusinessImpl());
58         JSONObject json = new JSONObject();
59         json.put("id", "123");
60         json.put("vimId", "vim123");
61         new MockUp<HostBusinessImpl>() {
62
63             @Mock
64             public int updateHostByVimId(HostEntity hostEntity) throws ServiceException {
65                 return 1;
66             }
67         };
68         int result = hostImpl.updateStatusByVimId(json);
69         int exceptedResult = 1;
70         assertEquals(exceptedResult, result);
71     }
72
73     @Test
74     public void testDelete() throws ServiceException {
75         HostImpl hostImpl = new HostImpl();
76         hostImpl.setHostBusiness(new HostBusinessImpl());
77         new MockUp<HostBusinessImpl>() {
78             @Mock
79             public int deleteHost(String id) throws ServiceException {
80                 return 1;
81             }
82         };
83
84         int result = hostImpl.delete("id");
85         int exceptedResult = 1;
86         assertEquals(exceptedResult, result);
87
88     }
89
90     @Test
91     public void testAdd1() throws ServiceException {
92         HostImpl hostImpl = new HostImpl();
93         hostImpl.setHostBusiness(new HostBusinessImpl());
94         JSONObject json = new JSONObject();
95         json.put("id", "1");
96         new MockUp<HostBusinessImpl>() {
97             @Mock
98             public int addHost(HostEntity hostEntity) throws ServiceException {
99                 return 1;
100             }
101
102         };
103         int result = hostImpl.add(json);
104         int exceptedResult = 1;
105         assertEquals(exceptedResult, result);
106     }
107
108     @Test
109     public void testupdate() throws ServiceException {
110         HostImpl hostImpl = new HostImpl();
111         hostImpl.setHostBusiness(new HostBusinessImpl());
112         HostEntity hostEntity = new HostEntity();
113         hostEntity.setId("123");
114         new MockUp<HostBusinessImpl>() {
115             @Mock
116             public int updateHostSelective(HostEntity hostEntity) throws ServiceException {
117                 return 1;
118             }
119
120         };
121         int result = hostImpl.update(hostEntity);
122         int exceptedResult = 1;
123         assertEquals(exceptedResult, result);
124     }
125
126     @Test
127     public void testUpdateResource() throws ServiceException {
128         HostImpl hostImpl = new HostImpl();
129         hostImpl.setHostBusiness(new HostBusinessImpl());
130         JSONObject json = new JSONObject();
131         json.put("id", "123");
132         json.put("vimId", "vim123");
133         new MockUp<HostBusinessImpl>() {
134
135             @Mock
136             public int updateHostSelective(HostEntity hostEntity) throws ServiceException {
137                 return 1;
138             }
139         };
140         int result = hostImpl.update(json);
141         int exceptedResult = 1;
142         assertEquals(exceptedResult, result);
143     }
144
145     @Test
146     public void testGetList() throws ServiceException {
147         Map<String, Object> condition = new HashMap<>();
148         HostImpl hostImpl = new HostImpl();
149         hostImpl.setHostBusiness(new HostBusinessImpl());
150         new MockUp<HostBusinessImpl>() {
151
152             @Mock
153             public List<HostEntity> getHosts(Map<String, Object> condition) {
154                 return null;
155             }
156         };
157         List<HostEntity> result = hostImpl.getList(condition);
158         List<HostEntity> exceptedResult = null;
159         assertEquals(exceptedResult, result);
160     }
161
162 }