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.vnfsdk.marketplace.resource;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map.Entry;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
35 import org.apache.commons.io.IOUtils;
36 import org.eclipse.jetty.http.HttpStatus;
37 import org.onap.vnfsdk.marketplace.common.ToolUtil;
38 import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException;
39 import org.open.infc.grpc.Result;
40 import org.open.infc.grpc.client.OpenRemoteCli;
42 import com.fasterxml.jackson.databind.JsonNode;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.fasterxml.jackson.databind.node.ArrayNode;
45 import com.fasterxml.jackson.databind.node.ObjectNode;
46 import com.google.gson.internal.LinkedTreeMap;
48 import io.swagger.annotations.Api;
49 import io.swagger.annotations.ApiOperation;
50 import io.swagger.annotations.ApiParam;
51 import io.swagger.annotations.ApiResponse;
52 import io.swagger.annotations.ApiResponses;
56 @Api(tags = { "VNF Test Platform" })
57 public class VTPResource {
60 @ApiOperation(value = "VTP Test cases", response = String.class)
61 @Produces(MediaType.APPLICATION_JSON)
62 @ApiResponses(value = {
63 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) })
64 public Response listTests() throws IOException, MarketplaceResourceException {
67 result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" });
68 } catch (Exception e) {
69 return Response.serverError().build();
72 if (result.getExitCode() != 0) {
73 return Response.serverError().entity(result.getOutput()).build();
76 return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
79 @Path("/tests/{testName}/run")
81 @ApiOperation(value = "Run VTP testcase")
82 @Consumes(MediaType.APPLICATION_JSON)
83 @Produces(MediaType.APPLICATION_JSON)
84 @ApiResponses(value = {
85 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class),
86 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) })
87 public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName,
88 @Context HttpServletRequest request)
89 throws IOException, MarketplaceResourceException {
90 String details = IOUtils.toString(request.getInputStream());
93 List<String> cmdArgsList = new ArrayList<>();
94 for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) {
95 cmdArgsList.add(defaultArg);
98 LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class);
99 for (Entry<String, String> arg : cmdArgs.entrySet()) {
100 cmdArgsList.add("--" + arg.getKey());
101 cmdArgsList.add(arg.getValue());
104 result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{}));
105 } catch (Exception e) {
106 return Response.serverError().build();
109 if (result.getExitCode() != 0) {
110 return Response.serverError().entity(result.getOutput()).build();
113 ObjectMapper mapper = new ObjectMapper();
114 JsonNode resultJson = mapper.readTree(result.getOutput());
116 ((ObjectNode)resultJson).put("build_tag", System.getenv("BUILD_TAG"));
118 JsonNode results = resultJson.get("results");
119 if (results != null && results.isArray()) {
120 ArrayNode resultsArray = (ArrayNode)results;
121 if (resultsArray.size() >= 0) {
122 String error = resultsArray.get(0).get("error").asText();
123 ((ObjectNode)resultJson).put("criteria", "SUCCESS".equalsIgnoreCase(error) ? "PASS" : "FAILED");
127 return Response.ok(resultJson.toString(), MediaType.APPLICATION_JSON).build();