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.http.HttpStatus;
28 import org.springframework.stereotype.Service;
30 import javax.inject.Named;
31 import javax.ws.rs.core.Response;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.stream.Collectors;
36 @SuppressWarnings("unused")
38 @Service("externaltesting")
39 @Scope(value = "prototype")
40 public class ExternalTestingImpl implements ExternalTesting {
42 private ExternalTestingManager testingManager;
44 private static final Logger logger =
45 LoggerFactory.getLogger(ExternalTestingImpl.class);
47 public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
48 this.testingManager = testingManager;
52 * Return the configuration of the feature to the client.
53 * @return JSON response content.
56 public Response getConfig() {
58 return Response.ok(testingManager.getConfig()).build();
60 catch (ExternalTestingException e) {
61 return convertTestingException(e);
66 * To enable automated functional testing, allow
67 * a put for the client configuration.
68 * @return JSON response content.
71 public Response setConfig(ClientConfiguration config) {
73 return Response.ok(testingManager.setConfig(config)).build();
75 catch (ExternalTestingException e) {
76 return convertTestingException(e);
82 * Return the test tree structure created by the testing manager.
83 * @return JSON response content.
86 public Response getTestCasesAsTree() {
88 return Response.ok(testingManager.getTestCasesAsTree()).build();
90 catch (ExternalTestingException e) {
91 return convertTestingException(e);
96 public Response getEndpoints() {
98 return Response.ok(testingManager.getEndpoints()).build();
100 catch (ExternalTestingException e) {
101 return convertTestingException(e);
106 * To enable automated functional testing, allow a put of the endpoints.
107 * @return JSON response content.
110 public Response setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
112 return Response.ok(testingManager.setEndpoints(endpoints)).build();
114 catch (ExternalTestingException e) {
115 return convertTestingException(e);
120 public Response getScenarios(String endpoint) {
122 return Response.ok(testingManager.getScenarios(endpoint)).build();
124 catch (ExternalTestingException e) {
125 return convertTestingException(e);
131 public Response getTestsuites(String endpoint, String scenario) {
133 return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
135 catch (ExternalTestingException e) {
136 return convertTestingException(e);
141 public Response getTestcases(String endpoint, String scenario) {
143 return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
145 catch (ExternalTestingException e) {
146 return convertTestingException(e);
151 public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
153 return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
155 catch (ExternalTestingException e) {
156 return convertTestingException(e);
161 public Response execute(List<VtpTestExecutionRequest> req, String requestId) {
163 List<VtpTestExecutionResponse> responses = testingManager.execute(req, requestId);
164 List<Integer> statuses = responses.stream().map(r-> Optional.ofNullable(r.getHttpStatus()).orElse(HttpStatus.OK.value())).distinct().collect(Collectors.toList());
165 if (statuses.size() == 1) {
166 return Response.status(HttpStatus.OK.value()).entity(responses).build();
169 return Response.status(HttpStatus.MULTI_STATUS.value()).entity(responses).build();
172 catch (ExternalTestingException e) {
173 return convertTestingException(e);
178 public Response getExecution(String endpoint, String executionId) {
180 return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
182 catch (ExternalTestingException e) {
183 return convertTestingException(e);
187 private Response convertTestingException(ExternalTestingException e) {
188 if (logger.isErrorEnabled()) {
189 logger.error("testing exception {} {} {}", e.getMessageCode(), e.getHttpStatus(), e.getDetail(), e);
191 TestErrorBody body = new TestErrorBody(e.getMessageCode(), e.getHttpStatus(), e.getDetail());
192 return Response.status(e.getHttpStatus()).entity(body).build();