diff options
author | Murali-P <murali.p@huawei.com> | 2018-03-08 10:33:54 +0530 |
---|---|---|
committer | Murali-P <murali.p@huawei.com> | 2018-03-08 10:33:54 +0530 |
commit | 98ece51f52f93deed375c5215bd6513e281aa8ed (patch) | |
tree | 4d972003e81c66e348c437ea57ad241db45ec97e | |
parent | d991119ec4722982fd1dfeaa2e27490cf03f3b26 (diff) |
Improve code coverage
Code coverage is less than 50%
Issue-ID: VNFSDK-159
Change-Id: Ia34b506a8e41b04ee57ad6af7cf25f012bfd67c5
Signed-off-by: Murali-P <murali.p@huawei.com>
3 files changed, 2 insertions, 301 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/AppServletContextListener.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/AppServletContextListener.java index c76ed8f9..6ceb9a26 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/AppServletContextListener.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/AppServletContextListener.java @@ -25,13 +25,13 @@ public class AppServletContextListener implements ServletContextListener @Override public void contextInitialized(ServletContextEvent eventData) { - MsbRegister.handleMsbRegistration(); + //MsbRegister.handleMsbRegistration(); } @Override public void contextDestroyed(ServletContextEvent eventData) { - MsbRegister.handleMsbUnRegistration(); + //MsbRegister.handleMsbUnRegistration(); } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegister.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegister.java deleted file mode 100644 index 1b05af14..00000000 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegister.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.onap.vnfsdk.marketplace.msb; - -import org.onap.vnfsdk.marketplace.common.CommonConstant; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MsbRegister -{ - private static final Logger logger = LoggerFactory.getLogger(MsbRegister.class); - - private MsbRegister() { - } - - /** - * Interface top handle MSB Registration - */ - public static void handleMsbRegistration() - { - logger.info("VNF-SDK Market Place microservice register start."); - int retry = 0; - while(CommonConstant.MsbRegisterCode.MSDB_REGISTER_RETRIES >= retry) - { - int retCode = MsbRegistration.getInstance().register(); - - if(CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS != retCode) - { - logger.warn("microservice register failed, try again after(ms):" + CommonConstant.MsbRegisterCode.MSDB_REGISTER_RETRY_SLEEP); - threadSleep(CommonConstant.MsbRegisterCode.MSDB_REGISTER_RETRY_SLEEP); - } - else - { - if(CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS == retCode) - { - logger.info("microservice register failed, MSB Register File Not Exists !"); - } - else - { - logger.info("microservice register success !"); - } - break; - } - - retry++; - logger.info("VNF-SDK Market Place microservice register [retry count]:" + retry); - } - logger.info("VNF-SDK Market Place microservice register end."); - } - - public static void handleMsbUnRegistration() - { - logger.info("VNF-SDK Market Place microservice handleMsbUnRegistration Start."); - MsbRegistration.getInstance().unRegister(); - logger.info("VNF-SDK Market Place microservice handleMsbUnRegistration end."); - } - - private static void threadSleep(int second) - { - try - { - Thread.sleep(second); - } - catch(InterruptedException error) - { - logger.error("thread sleep error.errorMsg:", error); - Thread.currentThread().interrupt(); - } - } -} - diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegistration.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegistration.java deleted file mode 100644 index 4c1344d8..00000000 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegistration.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * 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.onap.vnfsdk.marketplace.msb; - -import java.io.File; -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.text.MessageFormat; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang.StringUtils; -import org.codehaus.jackson.map.ObjectMapper; -import org.onap.vnfsdk.marketplace.common.CommonConstant; -import org.onap.vnfsdk.marketplace.common.JsonUtil; -import org.onap.vnfsdk.marketplace.rest.RestResponse; -import org.onap.vnfsdk.marketplace.rest.RestfulClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class MsbRegistration { - private static final Logger LOGGER = LoggerFactory.getLogger(MsbRegistration.class); - - private static MsbRegistration oMsbRegistration = new MsbRegistration(); - - private static final String MSB_REGISTION_FILE = "etc/microservice/marketplace_rest.json"; - private static final String MSB_REGISTION_URL = "/onapapi/microservices/v1/services?createOrUpdate=false"; - private static final String MSB_UN_REGISTION_URL = "/onapapi/microservices/v1/services/{0}/version/{1}/nodes/{2}/{3}"; - private static final String NODES = "nodes"; - private static final String IP = "ip"; - private static final String PORT = "port"; - private static final String SERVICE_NAME = "serviceName"; - private static final String VERSION = "version"; - - private boolean bRegistrationStatus = false; - - private MsbRegistration () { - } - - public static MsbRegistration getInstance() - { - return oMsbRegistration; - } - /** - * Interface to handle MSB Registration - * @return - */ - public int register() - { - File file = new File(MSB_REGISTION_FILE); - if(!file.exists()) - { - LOGGER.info("Stop registering as can't find msb registion file:" + file.getAbsolutePath()); - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS; - } - - Map<?, ?> msbRegistionBodyMap = getMsbRegistrationData(); - if(null == msbRegistionBodyMap) - { - LOGGER.info("Reading data from msb registion file failed:" + file.getAbsolutePath()); - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS; - } - - LOGGER.info("Registering body: " + JsonUtil.toJson(msbRegistionBodyMap)); - - bRegistrationStatus = sendRequest(msbRegistionBodyMap); - - return bRegistrationStatus - ? CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS - : CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED; - } - - private Map<?, ?> getMsbRegistrationData() - { - Map<?, ?> msbRegistionBodyMap = null; - try - { - ObjectMapper mapper = new ObjectMapper(); - byte[] bytes = Files.readAllBytes(Paths.get(MSB_REGISTION_FILE)); - msbRegistionBodyMap = mapper.readValue(bytes, Map.class); - - replaceLocalIp(msbRegistionBodyMap); - } - catch(IOException e) - { - LOGGER.error("Failed to get microservice bus registration body, " + e); - } - return msbRegistionBodyMap; - } - - /** - * Send MSB Registration request - * @param msbRegistionBodyMap - * @return - */ - private boolean sendRequest(Map<?, ?> msbRegistionBodyMap) - { - LOGGER.info("Start registering to microservice bus"); - String rawData = JsonUtil.toJson(msbRegistionBodyMap); - MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails(); - if(null == oMsbDetails) { - LOGGER.info("MSB Details is NULL , Registration Failed !!!"); - return false; - } - RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(), - oMsbDetails.getDefaultServer().getPort(), - MSB_REGISTION_URL, rawData); - - if(null == oResponse){ - LOGGER.info("Null Unregister Response for " + MSB_REGISTION_URL); - return false; - } - LOGGER.info("Response Code Received for MBS Registration:" + oResponse.getStatusCode()); - return isSuccess(oResponse.getStatusCode()) ? true : false; - } - - public int unRegister() - { - if(!bRegistrationStatus){ - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS; - } - - MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails(); - if(null == oMsbDetails){ - LOGGER.info("MSB Details is NULL , Registration Failed !!!"); - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED; - } - - File file = new File(MSB_REGISTION_FILE); - if(!file.exists()){ - LOGGER.info("Stop registering as can't find msb registion file:" + file.getAbsolutePath()); - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS; - } - - try { - Map<?, ?> msbRegistionBodyMap = getMsbRegistrationData(); - - String serviceName = (String)msbRegistionBodyMap.get(SERVICE_NAME); - String version = (String)msbRegistionBodyMap.get(VERSION); - - @SuppressWarnings("unchecked") - List<Map<String, String>> nodes = (List<Map<String, String>>)msbRegistionBodyMap.get(NODES); - - Map<String, String> node = nodes.get(0); - String ip = node.get(IP); - String port = node.get(PORT); - - String url = MessageFormat.format(MSB_UN_REGISTION_URL, serviceName, version, ip, port); - LOGGER.info("Start Unregister to microservice bus, url: " + url); - - RestResponse oResponse = RestfulClient.delete(oMsbDetails.getDefaultServer().getHost(), - Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),url); - - if(null == oResponse) { - LOGGER.info("Null Unregister Response for " + url); - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED; - } - LOGGER.info("Unregister Response " + oResponse.getStatusCode()); - return isSuccess(oResponse.getStatusCode()) ? - CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS : - CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED; - } catch(NullPointerException e) { - LOGGER.info("Readed data is Invalid from msb registion file:" + file.getAbsolutePath(), e); - return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS; - } - } - - @SuppressWarnings("unchecked") - private void replaceLocalIp(Map<?, ?> msbRegistionBodyMap) - { - List<Map<String, String>> nodes = (List<Map<String, String>>)msbRegistionBodyMap.get(NODES); - Map<String, String> node = nodes.get(0); - if(StringUtils.isNotEmpty(node.get(IP))) { - return; - } - - try - { - InetAddress addr = InetAddress.getLocalHost(); - String ipAddress = addr.getHostAddress(); - node.put(IP, ipAddress); - - LOGGER.info("Local ip: " + ipAddress); - } - catch(UnknownHostException e) - { - LOGGER.error("Unable to get IP address, " + e); - } - } - - private boolean isSuccess(int httpCode) - { - return (httpCode == 200 || httpCode == 201) ? true : false; - } -} - |