9e4d127302e68d15db979f6723dcd4a6485776ab
[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.PortImpl;\r
32 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Port;\r
33 import org.openo.nfvo.resmanagement.service.entity.PortEntity;\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 PortRoaTest {\r
49 \r
50     private PortRoa roa;\r
51 \r
52     private Port port;\r
53 \r
54     @Before\r
55     public void setUp() {\r
56         roa = new PortRoa();\r
57         port = new PortImpl();\r
58         roa.setPort(port);\r
59     }\r
60 \r
61     @Test\r
62     public void testGetPorts() throws ServiceException {\r
63         new MockUp<PortImpl>() {\r
64 \r
65             @Mock\r
66             public List<PortEntity> getList(Map<String, Object> condition) throws ServiceException {\r
67                 return new ArrayList<PortEntity>();\r
68             }\r
69         };\r
70         HttpServletRequest mock = new MockHttpServletRequest();\r
71         JSONObject result = roa.getPorts(mock);\r
72         assertNotNull(result);\r
73     }\r
74 \r
75     @Test\r
76     public void testGetPort() throws ServiceException {\r
77         new MockUp<PortImpl>() {\r
78 \r
79             @Mock\r
80             public List<PortEntity> getList(Map<String, Object> condition) throws ServiceException {\r
81                 return new ArrayList<PortEntity>();\r
82             }\r
83         };\r
84         HttpServletRequest mock = new MockHttpServletRequest();\r
85         JSONObject result = roa.getPort(mock, "id");\r
86         assertNotNull(result);\r
87     }\r
88 \r
89     @Test\r
90     public void testAddPort() 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<PortEntity>() {\r
99 \r
100             @Mock\r
101             public PortEntity toEntity(JSONObject jsonObject) {\r
102                 return new PortEntity();\r
103             }\r
104         };\r
105         new MockUp<PortImpl>() {\r
106 \r
107             @Mock\r
108             public int add(PortEntity portEntity) throws ServiceException {\r
109                 return 1;\r
110             }\r
111         };\r
112         HttpServletRequest mock = new MockHttpServletRequest();\r
113         JSONObject result = roa.addPort(mock);\r
114         assertNotNull(result);\r
115     }\r
116 \r
117     @Test\r
118     public void testAddPortByException() throws ServiceException {\r
119         new MockUp<RequestUtil>() {\r
120 \r
121             @Mock\r
122             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
123                 return new JSONObject();\r
124             }\r
125         };\r
126         new MockUp<PortEntity>() {\r
127 \r
128             @Mock\r
129             public PortEntity toEntity(JSONObject jsonObject) {\r
130                 return new PortEntity();\r
131             }\r
132         };\r
133         new MockUp<PortImpl>() {\r
134 \r
135             @Mock\r
136             public int add(PortEntity portEntity) throws ServiceException {\r
137                 throw new ServiceException();\r
138             }\r
139         };\r
140         HttpServletRequest mock = new MockHttpServletRequest();\r
141         JSONObject result = roa.addPort(mock);\r
142         assertNotNull(result);\r
143     }\r
144 \r
145     @Test\r
146     public void testDeletePort() throws ServiceException {\r
147         new MockUp<PortImpl>() {\r
148 \r
149             @Mock\r
150             public int delete(String id) throws ServiceException {\r
151                 return 1;\r
152             }\r
153         };\r
154         HttpServletRequest mock = new MockHttpServletRequest();\r
155         JSONObject result = roa.deletePort(mock, "id");\r
156         assertNotNull(result);\r
157     }\r
158 \r
159     @Test\r
160     public void testDeletePortByException() throws ServiceException {\r
161         new MockUp<PortImpl>() {\r
162 \r
163             @Mock\r
164             public int delete(String id) throws ServiceException {\r
165                 throw new ServiceException();\r
166             }\r
167         };\r
168         HttpServletRequest mock = new MockHttpServletRequest();\r
169         JSONObject result = roa.deletePort(mock, "id");\r
170         assertNotNull(result);\r
171     }\r
172 \r
173     @Test\r
174     public void testUpdatePort() throws ServiceException {\r
175         new MockUp<RequestUtil>() {\r
176 \r
177             @Mock\r
178             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
179                 return new JSONObject();\r
180             }\r
181         };\r
182         new MockUp<PortImpl>() {\r
183 \r
184             @Mock\r
185             public int update(JSONObject jsonObject) throws ServiceException {\r
186                 return 1;\r
187             }\r
188         };\r
189         HttpServletRequest mock = new MockHttpServletRequest();\r
190         JSONObject result = roa.updatePort(mock);\r
191         assertNotNull(result);\r
192     }\r
193 \r
194     @Test\r
195     public void testUpdatePortByException() throws ServiceException {\r
196         new MockUp<RequestUtil>() {\r
197 \r
198             @Mock\r
199             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
200                 return new JSONObject();\r
201             }\r
202         };\r
203         new MockUp<PortImpl>() {\r
204 \r
205             @Mock\r
206             public int update(JSONObject jsonObject) throws ServiceException {\r
207                 throw new ServiceException();\r
208             }\r
209         };\r
210         HttpServletRequest mock = new MockHttpServletRequest();\r
211         JSONObject result = roa.updatePort(mock);\r
212         assertNotNull(result);\r
213     }\r
214 }\r