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;
37 import org.onap.vnfsdk.marketplace.rest.RestfulUtil;
38 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class FunctionTestExceutor
44 private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);
46 private FunctionTestExceutor()
50 * Interface to Send Request to Start Function test
51 * @param onBoradFuncTestReq
54 public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq)
56 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
57 logger.info("Package file path Function test:" + packagePath);
59 String funcTestId = null;
60 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
61 if(null == oMsbDetails)
63 logger.error("Failed to get MSB details during execFunctionTest !!!");
67 FileInputStream ifs = null;
68 InputStream inStream = null;
72 ifs = new FileInputStream(packagePath);
73 inStream = new BufferedInputStream(ifs);
75 //IP and Port needs to be configured !!!
76 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
77 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
78 CommonConstant.functionTest.FUNCTEST_URL,buildRequest(inStream));
79 if(!checkValidResponse(rsp))
84 logger.error("Response for Function Test :" , rsp.getResult());
85 funcTestId = rsp.getResult();
86 return funcTestId.replaceAll("\"", "");
88 catch (FileNotFoundException exp)
90 logger.error("Fine not fond Exception for file:" , onBoradFuncTestReq.getPackagePath());
91 logger.error("Fine not fond Exception for :" , exp);
95 FileUtil.closeInputStream(inStream);
96 FileUtil.closeFileStream(ifs);
102 * Interface to get Function Test Results
106 public static String getTestResultsByFuncTestKey(String key)
108 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
109 if(null == oMsbDetails)
111 logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
115 logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);
116 RestResponse rspGet = RestfulClient.get(oMsbDetails.getDefaultServer().getHost(),
117 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
118 CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
119 if(!checkValidResponse(rspGet))
121 logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
124 logger.info("Function Test Results for Key:" + key + "Response:" + rspGet.getResult());
125 return rspGet.getResult();
129 * Interface to get Function Test Results
133 public static String executeFunctionTest(String strJsonRequest)
135 logger.info("executeFunctionTest Test request Received:" + strJsonRequest);
136 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
137 if(null == oMsbDetails)
139 logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
143 logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + strJsonRequest);
144 RestResponse rspGet = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
145 oMsbDetails.getDefaultServer().getPort(),
146 CommonConstant.functionTest.FUNCTEST_RESULT_URL,
148 if(!checkValidResponse(rspGet))
150 logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
153 logger.info("executeFunctionTest Function Test Result: " + rspGet.getResult());
154 return rspGet.getResult();
158 * Interface to get Function Test Results
162 public static String getTestResultsByFuncTestKeyMsb(String key)
164 logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);
166 Map<String, String> paramsMap = new HashMap<String, String>();
167 paramsMap.put(CommonConstant.HttpContext.URL, CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
168 paramsMap.put(CommonConstant.HttpContext.METHOD_TYPE, CommonConstant.MethodType.GET);
170 RestfulResponse response = RestfulUtil.sendRestRequest(paramsMap, null, null);
171 if(!checkValidRestResponse(response))
173 logger.error("Respone for getTestResultsByFuncTestKeyMsb is not valid !!!");
177 if(null != response.getResponseContent())
179 logger.info("Function Test Results via MSB for Key:" + key + "Response:" + response.getResponseContent());
183 logger.info("NULL Function Test Results via MSB for Key:" + key);
185 return response.getResponseContent();
189 * Check Response is Valid
193 private static boolean checkValidResponse(RestResponse rsp)
195 if (rsp.getStatusCode() == null || rsp.getResult() == null
196 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
203 private static boolean checkValidRestResponse(RestfulResponse rsp)
205 if ((rsp == null) || (RestConstant.RESPONSE_CODE_200 != rsp.getStatus() && RestConstant.RESPONSE_CODE_201 != rsp.getStatus()))
212 @SuppressWarnings("deprecation")
213 private static HttpEntity buildRequest(InputStream inputStream)
214 throws FileNotFoundException {
215 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
216 builder.seContentType(ContentType.MULTIPART_FORM_DATA);
217 builder.addBinaryBody("file", inputStream);
218 return builder.build();