update the serviceway for createService.
authorj180240 <jiaxiangli@huawei.com>
Sun, 18 Sep 2016 07:07:24 +0000 (15:07 +0800)
committerj180240 <jiaxiangli@huawei.com>
Sun, 18 Sep 2016 07:07:24 +0000 (15:07 +0800)
Change-Id: Ib90cb71e412db73720ce84d90bbd0916200bc5f1
Signed-off-by: jiaxiangli <jiaxiangli@huawei.com>
12 files changed:
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/activator/Activator.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/constant/Constant.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/ErrorCode.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImpl.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImpl.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/service/inf/IServiceGateway.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/ResponseUtils.java
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/register/RegisterUtil.java [new file with mode: 0644]
servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/validate/ValidateUtil.java [new file with mode: 0644]
servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/roa/impl/ServiceGatewayRoaModuleImplTest.java
servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/service/impl/ServiceGatewayImplTest.java
servicegateway/service/src/test/resources/json/createServiceInstance.json [new file with mode: 0644]

index befe932..2f539e7 100644 (file)
@@ -20,6 +20,8 @@ import java.io.File;
 \r
 import org.apache.commons.lang3.StringUtils;\r
 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;\r
+import org.openo.gso.gui.servicegateway.constant.Constant;\r
+import org.openo.gso.gui.servicegateway.util.register.RegisterUtil;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -33,6 +35,7 @@ public class Activator {
 \r
     private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);\r
 \r
