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.google.gson.JsonArray;
47 import com.google.gson.JsonElement;
48 import com.google.gson.JsonObject;
50 import io.swagger.annotations.Api;
51 import io.swagger.annotations.ApiOperation;
52 import io.swagger.annotations.ApiParam;
53 import io.swagger.annotations.ApiResponse;
54 import io.swagger.annotations.ApiResponses;
57 @Api(tags = {"VTP Scenario"})
58 public class VTPScenarioResource extends VTPResource{
59 private static final String DESCRIPTION = "description";
60 public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
61 List<String> args = new ArrayList<>();
63 args.addAll(Arrays.asList(new String[] {
64 "--product", "open-cli", "product-list", "--format", "json"
67 JsonElement results = null;
69 results = this.makeRpcAndGetJson(args);
70 } catch (IOException e) {
71 LOG.error("IOException occurs",e);
74 VTPTestScenarioList list = new VTPTestScenarioList();
76 if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
77 JsonArray resultsArray = results.getAsJsonArray();
78 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
79 JsonElement jsonElement = it.next();
80 JsonObject n = jsonElement.getAsJsonObject();
81 if (n.entrySet().iterator().hasNext()) {
82 String name = n.get("product").getAsString();
84 if ("open-cli".equalsIgnoreCase(name))
87 list.getScenarios().add(new VTPTestScenario().setName(name).setDescription(
88 n.get(DESCRIPTION).getAsString()));
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 JsonElement 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.isJsonArray() && results.getAsJsonArray().size()>0) {
125 JsonArray resultsArray = results.getAsJsonArray();
126 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
127 JsonElement jsonElement = it.next();
128 JsonObject n = jsonElement.getAsJsonObject();
129 if (n.entrySet().iterator().hasNext()) {
130 list.getSuites().add(new VTPTestSuite().setName(n.get("service").getAsString()).setDescription(
131 n.get(DESCRIPTION).getAsString()));
139 @Path("/scenarios/{scenario}/testsuites")
141 @ApiOperation(tags = "VTP Scenario", value = " List available test suties in given scenario", response = VTPTestSuite.class, responseContainer = "List")
142 @Produces(MediaType.APPLICATION_JSON)
143 @ApiResponses(value = {
144 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
145 message = "Failed to perform the operation",
146 response = VTPError.class) })
147 public Response listTestSuties(
148 @ApiParam("Test scenario name") @PathParam("scenario") String scenario) throws VTPException {
150 return Response.ok(this.listTestSutiesHandler(scenario).getSuites().toString(), MediaType.APPLICATION_JSON).build();
153 public VTPTestCaseList listTestcasesHandler(String testSuiteName, String scenario) throws VTPException {
154 List<String> args = new ArrayList<>();
156 args.addAll(Arrays.asList(new String[] {
157 "--product", "open-cli", "schema-list", "--product", scenario, "--format", "json"
159 if (testSuiteName != null) {
160 args.add("--service");
161 args.add(testSuiteName);
164 JsonElement results = null;
166 results = this.makeRpcAndGetJson(args);
167 } catch (IOException e) {
168 LOG.error("IOException occurs",e);
171 VTPTestCaseList list = new VTPTestCaseList();
173 if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
174 JsonArray resultsArray = results.getAsJsonArray();
175 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
176 JsonElement jsonElement = it.next();
177 JsonObject n = jsonElement.getAsJsonObject();
178 if (n.entrySet().iterator().hasNext())
179 list.getTestCases().add(
180 new VTPTestCase().setTestCaseName(
181 n.get("command").getAsString()).setTestSuiteName(
182 n.get("service").getAsString()));
189 @Path("/scenarios/{scenario}/testcases")
191 @ApiOperation(tags = "VTP Scenario", value = " List available test cases", response = VTPTestCase.class, responseContainer = "List")
192 @Produces(MediaType.APPLICATION_JSON)
193 @ApiResponses(value = {
194 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
195 message = "Failed to perform the operation",
196 response = VTPError.class) })
197 public Response listTestcases(
198 @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
199 @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName
200 ) throws VTPException {
202 return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build();
205 public VTPTestCase getTestcaseHandler(String scenario, String testSuiteName, String testCaseName) throws VTPException {
206 List<String> args = new ArrayList<>();
207 args.addAll(Arrays.asList(new String[] {
208 "--product", "open-cli", "schema-show", "--product", scenario, "--service", testSuiteName, "--command", testCaseName , "--format", "json"
210 JsonElement results = null;
212 results = this.makeRpcAndGetJson(args);
213 } catch (IOException e) {
214 LOG.error("IOException occurs",e);
217 JsonObject schema = results.getAsJsonObject().getAsJsonObject("schema");
219 VTPTestCase tc = new VTPTestCase();
220 tc.setTestCaseName(schema.get("name").getAsString());
221 tc.setDescription(schema.get(DESCRIPTION).getAsString());
222 tc.setTestSuiteName(schema.get("service").getAsString());
223 tc.setAuthor(schema.get("author").getAsString());
224 JsonElement inputsJson = schema.get("inputs");
225 if (inputsJson != null && inputsJson.isJsonArray()) {
226 for (final JsonElement jsonElement: inputsJson.getAsJsonArray()) {
227 JsonObject inputJson = jsonElement.getAsJsonObject();
228 VTPTestCaseInput input = new VTPTestCaseInput();
230 input.setName(inputJson.get("name").getAsString());
231 input.setDescription(inputJson.get(DESCRIPTION).getAsString());
232 input.setType(inputJson.get("type").getAsString());
234 if (inputJson.get("is_optional") != null)
235 input.setIsOptional(inputJson.get("is_optional").getAsBoolean());
237 if (inputJson.get("default_value") != null)
238 input.setDefaultValue(inputJson.get("default_value").getAsString());
240 if (inputJson.get("metadata") != null)
241 input.setMetadata(inputJson.get("metadata"));
243 tc.getInputs().add(input);
247 JsonElement outputsJson = schema.get("outputs");
248 if (outputsJson != null && outputsJson.isJsonArray() && outputsJson.getAsJsonArray().size()>0) {
249 for (final JsonElement jsonElement: outputsJson.getAsJsonArray()) {
250 JsonObject outputJson = jsonElement.getAsJsonObject();
251 VTPTestCaseOutput output = new VTPTestCaseOutput();
252 output.setName(outputJson.get("name").getAsString());
253 output.setDescription(outputJson.get(DESCRIPTION).getAsString());
254 output.setType(outputJson.get("type").getAsString());
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();