VTP: Run a test case under VTP

Issue-ID: VNFSDK-304

Change-Id: I0c8504f8876551be822cd49ca922902db5bb0ee0
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
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 9c3d700..be80937 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 @@
 
 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.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.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 @@
 
     @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 @@
         } 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();
     }