2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.gso.gui.servicegateway.roa.impl;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.sql.SQLException;
24 import javax.servlet.http.HttpServletRequest;
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;
43 * Test ServicemgrRoaModuleImpl class.<br/>
48 * @version GSO 0.5 2016/8/3
50 public class ServiceGatewayRoaModuleImplTest {
55 private static final String FILE_PATH = "src/test/resources/json/";
60 ServiceGatewayRoaModuleImpl serviceRoa = new ServiceGatewayRoaModuleImpl();
65 ServiceGatewayImpl serviceManager = new ServiceGatewayImpl();
71 HttpServletRequest httpRequest;
76 RestfulResponse responseSuccess;
79 * Before executing UT, start sql.<br/>
84 public void start() throws IOException, SQLException {
85 responseSuccess = new RestfulResponse();
86 responseSuccess.setStatus(HttpCode.RESPOND_OK);
92 * After executing UT, close session<br/>
102 * Test create service.<br/>
104 * @throws ServiceException when fail to operate database or parameter is wrong.
108 public void testCreateService() throws ServiceException {
110 mockGetRequestBody(FILE_PATH + "createServiceInstance.json");
112 mockPost(responseSuccess);
114 serviceRoa.createService(httpRequest);
118 * Test delete service.<br/>
120 * @throws ServiceException when fail to operate database or parameter is wrong.
124 public void testDeleteService() throws ServiceException {
125 serviceRoa.deleteService("1", httpRequest);
129 * Mock to get request body.<br/>
131 * @param file json file path.
134 private void mockGetRequestBody(final String file) {
135 new MockUp<RestUtils>() {
138 public String getRequestBody(HttpServletRequest request) {
139 return getJsonString(file);
145 * Mock rest request for post.<br/>
147 * @param response rest response
150 private void mockPost(final RestfulResponse response) {
151 new MockUp<HttpUtil>() {
154 public RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest) {
161 * Get json string from file.<br/>
163 * @param file the path of file
164 * @return json string
165 * @throws IOException when fail to read
168 private String getJsonString(final String file) {
169 if(StringUtils.isEmpty(file)) {
175 FileInputStream fileStream = new FileInputStream(new File(file));
176 json = IOUtils.toString(fileStream);
177 } catch(Exception e) {
178 Assert.fail(e.getMessage());