+\r
     /**\r
      * start operation by spring <br/>\r
      * \r
@@ -55,18 +58,18 @@ public class Activator {
     private void registerService() {\r
         // get the jsonString form the service file\r
         String root = SystemEnvVariablesFactory.getInstance().getAppRoot();\r
-//        String serviceFilePath = root + File.separator + Constant.FILE_PATH_ETC + File.separator\r
-//                + Constant.FILE_PATH_REGISTER + File.separator + Constant.FILE_PATH_JSON;\r
-//        String jsonInfo = RegisterUtil.readFile(serviceFilePath);\r
-//\r
-//        // check the jsonInfo\r
-//        if(StringUtils.isEmpty(jsonInfo)) {\r
-//            LOGGER.error("GSO ReadFile  fail: jsonInfo is null. the serviceFilePath=" + serviceFilePath);\r
-//            return;\r
-//        }\r
+        String serviceFilePath = root + File.separator + Constant.FILE_PATH_ETC + File.separator\r
+                + Constant.FILE_PATH_REGISTER + File.separator + Constant.FILE_PATH_JSON;\r
+        String jsonInfo = RegisterUtil.readFile(serviceFilePath);\r
+\r
+        // check the jsonInfo\r
+        if(StringUtils.isEmpty(jsonInfo)) {\r
+            LOGGER.error("ServiceGateway ReadFile  fail: jsonInfo is null. the serviceFilePath=" + serviceFilePath);\r
+            return;\r
+        }\r
 \r
         // register service by the jsonInfo\r
-//        RegisterUtil.registerService(jsonInfo);\r
+        RegisterUtil.registerService(jsonInfo);\r
     }\r
 \r
     /**\r
index c5283fa..58b0b22 100644 (file)
@@ -26,16 +26,11 @@ package org.openo.gso.gui.servicegateway.constant;
  */\r
 public class Constant {\r
 \r
-    /**\r
-     * Service instance name.\r
-     */\r
-    public static final String SERVICE_NAME = "name";\r
-\r
-    /**\r
-     * Service instance description.\r
+        /**\r
+     * Service instance ID.\r
      */\r
-    public static final String SERVICE_DESCRIPTION = "description";\r
-\r
+    public static final String SERVICE_INDENTIFY = "service";\r
+    \r
     /**\r
      * Service instance ID.\r
      */\r
@@ -47,19 +42,10 @@ public class Constant {
     public static final String SERVICE_DEF_ID = "serviceDefId";\r
 \r
     /**\r
-     * Service template ID.\r
+     * Service gateway URL.\r
      */\r
-    public static final String SERVICE_TEMPLATE_ID = "templateId";\r
+    public static final String SERVICE_GATEWAY_URI = "gatewayUri";\r
 \r
-    /**\r
-     * Service template name.\r
-     */\r
-    public static final String SERVICE_TEMPLATE_NAME = "templateName";\r
-\r
-    /**\r
-     * service parameters\r
-     */\r
-    public static final String SERVICE_PARAMETERS = "parameters";\r
 \r
     /**\r
      * Create workflow name.\r
@@ -112,19 +98,59 @@ public class Constant {
     public static final String RESPONSE_STATUS_FAIL = "fail";\r
 \r
     /**\r
-     * Identify of csar.\r
+     * ID of instance.\r
      */\r
-    public static final String CSAR_ID = "csarId";\r
+    public static final String NS_INSTANCE_ID = "nsInstanceId";\r
 \r
     /**\r
      * service identify.\r
      */\r
-    public static final String SERVICE_INDENTIFY = "service";\r
+    public static final String SERVICE_ID = "serviceId";\r
+\r
+    /**\r
+     * job Id\r
+     */\r
+    public static final String JOB_ID = "jobId";\r
+    \r
+    /**\r
+     * service file Path:etc\r
+     */\r
+    public static final String FILE_PATH_ETC = "etc";\r
+\r
+    /**\r
+     * service file Path:register\r
+     */\r
+    public static final String FILE_PATH_REGISTER = "register";\r
+\r
+    /**\r
+     * service file Path:file name\r
+     */\r
+    public static final String FILE_PATH_JSON = "service.json";\r
+    \r
+    /**\r
+     * the head type for Restful\r
+     */\r
+    public static final String HEAD_ERMAP_TYPE = "Content-Type";\r
+\r
+    /**\r
+     * the head value for Restful\r
+     */\r
+    public static final String HEAD_ERMAP_VALUE = "application/json;charset=UTF-8";\r
+    \r
+    /**\r
+     * the URL for Register service to the M-service Bus\r
+     */\r
+    public static final String M_BUS_REGISTER_URL = "/openoapi/microservices/v1/services";\r
+\r
+    /**\r
+     * the IP key for the service file\r
+     */\r
+    public static final String SERVICE_KEY_IP = "getInputIP";\r
 \r
     /**\r
-     * Predefine parameter for service design\r
+     * constant:0\r
      */\r
-    public static final String PREDEFINE_GSO_ID = "predefine_gsoId";\r
+    public static final int ZERO = 0;\r
 \r
     /**\r
      * Constructor<br/>\r
index 759344e..1a94e70 100644 (file)
@@ -46,6 +46,21 @@ public class ErrorCode {
      * Package is being deleted.\r
      */\r
     public static final String SVCMGR_PACKAGE_BEING_DELETED = "servicemgr.package.beingDelete";\r
+    \r
+    /**\r
+     * operation fail\r
+     */\r
+    public static final int FAIL = 3;\r
+\r
+    /**\r
+     * operation partial success\r
+     */\r
+    public static final int PARTIAL_SUCCESS = 1;\r
+    \r
+    /**\r
+     * operation success\r
+     */\r
+    public static final int SUCCESS = 0;\r
 \r
     /**\r
      * Constructor<br/>\r
index 9b7796f..7480bdc 100644 (file)
@@ -22,8 +22,14 @@ import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.Response;\r
 \r
 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
+import org.openo.baseservice.util.RestUtils;\r
+import org.openo.gso.gui.servicegateway.constant.Constant;\r
+import org.openo.gso.gui.servicegateway.exception.HttpCode;\r
 import org.openo.gso.gui.servicegateway.roa.inf.IServiceGatewayRoaModule;\r
+import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;\r
 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;\r
+import org.openo.gso.gui.servicegateway.util.http.ResponseUtils;\r
+import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -45,21 +51,7 @@ public class ServiceGatewayRoaModuleImpl implements IServiceGatewayRoaModule {
     /**\r
      * Service manager.\r
      */\r
-    private IServiceGateway serviceManager;\r
-\r
-    /**\r
-     * @return Returns the serviceManager.\r
-     */\r
-    public IServiceGateway getServiceGateway() {\r
-        return serviceManager;\r
-    }\r
-\r
-    /**\r
-     * @param serviceManager The serviceManager to set.\r
-     */\r
-    public void setServicemanager(IServiceGateway serviceManager) {\r
-        this.serviceManager = serviceManager;\r
-    }\r
+    private IServiceGateway serviceGateway = new ServiceGatewayImpl();\r
 \r
     /**\r
      * Create service instance.<br/>\r
@@ -71,7 +63,28 @@ public class ServiceGatewayRoaModuleImpl implements IServiceGatewayRoaModule {
      */\r
     @Override\r
     public Response createService(HttpServletRequest servletReq) {\r
+       Map<String, Object> operateStatus = null;\r
         Map<String, Object> result = null;\r
+        String serviceId = null;\r
+        try {\r
+            // 1. Check validation\r
+            String reqContent = RestUtils.getRequestBody(servletReq);\r
+            ValidateUtil.assertStringNotNull(reqContent);\r
+\r
+            // 2. Create service\r
+            serviceId = serviceGateway.createService(reqContent, servletReq);\r
+        } catch(ServiceException exception) {\r
+            LOGGER.error("Fail to create service instance.");\r
+            operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_FAIL, exception,\r
+                    String.valueOf(exception.getHttpCode()));            \r
+            result = ResponseUtils.setResult(serviceId, operateStatus);\r
+\r
+            return Response.accepted().entity(result).build();\r
+        }\r
+\r
+        operateStatus = ResponseUtils.setOperateStatus(Constant.RESPONSE_STATUS_SUCCESS, null,\r
+                String.valueOf(HttpCode.RESPOND_OK));\r
+        result = ResponseUtils.setResult(serviceId, operateStatus);\r
 \r
         return Response.accepted().entity(result).build();\r
     }\r
index 3f2a48a..e2200d4 100644 (file)
 \r
 package org.openo.gso.gui.servicegateway.service.impl;\r
 \r
+import java.util.Map;\r
+\r
 import javax.servlet.http.HttpServletRequest;\r
 \r
+import org.apache.commons.lang.StringUtils;\r
 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
+import org.openo.baseservice.roa.util.restclient.RestfulFactory;\r
+import org.openo.baseservice.roa.util.restclient.RestfulParametes;\r
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;\r
+import org.openo.gso.gui.servicegateway.constant.Constant;\r
 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;\r
+import org.openo.gso.gui.servicegateway.util.json.JsonUtil;\r
+import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -49,10 +58,63 @@ public class ServiceGatewayImpl implements IServiceGateway {
      */\r
     @SuppressWarnings("unchecked")\r
     @Override\r
-    public Object createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {\r
+    public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {\r
+       // check the value\r
+       if(StringUtils.isEmpty(reqContent))\r
+       {\r
+               LOGGER.error("ServiceGatewayImpl createService reqContent is null.");\r
+               return null;\r
+       }\r
+       \r
+       // Parse request\r
+        Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);\r
+        Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);\r
+        if(null == service)\r
+        {\r
+               service = requestBody;\r
+        }\r
+        ValidateUtil.assertObjectNotNull(requestBody);\r
+\r
+        // Validate data\r
+        String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);\r
+        ValidateUtil.assertStringNotNull(gatewayUri);\r
+        service.remove(Constant.SERVICE_GATEWAY_URI);\r
 \r
+        // call the restful \r
+        String id = null;\r
+        try {\r
+               RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,\r
+                    getRestfulParameters(JsonUtil.marshal(requestBody)));\r
+            if (null != restfulRsp) {\r
+                // Record the result of registration\r
+                // (201:success;415:Invalid Parameter;500:Internal Server Error)\r
+                LOGGER.info("restful call result:", restfulRsp.getStatus());\r
+                id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);\r
+                id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;\r
+                id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;\r
+            }\r
+        } catch(ServiceException e) {\r
+               LOGGER.error("service gateway create restful call result:", e);\r
+            throw e;\r
+        }\r
 \r
