fabe83d790742fc5dacbf72a9b49e986177dc076
[vnfsdk/refrepo.git] /
1 /*\r
2  * Copyright (c) 2016, 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.gso.gui.servicegateway.roa.impl;\r
18 \r
19 import java.io.File;\r
20 import java.io.FileInputStream;\r
21 import java.io.IOException;\r
22 import java.sql.SQLException;\r
23 \r
24 import javax.servlet.http.HttpServletRequest;\r
25 \r
26 import org.apache.commons.io.IOUtils;\r
27 import org.apache.commons.lang.StringUtils;\r
28 import org.junit.After;\r
29 import org.junit.Assert;\r
30 import org.junit.Before;\r
31 import org.junit.Test;\r
32 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
33 import org.openo.baseservice.roa.util.restclient.RestfulResponse;\r
34 import org.openo.baseservice.util.RestUtils;\r
35 import org.openo.gso.gui.servicegateway.exception.HttpCode;\r
36 import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;\r
37 import org.openo.gso.gui.servicegateway.util.http.HttpUtil;\r
38 \r
39 import mockit.Mock;\r
40 import mockit.MockUp;\r
41 \r
42 /**\r
43  * Test ServicemgrRoaModuleImpl class.<br/>\r
44  * <p>\r
45  * </p>\r
46  * \r
47  * @author\r
48  * @version GSO 0.5 2016/8/3\r
49  */\r
50 public class ServiceGatewayRoaModuleImplTest {\r
51 \r
52         /**\r
53      * File path\r
54      */\r
55     private static final String FILE_PATH = "src/test/resources/json/";\r
56         \r
57     /**\r
58      * Service ROA.\r
59      */\r
60         ServiceGatewayRoaModuleImpl serviceRoa = new ServiceGatewayRoaModuleImpl();\r
61 \r
62     /**\r
63      * Service manager.\r
64      */\r
65         ServiceGatewayImpl serviceManager = new ServiceGatewayImpl();\r
66 \r
67 \r
68     /**\r
69      * Http request.\r
70      */\r
71     HttpServletRequest httpRequest;\r
72     \r
73     /**\r
74      * Rest response.\r
75      */\r
76     RestfulResponse responseSuccess;\r
77 \r
78     /**\r
79      * Before executing UT, start sql.<br/>\r
80      * \r
81      * @since GSO 0.5\r
82      */\r
83     @Before\r
84     public void start() throws IOException, SQLException {\r
85         responseSuccess = new RestfulResponse();\r
86         responseSuccess.setStatus(HttpCode.RESPOND_OK);\r
87     }\r
88 \r
89 \r
90 \r
91     /**\r
92      * After executing UT, close session<br/>\r
93      * \r
94      * @since GSO 0.5\r
95      */\r
96     @After\r
97     public void stop() {\r
98 \r
99     }\r
100 \r
101     /**\r
102      * Test create service.<br/>\r
103      * \r
104      * @throws ServiceException when fail to operate database or parameter is wrong.\r
105      * @since GSO 0.5\r
106      */\r
107     @Test\r
108     public void testCreateService() throws ServiceException {\r
109         // mock request body\r
110         mockGetRequestBody(FILE_PATH + "createServiceInstance.json");\r
111         \r
112         mockPost(responseSuccess);\r
113 \r
114         serviceRoa.createService(httpRequest);\r
115     }\r
116 \r
117     /**\r
118      * Test delete service.<br/>\r
119      * \r
120      * @throws ServiceException when fail to operate database or parameter is wrong.\r
121      * @since GSO 0.5\r
122      */\r
123     @Test\r
124     public void testDeleteService() throws ServiceException {\r
125         serviceRoa.deleteService("1", httpRequest);\r
126     }\r
127 \r
128     /**\r
129      * Mock to get request body.<br/>\r
130      * \r
131      * @param file json file path.\r
132      * @since GSO 0.5\r
133      */\r
134     private void mockGetRequestBody(final String file) {\r
135         new MockUp<RestUtils>() {\r
136 \r
137             @Mock\r
138             public String getRequestBody(HttpServletRequest request) {\r
139                 return getJsonString(file);\r
140             }\r
141         };\r
142     }\r
143     \r
144     /**\r
145      * Mock rest request for post.<br/>\r
146      * \r
147      * @param response rest response\r
148      * @since GSO 0.5\r
149      */\r
150     private void mockPost(final RestfulResponse response) {\r
151         new MockUp<HttpUtil>() {\r
152 \r
153             @Mock\r
154             public RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest) {\r
155                 return response;\r
156             }\r
157         };\r
158     }\r
159     \r
160     /**\r
161      * Get json string from file.<br/>\r
162      * \r
163      * @param file the path of file\r
164      * @return json string\r
165      * @throws IOException when fail to read\r
166      * @since GSO 0.5\r
167      */\r
168     private String getJsonString(final String file) {\r
169         if(StringUtils.isEmpty(file)) {\r
170             return "";\r
171         }\r
172 \r
173         String json = null;\r
174         try {\r
175             FileInputStream fileStream = new FileInputStream(new File(file));\r
176             json = IOUtils.toString(fileStream);\r
177         } catch(Exception e) {\r
178             Assert.fail(e.getMessage());\r
179         }\r
180 \r
181         return json;\r
182     }\r
183 }\r