332cf2dab3e107cc5b210dbb88e214daf39c6271
[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.entity.VnfEntity;\r
32 import org.openo.nfvo.resmanagement.service.group.impl.VnfServiceImpl;\r
33 import org.openo.nfvo.resmanagement.service.group.inf.VnfService;\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 10, 2017\r
47  */\r
48 public class VnfRoaTest {\r
49 \r
50     private VnfRoa roa;\r
51 \r
52     private VnfService vnfService;\r
53 \r
54     @Before\r
55     public void setUp() {\r
56         roa = new VnfRoa();\r
57         vnfService = new VnfServiceImpl();\r
58         roa.setVnfService(vnfService);\r
59     }\r
60 \r
61     @Test\r
62     public void testGetVnfs() throws ServiceException {\r
63         new MockUp<VnfServiceImpl>() {\r
64 \r
65             @Mock\r
66             public List<VnfEntity> getList(Map<String, Object> map) throws ServiceException {\r
67                 return new ArrayList<VnfEntity>();\r
68             }\r
69         };\r
70         HttpServletRequest mock = new MockHttpServletRequest();\r
71         JSONObject result = roa.getVnfs(mock);\r
72         assertNotNull(result);\r
73     }\r
74 \r
75     @Test\r
76     public void testGetVnf() throws ServiceException {\r
77         new MockUp<VnfServiceImpl>() {\r
78 \r
79             @Mock\r
80             public List<VnfEntity> getList(Map<String, Object> map) throws ServiceException {\r
81                 return new ArrayList<VnfEntity>();\r
82             }\r
83         };\r
84         HttpServletRequest mock = new MockHttpServletRequest();\r
85         JSONObject result = roa.getVnf(mock, "id");\r
86         assertNotNull(result);\r
87     }\r
88 \r
89     @Test\r
90     public void testAddVnf() 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<VnfEntity>() {\r
99 \r
100             @Mock\r
101             public VnfEntity toEntity(JSONObject jsonObject) {\r
102                 return new VnfEntity();\r
103             }\r
104         };\r
105         new MockUp<VnfServiceImpl>() {\r
106 \r
107             @Mock\r
108             public JSONObject addVnf(VnfEntity vnfEntity) throws ServiceException {\r
109                 return new JSONObject();\r
110             }\r
111         };\r
112         HttpServletRequest mock = new MockHttpServletRequest();\r
113         JSONObject result = roa.addVnf(mock);\r
114         assertNotNull(result);\r
115     }\r
116 \r
117     @Test\r
118     public void testAddVnfByException() 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<VnfEntity>() {\r
127 \r
128             @Mock\r
129             public VnfEntity toEntity(JSONObject jsonObject) {\r
130                 return new VnfEntity();\r
131             }\r
132         };\r
133         new MockUp<VnfServiceImpl>() {\r
134 \r
135             @Mock\r
136             public JSONObject addVnf(VnfEntity vnfEntity) throws ServiceException {\r
137                 throw new ServiceException();\r
138             }\r
139         };\r
140         HttpServletRequest mock = new MockHttpServletRequest();\r
141         JSONObject result = roa.addVnf(mock);\r
142         assertNotNull(result);\r
143     }\r
144 \r
145     @Test(expected = ServiceException.class)\r
146     public void testAddVnfByNull() throws ServiceException {\r
147         new MockUp<RequestUtil>() {\r
148 \r
149             @Mock\r
150             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
151                 return null;\r
152             }\r
153         };\r
154         HttpServletRequest mock = new MockHttpServletRequest();\r
155         roa.addVnf(mock);\r
156     }\r
157 \r
158     @Test\r
159     public void testDeleteVnf() throws ServiceException {\r
160         new MockUp<VnfServiceImpl>() {\r
161 \r
162             @Mock\r
163             public int delete(String id) throws ServiceException {\r
164                 return 1;\r
165             }\r
166         };\r
167         HttpServletRequest mock = new MockHttpServletRequest();\r
168         JSONObject result = roa.deleteVnf(mock, "id");\r
169         assertNotNull(result);\r
170     }\r
171 \r
172     @Test\r
173     public void testDeleteVnfByException() throws ServiceException {\r
174         new MockUp<VnfServiceImpl>() {\r
175 \r
176             @Mock\r
177             public int delete(String id) throws ServiceException {\r
178                 throw new ServiceException();\r
179             }\r
180         };\r
181         HttpServletRequest mock = new MockHttpServletRequest();\r
182         JSONObject result = roa.deleteVnf(mock, "id");\r
183         assertNotNull(result);\r
184     }\r
185 \r
186     @Test(expected = ServiceException.class)\r
187     public void testDeleteVnfByNull() throws ServiceException {\r
188         HttpServletRequest mock = new MockHttpServletRequest();\r
189         roa.deleteVnf(mock, null);\r
190     }\r
191 }\r