-        return null;\r
+        return id;\r
+    }\r
+    \r
+    /**\r
+     * get the parameters for restful<br/>\r
+     * \r
+     * @author\r
+     * @param bodyData\r
+     *            Json Body\r
+     * @return the RestfulParametes Instance\r
+     * @since GSO 0.5, 2016-8-9\r
+     */\r
+    private static RestfulParametes getRestfulParameters(final String bodyData) {\r
+        RestfulParametes param = new RestfulParametes();\r
+        param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);\r
+        param.setRawData(bodyData);\r
+        return param;\r
     }\r
 \r
     /**\r
index 77b27bd..f9ecb74 100644 (file)
@@ -40,7 +40,7 @@ public interface IServiceGateway {
      * @throws ServiceException when operate DB or parameter is wrong.\r
      * @since GSO 0.5\r
      */\r
-    Object createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException;\r
+       String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException;\r
 \r
     /**\r
      * Delete service instances.<br/>\r
index d0df078..7780e6e 100644 (file)
@@ -155,4 +155,21 @@ public class ResponseUtils {
 \r
         return result;\r
     }\r
+    \r
+    /**\r
+     * Assemble operation result.<br/>\r
+     * \r
+     * @param objectId instance ID\r
+     * @param operateCode operation result\r
+     * @return Response result\r
+     * @since GSO 0.5\r
+     */\r
+    public static Map<String, Object> setResult(String objectId, Object operateCode) {\r
+        Map<String, Object> result = new HashMap<String, Object>();\r
+        result.put(Constant.RESPONSE_RESULT, operateCode);\r
+        String serviceId = (null != objectId) ? objectId : null;\r
+        result.put(Constant.SERVICE_INSTANCE_ID, serviceId);\r
+\r
+        return result;\r
+    }\r
 }\r
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 (file)
index 0000000..4e8b6bd
--- /dev/null
@@ -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 (file)
index 0000000..461163e
--- /dev/null
@@ -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.");
+        }
+
+    }
+
+}
index 8b5c6c6..fabe83d 100644 (file)
 \r
 package org.openo.gso.gui.servicegateway.roa.impl;\r
 \r
