4e8134ff6952e3be91f610be51af4a607ad9796f
[sdc.git] /
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.http.HttpStatus;
28 import org.springframework.stereotype.Service;
29
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;
35
36 @SuppressWarnings("unused")
37 @Named
38 @Service("externaltesting")
39 @Scope(value = "prototype")
40 public class ExternalTestingImpl implements ExternalTesting {
41
42   private ExternalTestingManager testingManager;
43
44   private static final Logger logger =
45       LoggerFactory.getLogger(ExternalTestingImpl.class);
46
47   public ExternalTestingImpl(@Autowired ExternalTestingManager testingManager) {
48     this.testingManager = testingManager;
49   }
50
51   /**
52    * Return the configuration of the feature to the client.
53    * @return JSON response content.
54    */
55   @Override
56   public Response getConfig() {
57     try {
58       return Response.ok(testingManager.getConfig()).build();
59     }
60     catch (ExternalTestingException e) {
61       return convertTestingException(e);
62     }
63   }
64
65   /**
66    * To enable automated functional testing, allow
67    * a put for the client configuration.
68    * @return JSON response content.
69    */
70   @Override
71   public Response setConfig(ClientConfiguration config) {
72     try {
73       return Response.ok(testingManager.setConfig(config)).build();
74     }
75     catch (ExternalTestingException e) {
76       return convertTestingException(e);
77     }
78   }
79
80
81   /**
82    * Return the test tree structure created by the testing manager.
83    * @return JSON response content.
84    */
85   @Override
86   public Response getTestCasesAsTree() {
87     try {
88       return Response.ok(testingManager.getTestCasesAsTree()).build();
89     }
90     catch (ExternalTestingException e) {
91       return convertTestingException(e);
92     }
93   }
94
95   @Override
96   public Response getEndpoints() {
97     try {
98       return Response.ok(testingManager.getEndpoints()).build();
99     }
100     catch (ExternalTestingException e) {
101       return convertTestingException(e);
102     }
103   }
104
105   /**
106    * To enable automated functional testing, allow a put of the endpoints.
107    * @return JSON response content.
108    */
109   @Override
110   public Response setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
111     try {
112       return Response.ok(testingManager.setEndpoints(endpoints)).build();
113     }
114     catch (ExternalTestingException e) {
115       return convertTestingException(e);
116     }
117   }
118
119   @Override
120   public Response getScenarios(String endpoint) {
121     try {
122       return Response.ok(testingManager.getScenarios(endpoint)).build();
123     }
124     catch (ExternalTestingException e) {
125       return convertTestingException(e);
126     }
127
128   }
129
130   @Override
131   public Response getTestsuites(String endpoint, String scenario) {
132     try {
133       return Response.ok(testingManager.getTestSuites(endpoint, scenario)).build();
134     }
135     catch (ExternalTestingException e) {
136       return convertTestingException(e);
137     }
138   }
139
140   @Override
141   public Response getTestcases(String endpoint, String scenario) {
142     try {
143       return Response.ok(testingManager.getTestCases(endpoint, scenario)).build();
144     }
145     catch (ExternalTestingException e) {
146       return convertTestingException(e);
147     }
148   }
149
150   @Override
151   public Response getTestcase(String endpoint, String scenario, String testsuite, String testcase) {
152     try {
153       return Response.ok(testingManager.getTestCase(endpoint, scenario, testsuite, testcase)).build();
154     }
155     catch (ExternalTestingException e) {
156       return convertTestingException(e);
157     }
158   }
159
160   @Override
161   public Response execute(List<VtpTestExecutionRequest> req, String requestId) {
162     try {
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();
167       }
168       else {
169         return Response.status(HttpStatus.MULTI_STATUS.value()).entity(responses).build();
170       }
171     }
172     catch (ExternalTestingException e) {
173       return convertTestingException(e);
174     }
175   }
176
177   @Override
178   public Response getExecution(String endpoint, String executionId) {
179     try {
180       return Response.ok(testingManager.getExecution(endpoint, executionId)).build();
181     }
182     catch (ExternalTestingException e) {
183       return convertTestingException(e);
184     }
185   }
186
187   private Response convertTestingException(ExternalTestingException e) {
188     if (logger.isErrorEnabled()) {
189       logger.error("testing exception {} {} {}", e.getMessageCode(), e.getHttpStatus(), e.getDetail(), e);
190     }
191     TestErrorBody body = new TestErrorBody(e.getMessageCode(), e.getHttpStatus(), e.getDetail());
192     return Response.status(e.getHttpStatus()).entity(body).build();
193   }
194 }