4fd620ccdb10abcf927101187a9ca9b890258b7c
[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         mockGetRequestBody(FILE_PATH + "createServiceInstance.json");        
126         mockPost(responseSuccess);
127         serviceRoa.deleteService("1", httpRequest);
128     }
129
130     /**
131      * Mock to get request body.<br/>
132      * 
133      * @param file json file path.
134      * @since GSO 0.5
135      */
136     private void mockGetRequestBody(final String file) {
137         new MockUp<RestUtils>() {
138
139             @Mock
140             public String getRequestBody(HttpServletRequest request) {
141                 return getJsonString(file);
142             }
143         };
144     }
145     
146     /**
147      * Mock rest request for post.<br/>
148      * 
149      * @param response rest response
150      * @since GSO 0.5
151      */
152     private void mockPost(final RestfulResponse response) {
153         new MockUp<HttpUtil>() {
154
155             @Mock
156             public RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest) {
157                 return response;
158             }
159         };
160     }
161     
162     /**
163      * Get json string from file.<br/>
164      * 
165      * @param file the path of file
166      * @return json string
167      * @throws IOException when fail to read
168      * @since GSO 0.5
169      */
170     private String getJsonString(final String file) {
171         if(StringUtils.isEmpty(file)) {
172             return "";
173         }
174
175         String json = null;
176         try {
177             FileInputStream fileStream = new FileInputStream(new File(file));
178             json = IOUtils.toString(fileStream);
179         } catch(Exception e) {
180             Assert.fail(e.getMessage());
181         }
182
183         return json;
184     }
185 }