2 * Copyright © 2019 iconectiv
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.
15 * ============LICENSE_END=========================================================
16 * Modifications copyright (c) 2019 Nokia
17 * ================================================================================
20 package org.openecomp.sdcrests.externaltesting.rest.services;
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;
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;
52 @SuppressWarnings("unused")
54 @Service("externaltesting")
55 @Scope(value = "prototype")
56 public class ExternalTestingImpl implements ExternalTesting {
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;
63 private static final Logger logger = LoggerFactory.getLogger(ExternalTestingImpl.class);
65 public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
66 this.testingManager = testingManager;
67 this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface();
70 public ExternalTestingImpl(ExternalTestingManager testingManager,
71 VendorSoftwareProductManager vendorSoftwareProductManager) {
72 this.testingManager = testingManager;
73 this.vendorSoftwareProductManager = vendorSoftwareProductManager;
77 * Return the configuration of the feature to the client.
79 * @return JSON response content.
82 public Response getConfig() {
84 return Response.ok(testingManager.getConfig()).build();
85 } catch (ExternalTestingException e) {
86 return convertTestingException(e);
91 * To enable automated functional testing, allow
92 * a put for the client configuration.
94 * @return JSON response content.
97 public Response setConfig(ClientConfiguration config) {
99 return Response.ok(testingManager.setConfig(config)).build();
100 } catch (ExternalTestingException e) {
101 return convertTestingException(e);
107 * Return the test tree structure created by the testing manager.
109 * @return JSON response content.
112 public Response getTestCasesAsTree() {
114 return Response.ok(testingManager.getTestCasesAsTree()).build();
115 } catch (ExternalTestingException e) {
116 return convertTestingException(e);
121 public Response getEndpoints() {
123 return Response.ok(testingManager.getEndpoints()).build();
124 } catch (ExternalTestingException e) {
125 return convertTestingException(e);
130 * To enable automated functional testing, allow a put of the endpoints.
132 * @return JSON response content.
135 public Response setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
137 return Response.ok(testingManager.setEndpoints(endpoints)).build();
138 } catch (ExternalTestingException e) {
139 return convertTestingException(e);
144 public Response getScenarios(String endpoint) {
146 return Response.ok(testingManager.getScenarios(endpoint)).build();
147 } catch (ExternalTestingException e) {
148 return convertTestingException(e);
154 public Response getTestsuites(String endpoint, String scenario) {
156 return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
157 } catch (ExternalTestingException e) {
158 return convertTestingException(e);
163 public Response getTestcases(String endpoint, String scenario) {
165 return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
166 } catch (ExternalTestingException e) {
167 return convertTestingException(e);
172 public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
174 return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
175 } catch (ExternalTestingException e) {
176 return convertTestingException(e);
181 public Response execute(String vspId, String vspVersionId, String requestId, List<Attachment> files,
182 String testDataString) {
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);
196 public Response getValidationResult(String requestId, List<String> endPoints) {
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);
207 return Response.status(HttpStatus.OK.value()).entity(resultsFromVtp).build();
208 } catch (ExternalTestingException e) {
209 return convertTestingException(e);
213 private List<VtpTestExecutionRequest> getVtpTestExecutionRequestObj(String testDataString) {
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);
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);
229 return vtpTestExecutionResponses;
234 public Response getExecution(String endpoint, String executionId) {
236 return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
237 } catch (ExternalTestingException e) {
238 return convertTestingException(e);
242 private Map<String, byte[]> getFileMap(List<Attachment> files) {
243 if (files != null && !files.isEmpty()) {
245 return files.stream().collect(
246 Collectors.toMap(attachment -> attachment.getDataHandler().getName(), attachment -> {
248 return IOUtils.toByteArray(attachment.getDataHandler().getInputStream());
249 } catch (IOException e) {
250 throw new ExternalTestingException(TESTING_INTERNAL_ERROR, 500, e.getMessage(), e);
258 private Response convertTestingException(ExternalTestingException e) {
259 if (logger.isErrorEnabled()) {
260 logger.error("testing exception {} {} {}", e.getMessageCode(), e.getHttpStatus(), e.getDetail(), e);
262 TestErrorBody body = new TestErrorBody(e.getMessageCode(), e.getHttpStatus(), e.getDetail());
263 return Response.status(e.getHttpStatus()).entity(body).build();