e2c402a0ea0c70034708238aa3e59a39c3bac178
[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.onap.vfc.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.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;\r
30 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.impl.HostImpl;\r
31 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Host;\r
32 import org.onap.vfc.nfvo.resmanagement.service.entity.HostEntity;\r
33 import org.onap.vfc.nfvo.resmanagement.service.rest.HostRoa;\r
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;\r
35 import org.springframework.mock.web.MockHttpServletRequest;\r
36 \r
37 import mockit.Mock;\r
38 import mockit.MockUp;\r
39 import net.sf.json.JSONObject;\r
40 \r
41 /**\r
42  * <br>\r
43  * <p>\r
44  * </p>\r
45  * \r
46  * @author\r
47  * @version NFVO 0.5 Feb 9, 2017\r
48  */\r
49 public class HostRoaTest {\r
50 \r
51     private HostRoa roa;\r
52 \r
53     private Host host;\r
54 \r
55     @Before\r
56     public void setUp() {\r
57         roa = new HostRoa();\r
58         host = new HostImpl();\r
59         roa.setHost(host);\r
60     }\r
61 \r
62     @Test\r
63     public void testGetHosts() throws ServiceException {\r
64         new MockUp<HostImpl>() {\r
65 \r
66             @Mock\r
67             public List<HostEntity> getList(Map<String, Object> condition) throws ServiceException {\r
68                 return new ArrayList<HostEntity>();\r
69             }\r
70         };\r
71         HttpServletRequest mock = new MockHttpServletRequest();\r
72         JSONObject result = roa.getHosts(mock);\r
73         assertNotNull(result);\r
74     }\r
75 \r
76     @Test\r
77     public void testGetHost() throws ServiceException {\r
78         new MockUp<HostImpl>() {\r
79 \r
80             @Mock\r
81             public List<HostEntity> getList(Map<String, Object> condition) throws ServiceException {\r
82                 return new ArrayList<HostEntity>();\r
83             }\r
84         };\r
85         HttpServletRequest mock = new MockHttpServletRequest();\r
86         JSONObject result = roa.getHost(mock, "id");\r
87         assertNotNull(result);\r
88     }\r
89 \r
90     @Test\r
91     public void testAddHost() throws ServiceException {\r
92         new MockUp<RequestUtil>() {\r
93 \r
94             @Mock\r
95             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
96                 return new JSONObject();\r
97             }\r
98         };\r
99         new MockUp<HostImpl>() {\r
100 \r
101             @Mock\r
102             public int add(JSONObject jsonObject) throws ServiceException {\r
103                 return 1;\r
104             }\r
105         };\r
106         HttpServletRequest mock = new MockHttpServletRequest();\r
107         JSONObject result = roa.addHost(mock);\r
108         assertNotNull(result);\r
109     }\r
110 \r
111     @Test\r
112     public void testAddHostByException() throws ServiceException {\r
113         new MockUp<RequestUtil>() {\r
114 \r
115             @Mock\r
116             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
117                 return new JSONObject();\r
118             }\r
119         };\r
120         new MockUp<HostImpl>() {\r
121 \r
122             @Mock\r
123             public int add(JSONObject jsonObject) throws ServiceException {\r
124                 throw new ServiceException();\r
125             }\r
126         };\r
127         HttpServletRequest mock = new MockHttpServletRequest();\r
128         JSONObject result = roa.addHost(mock);\r
129         assertNotNull(result);\r
130     }\r
131 \r
132     @Test\r
133     public void testDeleteHost() throws ServiceException {\r
134         new MockUp<HostImpl>() {\r
135 \r
136             @Mock\r
137             public int delete(String id) throws ServiceException {\r
138                 return 1;\r
139             }\r
140         };\r
141         HttpServletRequest mock = new MockHttpServletRequest();\r
142         JSONObject result = roa.deleteHost(mock, "id");\r
143         assertNotNull(result);\r
144     }\r
145 \r
146     @Test\r
147     public void testDeleteHostByException() throws ServiceException {\r
148         new MockUp<HostImpl>() {\r
149 \r
150             @Mock\r
151             public int delete(String id) throws ServiceException {\r
152                 throw new ServiceException();\r
153             }\r
154         };\r
155         HttpServletRequest mock = new MockHttpServletRequest();\r
156         JSONObject result = roa.deleteHost(mock, "id");\r
157         assertNotNull(result);\r
158     }\r
159 \r
160     @Test\r
161     public void testUpdateHost() throws ServiceException {\r
162         new MockUp<RequestUtil>() {\r
163 \r
164             @Mock\r
165             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
166                 return new JSONObject();\r
167             }\r
168         };\r
169         new MockUp<HostImpl>() {\r
170 \r
171             @Mock\r
172             public int update(JSONObject jsonObject) throws ServiceException {\r
173                 return 1;\r
174             }\r
175         };\r
176         HttpServletRequest mock = new MockHttpServletRequest();\r
177         JSONObject result = roa.updateHost(mock);\r
178         assertNotNull(result);\r
179     }\r
180 \r
181     @Test\r
182     public void testUpdateHostByException() throws ServiceException {\r
183         new MockUp<RequestUtil>() {\r
184 \r
185             @Mock\r
186             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
187                 return new JSONObject();\r
188             }\r
189         };\r
190         new MockUp<HostImpl>() {\r
191 \r
192             @Mock\r
193             public int update(JSONObject jsonObject) throws ServiceException {\r
194                 throw new ServiceException();\r
195             }\r
196         };\r
197         HttpServletRequest mock = new MockHttpServletRequest();\r
198         JSONObject result = roa.updateHost(mock);\r
199         assertNotNull(result);\r
200     }\r
201 }\r