2 * Copyright 2018 Huawei Technologies Co., Ltd.
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.onap.vtp.scenario;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Iterator;
23 import java.util.List;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.QueryParam;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.eclipse.jetty.http.HttpStatus;
34 import org.onap.vtp.VTPResource;
35 import org.onap.vtp.error.VTPError;
36 import org.onap.vtp.error.VTPError.VTPException;
37 import org.onap.vtp.scenario.model.VTPTestCase;
38 import org.onap.vtp.scenario.model.VTPTestScenario;
39 import org.onap.vtp.scenario.model.VTPTestSuite;
40 import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseInput;
41 import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseList;
42 import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseOutput;
43 import org.onap.vtp.scenario.model.VTPTestScenario.VTPTestScenarioList;
44 import org.onap.vtp.scenario.model.VTPTestSuite.VTPTestSuiteList;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.node.ArrayNode;
49 import io.swagger.annotations.Api;
50 import io.swagger.annotations.ApiOperation;
51 import io.swagger.annotations.ApiParam;
52 import io.swagger.annotations.ApiResponse;
53 import io.swagger.annotations.ApiResponses;
56 @Api(tags = {"VTP Scenario"})
57 public class VTPScenarioResource extends VTPResource{
58 private static final String DESCRIPTION = "description";
59 public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
60 List<String> args = new ArrayList<>();
62 args.addAll(Arrays.asList(new String[] {
63 "--product", "open-cli", "product-list", "--format", "json"
66 JsonNode results = null;
68 results = this.makeRpcAndGetJson(args);
69 } catch (IOException e) {
70 LOG.error("IOException occurs",e);
73 VTPTestScenarioList list = new VTPTestScenarioList();
75 if (results != null && results.isArray()) {
76 ArrayNode resultsArray = (ArrayNode)results;
77 if (resultsArray.size() >= 0) {
78 for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) {
79 JsonNode n = it.next();
80 if (n.elements().hasNext()) {
81 String name = n.get("product").asText();
83 if ("open-cli".equalsIgnoreCase(name))
86 list.getScenarios().add(new VTPTestScenario().setName(name).setDescription(
87 n.get(DESCRIPTION).asText()));
98 @ApiOperation(tags = "VTP Scenario", value = " List available test scenarios", response = VTPTestScenario.class, responseContainer = "List")
99 @Produces(MediaType.APPLICATION_JSON)
100 @ApiResponses(value = {
101 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
102 message = "Failed to perform the operation",
103 response = VTPError.class) })
104 public Response listTestScenarios() throws VTPException {
105 return Response.ok(this.listTestScenariosHandler().getScenarios().toString(), MediaType.APPLICATION_JSON).build();
108 public VTPTestSuiteList listTestSutiesHandler(String scenario) throws VTPException {
109 List<String> args = new ArrayList<>();
111 args.addAll(Arrays.asList(new String[] {
112 "--product", "open-cli", "service-list", "--product", scenario, "--format", "json"
115 JsonNode results = null;
117 results = this.makeRpcAndGetJson(args);
118 } catch (IOException e) {
119 LOG.error("IOException occurs",e);
122 VTPTestSuiteList list = new VTPTestSuiteList();
124 if (results != null && results.isArray()) {
125 ArrayNode resultsArray = (ArrayNode)results;
126 if (resultsArray.size() >= 0) {
127 for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) {
128 JsonNode n = it.next();
129 if (n.elements().hasNext()) {
130 list.getSuites().add(new VTPTestSuite().setName(n.get("service").asText()).setDescription(
131 n.get(DESCRIPTION).asText()));
140 @Path("/scenarios/{scenario}/testsuites")
142 @ApiOperation(tags = "VTP Scenario", value = " List available test suties in given scenario", response = VTPTestSuite.class, responseContainer = "List")
143 @Produces(MediaType.APPLICATION_JSON)
144 @ApiResponses(value = {
145 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
146 message = "Failed to perform the operation",
147 response = VTPError.class) })
148 public Response listTestSuties(
149 @ApiParam("Test scenario name") @PathParam("scenario") String scenario) throws VTPException {
151 return Response.ok(this.listTestSutiesHandler(scenario).getSuites().toString(), MediaType.APPLICATION_JSON).build();
154 public VTPTestCaseList listTestcasesHandler(String testSuiteName, String scenario) throws VTPException {
155 List<String> args = new ArrayList<>();
157 args.addAll(Arrays.asList(new String[] {
158 "--product", "open-cli", "schema-list", "--product", scenario, "--format", "json"
160 if (testSuiteName != null) {
161 args.add("--service");
162 args.add(testSuiteName);
165 JsonNode results = null;
167 results = this.makeRpcAndGetJson(args);
168 } catch (IOException e) {
169 LOG.error("IOException occurs",e);
172 VTPTestCaseList list = new VTPTestCaseList();
174 if (results != null && results.isArray()) {
175 ArrayNode resultsArray = (ArrayNode)results;
176 if (resultsArray.size() >= 0) {
177 for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) {
178 JsonNode n = it.next();
179 if (n.elements().hasNext())
180 list.getTestCases().add(
181 new VTPTestCase().setTestCaseName(
182 n.get("command").asText()).setTestSuiteName(
183 n.get("service").asText()));
191 @Path("/scenarios/{scenario}/testcases")
193 @ApiOperation(tags = "VTP Scenario", value = " List available test cases", response = VTPTestCase.class, responseContainer = "List")
194 @Produces(MediaType.APPLICATION_JSON)
195 @ApiResponses(value = {
196 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
197 message = "Failed to perform the operation",
198 response = VTPError.class) })
199 public Response listTestcases(
200 @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
201 @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName
202 ) throws VTPException {
204 return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build();
207 public VTPTestCase getTestcaseHandler(String scenario, String testSuiteName, String testCaseName) throws VTPException {
208 List<String> args = new ArrayList<>();
209 args.addAll(Arrays.asList(new String[] {
210 "--product", "open-cli", "schema-show", "--product", scenario, "--service", testSuiteName, "--command", testCaseName , "--format", "json"
212 JsonNode results = null;
214 results = this.makeRpcAndGetJson(args);
215 } catch (IOException e) {
216 LOG.error("IOException occurs",e);
219 JsonNode schema = results.get("schema");
221 VTPTestCase tc = new VTPTestCase();
222 tc.setTestCaseName(schema.get("name").asText());
223 tc.setDescription(schema.get(DESCRIPTION).asText());
224 tc.setTestSuiteName(schema.get("service").asText());
225 tc.setAuthor(schema.get("author").asText());
226 JsonNode inputsJson = schema.get("inputs");
227 if (inputsJson != null && inputsJson.isArray()) {
228 for (final JsonNode inputJson: inputsJson) {
229 VTPTestCaseInput input = new VTPTestCaseInput();
231 input.setName(inputJson.get("name").asText());
232 input.setDescription(inputJson.get(DESCRIPTION).asText());
233 input.setType(inputJson.get("type").asText());
235 if (inputJson.get("is_optional") != null)
236 input.setIsOptional(inputJson.get("is_optional").asBoolean());
238 if (inputJson.get("default_value") != null)
239 input.setDefaultValue(inputJson.get("default_value").asText());
241 if (inputJson.get("metadata") != null)
242 input.setMetadata(inputJson.get("metadata"));
244 tc.getInputs().add(input);
248 JsonNode outputsJson = schema.get("outputs");
249 if (outputsJson != null && outputsJson.isArray()) {
250 for (final JsonNode outputJson: outputsJson) {
251 VTPTestCaseOutput output = new VTPTestCaseOutput();
252 output.setName(outputJson.get("name").asText());
253 output.setDescription(outputJson.get(DESCRIPTION).asText());
254 output.setType(outputJson.get("type").asText());
256 tc.getOutputs().add(output);
263 @Path("/scenarios/{scenario}/testsuites/{testSuiteName}/testcases/{testCaseName}")
265 @ApiOperation(tags = "VTP Scenario", value = "Retrieve test cases details like inputs outputs and test suite name", response = VTPTestCase.class)
266 @Produces(MediaType.APPLICATION_JSON)
267 @ApiResponses(value = {
268 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
269 message = "Failed to perform the operation", response = VTPError.class),
270 @ApiResponse(code = HttpStatus.NOT_FOUND_404,
271 message = "Test case does not exist", response = VTPError.class)})
272 public Response getTestcase(
273 @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
274 @ApiParam(value = "Test case name") @PathParam("testSuiteName") String testSuiteName,
275 @ApiParam(value = "Test case name") @PathParam("testCaseName") String testCaseName)
276 throws VTPException {
278 return Response.ok(this.getTestcaseHandler(scenario, testSuiteName, testCaseName).toString(), MediaType.APPLICATION_JSON).build();