bc0b8b7dfe8b688772655bf6e75cd3f608b3d0a1
[vfc/nfvo/resmanagement.git] /
1 /*\r
2  * Copyright 2017 Huawei Technologies Co., Ltd.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.nfvo.resmanagement.service.rest;\r
18 \r
19 import static org.junit.Assert.assertNotNull;\r
20 \r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 import java.util.Map;\r
24 \r
25 import javax.servlet.http.HttpServletRequest;\r
26 \r
27 import org.junit.Before;\r
28 import org.junit.Test;\r
29 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
30 import org.openo.nfvo.resmanagement.common.util.request.RequestUtil;\r
31 import org.openo.nfvo.resmanagement.service.base.openstack.impl.HostImpl;\r
32 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Host;\r
33 import org.openo.nfvo.resmanagement.service.entity.HostEntity;\r
34 import org.springframework.mock.web.MockHttpServletRequest;\r
35 \r
36 import mockit.Mock;\r
37 import mockit.MockUp;\r
38 import net.sf.json.JSONObject;\r
39 \r
40 /**\r
41  * <br>\r
42  * <p>\r
43  * </p>\r
44  * \r
45  * @author\r
46  * @version NFVO 0.5 Feb 9, 2017\r
47  */\r
48 public class HostRoaTest {\r
49 \r
50     private HostRoa roa;\r
51 \r
52     private Host host;\r
53 \r
54     @Before\r
55     public void setUp() {\r
56         roa = new HostRoa();\r
57         host = new HostImpl();\r
58         roa.setHost(host);\r
59     }\r
60 \r
61     @Test\r
62     public void testGetHosts() throws ServiceException {\r
63         new MockUp<HostImpl>() {\r
64 \r
65             @Mock\r
66             public List<HostEntity> getList(Map<String, Object> condition) throws ServiceException {\r
67                 return new ArrayList<HostEntity>();\r
68             }\r
69         };\r
70         HttpServletRequest mock = new MockHttpServletRequest();\r
71         JSONObject result = roa.getHosts(mock);\r
72         assertNotNull(result);\r
73     }\r
74 \r
75     @Test\r
76     public void testGetHost() throws ServiceException {\r
77         new MockUp<HostImpl>() {\r
78 \r
79             @Mock\r
80             public List<HostEntity> getList(Map<String, Object> condition) throws ServiceException {\r
81                 return new ArrayList<HostEntity>();\r
82             }\r
83         };\r
84         HttpServletRequest mock = new MockHttpServletRequest();\r
85         JSONObject result = roa.getHost(mock, "id");\r
86         assertNotNull(result);\r
87     }\r
88 \r
89     @Test\r
90     public void testAddHost() throws ServiceException {\r
91         new MockUp<RequestUtil>() {\r
92 \r
93             @Mock\r
94             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
95                 return new JSONObject();\r
96             }\r
97         };\r
98         new MockUp<HostImpl>() {\r
99 \r
100             @Mock\r
101             public int add(JSONObject jsonObject) throws ServiceException {\r
102                 return 1;\r
103             }\r
104         };\r
105         HttpServletRequest mock = new MockHttpServletRequest();\r
106         JSONObject result = roa.addHost(mock);\r
107         assertNotNull(result);\r
108     }\r
109 \r
110     @Test\r
111     public void testAddHostByException() throws ServiceException {\r
112         new MockUp<RequestUtil>() {\r
113 \r
114             @Mock\r
115             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
116                 return new JSONObject();\r
117             }\r
118         };\r
119         new MockUp<HostImpl>() {\r
120 \r
121             @Mock\r
122             public int add(JSONObject jsonObject) throws ServiceException {\r
123                 throw new ServiceException();\r
124             }\r
125         };\r
126         HttpServletRequest mock = new MockHttpServletRequest();\r
127         JSONObject result = roa.addHost(mock);\r
128         assertNotNull(result);\r
129     }\r
130 \r
131     @Test\r
132     public void testDeleteHost() throws ServiceException {\r
133         new MockUp<HostImpl>() {\r
134 \r
135             @Mock\r
136             public int delete(String id) throws ServiceException {\r
137                 return 1;\r
138             }\r
139         };\r
140         HttpServletRequest mock = new MockHttpServletRequest();\r
141         JSONObject result = roa.deleteHost(mock, "id");\r
142         assertNotNull(result);\r
143     }\r
144 \r
145     @Test\r
146     public void testDeleteHostByException() throws ServiceException {\r
147         new MockUp<HostImpl>() {\r
148 \r
149             @Mock\r
150             public int delete(String id) throws ServiceException {\r
151                 throw new ServiceException();\r
152             }\r
153         };\r
154         HttpServletRequest mock = new MockHttpServletRequest();\r
155         JSONObject result = roa.deleteHost(mock, "id");\r
156         assertNotNull(result);\r
157     }\r
158 \r
159     @Test\r
160     public void testUpdateHost() throws ServiceException {\r
161         new MockUp<RequestUtil>() {\r
162 \r
163             @Mock\r
164             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
165                 return new JSONObject();\r
166             }\r
167         };\r
168         new MockUp<HostImpl>() {\r
169 \r
170             @Mock\r
171             public int update(JSONObject jsonObject) throws ServiceException {\r
172                 return 1;\r
173             }\r
174         };\r
175         HttpServletRequest mock = new MockHttpServletRequest();\r
176         JSONObject result = roa.updateHost(mock);\r
177         assertNotNull(result);\r
178     }\r
179 \r
180     @Test\r
181     public void testUpdateHostByException() throws ServiceException {\r
182         new MockUp<RequestUtil>() {\r
183 \r
184             @Mock\r
185             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
186                 return new JSONObject();\r
187             }\r
188         };\r
189         new MockUp<HostImpl>() {\r
190 \r
191             @Mock\r
192             public int update(JSONObject jsonObject) throws ServiceException {\r
193                 throw new ServiceException();\r
194             }\r
195         };\r
196         HttpServletRequest mock = new MockHttpServletRequest();\r
197         JSONObject result = roa.updateHost(mock);\r
198         assertNotNull(result);\r
199     }\r
200 }\r