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.mime.MultipartEntityBuilder;
27 import org.onap.vnfsdk.marketplace.common.CommonConstant;
28 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
29 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
30 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
31 import org.onap.vnfsdk.marketplace.rest.RestConstant;
32 import org.onap.vnfsdk.marketplace.rest.RestResponse;
33 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
34 import org.onap.vnfsdk.marketplace.common.FileUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 /* CALL Flow: onBoardingHandler --> FunctionTestHook--> FunctionTestExecutor */
39 public class FunctionTestExceutor {
40 private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);
42 private FunctionTestExceutor() {
47 * Interface to Send Request to Start Function test
49 * @param onBoradFuncTestReq
50 * @return null (in case of failure) or function Test Id
52 public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq) {
54 String funcTestId = null;
56 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
57 logger.info("Package file path Function test:" + packagePath);
59 // Validate package path
60 if (false == FileUtil.validatePath(packagePath)) {
61 logger.error("Failed to validate path");
65 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
66 if (null == oMsbDetails) {
67 logger.error("Failed to get MSB details during execFunctionTest !!!");
71 try (FileInputStream ifs = new FileInputStream(packagePath);
72 InputStream inStream = new BufferedInputStream(ifs);) {
74 // Validate input stream
75 if (false == FileUtil.validateStream(ifs)) {
76 logger.error("Failed to validate file stream");
80 // IP and Port needs to be configured !!!
81 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
82 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
83 CommonConstant.functionTest.FUNCTEST_URL, buildRequest(inStream));
84 if (!checkValidResponse(rsp)) {
85 logger.error("Failed to validate response");
89 funcTestId = rsp.getResult();
90 logger.info("Response for Function Test :", funcTestId);
92 return funcTestId.replaceAll("\"", "");
93 } catch (NumberFormatException e) {
94 logger.error("Invalid port number :", oMsbDetails.getDefaultServer().getPort());
95 } catch (FileNotFoundException exp) {
96 logger.error("File not found Exception for file:", onBoradFuncTestReq.getPackagePath());
97 logger.error("File not found Exception for :", exp);
98 } catch (IOException e) {
99 logger.error("IOException:", e);
106 * Interface to get Function Test Results
109 * @return null or resultkey
111 public static String getTestResultsByFuncTestKey(String key) {
113 // Input key cannot be null- no need to validate
115 String result = null;
116 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
117 if (null == oMsbDetails) {
118 logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
122 logger.info("GetTestResultsByFuncTestKey for Function Test Results for :" + key);
123 RestResponse rspGet = RestfulClient.get(oMsbDetails.getDefaultServer().getHost(),
124 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
125 CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
126 if (false == checkValidResponse(rspGet)) {
127 logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
131 result = rspGet.getResult();
132 logger.info("Function Test Results for Key:" + key + "Response:" + rspGet.getResult());
137 * Interface to get Function Test Results
139 * @param strJsonRequest
142 public static String executeFunctionTest(String strJsonRequest) {
144 String result = null;
145 if (null == strJsonRequest) {
146 logger.error("Invalid input- Input is null");
150 logger.info("ExecuteFunctionTest Test request Received:" + strJsonRequest);
152 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
153 if (null == oMsbDetails) {
154 logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
158 logger.info("GetTestResultsByFuncTestKey for Function Test Results for :" + strJsonRequest);
159 RestResponse rspGet = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
160 oMsbDetails.getDefaultServer().getPort(), CommonConstant.functionTest.FUNCTEST_RESULT_URL,
162 if (false == checkValidResponse(rspGet)) {
163 logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
167 result = rspGet.getResult();
168 logger.info("ExecuteFunctionTest Function Test Result: " + rspGet.getResult());
173 * Check Response is Valid
176 * @return valid or invalid
178 private static boolean checkValidResponse(RestResponse rsp) {
179 if ((null == rsp.getStatusCode()) || (null == rsp.getResult())
180 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode()
181 && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode())) {
187 private static HttpEntity buildRequest(InputStream inputStream) throws FileNotFoundException {
188 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
189 builder.addBinaryBody("file", inputStream);
190 return builder.build();