List of Input Parameters for VSP
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / externaltesting-rest / externaltesting-rest-services / src / main / java / org / openecomp / sdcrests / externaltesting / rest / services / ExternalTestingImpl.java
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  */
16
17 package org.openecomp.sdcrests.externaltesting.rest.services;
18
19
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;
28
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;
34
35 @SuppressWarnings("unused")
36 @Named
37 @Service("externaltesting")
38 @Scope(value = "prototype")
39 public class ExternalTestingImpl implements ExternalTesting {
40
41   private ExternalTestingManager testingManager;
42
43   private static final Logger logger =
44       LoggerFactory.getLogger(ExternalTestingImpl.class);
45
46   public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
47     this.testingManager = testingManager;
48   }
49
50   /**
51    * Return the configuration of the feature to the client.
52    * @return JSON response content.
53    */
54   @Override
55   public Response getConfig() {
56     try {
57       return Response.ok(testingManager.getConfig()).build();
58     }
59     catch (ExternalTestingException e) {
60       return convertTestingException(e);
61     }
62   }
63
64   /**
65    * Return the test tree structure created by the testing manager.
66    * @return JSON response content.
67    */
68   @Override
69   public Response getTestCasesAsTree() {
70     try {
71       return Response.ok(testingManager.getTestCasesAsTree()).build();
72     }
73     catch (ExternalTestingException e) {
74       return convertTestingException(e);
75     }
76   }
77
78   @Override
79   public Response getEndpoints() {
80     try {
81       return Response.ok(testingManager.getEndpoints()).build();
82     }
83     catch (ExternalTestingException e) {
84       return convertTestingException(e);
85     }
86
87   }
88   @Override
89   public Response getScenarios(String endpoint) {
90     try {
91       return Response.ok(testingManager.getScenarios(endpoint)).build();
92     }
93     catch (ExternalTestingException e) {
94       return convertTestingException(e);
95     }
96
97   }
98
99   @Override
100   public Response getTestsuites(String endpoint, String scenario) {
101     try {
102       return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
103     }
104     catch (ExternalTestingException e) {
105       return convertTestingException(e);
106     }
107   }
108
109   @Override
110   public Response getTestcases(String endpoint, String scenario) {
111     try {
112       return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
113     }
114     catch (ExternalTestingException e) {
115       return convertTestingException(e);
116     }
117   }
118
119   @Override
120   public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
121     try {
122       return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
123     }
124     catch (ExternalTestingException e) {
125       return convertTestingException(e);
126     }
127   }
128
129   @Override
130   public Response execute(List<VtpTestExecutionRequest> req, String requestId) {
131     try {
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();
137       }
138       else {
139         return Response.status(207).entity(responses).build();
140       }
141     }
142     catch (ExternalTestingException e) {
143       return convertTestingException(e);
144     }
145   }
146
147   @Override
148   public Response getExecution(String endpoint, String executionId) {
149     try {
150       return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
151     }
152     catch (ExternalTestingException e) {
153       return convertTestingException(e);
154     }
155   }
156
157   private Response convertTestingException(ExternalTestingException e) {
158     if (logger.isErrorEnabled()) {
159       logger.error("testing exception {} {} {}", e.getTitle(), e.getCode(), e.getDetail(), e);
160     }
161     TestErrorBody body = new TestErrorBody(e.getTitle(), e.getCode(), e.getDetail());
162     return Response.status(e.getCode()).entity(body).build();
163   }
164 }