-import static org.junit.Assert.assertNotNull;\r
-\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
 import java.io.IOException;\r
-import java.io.Reader;\r
-import java.sql.Connection;\r
 import java.sql.SQLException;\r
 \r
 import javax.servlet.http.HttpServletRequest;\r
 \r
-import org.apache.ibatis.io.Resources;\r
-import org.apache.ibatis.jdbc.ScriptRunner;\r
-import org.apache.ibatis.session.SqlSession;\r
-import org.apache.ibatis.session.SqlSessionFactory;\r
-import org.apache.ibatis.session.SqlSessionFactoryBuilder;\r
+import org.apache.commons.io.IOUtils;\r
+import org.apache.commons.lang.StringUtils;\r
 import org.junit.After;\r
+import org.junit.Assert;\r
 import org.junit.Before;\r
 import org.junit.Test;\r
 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;\r
+import org.openo.baseservice.util.RestUtils;\r
+import org.openo.gso.gui.servicegateway.exception.HttpCode;\r
 import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;\r
+import org.openo.gso.gui.servicegateway.util.http.HttpUtil;\r
+\r
+import mockit.Mock;\r
+import mockit.MockUp;\r
 \r
 /**\r
  * Test ServicemgrRoaModuleImpl class.<br/>\r
@@ -46,6 +49,11 @@ import org.openo.gso.gui.servicegateway.service.impl.ServiceGatewayImpl;
  */\r
 public class ServiceGatewayRoaModuleImplTest {\r
 \r
+       /**\r
+     * File path\r
+     */\r
+    private static final String FILE_PATH = "src/test/resources/json/";\r
+       \r
     /**\r
      * Service ROA.\r
      */\r
@@ -61,6 +69,11 @@ public class ServiceGatewayRoaModuleImplTest {
      * Http request.\r
      */\r
     HttpServletRequest httpRequest;\r
+    \r
+    /**\r
+     * Rest response.\r
+     */\r
+    RestfulResponse responseSuccess;\r
 \r
     /**\r
      * Before executing UT, start sql.<br/>\r
@@ -69,7 +82,8 @@ public class ServiceGatewayRoaModuleImplTest {
      */\r
     @Before\r
     public void start() throws IOException, SQLException {\r
-\r
+       responseSuccess = new RestfulResponse();\r
+       responseSuccess.setStatus(HttpCode.RESPOND_OK);\r
     }\r
 \r
 \r
@@ -92,7 +106,12 @@ public class ServiceGatewayRoaModuleImplTest {
      */\r
     @Test\r
     public void testCreateService() throws ServiceException {\r
+       // mock request body\r
+        mockGetRequestBody(FILE_PATH + "createServiceInstance.json");\r
+        \r
+        mockPost(responseSuccess);\r
 \r
+        serviceRoa.createService(httpRequest);\r
     }\r
 \r
     /**\r
@@ -102,8 +121,63 @@ public class ServiceGatewayRoaModuleImplTest {
      * @since GSO 0.5\r
      */\r
     @Test\r
-    public void testTeleteService() throws ServiceException {\r
+    public void testDeleteService() throws ServiceException {\r
         serviceRoa.deleteService("1", httpRequest);\r
     }\r
 \r
+    /**\r
+     * Mock to get request body.<br/>\r
+     * \r
+     * @param file json file path.\r
+     * @since GSO 0.5\r
+     */\r
+    private void mockGetRequestBody(final String file) {\r
+        new MockUp<RestUtils>() {\r
+\r
+            @Mock\r
+            public String getRequestBody(HttpServletRequest request) {\r
+                return getJsonString(file);\r
+            }\r
+        };\r
+    }\r
+    \r
+    /**\r
+     * Mock rest request for post.<br/>\r
+     * \r
+     * @param response rest response\r
+     * @since GSO 0.5\r
+     */\r
+    private void mockPost(final RestfulResponse response) {\r
+        new MockUp<HttpUtil>() {\r
+\r
+            @Mock\r
+            public RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest) {\r
+                return response;\r
+            }\r
+        };\r
+    }\r
+    \r
+    /**\r
+     * Get json string from file.<br/>\r
+     * \r
+     * @param file the path of file\r
+     * @return json string\r
+     * @throws IOException when fail to read\r
+     * @since GSO 0.5\r
+     */\r
+    private String getJsonString(final String file) {\r
+        if(StringUtils.isEmpty(file)) {\r
+            return "";\r
+        }\r
+\r
+        String json = null;\r
+        try {\r
+            FileInputStream fileStream = new FileInputStream(new File(file));\r
+            json = IOUtils.toString(fileStream);\r
+        } catch(Exception e) {\r
+            Assert.fail(e.getMessage());\r
+        }\r
+\r
+        return json;\r
+    }\r
 }\r
diff --git a/servicegateway/service/src/test/resources/json/createServiceInstance.json b/servicegateway/service/src/test/resources/json/createServiceInstance.json
new file mode 100644 (file)
index 0000000..d8ba2fe
--- /dev/null
@@ -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