851dec2f718a8f4ebf0ec81062f0b5d3896ef8b0
[sdc.git] /
1 /*
2  * Copyright © 2019 iconectiv
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  * ============LICENSE_END=========================================================
16  * Modifications copyright (c) 2019 Nokia
17  * ================================================================================
18  */
19
20 package org.openecomp.sdcrests.externaltesting.rest.services;
21
22 import com.fasterxml.jackson.core.type.TypeReference;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import javax.inject.Named;
31 import javax.ws.rs.core.Response;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
34 import org.openecomp.core.externaltesting.api.ClientConfiguration;
35 import org.openecomp.core.externaltesting.api.ExternalTestingManager;
36 import org.openecomp.core.externaltesting.api.RemoteTestingEndpointDefinition;
37 import org.openecomp.core.externaltesting.api.TestErrorBody;
38 import org.openecomp.core.externaltesting.api.VtpTestExecutionOutput;
39 import org.openecomp.core.externaltesting.api.VtpTestExecutionRequest;
40 import org.openecomp.core.externaltesting.api.VtpTestExecutionResponse;
41 import org.openecomp.core.externaltesting.errors.ExternalTestingException;
42 import org.openecomp.sdc.logging.api.Logger;
43 import org.openecomp.sdc.logging.api.LoggerFactory;
44 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
45 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
46 import org.openecomp.sdcrests.externaltesting.rest.ExternalTesting;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.context.annotation.Scope;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.stereotype.Service;
51
52 @SuppressWarnings("unused")
53 @Named
54 @Service("externaltesting")
55 @Scope(value = "prototype")
56 public class ExternalTestingImpl implements ExternalTesting {
57
58     private final ExternalTestingManager testingManager;
59     private static final int REQUEST_ID_LENGTH = 8;
60     private static final String TESTING_INTERNAL_ERROR = "SDC-TEST-005";
61     private final VendorSoftwareProductManager vendorSoftwareProductManager;
62
63     private static final Logger logger = LoggerFactory.getLogger(ExternalTestingImpl.class);
64
65     public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
66         this.testingManager = testingManager;
67         this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
68     }
69
70     public ExternalTestingImpl(ExternalTestingManager testingManager,
71         VendorSoftwareProductManager vendorSoftwareProductManager) {
72         this.testingManager = testingManager;
73         this.vendorSoftwareProductManager = vendorSoftwareProductManager;
74     }
75
76     /**
77      * Return the configuration of the feature to the client.
78      *
79      * @return JSON response content.
80      */
81     @Override
82     public Response getConfig() {
83         try {
84             return Response.ok(testingManager.getConfig()).build();
85         } catch (ExternalTestingException e) {
86             return convertTestingException(e);
87         }
88     }
89
90     /**
91      * To enable automated functional testing, allow
92      * a put for the client configuration.
93      *
94      * @return JSON response content.
95      */
96     @Override
97     public Response setConfig(ClientConfiguration config) {
98         try {
99             return Response.ok(testingManager.setConfig(config)).build();
100         } catch (ExternalTestingException e) {
101             return convertTestingException(e);
102         }
103     }
104
105
106     /**
107      * Return the test tree structure created by the testing manager.
108      *
109      * @return JSON response content.
110      */
111     @Override
112     public Response getTestCasesAsTree() {
113         try {
114             return Response.ok(testingManager.getTestCasesAsTree()).build();
115         } catch (ExternalTestingException e) {
116             return convertTestingException(e);
117         }
118     }
119
120     @Override
121     public Response getEndpoints() {
122         try {
123             return Response.ok(testingManager.getEndpoints()).build();
124         } catch (ExternalTestingException e) {
125             return convertTestingException(e);
126         }
127     }
128
129     /**
130      * To enable automated functional testing, allow a put of the endpoints.
131      *
132      * @return JSON response content.
133      */
134     @Override
135     public Response setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
136         try {
137             return Response.ok(testingManager.setEndpoints(endpoints)).build();
138         } catch (ExternalTestingException e) {
139             return convertTestingException(e);
140         }
141     }
142
143     @Override
144     public Response getScenarios(String endpoint) {
145         try {
146             return Response.ok(testingManager.getScenarios(endpoint)).build();
147         } catch (ExternalTestingException e) {
148             return convertTestingException(e);
149         }
150
151     }
152
153     @Override
154     public Response getTestsuites(String endpoint, String scenario) {
155         try {
156             return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
157         } catch (ExternalTestingException e) {
158             return convertTestingException(e);
159         }
160     }
161
162     @Override
163     public Response getTestcases(String endpoint, String scenario) {
164         try {
165             return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
166         } catch (ExternalTestingException e) {
167             return convertTestingException(e);
168         }
169     }
170
171     @Override
172     public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
173         try {
174             return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
175         } catch (ExternalTestingException e) {
176             return convertTestingException(e);
177         }
178     }
179
180     @Override
181     public Response execute(String vspId, String vspVersionId, String requestId, List<Attachment> files,
182             String testDataString) {
183         try {
184             List<VtpTestExecutionRequest> req = getVtpTestExecutionRequestObj(testDataString);
185             Map<String, byte[]> fileMap = getFileMap(files);
186             List<VtpTestExecutionResponse> vtpTestExecutionResponses =
187                     testingManager.execute(req, vspId, vspVersionId, requestId, fileMap);
188             return Response.status(HttpStatus.OK.value()).entity(vtpTestExecutionResponses).build();
189         } catch (ExternalTestingException e) {
190             return convertTestingException(e);
191         }
192
193     }
194
195     @Override
196     public Response getValidationResult(String requestId, List<String> endPoints) {
197         try {
198             List<VtpTestExecutionResponse> resultsFromVtp = new ArrayList<>();
199             for (String endPoint : endPoints) {
200                 List<VtpTestExecutionOutput> vtpTestExecutionOutput =
201                         testingManager.getExecutionIds(endPoint, requestId);
202                 List<String> execIds = vtpTestExecutionOutput.stream().map(VtpTestExecutionOutput::getExecutionId)
203                                                .collect(Collectors.toList());
204                 List<VtpTestExecutionResponse> resultFromVtp = getVtpResultbyExecutionId(execIds, endPoint);
205                 resultsFromVtp.addAll(resultFromVtp);
206             }
207             return Response.status(HttpStatus.OK.value()).entity(resultsFromVtp).build();
208         } catch (ExternalTestingException e) {
209             return convertTestingException(e);
210         }
211     }
212
213     private List<VtpTestExecutionRequest> getVtpTestExecutionRequestObj(String testDataString) {
214         try {
215             return new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true).reader()
216                            .forType(new TypeReference<List<VtpTestExecutionRequest>>() { }).readValue(testDataString);
217         } catch (IOException e) {
218             throw new ExternalTestingException(TESTING_INTERNAL_ERROR, 500, e.getMessage(), e);
219
220         }
221     }
222
223     private List<VtpTestExecutionResponse> getVtpResultbyExecutionId(List<String> executionIds, String endPoint) {
224         List<VtpTestExecutionResponse> vtpTestExecutionResponses = new ArrayList<>();
225         executionIds.stream().forEach(executionId -> {
226             VtpTestExecutionResponse executionResult = testingManager.getExecution(endPoint, executionId);
227             vtpTestExecutionResponses.add(executionResult);
228         });
229         return vtpTestExecutionResponses;
230     }
231
232
233     @Override
234     public Response getExecution(String endpoint, String executionId) {
235         try {
236             return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
237         } catch (ExternalTestingException e) {
238             return convertTestingException(e);
239         }
240     }
241
242     private Map<String, byte[]> getFileMap(List<Attachment> files) {
243         if (files != null && !files.isEmpty()) {
244
245             return files.stream().collect(
246                     Collectors.toMap(attachment -> attachment.getDataHandler().getName(), attachment -> {
247                         try {
248                             return IOUtils.toByteArray(attachment.getDataHandler().getInputStream());
249                         } catch (IOException e) {
250                             throw new ExternalTestingException(TESTING_INTERNAL_ERROR, 500, e.getMessage(), e);
251                         }
252                     }));
253         }
254
255         return null;
256     }
257
258     private Response convertTestingException(ExternalTestingException e) {
259         if (logger.isErrorEnabled()) {
260             logger.error("testing exception {} {} {}", e.getMessageCode(), e.getHttpStatus(), e.getDetail(), e);
261         }
262         TestErrorBody body = new TestErrorBody(e.getMessageCode(), e.getHttpStatus(), e.getDetail());
263         return Response.status(e.getHttpStatus()).entity(body).build();
264     }
265 }