diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-09-08 18:41:22 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-09-10 11:16:46 +0530 |
commit | cc11e7ea725fd876dc7db0357f0b212d242c8c05 (patch) | |
tree | bc69124462b40721b0224c9e04f92d4dbd48f12d | |
parent | becfa217fe08c8c219d9e091dee48611d47d3ee0 (diff) |
VTP: Run a test case under VTP
Issue-ID: VNFSDK-304
Change-Id: I0c8504f8876551be822cd49ca922902db5bb0ee0
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
-rw-r--r-- | vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java index 9c3d7009..be80937b 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 Huawei Technologies Co., Ltd. + * Copyright 2017-18 Huawei Technologies Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,9 @@ package org.onap.vnfsdk.marketplace.resource; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; @@ -33,10 +36,12 @@ import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.commons.io.IOUtils; import org.eclipse.jetty.http.HttpStatus; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataParam; import org.onap.vnfsdk.marketplace.common.CommonConstant; +import org.onap.vnfsdk.marketplace.common.ToolUtil; import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException; import org.onap.vnfsdk.marketplace.db.resource.PackageManager; import org.onap.vnfsdk.marketplace.entity.response.CsarFileUriResponse; @@ -50,6 +55,8 @@ import org.onap.vnfsdk.marketplace.wrapper.PackageWrapper; import org.open.infc.grpc.Result; import org.open.infc.grpc.client.OpenRemoteCli; +import com.google.gson.internal.LinkedTreeMap; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -128,10 +135,9 @@ public class PackageResource { @Path("/vtp/tests") @GET - @ApiOperation(value = "VTP Test cases", response = UploadPackageResponse.class) + @ApiOperation(value = "VTP Test cases", response = String.class) @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { - @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unsupported type", response = String.class), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) }) public Response listTests() throws IOException, MarketplaceResourceException { Result result = null; @@ -140,6 +146,48 @@ public class PackageResource { } catch (Exception e) { return Response.serverError().build(); } + + if (result.getExitCode() != 0) { + return Response.serverError().entity(result.getOutput()).build(); + } + + return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build(); + } + + @Path("/vtp/tests/{testName}/run") + @POST + @ApiOperation(value = "Run VTP testcase") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) }) + public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName, + @Context HttpServletRequest request) + throws IOException, MarketplaceResourceException { + String details = IOUtils.toString(request.getInputStream()); + Result result = null; + try { + List<String> cmdArgsList = new ArrayList<>(); + for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) { + cmdArgsList.add(defaultArg); + } + + LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class); + for (Entry<String, String> arg : cmdArgs.entrySet()) { + cmdArgsList.add("--" + arg.getKey()); + cmdArgsList.add(arg.getValue()); + } + + result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{})); + } catch (Exception e) { + return Response.serverError().build(); + } + + if (result.getExitCode() != 0) { + return Response.serverError().entity(result.getOutput()).build(); + } + return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build(); } |