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;
23 import org.openecomp.core.externaltesting.api.*;
24 import org.openecomp.core.externaltesting.errors.ExternalTestingException;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.openecomp.sdcrests.externaltesting.rest.ExternalTesting;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.Scope;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Named;
34 import javax.ws.rs.core.Response;
35 import java.util.List;
36 import java.util.Optional;
37 import java.util.stream.Collectors;
39 @SuppressWarnings("unused")
41 @Service("externaltesting")
42 @Scope(value = "prototype")
43 public class ExternalTestingImpl implements ExternalTesting {
45 private final ExternalTestingManager testingManager;
47 private static final Logger logger =
48 LoggerFactory.getLogger(ExternalTestingImpl.class);
50 public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
51 this.testingManager = testingManager;
55 * Return the configuration of the feature to the client.
56 * @return JSON response content.
59 public Response getConfig() {
61 return Response.ok(testingManager.getConfig()).build();
63 catch (ExternalTestingException e) {
64 return convertTestingException(e);
69 * To enable automated functional testing, allow
70 * a put for the client configuration.
71 * @return JSON response content.
74 public Response setConfig(ClientConfiguration config) {
76 return Response.ok(testingManager.setConfig(config)).build();
78 catch (ExternalTestingException e) {
79 return convertTestingException(e);
85 * Return the test tree structure created by the testing manager.
86 * @return JSON response content.
89 public Response getTestCasesAsTree() {
91 return Response.ok(testingManager.getTestCasesAsTree()).build();
93 catch (ExternalTestingException e) {
94 return convertTestingException(e);
99 public Response getEndpoints() {
101 return Response.ok(testingManager.getEndpoints()).build();
103 catch (ExternalTestingException e) {
104 return convertTestingException(e);
109 * To enable automated functional testing, allow a put of the endpoints.
110 * @return JSON response content.
113 public Response setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
115 return Response.ok(testingManager.setEndpoints(endpoints)).build();
117 catch (ExternalTestingException e) {
118 return convertTestingException(e);
123 public Response getScenarios(String endpoint) {
125 return Response.ok(testingManager.getScenarios(endpoint)).build();
127 catch (ExternalTestingException e) {
128 return convertTestingException(e);
134 public Response getTestsuites(String endpoint, String scenario) {
136 return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
138 catch (ExternalTestingException e) {
139 return convertTestingException(e);
144 public Response getTestcases(String endpoint, String scenario) {
146 return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
148 catch (ExternalTestingException e) {
149 return convertTestingException(e);
154 public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
156 return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
158 catch (ExternalTestingException e) {
159 return convertTestingException(e);
164 public Response execute(List<VtpTestExecutionRequest> req, String requestId) {
166 List<VtpTestExecutionResponse> responses = testingManager.execute(req, requestId);
167 List<Integer> statuses = responses.stream().map(r-> Optional.ofNullable(r.getHttpStatus()).orElse(HttpStatus.OK.value())).distinct().collect(Collectors.toList());
168 if (statuses.size() == 1) {
169 return Response.status(HttpStatus.OK.value()).entity(responses).build();
172 return Response.status(HttpStatus.MULTI_STATUS.value()).entity(responses).build();
175 catch (ExternalTestingException e) {
176 return convertTestingException(e);
181 public Response getExecution(String endpoint, String executionId) {
183 return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
185 catch (ExternalTestingException e) {
186 return convertTestingException(e);
190 private Response convertTestingException(ExternalTestingException e) {
191 if (logger.isErrorEnabled()) {
192 logger.error("testing exception {} {} {}", e.getMessageCode(), e.getHttpStatus(), e.getDetail(), e);
194 TestErrorBody body = new TestErrorBody(e.getMessageCode(), e.getHttpStatus(), e.getDetail());
195 return Response.status(e.getHttpStatus()).entity(body).build();