aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorj180240 <jiaxiangli@huawei.com>2016-09-18 15:07:24 +0800
committerj180240 <jiaxiangli@huawei.com>2016-09-18 15:07:24 +0800
commit02d4efdac4a6c3ce0b5faf9f1d392c21009f1dd7 (patch)
tree8c254541646838da284790c3ecbd7079894bf6c3
parent2c0dc0d3729fae91cafd8c1843c2aa3357a056f1 (diff)
update the serviceway for createService.
Change-Id: Ib90cb71e412db73720ce84d90bbd0916200bc5f1 Signed-off-by: jiaxiangli <jiaxiangli@huawei.com>
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/activator/Activator.java23
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/constant/Constant.java76
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/ErrorCode.java15
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImpl.java43
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImpl.java66
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/inf/IServiceGateway.java2
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/ResponseUtils.java17
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/register/RegisterUtil.java161
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/validate/ValidateUtil.java83
-rw-r--r--servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImplTest.java96
-rw-r--r--servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImplTest.java1
-rw-r--r--servicegateway/service/src/test/resources/json/createServiceInstance.json12
12 files changed, 530 insertions, 65 deletions
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/activator/Activator.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/activator/Activator.java
index befe9328..2f539e7e 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/activator/Activator.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/activator/Activator.java
@@ -20,6 +20,8 @@ import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
+import org.openo.gso.gui.servicegateway.constant.Constant;
+import org.openo.gso.gui.servicegateway.util.register.RegisterUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,6 +35,7 @@ public class Activator {
private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
+
/**
* start operation by spring <br/>
*
@@ -55,18 +58,18 @@ public class Activator {
private void registerService() {
// get the jsonString form the service file
String root = SystemEnvVariablesFactory.getInstance().getAppRoot();
-// String serviceFilePath = root + File.separator + Constant.FILE_PATH_ETC + File.separator
-// + Constant.FILE_PATH_REGISTER + File.separator + Constant.FILE_PATH_JSON;
-// String jsonInfo = RegisterUtil.readFile(serviceFilePath);
-//
-// // check the jsonInfo
-// if(StringUtils.isEmpty(jsonInfo)) {
-// LOGGER.error("GSO ReadFile fail: jsonInfo is null. the serviceFilePath=" + serviceFilePath);
-// return;
-// }
+ String serviceFilePath = root + File.separator + Constant.FILE_PATH_ETC + File.separator
+ + Constant.FILE_PATH_REGISTER + File.separator + Constant.FILE_PATH_JSON;
+ String jsonInfo = RegisterUtil.readFile(serviceFilePath);
+
+ // check the jsonInfo
+ if(StringUtils.isEmpty(jsonInfo)) {
+ LOGGER.error("ServiceGateway ReadFile fail: jsonInfo is null. the serviceFilePath=" + serviceFilePath);
+ return;
+ }
// register service by the jsonInfo
-// RegisterUtil.registerService(jsonInfo);
+ RegisterUtil.registerService(jsonInfo);
}
/**
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/constant/Constant.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/constant/Constant.java
index c5283fac..58b0b227 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/constant/Constant.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/constant/Constant.java
@@ -26,16 +26,11 @@ package org.openo.gso.gui.servicegateway.constant;
*/
public class Constant {
- /**
- * Service instance name.
- */
- public static final String SERVICE_NAME = "name";
-
- /**
- * Service instance description.
+ /**
+ * Service instance ID.
*/
- public static final String SERVICE_DESCRIPTION = "description";
-
+ public static final String SERVICE_INDENTIFY = "service";
+
/**
* Service instance ID.
*/
@@ -47,19 +42,10 @@ public class Constant {
public static final String SERVICE_DEF_ID = "serviceDefId";
/**
- * Service template ID.
+ * Service gateway URL.
*/
- public static final String SERVICE_TEMPLATE_ID = "templateId";
+ public static final String SERVICE_GATEWAY_URI = "gatewayUri";
- /**
- * Service template name.
- */
- public static final String SERVICE_TEMPLATE_NAME = "templateName";
-
- /**
- * service parameters
- */
- public static final String SERVICE_PARAMETERS = "parameters";
/**
* Create workflow name.
@@ -112,19 +98,59 @@ public class Constant {
public static final String RESPONSE_STATUS_FAIL = "fail";
/**
- * Identify of csar.
+ * ID of instance.
*/
- public static final String CSAR_ID = "csarId";
+ public static final String NS_INSTANCE_ID = "nsInstanceId";
/**
* service identify.
*/
- public static final String SERVICE_INDENTIFY = "service";
+ public static final String SERVICE_ID = "serviceId";
+
+ /**
+ * job Id
+ */
+ public static final String JOB_ID = "jobId";
+
+ /**
+ * service file Path:etc
+ */
+ public static final String FILE_PATH_ETC = "etc";
+
+ /**
+ * service file Path:register
+ */
+ public static final String FILE_PATH_REGISTER = "register";
+
+ /**
+ * service file Path:file name
+ */
+ public static final String FILE_PATH_JSON = "service.json";
+
+ /**
+ * the head type for Restful
+ */
+ public static final String HEAD_ERMAP_TYPE = "Content-Type";
+
+ /**
+ * the head value for Restful
+ */
+ public static final String HEAD_ERMAP_VALUE = "application/json;charset=UTF-8";
+
+ /**
+ * the URL for Register service to the M-service Bus
+ */
+ public static final String M_BUS_REGISTER_URL = "/openoapi/microservices/v1/services";
+
+ /**
+ * the IP key for the service file
+ */
+ public static final String SERVICE_KEY_IP = "getInputIP";
/**
- * Predefine parameter for service design
+ * constant:0
*/
- public static final String PREDEFINE_GSO_ID = "predefine_gsoId";
+ public static final int ZERO = 0;
/**
* Constructor<br/>
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/ErrorCode.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/ErrorCode.java
index 759344e1..1a94e70d 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/ErrorCode.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/ErrorCode.java
@@ -46,6 +46,21 @@ public class ErrorCode {
* Package is being deleted.
*/
public static final String SVCMGR_PACKAGE_BEING_DELETED = "servicemgr.package.beingDelete";
+
+ /**
+ * operation fail
+ */
+ public static final int FAIL = 3;
+
+ /**
+ * operation partial success
+ */
+ public static final int PARTIAL_SUCCESS = 1;
+
+ /**
+ * operation success
+ */
+ public static final int SUCCESS = 0;
/**
* Constructor<br/>
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImpl.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImpl.java
index 9b7796f9..7480bdc4 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImpl.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImpl.java
@@ -22,8 +22,14 @@ import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.util.RestUtils;
+import org.openo.gso.gui.servicegateway.constant.Constant;
+import org.openo.gso.gui.servicegateway.exception.HttpCode;
import org.openo.gso.gui.servicegateway.roa.inf.IServiceGatewayRoaModule;
+import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;
import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
+import org.openo.gso.gui.servicegateway.util.http.ResponseUtils;
+import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -45,21 +51,7 @@ public class ServiceGatewayRoaModuleImpl implements IServiceGatewayRoaModule {
/**
* Service manager.
*/
- private IServiceGateway serviceManager;
-
- /**
- * @return Returns the serviceManager.
- */
- public IServiceGateway getServiceGateway() {
- return serviceManager;
- }
-
- /**
- * @param serviceManager The serviceManager to set.
- */
- public void setServicemanager(IServiceGateway serviceManager) {
- this.serviceManager = serviceManager;
- }
+ private IServiceGateway serviceGateway = new ServiceGatewayImpl();
/**
* Create service instance.<br/>
@@ -71,7 +63,28 @@ public class ServiceGatewayRoaModuleImpl implements IServiceGatewayRoaModule {
*/
@Override
public Response createService(HttpServletRequest servletReq) {
+ Map<String, Object> operateStatus = null;
Map<String, Object> result = null;
+ String serviceId = null;
+ try {
+ // 1. Check validation
+ String reqContent = RestUtils.getRequestBody(servletReq);
+ ValidateUtil.assertStringNotNull(reqContent);
+
+ // 2. Create service
+ serviceId = serviceGateway.createService(reqContent, servletReq);
+ } catch(ServiceException exception) {
+ LOGGER.error("Fail to create service instance.");
+ operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_FAIL, exception,
+ String.valueOf(exception.getHttpCode()));
+ result = ResponseUtils.setResult(serviceId, operateStatus);
+
+ return Response.accepted().entity(result).build();
+ }
+
+ operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,
+ String.valueOf(HttpCode.RESPOND_OK));
+ result = ResponseUtils.setResult(serviceId, operateStatus);
return Response.accepted().entity(result).build();
}
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImpl.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImpl.java
index 3f2a48a7..e2200d4d 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImpl.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImpl.java
@@ -16,10 +16,19 @@
package org.openo.gso.gui.servicegateway.service.impl;
+import java.util.Map;
+
import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.lang.StringUtils;
import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.roa.util.restclient.RestfulFactory;
+import org.openo.baseservice.roa.util.restclient.RestfulParametes;
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;
+import org.openo.gso.gui.servicegateway.constant.Constant;
import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
+import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
+import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,10 +58,63 @@ public class ServiceGatewayImpl implements IServiceGateway {
*/
@SuppressWarnings("unchecked")
@Override
- public Object createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
+ public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
+ // check the value
+ if(StringUtils.isEmpty(reqContent))
+ {
+ LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
+ return null;
+ }
+
+ // Parse request
+ Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
+ Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
+ if(null == service)
+ {
+ service = requestBody;
+ }
+ ValidateUtil.assertObjectNotNull(requestBody);
+
+ // Validate data
+ String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
+ ValidateUtil.assertStringNotNull(gatewayUri);
+ service.remove(Constant.SERVICE_GATEWAY_URI);
+ // call the restful
+ String id = null;
+ try {
+ RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
+ getRestfulParameters(JsonUtil.marshal(requestBody)));
+ if (null != restfulRsp) {
+ // Record the result of registration
+ // (201:success;415:Invalid Parameter;500:Internal Server Error)
+ LOGGER.info("restful call result:", restfulRsp.getStatus());
+ id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
+ id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
+ id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
+ }
+ } catch(ServiceException e) {
+ LOGGER.error("service gateway create restful call result:", e);
+ throw e;
+ }
- return null;
+ return id;
+ }
+
+ /**
+ * get the parameters for restful<br/>
+ *
+ * @author
+ * @param bodyData
+ * Json Body
+ * @return the RestfulParametes Instance
+ * @since GSO 0.5, 2016-8-9
+ */
+ private static RestfulParametes getRestfulParameters(final String bodyData) {
+ RestfulParametes param = new RestfulParametes();
+ param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
+ param.setRawData(bodyData);
+ return param;
}
/**
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/inf/IServiceGateway.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/inf/IServiceGateway.java
index 77b27bd7..f9ecb748 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/inf/IServiceGateway.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/inf/IServiceGateway.java
@@ -40,7 +40,7 @@ public interface IServiceGateway {
* @throws ServiceException when operate DB or parameter is wrong.
* @since GSO 0.5
*/
- Object createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException;
+ String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException;
/**
* Delete service instances.<br/>
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/ResponseUtils.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/ResponseUtils.java
index d0df0781..7780e6e5 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/ResponseUtils.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/ResponseUtils.java
@@ -155,4 +155,21 @@ public class ResponseUtils {
return result;
}
+
+ /**
+ * Assemble operation result.<br/>
+ *
+ * @param objectId instance ID
+ * @param operateCode operation result
+ * @return Response result
+ * @since GSO 0.5
+ */
+ public static Map<String, Object> setResult(String objectId, Object operateCode) {
+ Map<String, Object> result = new HashMap<String, Object>();
+ result.put(Constant.RESPONSE_RESULT, operateCode);
+ String serviceId = (null != objectId) ? objectId : null;
+ result.put(Constant.SERVICE_INSTANCE_ID, serviceId);
+
+ return result;
+ }
}
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/register/RegisterUtil.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/register/RegisterUtil.java
new file mode 100644
index 00000000..4e8b6bd3
--- /dev/null
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/register/RegisterUtil.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2016 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.gso.gui.servicegateway.util.register;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.apache.commons.lang.StringUtils;
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.roa.util.restclient.RestfulFactory;
+import org.openo.baseservice.roa.util.restclient.RestfulParametes;
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;
+import org.openo.gso.commsvc.common.constant.Constant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Register Tool <br/>
+ *
+ * @author
+ * @since GSO 0.5, 2016-8-9
+ */
+public class RegisterUtil {
+
+ /**
+ * Logger
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(RegisterUtil.class);
+
+ private RegisterUtil() {
+ }
+
+ /**
+ * register the service to M-Bus by the parameter<br/>
+ *
+ * @author
+ * @param jsonInfo
+ * register body Data
+ * @since GSO 0.5, 2016-8-9
+ */
+ public static void registerService(final String jsonInfo) {
+ // check the parameter
+ if (StringUtils.isEmpty(jsonInfo)) {
+ LOGGER.error("RegisterUtil registerService jsonInfo is null");
+ return;
+ }
+
+ // replace the remote IP Address by the jsonInfo
+ String bodyData = jsonInfo;
+ boolean isIPExist = bodyData.indexOf(Constant.SERVICE_KEY_IP) > Constant.ZERO;
+ try {
+ // get the local IP address
+ String localIP = InetAddress.getLocalHost().getHostAddress();
+
+ // if the jsonInfo have the getInputIP string,start to replace the
+ // local IP
+ if (isIPExist) {
+ if (!StringUtils.isEmpty(localIP)) {
+ bodyData = bodyData.replace(Constant.SERVICE_KEY_IP, localIP);
+ } else {
+ LOGGER.error("RegisterUtil registerService localIP is null");
+ return;
+ }
+ }
+ } catch (UnknownHostException e) {
+ LOGGER.error("RegisterUtil registerService getHostAddress fail:", e);
+ if (isIPExist) {
+ // if get local IP failed In the isIPExist is true ,operation is
+ // stopped.
+ return;
+ }
+ }
+
+ // register the service to M-bus by the restful Interface
+ try {
+ RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(Constant.M_BUS_REGISTER_URL,
+ getRestfulParameters(bodyData));
+ if (null != restfulRsp) {
+ // Record the result of registration
+ // (201:success;415:Invalid Parameter;500:Internal Server Error)
+ LOGGER.info("RegisterUtil registerService register result:", restfulRsp.getStatus());
+ }
+ } catch (ServiceException e) {
+ LOGGER.error("RegisterUtil registerService post fail:", e);
+ }
+ }
+
+ /**
+ * get the parameters for restful<br/>
+ *
+ * @author
+ * @param bodyData
+ * Json Body
+ * @return the RestfulParametes Instance
+ * @since GSO 0.5, 2016-8-9
+ */
+ private static RestfulParametes getRestfulParameters(final String bodyData) {
+ RestfulParametes param = new RestfulParametes();
+ param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
+ param.setRawData(bodyData);
+ return param;
+ }
+
+ /**
+ * read the service file<br/>
+ *
+ * @param path
+ * the service File Path
+ * @return jsonString
+ * @since GSO 0.5, 2016-8-9
+ */
+ public static String readFile(String path) {
+ // check parameter
+ if (StringUtils.isEmpty(path)) {
+ return null;
+ }
+
+ File file = new File(path);
+ BufferedReader reader = null;
+ String laststr = "";
+ try {
+ reader = new BufferedReader(new FileReader(file));
+ String tempString = null;
+ // Read one line at a time until the end of the null file.
+ while ((tempString = reader.readLine()) != null) {
+ // add the line
+ laststr = laststr + tempString;
+ }
+ reader.close();
+ } catch (IOException e) {
+ LOGGER.error("GSO ReadFile fail.", e);
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e1) {
+ LOGGER.error("GSO ReadFile reader close fail.", e1);
+ }
+ }
+ }
+ return laststr;
+ }
+}
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/validate/ValidateUtil.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/validate/ValidateUtil.java
new file mode 100644
index 00000000..461163ea
--- /dev/null
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/validate/ValidateUtil.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2016 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.gso.gui.servicegateway.util.validate;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.gso.gui.servicegateway.exception.ErrorCode;
+import org.openo.gso.gui.servicegateway.exception.HttpCode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.StringUtils;
+
+/**
+ * Validate Util <br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/8/19
+ */
+public class ValidateUtil {
+
+ /**
+ * Log server.
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(ValidateUtil.class);
+
+ /**
+ * Constructor<br/>
+ * <p>
+ * </p>
+ *
+ * @since GSO 0.5
+ */
+ private ValidateUtil() {
+
+ }
+
+ /**
+ * Assert String parameter.<br/>
+ *
+ * @param param parameter data
+ * @throws ServiceException when parameter is null or empty.
+ * @since GSO 0.5
+ */
+ public static void assertStringNotNull(String param) throws ServiceException {
+ if(StringUtils.hasLength(param)) {
+ return;
+ }
+
+ LOGGER.error("Parameter is null or empty.");
+ throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, HttpCode.BAD_REQUEST);
+ }
+
+ /**
+ * Assert object is null.<br/>
+ *
+ * @param object data object
+ * @throws ServiceException when object is null.
+ * @since GSO 0.5
+ */
+ public static void assertObjectNotNull(Object object) throws ServiceException {
+ if(null == object) {
+ LOGGER.error("Object is null.");
+ throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "Object is null.");
+ }
+
+ }
+
+}
diff --git a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImplTest.java b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImplTest.java
index 8b5c6c67..fabe83d7 100644
--- a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImplTest.java
+++ b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImplTest.java
@@ -16,25 +16,28 @@
package org.openo.gso.gui.servicegateway.roa.impl;
-import static org.junit.Assert.assertNotNull;
-
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
-import java.io.Reader;
-import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
-import org.apache.ibatis.io.Resources;
-import org.apache.ibatis.jdbc.ScriptRunner;
-import org.apache.ibatis.session.SqlSession;
-import org.apache.ibatis.session.SqlSessionFactory;
-import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
import org.junit.After;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;
+import org.openo.baseservice.util.RestUtils;
+import org.openo.gso.gui.servicegateway.exception.HttpCode;
import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;
+import org.openo.gso.gui.servicegateway.util.http.HttpUtil;
+
+import mockit.Mock;
+import mockit.MockUp;
/**
* Test ServicemgrRoaModuleImpl class.<br/>
@@ -46,6 +49,11 @@ import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;
*/
public class ServiceGatewayRoaModuleImplTest {
+ /**
+ * File path
+ */
+ private static final String FILE_PATH = "src/test/resources/json/";
+
/**
* Service ROA.
*/
@@ -61,6 +69,11 @@ public class ServiceGatewayRoaModuleImplTest {
* Http request.
*/
HttpServletRequest httpRequest;
+
+ /**
+ * Rest response.
+ */
+ RestfulResponse responseSuccess;
/**
* Before executing UT, start sql.<br/>
@@ -69,7 +82,8 @@ public class ServiceGatewayRoaModuleImplTest {
*/
@Before
public void start() throws IOException, SQLException {
-
+ responseSuccess = new RestfulResponse();
+ responseSuccess.setStatus(HttpCode.RESPOND_OK);
}
@@ -92,7 +106,12 @@ public class ServiceGatewayRoaModuleImplTest {
*/
@Test
public void testCreateService() throws ServiceException {
+ // mock request body
+ mockGetRequestBody(FILE_PATH + "createServiceInstance.json");
+
+ mockPost(responseSuccess);
+ serviceRoa.createService(httpRequest);
}
/**
@@ -102,8 +121,63 @@ public class ServiceGatewayRoaModuleImplTest {
* @since GSO 0.5
*/
@Test
- public void testTeleteService() throws ServiceException {
+ public void testDeleteService() throws ServiceException {
serviceRoa.deleteService("1", httpRequest);
}
+ /**
+ * Mock to get request body.<br/>
+ *
+ * @param file json file path.
+ * @since GSO 0.5
+ */
+ private void mockGetRequestBody(final String file) {
+ new MockUp<RestUtils>() {
+
+ @Mock
+ public String getRequestBody(HttpServletRequest request) {
+ return getJsonString(file);
+ }
+ };
+ }
+
+ /**
+ * Mock rest request for post.<br/>
+ *
+ * @param response rest response
+ * @since GSO 0.5
+ */
+ private void mockPost(final RestfulResponse response) {
+ new MockUp<HttpUtil>() {
+
+ @Mock
+ public RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest) {
+ return response;
+ }
+ };
+ }
+
+ /**
+ * Get json string from file.<br/>
+ *
+ * @param file the path of file
+ * @return json string
+ * @throws IOException when fail to read
+ * @since GSO 0.5
+ */
+ private String getJsonString(final String file) {
+ if(StringUtils.isEmpty(file)) {
+ return "";
+ }
+
+ String json = null;
+ try {
+ FileInputStream fileStream = new FileInputStream(new File(file));
+ json = IOUtils.toString(fileStream);
+ } catch(Exception e) {
+ Assert.fail(e.getMessage());
+ }
+
+ return json;
+ }
}
diff --git a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImplTest.java b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImplTest.java
index 734add90..bf2efa02 100644
--- a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImplTest.java
+++ b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImplTest.java
@@ -38,7 +38,6 @@ public class ServiceGatewayImplTest {
*/
ServiceGatewayImpl serviceManager = new ServiceGatewayImpl();
-
/**
* Http request.
*/
diff --git a/servicegateway/service/src/test/resources/json/createServiceInstance.json b/servicegateway/service/src/test/resources/json/createServiceInstance.json
new file mode 100644
index 00000000..d8ba2fea
--- /dev/null
+++ b/servicegateway/service/src/test/resources/json/createServiceInstance.json
@@ -0,0 +1,12 @@
+{
+ "name": "gso_service",
+ "description": "siteToDc",
+ "serviceDefId": "78ede6f3-66cc-46ab-b748-38a6c010d272",
+ "templateId": "38a6c01-66cc-46ab-b748-38a6c010d272",
+ "templateName": "GsoTemplate",
+ "gatewayUri":"/openoapi/lifecyclymgr/v1/services",
+ "parameters": {
+ "POP.vFW-moc": "xxx",
+ "POP.vCPE-moc": "xxx"
+ }
+} \ No newline at end of file