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