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.InputStream;
23 import java.util.HashMap;
26 import org.apache.http.HttpEntity;
27 import org.apache.http.entity.ContentType;
28 import org.apache.http.entity.mime.MultipartEntityBuilder;
29 import org.onap.vnfsdk.marketplace.common.CommonConstant;
30 import org.onap.vnfsdk.marketplace.common.FileUtil;
31 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
32 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
33 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
34 import org.onap.vnfsdk.marketplace.rest.RestConstant;
35 import org.onap.vnfsdk.marketplace.rest.RestResponse;
36 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class FunctionTestExceutor
43 private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);
45 private FunctionTestExceutor()
49 * Interface to Send Request to Start Function test
50 * @param onBoradFuncTestReq
53 public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq)
55 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
56 logger.info("Package file path Function test:" + packagePath);
58 String funcTestId = null;
59 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
60 if(null == oMsbDetails)
62 logger.error("Failed to get MSB details during execFunctionTest !!!");
66 FileInputStream ifs = null;
67 InputStream inStream = null;
71 ifs = new FileInputStream(packagePath);
72 inStream = new BufferedInputStream(ifs);
74 //IP and Port needs to be configured !!!
75 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
76 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
77 CommonConstant.functionTest.FUNCTEST_URL,buildRequest(inStream));
78 if(!checkValidResponse(rsp))
83 logger.error("Response for Function Test :" , rsp.getResult());
84 funcTestId = rsp.getResult();
85 return funcTestId.replaceAll("\"", "");
87 catch (FileNotFoundException exp)
89 logger.error("Fine not fond Exception for file:" , onBoradFuncTestReq.getPackagePath());
90 logger.error("Fine not fond Exception for :" , exp);
94 FileUtil.closeInputStream(inStream);
95 FileUtil.closeFileStream(ifs);
101 * Interface to get Function Test Results
105 public static String getTestResultsByFuncTestKey(String key)
107 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
108 if(null == oMsbDetails)
110 logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
114 logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);
115 RestResponse rspGet = RestfulClient.get(oMsbDetails.getDefaultServer().getHost(),
116 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
117 CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
118 if(!checkValidResponse(rspGet))
120 logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
123 logger.info("Function Test Results for Key:" + key + "Response:" + rspGet.getResult());
124 return rspGet.getResult();
128 * Interface to get Function Test Results
132 public static String executeFunctionTest(String strJsonRequest)
134 logger.info("executeFunctionTest Test request Received:" + strJsonRequest);
135 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
136 if(null == oMsbDetails)
138 logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
142 logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + strJsonRequest);
143 RestResponse rspGet = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
144 oMsbDetails.getDefaultServer().getPort(),
145 CommonConstant.functionTest.FUNCTEST_RESULT_URL,
147 if(!checkValidResponse(rspGet))
149 logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
152 logger.info("executeFunctionTest Function Test Result: " + rspGet.getResult());
153 return rspGet.getResult();
157 * Interface to get Function Test Results
161 public static String getTestResultsByFuncTestKeyMsb(String key)
163 logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);
165 Map<String, String> paramsMap = new HashMap<String, String>();
166 paramsMap.put(CommonConstant.HttpContext.URL, CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
167 paramsMap.put(CommonConstant.HttpContext.METHOD_TYPE, CommonConstant.MethodType.GET);
169 /*RestfulResponse response = RestfulUtil.sendRestRequest(paramsMap, null, null);
170 if(!checkValidRestResponse(response))
172 logger.error("Respone for getTestResultsByFuncTestKeyMsb is not valid !!!");
176 if(null != response.getResponseContent())
178 logger.info("Function Test Results via MSB for Key:" + key + "Response:" + response.getResponseContent());
182 logger.info("NULL Function Test Results via MSB for Key:" + key);
188 * Check Response is Valid
192 private static boolean checkValidResponse(RestResponse rsp)
194 if (rsp.getStatusCode() == null || rsp.getResult() == null
195 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
202 /*private static boolean checkValidRestResponse(RestfulResponse rsp)
204 if ((rsp == null) || (RestConstant.RESPONSE_CODE_200 != rsp.getStatus() && RestConstant.RESPONSE_CODE_201 != rsp.getStatus()))
211 @SuppressWarnings("deprecation")
212 private static HttpEntity buildRequest(InputStream inputStream)
213 throws FileNotFoundException {
214 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
215 builder.seContentType(ContentType.MULTIPART_FORM_DATA);
216 builder.addBinaryBody("file", inputStream);
217 return builder.build();