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