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