0f94734779ca5850e9c67e649c83ca1179c4ad8f
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vnfsdk.marketplace.onboarding.hooks.functiontest;
17
18 import java.io.BufferedInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24
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;
37
38 public class FunctionTestExceutor
39 {
40     private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);
41
42     private FunctionTestExceutor()
43     {}
44
45     /**
46      * Interface to Send Request to Start Function test
47      * @param onBoradFuncTestReq
48      * @return
49      */
50     public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq)
51     {
52         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
53         logger.info("Package file path Function test:" + packagePath);
54
55         String funcTestId = null;
56         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
57         if(null == oMsbDetails)
58         {
59             logger.error("Failed to get MSB details during execFunctionTest !!!");
60             return funcTestId;
61         }
62
63         try (
64             FileInputStream ifs = new FileInputStream(packagePath);
65             InputStream inStream  = new BufferedInputStream(ifs);
66         ) {
67
68
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))
74             {
75                 return funcTestId;
76             }
77
78             logger.error("Response for Function Test :" , rsp.getResult());
79             funcTestId = rsp.getResult();
80             return funcTestId.replaceAll("\"", "");
81         }
82         catch (FileNotFoundException exp)
83         {
84             logger.error("Fine not fond Exception for file:" , onBoradFuncTestReq.getPackagePath());
85             logger.error("Fine not fond Exception for :" , exp);
86         }
87         catch (IOException e)
88         {
89             logger.error("IOException:" , e);
90         }
91
92         return funcTestId;
93     }
94
95     /**
96      * Interface to get Function Test Results
97      * @param key
98      * @return
99      */
100     public static String getTestResultsByFuncTestKey(String key)
101     {
102         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
103         if(null == oMsbDetails)
104         {
105             logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
106             return null;
107         }
108
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))
114         {
115             logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
116             return null;
117         }
118         logger.info("Function Test Results for Key:" + key + "Response:" + rspGet.getResult());
119         return  rspGet.getResult();
120     }
121
122     /**
123      * Interface to get Function Test Results
124      * @param key
125      * @return
126      */
127     public static String executeFunctionTest(String strJsonRequest)
128     {
129         logger.info("executeFunctionTest Test request Received:" + strJsonRequest);
130         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
131         if(null == oMsbDetails)
132         {
133             logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
134             return null;
135         }
136
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,
141                                                             strJsonRequest);
142         if(!checkValidResponse(rspGet))
143         {
144             logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
145             return null;
146         }
147         logger.info("executeFunctionTest Function Test Result: " + rspGet.getResult());
148         return  rspGet.getResult();
149     }
150
151     /**
152      * Check Response is Valid
153      * @param rsp
154      * @return
155      */
156     private static boolean checkValidResponse(RestResponse rsp)
157     {
158         if (rsp.getStatusCode() == null || rsp.getResult() == null
159                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
160         {
161             return false;
162         }
163         return true;
164     }
165
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();
173         }
174 }
175