2  * Copyright 2017 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.
 
  16 package org.onap.vnfsdk.marketplace.onboarding.hooks.functiontest;
 
  18 import java.io.BufferedInputStream;
 
  20 import java.io.FileInputStream;
 
  21 import java.io.FileNotFoundException;
 
  22 import java.io.IOException;
 
  23 import java.io.InputStream;
 
  25 import org.apache.http.HttpEntity;
 
  26 import org.apache.http.entity.ContentType;
 
  27 import org.apache.http.entity.mime.MultipartEntityBuilder;
 
  28 import org.onap.vnfsdk.marketplace.common.CommonConstant;
 
  29 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
 
  30 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
 
  31 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
 
  32 import org.onap.vnfsdk.marketplace.rest.RestConstant;
 
  33 import org.onap.vnfsdk.marketplace.rest.RestResponse;
 
  34 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  38 public class FunctionTestExceutor
 
  40     private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);
 
  42     private FunctionTestExceutor()
 
  46      * Interface to Send Request to Start Function test
 
  47      * @param onBoradFuncTestReq
 
  50     public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq)
 
  52         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
 
  53         logger.info("Package file path Function test:" + packagePath);
 
  55         String funcTestId = null;
 
  56         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
 
  57         if(null == oMsbDetails)
 
  59             logger.error("Failed to get MSB details during execFunctionTest !!!");
 
  64             FileInputStream ifs = new FileInputStream(packagePath);
 
  65             InputStream inStream  = new BufferedInputStream(ifs);
 
  69             //IP and Port needs to be configured !!!
 
  70             RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
 
  71                                                     Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
 
  72                                                     CommonConstant.functionTest.FUNCTEST_URL,buildRequest(inStream));
 
  73             if(!checkValidResponse(rsp))
 
  78             logger.error("Response for Function Test :" , rsp.getResult());
 
  79             funcTestId = rsp.getResult();
 
  80             return funcTestId.replaceAll("\"", "");
 
  82         catch (FileNotFoundException exp)
 
  84             logger.error("Fine not fond Exception for file:" , onBoradFuncTestReq.getPackagePath());
 
  85             logger.error("Fine not fond Exception for :" , exp);
 
  89             logger.error("IOException:" , e);
 
  96      * Interface to get Function Test Results
 
 100     public static String getTestResultsByFuncTestKey(String key)
 
 102         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
 
 103         if(null == oMsbDetails)
 
 105             logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
 
 109         logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);
 
 110         RestResponse rspGet  = RestfulClient.get(oMsbDetails.getDefaultServer().getHost(),
 
 111                                 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
 
 112                                 CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
 
 113         if(!checkValidResponse(rspGet))
 
 115             logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
 
 118         logger.info("Function Test Results for Key:" + key + "Response:" + rspGet.getResult());
 
 119         return  rspGet.getResult();
 
 123      * Interface to get Function Test Results
 
 127     public static String executeFunctionTest(String strJsonRequest)
 
 129         logger.info("executeFunctionTest Test request Received:" + strJsonRequest);
 
 130         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
 
 131         if(null == oMsbDetails)
 
 133             logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
 
 137         logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + strJsonRequest);
 
 138         RestResponse rspGet  = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
 
 139                                                             oMsbDetails.getDefaultServer().getPort(),
 
 140                                                             CommonConstant.functionTest.FUNCTEST_RESULT_URL,
 
 142         if(!checkValidResponse(rspGet))
 
 144             logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
 
 147         logger.info("executeFunctionTest Function Test Result: " + rspGet.getResult());
 
 148         return  rspGet.getResult();
 
 152      * Check Response is Valid
 
 156     private static boolean checkValidResponse(RestResponse rsp)
 
 158         if (rsp.getStatusCode() == null || rsp.getResult() == null
 
 159                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
 
 166     @SuppressWarnings("deprecation")
 
 167     private static HttpEntity buildRequest(InputStream inputStream)
 
 168             throws FileNotFoundException {
 
 169           MultipartEntityBuilder builder = MultipartEntityBuilder.create();
 
 170           builder.seContentType(ContentType.MULTIPART_FORM_DATA);
 
 171           builder.addBinaryBody("file", inputStream);
 
 172           return builder.build();