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