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.
17 package org.openecomp.sdcrests.externaltesting.rest.services;
20 import org.openecomp.core.externaltesting.api.*;
21 import org.openecomp.core.externaltesting.errors.ExternalTestingException;
22 import org.openecomp.sdc.logging.api.Logger;
23 import org.openecomp.sdc.logging.api.LoggerFactory;
24 import org.openecomp.sdcrests.externaltesting.rest.ExternalTesting;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.context.annotation.Scope;
27 import org.springframework.stereotype.Service;
29 import javax.inject.Named;
30 import javax.ws.rs.core.Response;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.stream.Collectors;
35 @SuppressWarnings("unused")
37 @Service("externaltesting")
38 @Scope(value = "prototype")
39 public class ExternalTestingImpl implements ExternalTesting {
41 private ExternalTestingManager testingManager;
43 private static final Logger logger =
44 LoggerFactory.getLogger(ExternalTestingImpl.class);
46 public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
47 this.testingManager = testingManager;
51 * Return the configuration of the feature to the client.
52 * @return JSON response content.
55 public Response getConfig() {
57 return Response.ok(testingManager.getConfig()).build();
59 catch (ExternalTestingException e) {
60 return convertTestingException(e);
65 * Return the test tree structure created by the testing manager.
66 * @return JSON response content.
69 public Response getTestCasesAsTree() {
71 return Response.ok(testingManager.getTestCasesAsTree()).build();
73 catch (ExternalTestingException e) {
74 return convertTestingException(e);
79 public Response getEndpoints() {
81 return Response.ok(testingManager.getEndpoints()).build();
83 catch (ExternalTestingException e) {
84 return convertTestingException(e);
89 public Response getScenarios(String endpoint) {
91 return Response.ok(testingManager.getScenarios(endpoint)).build();
93 catch (ExternalTestingException e) {
94 return convertTestingException(e);
100 public Response getTestsuites(String endpoint, String scenario) {
102 return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
104 catch (ExternalTestingException e) {
105 return convertTestingException(e);
110 public Response getTestcases(String endpoint, String scenario) {
112 return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
114 catch (ExternalTestingException e) {
115 return convertTestingException(e);
120 public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
122 return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
124 catch (ExternalTestingException e) {
125 return convertTestingException(e);
130 public Response execute(List<VtpTestExecutionRequest> req, String requestId) {
132 List<VtpTestExecutionResponse> responses = testingManager.execute(req, requestId);
133 List<Integer> statuses = responses.stream().map(r-> Optional.ofNullable(r.getHttpStatus()).orElse(200)).distinct().collect(Collectors.toList());
134 if (statuses.size() == 1) {
135 // 1 status so use it...
136 return Response.status(statuses.get(0)).entity(responses).build();
139 return Response.status(207).entity(responses).build();
142 catch (ExternalTestingException e) {
143 return convertTestingException(e);
148 public Response getExecution(String endpoint, String executionId) {
150 return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
152 catch (ExternalTestingException e) {
153 return convertTestingException(e);
157 private Response convertTestingException(ExternalTestingException e) {
158 if (logger.isErrorEnabled()) {
159 logger.error("testing exception {} {} {}", e.getTitle(), e.getCode(), e.getDetail(), e);
161 TestErrorBody body = new TestErrorBody(e.getTitle(), e.getCode(), e.getDetail());
162 return Response.status(e.getCode()).entity(body).build();