8ecd1d530286f54d28f46874172634880bccd13a
[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.entity.VmEntity;\r
31 import org.onap.vfc.nfvo.resmanagement.service.group.impl.VmServiceImpl;\r
32 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VmService;\r
33 import org.onap.vfc.nfvo.resmanagement.service.rest.VmRoa;\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 VmRoaTest {\r
50 \r
51     private VmRoa roa;\r
52 \r
53     private VmService vmService;\r
54 \r
55     @Before\r
56     public void setUp() {\r
57         roa = new VmRoa();\r
58         vmService = new VmServiceImpl();\r
59         roa.setVmService(vmService);\r
60     }\r
61 \r
62     @Test\r
63     public void testGetVms() throws ServiceException {\r
64         new MockUp<VmServiceImpl>() {\r
65 \r
66             @Mock\r
67             public List<VmEntity> getList(Map<String, Object> map) throws ServiceException {\r
68                 return new ArrayList<VmEntity>();\r
69             }\r
70         };\r
71         HttpServletRequest mock = new MockHttpServletRequest();\r
72         JSONObject result = roa.getVms(mock);\r
73         assertNotNull(result);\r
74     }\r
75 \r
76     @Test\r
77     public void testGetVm() throws ServiceException {\r
78         new MockUp<VmServiceImpl>() {\r
79 \r
80             @Mock\r
81             public List<VmEntity> getList(Map<String, Object> map) throws ServiceException {\r
82                 return new ArrayList<VmEntity>();\r
83             }\r
84         };\r
85         HttpServletRequest mock = new MockHttpServletRequest();\r
86         JSONObject result = roa.getVm(mock, "id");\r
87         assertNotNull(result);\r
88     }\r
89 \r
90     @Test\r
91     public void testAddVm() 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<VmEntity>() {\r
100 \r
101             @Mock\r
102             public VmEntity toEntity(JSONObject jsonObject) {\r
103                 return new VmEntity();\r
104             }\r
105         };\r
106         new MockUp<VmServiceImpl>() {\r
107 \r
108             @Mock\r
109             public JSONObject addVm(VmEntity vmEntity) throws ServiceException {\r
110                 return new JSONObject();\r
111             }\r
112         };\r
113         HttpServletRequest mock = new MockHttpServletRequest();\r
114         JSONObject result = roa.addVm(mock);\r
115         assertNotNull(result);\r
116     }\r
117 \r
118     @Test\r
119     public void testAddVmByException() throws ServiceException {\r
120         new MockUp<RequestUtil>() {\r
121 \r
122             @Mock\r
123             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
124                 return new JSONObject();\r
125             }\r
126         };\r
127         new MockUp<VmEntity>() {\r
128 \r
129             @Mock\r
130             public VmEntity toEntity(JSONObject jsonObject) {\r
131                 return new VmEntity();\r
132             }\r
133         };\r
134         new MockUp<VmServiceImpl>() {\r
135 \r
136             @Mock\r
137             public JSONObject addVm(VmEntity vmEntity) throws ServiceException {\r
138                 throw new ServiceException();\r
139             }\r
140         };\r
141         HttpServletRequest mock = new MockHttpServletRequest();\r
142         JSONObject result = roa.addVm(mock);\r
143         assertNotNull(result);\r
144     }\r
145 \r
146     @Test(expected = ServiceException.class)\r
147     public void testAddVmByNull() throws ServiceException {\r
148         new MockUp<RequestUtil>() {\r
149 \r
150             @Mock\r
151             public JSONObject getJsonRequestBody(HttpServletRequest context) {\r
152                 return null;\r
153             }\r
154         };\r
155         HttpServletRequest mock = new MockHttpServletRequest();\r
156         roa.addVm(mock);\r
157     }\r
158 \r
159     @Test\r
160     public void testDeleteVm() throws ServiceException {\r
161         new MockUp<VmServiceImpl>() {\r
162 \r
163             @Mock\r
164             public int delete(String id) throws ServiceException {\r
165                 return 1;\r
166             }\r
167         };\r
168         HttpServletRequest mock = new MockHttpServletRequest();\r
169         JSONObject result = roa.deleteVm(mock, "id");\r
170         assertNotNull(result);\r
171     }\r
172 \r
173     @Test\r
174     public void testDeleteVmByException() throws ServiceException {\r
175         new MockUp<VmServiceImpl>() {\r
176 \r
177             @Mock\r
178             public int delete(String id) throws ServiceException {\r
179                 throw new ServiceException();\r
180             }\r
181         };\r
182         HttpServletRequest mock = new MockHttpServletRequest();\r
183         JSONObject result = roa.deleteVm(mock, "id");\r
184         assertNotNull(result);\r
185     }\r
186 \r
187     @Test(expected = ServiceException.class)\r
188     public void testDeleteVmByNull() throws ServiceException {\r
189         HttpServletRequest mock = new MockHttpServletRequest();\r
190         JSONObject result = roa.deleteVm(mock, null);\r
191         assertNotNull(result);\r
192     }\r
193 \r
194 }\r