Add dist mode 79/122779/2
authorsharath reddy <bs.reddy@huawei.com>
Wed, 21 Jul 2021 10:23:02 +0000 (15:53 +0530)
committersharath reddy <bs.reddy@huawei.com>
Thu, 22 Jul 2021 13:59:23 +0000 (19:29 +0530)
Issue-ID: VNFSDK-792

Signed-off-by: sharath reddy <bs.reddy@huawei.com>
Change-Id: Ic49782ee2a3d4f23a86e6cf9592bed490b670df2
Signed-off-by: sharath reddy <bs.reddy@huawei.com>
13 files changed:
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/VTPResource.java
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/execution/VTPExecutionResource.java
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/DistManager.java [new file with mode: 0644]
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/Manager.java [new file with mode: 0644]
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/model/Tester.java [new file with mode: 0644]
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/VTPProfileResource.java
vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java
vnfmarket-be/vnf-sdk-marketplace/src/main/resources/vtp.properties
vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java
vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResourceTest.java
vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/manager/DistManagerTest.java [new file with mode: 0644]
vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/manager/model/TesterTest.java [new file with mode: 0644]
vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/scenario/VTPScenarioResourceTest.java

index 9e1a9c8..a756cb0 100644 (file)
@@ -18,16 +18,14 @@ package org.onap.vtp;
 
 import java.io.IOException;
 import java.text.SimpleDateFormat;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Properties;
-import java.util.TimeZone;
-import java.util.UUID;
+import java.util.*;
 
+import com.google.gson.*;
 import org.apache.http.HttpStatus;
 import org.onap.vtp.error.VTPError;
 import org.onap.vtp.error.VTPError.VTPException;
+import org.onap.vtp.manager.DistManager;
+import org.onap.vtp.manager.model.Tester;
 import org.open.infc.grpc.Output;
 import org.open.infc.grpc.Result;
 import org.open.infc.grpc.client.OpenInterfaceGrpcClient;
@@ -35,13 +33,15 @@ import org.open.infc.grpc.client.OpenRemoteCli;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.gson.Gson;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
 import com.google.gson.reflect.TypeToken;
 import org.yaml.snakeyaml.DumperOptions;
 import org.yaml.snakeyaml.Yaml;
 
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+
 public class VTPResource {
 
     protected static final Logger LOG = LoggerFactory.getLogger(VTPResource.class);
@@ -54,9 +54,10 @@ public class VTPResource {
     protected static int VTP_EXECUTION_GRPC_TIMEOUT;  // NOSONAR
     protected static String VTP_YAML_STORE;  // NOSONAR
     protected static String VTP_SCRIPT_STORE;  // NOSONAR
-
+    public static boolean mode=false;
     protected static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);  // NOSONAR
-
+    DistManager distManager = null;
+    Tester tester = null;
     static {
         dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
 
@@ -70,6 +71,8 @@ public class VTPResource {
             VTP_EXECUTION_GRPC_TIMEOUT = Integer.parseInt(prp.getProperty("vtp.grpc.timeout")) * 1000 ;
             VTP_YAML_STORE = prp.getProperty("vtp.yaml.store");
             VTP_SCRIPT_STORE = prp.getProperty("vtp.script.store");
+            if (prp.getProperty("vtp.execution.mode").equals("dist"))
+                mode=true;
         } catch (Exception e) {  // NOSONAR
             LOG.error(e.getMessage());
         }
@@ -80,6 +83,24 @@ public class VTPResource {
     }
 
     protected Result makeRpc(List <String> args, int timeout) throws VTPException {
+        String executionId=args.get(4);
+        if(isDistMode() && (executionId.contains("-") || args.contains("schema-show"))) {
+            distManager =new DistManager();
+
+            if (executionId.contains("-")){
+                tester = distManager.httpRequestExecutions(executionId);
+            }
+            else {
+                String scenario=args.get(4);
+                String testSuiteName=args.get(6);
+                String testCaseName=args.get(8);
+                tester = distManager.httpRequestTestcase(testSuiteName,scenario,testCaseName);
+            }
+
+            VTP_TEST_CENTER_IP = tester.getIp();
+            VTP_TEST_CENTER_PORT = tester.getPort();
+
+        }
         Result result = null;
         String requestId = UUID.randomUUID().toString();
         try {
@@ -119,11 +140,17 @@ public class VTPResource {
         return jsonParser.parse(result.getOutput());
     }
 
-    protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPException {
-        return this.makeRpc(scenario, requestId, profile, testCase, argsJsonNode, VTP_EXECUTION_GRPC_TIMEOUT);
+    protected Output makeRpc(String testSuite,String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPException {
+        return this.makeRpc(testSuite,scenario, requestId, profile, testCase, argsJsonNode, VTP_EXECUTION_GRPC_TIMEOUT);
     }
 
-    protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode, int timeout) throws VTPException {
+    protected Output makeRpc(String testSuite,String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode, int timeout) throws VTPException {
+        if (isDistMode()){
+            distManager =new DistManager();
+            tester = distManager.httpRequestTestcase(testSuite,scenario,testCase);
+            VTP_TEST_CENTER_IP =tester.getIp();
+            VTP_TEST_CENTER_PORT = tester.getPort();
+        }
         Output output = null;
         Map <String, String> args = gson.fromJson(argsJsonNode, new TypeToken<Map<String,String>>(){}.getType());
         try {
@@ -141,7 +168,12 @@ public class VTPResource {
             throw new VTPException(
                     new VTPError().setMessage(e.getMessage()));
         }
-
+        if (isDistMode())
+        {
+            String executionId= output.getAddonsMap().get("execution-id");
+            assert distManager != null;
+            distManager.postDataToManager(executionId,tester.getId(),tester.getTesterId());
+        }
         return output;
     }
 
@@ -156,4 +188,53 @@ public class VTPResource {
         dumperOptions.setPrettyFlow(false);
         return new Yaml(dumperOptions);
     }
+    public boolean isDistMode() {
+        return mode;
+    }
+    protected JsonElement makeRpcAndGetJson(List<String> args,int count,int index) throws VTPException, IOException {
+        return this.makeRpcAndGetJson(args,count,index, VTP_EXECUTION_GRPC_TIMEOUT);
+    }
+
+    protected JsonElement makeRpcAndGetJson(List<String> args,int count,int index, int timeout) throws VTPException {
+        List<String> result = this.makeRpc(args,count,index, timeout);
+        JsonArray jsonArray = new JsonArray();
+        for (String jsonString : result) {
+            JsonElement obj = new JsonParser().parse(jsonString);
+            jsonArray.add(obj);
+        }
+        return jsonArray;
+    }
+
+    protected List<String> makeRpc(List<String> args, int count, int index, int timeout) {
+        distManager = new DistManager();
+        JsonElement jsonElement = distManager.getExecutionJson(count, index);
+        List<String> resultList = new ArrayList<>();
+        if (jsonElement != null && jsonElement.isJsonArray() && jsonElement.getAsJsonArray().size() > 0) {
+            JsonArray resultsArray = jsonElement.getAsJsonArray();
+            Client client = ClientBuilder.newClient();
+            for (JsonElement jElement : resultsArray) {
+                JsonObject jsonObject = jElement.getAsJsonObject();
+                String testerId = null;
+                String executionId = null;
+                if (jsonObject.has("tester_id"))
+                    testerId = jsonObject.get("tester_id").getAsString();
+                if (jsonObject.has("execution_id"))
+                    executionId = jsonObject.get("execution_id").getAsString();
+                if (testerId == null || executionId == null)
+                    throw new IllegalArgumentException("testerId: " + testerId + " and " + " executionId: " + executionId + " should not be null");
+
+                String testerPath = "/manager/tester/" + testerId;
+                JsonObject jObject = distManager.getResponseFromTester(client, distManager.getManagerURL(), testerPath).getAsJsonObject();
+                String vtpTestCenterIp = jObject.get("iP").getAsString();
+                int vtpTestCenterPort = Integer.parseInt(jObject.get("port").getAsString());
+                args.set(4, executionId);
+                try {
+                    resultList.add(distManager.getExecutionDetails(vtpTestCenterIp, vtpTestCenterPort, args, timeout).getOutput());
+                } catch (Exception e) {
+                    LOG.error("executionId : " + executionId + " not valid::: " + e.getMessage());
+                }
+            }
+        }
+        return resultList;
+    }
 }
index 2b06fe8..313206e 100644 (file)
@@ -103,6 +103,7 @@ public class VTPExecutionResource  extends VTPResource{
 
             //Run execution
             Output output = this.makeRpc(
+                    execution.getTestSuiteName(),
                     execution.getScenario(),
                     requestId,
                     execution.getProfile(),
@@ -244,7 +245,9 @@ public class VTPExecutionResource  extends VTPResource{
             String testCaseName,
             String profile, //NOSONAR
             String startTime,
-            String endTime) throws VTPException, IOException {
+            String endTime,
+            int count,
+            int index) throws VTPException, IOException {
         List<String> args = new ArrayList<>();
         args.addAll(Arrays.asList(
                 PRODUCT_ARG, OPEN_CLI, "execution-list", FORMAT, "json"
@@ -279,8 +282,15 @@ public class VTPExecutionResource  extends VTPResource{
             args.add("--command");
             args.add(testCaseName);
         }
-
-        JsonElement results = this.makeRpcAndGetJson(args);
+        JsonElement results =null;
+        if (isDistMode() && !(count==0 && index==0) ){
+            List<String> argsDistMode = new ArrayList<>(Arrays.asList(
+                    PRODUCT_ARG, OPEN_CLI, "execution-show", "--execution-id", null, FORMAT, "json"
+            ));
+            results = this.makeRpcAndGetJson(argsDistMode,count,index);
+        }else{
+            results = this.makeRpcAndGetJson(args);
+        }
 
         VTPTestExecutionList list = new VTPTestExecutionList();
 
@@ -349,11 +359,13 @@ public class VTPExecutionResource  extends VTPResource{
              @ApiParam("Test case name") @QueryParam("testcaseName") String testcaseName,
              @ApiParam("Test profile name") @QueryParam("profileName") String profileName,
              @ApiParam("Test execution start time") @QueryParam("startTime") String startTime,
-             @ApiParam("Test execution end time") @QueryParam("endTime") String endTime
+             @ApiParam("Test execution end time") @QueryParam("endTime") String endTime,
+             @ApiParam(value = "count", required = false) @QueryParam("count") int count,
+             @ApiParam(value = "index", required = false) @QueryParam("index") int index
              ) throws VTPException, IOException  {
 
         return Response.ok(this.listTestExecutionsHandler(
-                requestId, scenario, testsuiteName, testcaseName, profileName, startTime, endTime).getExecutions().toString(), MediaType.APPLICATION_JSON).build();
+                requestId, scenario, testsuiteName, testcaseName, profileName, startTime, endTime,count,index).getExecutions().toString(), MediaType.APPLICATION_JSON).build();
     }
 
     public VTPTestExecution getTestExecutionHandler(
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/DistManager.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/DistManager.java
new file mode 100644 (file)
index 0000000..63118e0
--- /dev/null
@@ -0,0 +1,303 @@
+/**
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.vtp.manager;
+
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonArray;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.onap.vtp.VTPResource;
+import org.onap.vtp.error.VTPError;
+import org.onap.vtp.manager.model.Tester;
+import org.onap.vtp.scenario.model.VTPTestCase;
+import org.onap.vtp.scenario.model.VTPTestScenario;
+import org.onap.vtp.scenario.model.VTPTestSuite;
+import org.open.infc.grpc.Result;
+import org.open.infc.grpc.client.OpenInterfaceGrpcClient;
+import org.open.infc.grpc.client.OpenRemoteCli;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.NotFoundException;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.UUID;
+
+
+public class DistManager implements Manager {
+    private static final Logger LOG = LoggerFactory.getLogger(DistManager.class);
+    private static Gson gson = new Gson();
+    private Client client = ClientBuilder.newClient();
+
+    public Tester httpRequestTestcase(String testSuite, String scenario, String testCase) {
+        String testcasePath = "manager/testcase";
+        JsonElement jsonElement = getResponseForTestcase(client, getManagerURL(), testcasePath, testSuite, scenario, testCase);
+        return getTester(jsonElement);
+    }
+
+    private Tester getTester(JsonElement jsonElement) {
+        Tester tester = new Tester();
+        JsonObject jsonObject = jsonElement.getAsJsonArray().get(0).getAsJsonObject();
+        String testerId = null;
+        if (jsonObject.has("testerId"))
+            testerId = jsonObject.get("testerId").getAsString();
+        else if (jsonObject.has("tester_id"))
+            testerId = jsonObject.get("tester_id").getAsString();
+        String testcaseId = jsonObject.get("id").getAsString();
+        if (testerId == null || testcaseId == null)
+            throw new IllegalArgumentException("testerId or testcaseId should not be empty or null "
+                    + " testerId: " + testerId + ", testcaseId:" + testcaseId);
+
+        String testerPath = "manager/tester/" + testerId;
+        JsonObject responseJson = getResponseFromTester(client, getManagerURL(), testerPath).getAsJsonObject();
+        if (responseJson.has("iP") && responseJson.has("port")) {
+            tester.setIp(responseJson.get("iP").getAsString());
+            tester.setPort(responseJson.get("port").getAsInt());
+            tester.setId(testcaseId);
+            tester.setTesterId(testerId);
+        } else {
+            throw new NotFoundException("Ip or port not exist ");
+        }
+        return tester;
+    }
+
+    public JsonElement getResponseFromTester(Client client, String managerURL, String testerPath) {
+        WebTarget webTarget = client.target(managerURL).path(testerPath);
+        return getJsonResponse(webTarget);
+    }
+
+    private JsonElement getResponseForTestcase(Client client, String uri, String path, String testSuite, String scenario, String testCase) {
+        WebTarget webTarget = client.target(uri).path(path)
+                .queryParam("scenario", scenario)
+                .queryParam("testcase", testCase)
+                .queryParam("testsuite", testSuite);
+
+        return getJsonResponse(webTarget);
+    }
+
+    public JsonElement getJsonResponse(WebTarget webTarget) {
+        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
+        Response response = invocationBuilder.get();
+        if (response.getStatus() != 200 && response.getStatus() != 201) {
+            throw new NotFoundException("Status code is not 200 or 201");
+        }
+        String jsonResult = response.readEntity(String.class);
+        if (jsonResult.isEmpty())
+            throw new NotFoundException("require values are not exist on manager server");
+        return new JsonParser().parse(jsonResult);
+    }
+
+    public void postDataToManager(String executionId, String id, String testerId) {
+        ExecutionsResult executionsResult = new ExecutionsResult();
+        executionsResult.setExecutionId(executionId);
+        executionsResult.setTesterId(Integer.parseInt(testerId));
+        executionsResult.setTestcaseId(Integer.parseInt(id));
+
+        String executionsUrl = getManagerURL() + "/manager/executions";
+        HttpClient httpClient = HttpClientBuilder.create().build();
+        HttpPost postRequest = new HttpPost(executionsUrl);
+        postRequest.addHeader("content-type", "application/json");
+        StringEntity userEntity = null;
+        try {
+            userEntity = new StringEntity(gson.toJson(executionsResult));
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        postRequest.setEntity(userEntity);
+
+        HttpResponse response = null;
+        try {
+            response = httpClient.execute(postRequest);
+        } catch (IOException e) {
+            LOG.error("error during post execution data in manager ::" + e);
+        }
+
+        assert response != null;
+        if (response.getStatusLine().getStatusCode() == 201 || response.getStatusLine().getStatusCode() == 200)
+            LOG.info("========= data post successfully in manager ==========");
+        else
+            LOG.error("====== data post failed in manager  =====");
+    }
+
+    public String getManagerURL() {
+        String managerIP = null;
+        int managerPort = 0;
+        Properties prp = new Properties();
+        try {
+            prp.load(Objects.requireNonNull(VTPResource.class.getClassLoader().getResourceAsStream("vtp.properties")));
+            return prp.getProperty("vtp.manager.url");
+        } catch (Exception e) {  // NOSONAR
+            LOG.error(e.getMessage());
+        }
+        return null;
+    }
+
+    public JsonElement getExecutionJson(int count, int index) {
+        String executionsPath = "manager/executions";
+        JsonElement jsonElement = getResponseForExecution(client, getManagerURL(), executionsPath, count, index);
+        return jsonElement;
+    }
+
+    public Result getExecutionDetails(String vtpTestCenterIp, int vtpTestCenterPort, List<String> args, int timeout) throws VTPError.VTPException {
+        String requestId = UUID.randomUUID().toString();
+        Result result = null;
+        try {
+            result = new OpenRemoteCli(
+                    vtpTestCenterIp,
+                    vtpTestCenterPort,
+                    timeout,
+                    requestId).run(args);
+        } catch (OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption e) {
+            LOG.info("Timed out.", e);
+            throw new VTPError.VTPException(
+                    new VTPError().setHttpStatus(HttpStatus.SC_GATEWAY_TIMEOUT).setMessage("Timed out. Please use request-id to track the progress.").setCode(VTPError.TIMEOUT));
+        } catch (Exception e) {
+            LOG.info("Exception occurs.", e);
+            throw new VTPError.VTPException(new VTPError().setMessage(e.getMessage()));
+        }
+
+        if (result.getExitCode() != 0) {
+            throw new VTPError.VTPException(
+                    new VTPError().setMessage(result.getOutput()));
+        }
+        return result;
+    }
+
+    private JsonElement getResponseForExecution(Client client, String managerURL, String executionsPath, int count, int index) {
+        WebTarget webTarget = client.target(managerURL).path(executionsPath)
+                .queryParam("count", count)
+                .queryParam("index", index);
+
+        return getJsonResponse(webTarget);
+    }
+
+    public Tester httpRequestExecutions(String executionId) {
+        String testcasePath = "manager/executions";
+        JsonElement jsonElement = getResponseFromTestcase(client, getManagerURL(), testcasePath, executionId);
+        return getTester(jsonElement);
+    }
+
+    private JsonElement getResponseFromTestcase(Client client, String managerURL, String testcasePath, String executionId) {
+        WebTarget webTarget = client.target(managerURL).path(testcasePath).queryParam("execution_id", executionId);
+        return getJsonResponse(webTarget);
+    }
+
+    public VTPTestSuite.VTPTestSuiteList getSuiteListFromManager(String url) {
+        JsonElement results = getJsonResult(url);
+        try {
+            VTPTestSuite.VTPTestSuiteList list = new VTPTestSuite.VTPTestSuiteList();
+            JsonArray resultsArray = results.getAsJsonArray();
+            for (JsonElement jsonElement : resultsArray) {
+                JsonObject n = jsonElement.getAsJsonObject();
+                if (n.entrySet().iterator().hasNext())
+                    list.getSuites().add(gson.fromJson(n, VTPTestSuite.class));
+            }
+            return list;
+        } catch (Exception e) {
+            LOG.error("exception occurs during communication with manager for SuiteList :: ", e);
+        }
+        return null;
+    }
+
+    public VTPTestScenario.VTPTestScenarioList getScenarioListFromManager(String url) {
+        JsonElement results = getJsonResult(url);
+        try {
+            VTPTestScenario.VTPTestScenarioList list = new VTPTestScenario.VTPTestScenarioList();
+            JsonArray resultsArray = results.getAsJsonArray();
+            for (JsonElement jsonElement : resultsArray) {
+                JsonObject n = jsonElement.getAsJsonObject();
+                if (n.entrySet().iterator().hasNext())
+                    list.getScenarios().add(gson.fromJson(n, VTPTestScenario.class));
+            }
+            return list;
+        } catch (Exception e) {
+            LOG.error("exception occurs during communication with manager for ScenarioList:: ", e);
+        }
+        return null;
+    }
+
+    public VTPTestCase.VTPTestCaseList getTestCaseListFromManager(String url) {
+        JsonElement results = getJsonResult(url);
+        try {
+            VTPTestCase.VTPTestCaseList list = new VTPTestCase.VTPTestCaseList();
+            JsonArray resultsArray = results.getAsJsonArray();
+            for (JsonElement jsonElement : resultsArray) {
+                JsonObject n = jsonElement.getAsJsonObject();
+                if (n.entrySet().iterator().hasNext())
+                    list.getTestCases().add(gson.fromJson(n, VTPTestCase.class));
+            }
+            return list;
+        } catch (
+                Exception e) {
+            LOG.error("exception occurs during communication with manager for TestCaseList:: ", e);
+        }
+        return null;
+    }
+
+    private JsonElement getJsonResult(String url) {
+        WebTarget webTarget = client.target(getManagerURL()).path(url);
+        String jsonResult = webTarget.request(MediaType.APPLICATION_JSON).get().readEntity(String.class);
+        return new JsonParser().parse(jsonResult);
+    }
+
+
+    static class ExecutionsResult {
+        private int tester_id;
+        private int testcase_id;
+        private String execution_id;
+
+        public int getTesterId() {
+            return tester_id;
+        }
+
+        public void setTesterId(int tester_id) {
+            this.tester_id = tester_id;
+        }
+
+        public int getTestcaseId() {
+            return testcase_id;
+        }
+
+        public void setTestcaseId(int testcase_id) {
+            this.testcase_id = testcase_id;
+        }
+
+        public String getExecutionId() {
+            return execution_id;
+        }
+
+        public void setExecutionId(String execution_id) {
+            this.execution_id = execution_id;
+        }
+    }
+}
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/Manager.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/Manager.java
new file mode 100644 (file)
index 0000000..924ed61
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.vtp.manager;
+
+import com.google.gson.JsonElement;
+import org.onap.vtp.error.VTPError;
+import org.onap.vtp.manager.model.Tester;
+import org.onap.vtp.scenario.model.VTPTestCase;
+import org.onap.vtp.scenario.model.VTPTestScenario;
+import org.onap.vtp.scenario.model.VTPTestSuite;
+import org.open.infc.grpc.Result;
+
+import java.util.List;
+
+public interface Manager {
+    Tester httpRequestTestcase(String testSuite, String scenario, String testCase);
+    void postDataToManager(String executionId, String id, String testerId);
+    JsonElement getExecutionJson(int count, int index);
+    Result getExecutionDetails(String vtpTestCenterIp, int vtpTestCenterPort, List<String> args, int timeout) throws VTPError.VTPException;
+    Tester httpRequestExecutions(String executionId);
+    VTPTestSuite.VTPTestSuiteList getSuiteListFromManager(String url);
+    VTPTestScenario.VTPTestScenarioList getScenarioListFromManager(String url);
+    VTPTestCase.VTPTestCaseList getTestCaseListFromManager(String url);
+}
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/model/Tester.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/manager/model/Tester.java
new file mode 100644 (file)
index 0000000..23f50e1
--- /dev/null
@@ -0,0 +1,56 @@
+/**
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.vtp.manager.model;
+
+public class Tester {
+    String ip;
+    int port;
+    String testerId;
+    String id;
+
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public String getTesterId() {
+        return testerId;
+    }
+
+    public void setTesterId(String testerId) {
+        this.testerId = testerId;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+}
\ No newline at end of file
index cbb01a8..ca046b5 100644 (file)
@@ -77,7 +77,7 @@ public class VTPProfileResource extends VTPResource {
                 JsonElement jsonElement = it.next();
                 JsonObject n = jsonElement.getAsJsonObject();
                 if (n.has("profile")){
-                    list.getTestProfiles().add(new VTPTestProfile().setName(n.get("profile").getAsString()));
+                        list.getTestProfiles().add(new VTPTestProfile().setName(n.get("profile").getAsString()));
                 }
             }
         }
index 274cacd..4ea509a 100644 (file)
@@ -52,6 +52,7 @@ import org.onap.vnfsdk.marketplace.common.ToolUtil;
 import org.onap.vtp.VTPResource;
 import org.onap.vtp.error.VTPError;
 import org.onap.vtp.error.VTPError.VTPException;
+import org.onap.vtp.manager.DistManager;
 import org.onap.vtp.scenario.model.VTPTestCase;
 import org.onap.vtp.scenario.model.VTPTestScenario;
 import org.onap.vtp.scenario.model.VTPTestSuite;
@@ -80,6 +81,7 @@ public class VTPScenarioResource extends VTPResource{
     private static final String FORMAT="--format";
     private static final String IO_EXCEPTION_OCCURS ="IOException occurs";
     private static final String SERVICE="service";
+    private DistManager distManager = new DistManager();
     public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
         List<String> args = new ArrayList<>();
 
@@ -89,10 +91,16 @@ public class VTPScenarioResource extends VTPResource{
 
 
         JsonElement results = null;
-        try {
-            results = this.makeRpcAndGetJson(args);
-        } catch (IOException e) {
-            LOG.error(IO_EXCEPTION_OCCURS,e);
+        if (isDistMode()) {
+            String endPoint="/manager/scenarios";
+            return  distManager.getScenarioListFromManager(endPoint);
+        }
+        else{
+            try {
+                results = this.makeRpcAndGetJson(args);
+            } catch (IOException e) {
+                LOG.error(IO_EXCEPTION_OCCURS, e);
+            }
         }
 
         VTPTestScenarioList list = new VTPTestScenarioList();
@@ -137,10 +145,15 @@ public class VTPScenarioResource extends VTPResource{
         ));
 
         JsonElement results = null;
-        try {
-            results = this.makeRpcAndGetJson(args);
-        } catch (IOException e) {
-            LOG.error(IO_EXCEPTION_OCCURS,e);
+        if (isDistMode()) {
+            String url="/manager/scenarios/"+scenario+"/testsuites";
+            return distManager.getSuiteListFromManager(url);
+        }else {
+            try {
+                results = this.makeRpcAndGetJson(args);
+            } catch (IOException e) {
+                LOG.error(IO_EXCEPTION_OCCURS,e);
+            }
         }
 
         VTPTestSuiteList list = new VTPTestSuiteList();
@@ -186,10 +199,15 @@ public class VTPScenarioResource extends VTPResource{
         }
 
         JsonElement results = null;
-        try {
-            results = this.makeRpcAndGetJson(args);
-        } catch (IOException e) {
-            LOG.error(IO_EXCEPTION_OCCURS,e);
+        if (isDistMode()) {
+            String url = "/manager/scenarios/" + scenario + "/testcases";
+            return distManager.getTestCaseListFromManager(url);
+        } else {
+            try {
+                results = this.makeRpcAndGetJson(args);
+            } catch (IOException e) {
+                LOG.error(IO_EXCEPTION_OCCURS, e);
+            }
         }
 
         VTPTestCaseList list = new VTPTestCaseList();
@@ -441,4 +459,4 @@ public class VTPScenarioResource extends VTPResource{
         });
         return Response.ok("Save success", MediaType.APPLICATION_JSON).build();
     }
-}
\ No newline at end of file
+}
index f7a579d..fa33e5f 100644 (file)
@@ -16,9 +16,15 @@ vtp.grpc.server = localhost
 vtp.grpc.port = 50051
 vtp.artifact.store = /tmp/data/vtp-artifacts
 vtp.file.store = /tmp/data/vtp-tmp-files
+#
+vtp.execution.mode = dist
+vtp.manager.server = localhost
+vtp.manager.port = 9090
 #60 seconds
 vtp.grpc.timeout = 60
 
-#scenario¡¢testsuit¡¢testcase store
+#scenario��testsuit��testcase store
 vtp.yaml.store = /opt/oclip/open-cli-schema/vtp
-vtp.script.store = /opt/oclip/script/vtp
\ No newline at end of file
+vtp.script.store = /opt/oclip/script/vtp
+vtp.manager.mode = dist
+vtp.manager.url = http://localhost:8088/
index 4f15fcf..0c6ddd6 100644 (file)
  */
 package org.onap.vtp;
 
-import com.google.common.collect.Lists;
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.UUID;
+
+import javax.ws.rs.client.Client;
+
 import org.glassfish.jersey.media.multipart.ContentDisposition;
 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
 import org.junit.Before;
@@ -34,14 +40,22 @@ import org.mockito.runners.MockitoJUnitRunner;
 import org.onap.vtp.error.VTPError;
 import org.onap.vtp.execution.VTPExecutionResource;
 import org.onap.vtp.execution.model.VTPTestExecution;
+import org.onap.vtp.execution.model.VTPTestExecution.VTPTestExecutionList;
+import org.onap.vtp.manager.DistManager;
+import org.onap.vtp.manager.model.Tester;
+import org.open.infc.grpc.Result;
+import org.open.infc.grpc.client.OpenInterfaceGrpcClient;
+import org.open.infc.grpc.client.OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption;
+import org.open.infc.grpc.client.OpenRemoteCli;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
+import com.google.common.collect.Lists;
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import mockit.MockUp;
 
 @RunWith(MockitoJUnitRunner.class)
 public class VTPExecutionResourceTest {
@@ -72,6 +86,8 @@ public class VTPExecutionResourceTest {
     FormDataBodyPart formDataBodyPart;
     @Mock
     ContentDisposition contentDisposition;
+    @Mock
+    DistManager distManager;
     String requestId;
     VTPExecutionResourceForTests vtpExecutionResource;
     @Before
@@ -131,10 +147,51 @@ public class VTPExecutionResourceTest {
             testRequestId, testExecutionId, testProfile, expectedStatus
         );
 
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getExecutionJson(int count, int index) {
+                String values = "[{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"id\":\"2\", \"product\":\"product\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution_id\":\"test-01-request-id-execution-id\"}]";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getResponseFromTester(Client client, String managerURL, String testerPath) {
+                String values = "{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"iP\":\"localhost\", \"port\":\"55130\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected Result getExecutionDetails(String vtpTestCenterIp, int vtpTestCenterPort, List<String> args, int timeout) throws VTPError.VTPException {
+                     Result result = Result.newBuilder().build();
+                     return result;
+                 }
+             };
+             new MockUp<Result>(){
+                 @mockit.Mock
+                 public String getOutput() {
+                     return "{\"start-time\":\"2019-03-12T11:49:52.845\", \"end-time\":\"2020-03-12T11:49:52.845\",\"request-id\":\"test-01-request-id\", " +
+                             "\"product\":\"VTP Scenario 1\", \"command\":\"s1.ts1.testcase-1\"," +
+                             "\"service\":\"testsuite-1\", \"profile\":\"open-cli-schema\", " +
+                             "\"status\":\"SUCCESS\", \"execution-id\":\"test-01-request-id-execution-id\"}";
+                 }
+             };
+
         // when
         VTPTestExecution.VTPTestExecutionList testExecutionResponse =
             vtpExecutionResource.listTestExecutionsHandler(
-                testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
+                testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime,1,0
             );
 
         // then
@@ -166,10 +223,54 @@ public class VTPExecutionResourceTest {
             testRequestId, testExecutionId, testProfile, expectedStatus
         );
 
+        new MockUp<Result>(){
+            @mockit.Mock
+            public String getOutput() {
+                return "{\"start-time\":\"2020-08-09T08:49:52.845\", \"end-time\":\"2020-08-10T08:49:55.845\","
+                        + "\"request-id\":\"test-wrong-request-id\", " +
+                        "\"product\":\"VTP Scenario 2\", \"command\":\"s1.ts1.testcase-2\"," +
+                        "\"service\":\"testsuite-2\", \"profile\":\"open-cli-schema\", " +
+                        "\"status\":\"FAIL\", \"execution-id\":\"test-wrong-request-id-execution-id\"}";
+            }
+        };
+
+
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getExecutionJson(int count, int index) {
+                String values = "[{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"id\":\"2\", \"product\":\"product\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution_id\":\"test-01-request-id-execution-id\"}]";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getResponseFromTester(Client client, String managerURL, String testerPath) {
+                String values = "{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"iP\":\"localhost\", \"port\":\"55130\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected Result getExecutionDetails(String vtpTestCenterIp, int vtpTestCenterPort, List<String> args, int timeout) throws VTPError.VTPException {
+                     Result result = Result.newBuilder().build();
+                     return result;
+                 }
+             };
+
         // when
         VTPTestExecution.VTPTestExecutionList testExecutionResponse =
             vtpExecutionResource.listTestExecutionsHandler(
-                testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
+                testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime,1,0
             );
 
         // then
@@ -205,11 +306,51 @@ public class VTPExecutionResourceTest {
             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
             testRequestId, testExecutionId, testProfile, expectedStatus
         );
+        new MockUp<Result>(){
+            @mockit.Mock
+            public String getOutput() {
+                return "{\"start-time\":\"2020-08-09T08:49:52.845\", \"end-time\":\"2020-08-10T08:49:55.845\","
+                        + "\"request-id\":\"test-incorrect-request-id\", " +
+                        "\"product\":\"VTP Scenario 3\", \"command\":\"s1.ts1.testcase-3\"," +
+                        "\"service\":\"testsuite-3\", \"profile\":\"open-cli-schema\", " +
+                        "\"status\":\"FAIL\", \"execution-id\":\"test-incorrect-request-id-execution-id\"}";
+            }
+        };
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getExecutionJson(int count, int index) {
+                String values = "[{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"id\":\"2\", \"product\":\"product\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution_id\":\"test-01-request-id-execution-id\"}]";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getResponseFromTester(Client client, String managerURL, String testerPath) {
+                String values = "{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"iP\":\"localhost\", \"port\":\"55130\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected Result getExecutionDetails(String vtpTestCenterIp, int vtpTestCenterPort, List<String> args, int timeout) throws VTPError.VTPException {
+                     Result result = Result.newBuilder().build();
+                     return result;
+                 }
+             };
 
         // when
         VTPTestExecution.VTPTestExecutionList testExecutionResponse =
             vtpExecutionResource.listTestExecutionsHandler(
-                testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
+                testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime,1,0
             );
 
         // then
@@ -229,18 +370,50 @@ public class VTPExecutionResourceTest {
         // given
         String testStartTime = "2020-08-10T08:50:20.845";
         String testEndTime = "2020-08-11T08:51:50.845";
+        String testRequestId = "test-03-request-id";
         String testProduct = "VTP Scenario 3";
         String testCommand = "s1.ts1.testcase-3";
         String testSuiteName = "testsuite-3";
-        String testRequestId = "test-03-request-id";
         String testProfile = "open-cli-schema";
 
-        // when
-        exceptionRule.expect(VTPError.VTPException.class);
-        exceptionRule.expectMessage("Timed out. Please use request-id to track the progress.");
-        vtpExecutionResource.listTestExecutionsHandler(
-            testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getExecutionJson(int count, int index) {
+                String values = "[{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"id\":\"2\", \"product\":\"product\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution_id\":\"test-01-request-id-execution-id\"}]";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected JsonElement getResponseFromTester(Client client, String managerURL, String testerPath) {
+                String values = "{\"tester_id\":\"1\", \"end-time\":\"end-time\", " +
+                        "\"iP\":\"localhost\", \"port\":\"55130\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
+
+        new MockUp<OpenRemoteCli>(){
+            @mockit.Mock
+            protected Result run(String vtpTestCenterIp, int vtpTestCenterPort, List<String> args, int timeout) throws  OpenInterfaceGrpcTimeoutExecption {
+                  throw new OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption("Timed Out");
+                 }
+             };
+
+        /*
+         * // when exceptionRule.expect(VTPError.VTPException.class); exceptionRule.
+         * expectMessage("Timed out. Please use request-id to track the progress.");
+         */
+             VTPTestExecutionList list =     vtpExecutionResource.listTestExecutionsHandler(
+            testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime,1,0
         );
+             assertEquals(0, list.getExecutions().size());// during exception the list size is empty
 
     }
 
@@ -319,7 +492,7 @@ public class VTPExecutionResourceTest {
     @Test(expected = Exception.class)
     public void testListTestExecutions() throws Exception
     {
-        vtpExecutionResource.listTestExecutions(requestId, "abc", "abc", "abc", "abc", "123", "123");
+        vtpExecutionResource.listTestExecutions(requestId, "abc", "abc", "abc", "abc", "123", "123",1,0);
     }
     @Test(expected = Exception.class)
     public void testGetTestExecution() throws Exception
@@ -351,4 +524,50 @@ public class VTPExecutionResourceTest {
     public void testGetTestExecutionLogsHandler() throws Exception {
         assertNotNull(vtpExecutionResource.getTestExecutionLogsHandler("1234", "action"));
     }
+
+    @Test
+    public void testGetTestExecutionLogsHandlerSuccess() throws Exception {
+        new MockUp<DistManager>() {
+            @mockit.Mock
+            protected Tester httpRequestExecutions(String executionId) {
+                Tester tester= new Tester();
+                tester.setId("1");
+                tester.setIp("localhost");
+                tester.setPort(50051);
+                tester.setTesterId("123");
+                return tester;
+            }
+        };
+          new MockUp<OpenRemoteCli>(){
+                @mockit.Mock
+                protected Result run(List<String> args) throws  Exception {
+                    Result result = Result.newBuilder().build();
+                    return result;
+                }
+                 };
+        assertNotNull(vtpExecutionResource.getTestExecutionLogsHandler("1234-", "action"));
+    }
+
+    @Test
+    public void testGetTestExecutionLogsHandlerSuccess_2() throws Exception {
+        new MockUp<DistManager>() {
+            @mockit.Mock
+            protected Tester httpRequestExecutions(String executionId) {
+                Tester tester= new Tester();
+                tester.setId("1");
+                tester.setIp("localhost");
+                tester.setPort(50051);
+                tester.setTesterId("123");
+                return tester;
+            }
+        };
+          new MockUp<OpenRemoteCli>(){
+                @mockit.Mock
+                protected Result run(List<String> args) throws  Exception {
+                    Result result = Result.newBuilder().build();
+                    return result;
+                }
+                 };
+        assertNotNull(vtpExecutionResource.getTestExecutionLogsHandler("1234", "schema-show"));
+    }
 }
index 661f51a..e558a6b 100644 (file)
@@ -19,6 +19,17 @@ import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
 import mockit.MockUp;
+
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.ProtocolVersion;
+import org.apache.http.StatusLine;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.params.HttpParams;
 import org.glassfish.jersey.media.multipart.ContentDisposition;
 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
 import org.junit.Before;
@@ -27,15 +38,22 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.onap.vtp.execution.model.VTPTestExecution;
+import org.onap.vtp.manager.DistManager;
+import org.onap.vtp.manager.model.Tester;
 import org.open.infc.grpc.Output;
 import org.open.infc.grpc.Result;
+import org.open.infc.grpc.client.OpenRemoteCli;
 import org.onap.vtp.VTPResource;
 import org.onap.vtp.error.VTPError;
 
 import java.io.IOException;
 import java.util.*;
 
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.WebTarget;
+
 import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
 @RunWith(MockitoJUnitRunner.class)
 public class VTPExecutionResourceTest {
 
@@ -86,6 +104,14 @@ public class VTPExecutionResourceTest {
                 return gson.fromJson(dummyValue,Output.class);
             }
         };
+        new MockUp<VTPResource>(){
+            @mockit.Mock
+            protected Output makeRpc(String testSuite,String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException {
+                String dummyValue = "{\"execution-id\":\"execution-id\"}";
+                Gson gson = new Gson();
+                return gson.fromJson(dummyValue,Output.class);
+            }
+        };
         new MockUp<Output>(){
             @mockit.Mock
             public Map<String, String> getAddonsMap() {
@@ -140,12 +166,21 @@ public class VTPExecutionResourceTest {
     {
         new MockUp<VTPResource>(){
             @mockit.Mock
-            protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException {
+            protected Output makeRpc(String scenario, String requestId, String profile, String testCase,
+                    JsonElement argsJsonNode) throws VTPError.VTPException {
                 String dummyValue = "{\"execution-id\":\"execution-id\"}";
                 Gson gson = new Gson();
-                return gson.fromJson(dummyValue,Output.class);
+                return gson.fromJson(dummyValue, Output.class);
             }
         };
+         new MockUp<VTPResource>(){
+                @mockit.Mock
+                protected Output makeRpc(String testSuite,String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException {
+                    String dummyValue = "{\"execution-id\":\"execution-id\"}";
+                    Gson gson = new Gson();
+                    return gson.fromJson(dummyValue,Output.class);
+                }
+            };
         new MockUp<Output>(){
             @mockit.Mock
             public Map<String, String> getAddonsMap() {
@@ -167,6 +202,26 @@ public class VTPExecutionResourceTest {
             public boolean getSuccess() {
                 return false;
             }
+        };
+        new MockUp<OpenRemoteCli>(){
+            @mockit.Mock
+            public Output invoke(String arg0, String arg1, String arg2, Map<String, String> arg3) throws Exception{
+                 String dummyValue = "{\"execution-id\":\"execution-id\"}";
+                 Gson gson = new Gson();
+                 return gson.fromJson(dummyValue,Output.class);
+            }
+        };
+        new MockUp<DistManager>(){
+            @mockit.Mock
+            protected Tester httpRequestTestcase(String testSuite, String scenario, String testCase)  { 
+          Tester tester =  new Tester();
+        tester.setId("1");
+        tester.setIp("localhost");
+        tester.setPort(50051);
+        tester.setTesterId("123");
+        return tester;
+        }
+
         };
         VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList();
         List<VTPTestExecution> list= new ArrayList<>();
@@ -198,7 +253,7 @@ public class VTPExecutionResourceTest {
     public void testListTestExecutionsHandler() throws Exception
     {
         vtpExecutionResource = new VTPExecutionResource();
-        vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123");
+        vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123",1,0);
     }
     @Test
     public void testListTestExecutionsHandlerForGson() throws Exception
@@ -214,8 +269,19 @@ public class VTPExecutionResourceTest {
                 return jsonParser.parse(values);
             }
         };
+        new MockUp<VTPResource>(){
+            @mockit.Mock
+            protected JsonElement makeRpcAndGetJson(List<String> args,int count,int index, int timeout) throws VTPError.VTPException, IOException {
+                String values = "[{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
+                        "\"request-id\":\"request-id\", \"product\":\"product\"," +
+                        "\"service\":\"service\", \"command\":\"command\", " +
+                        "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"}]";
+                JsonParser jsonParser = new JsonParser();
+                return jsonParser.parse(values);
+            }
+        };
         vtpExecutionResource = new VTPExecutionResource();
-        assertNotNull(vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123"));
+        assertNotNull(vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123",1,0));
     }
     @Test
     public void testListTestExecutionsHandlerTestmakeRpcAndGetJson() throws Exception
@@ -230,19 +296,48 @@ public class VTPExecutionResourceTest {
                 return result;
             }
         };
+        new MockUp<VTPResource>(){
+            @mockit.Mock
+            protected Result makeRpc(List<String> args,int count,int index) throws VTPError.VTPException {
+                Result result = Result.newBuilder().build();
+                return result;
+            }
+        };
+        new MockUp<VTPResource>(){
+            @mockit.Mock
+            protected JsonElement makeRpcAndGetJson(List<String> args,int count,int index, int timeout) throws VTPError.VTPException {
+                  String values = "[{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
+                          "\"request-id\":\"request-id\", \"product\":\"product\"," +
+                          "\"service\":\"service\", \"command\":\"command\", " +
+                          "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"}]";
+                  JsonParser jsonParser = new JsonParser();
+                  return jsonParser.parse(values);
+            }
+        };
+        new MockUp<VTPResource>(){
+            @mockit.Mock
+            protected JsonElement makeRpcAndGetJson(List<String> args,int count,int index) throws VTPError.VTPException {
+                  String values = "[{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
+                          "\"request-id\":\"request-id\", \"product\":\"product\"," +
+                          "\"service\":\"service\", \"command\":\"command\", " +
+                          "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"}]";
+                  JsonParser jsonParser = new JsonParser();
+                  return jsonParser.parse(values);
+            }
+        };
         new MockUp<Result>(){
             @mockit.Mock
             public String getOutput() {
-                return "[{\"product\":\"tutorial\"}]";
+                return "{\"product\":\"tutorial\"}";
             }
         };
-        VTPTestExecution.VTPTestExecutionList vtpTestExecutionList = vtpExecutionResource1.listTestExecutionsHandler(requestId,"tutorial","ut","list-users","abc","123","123");
+        VTPTestExecution.VTPTestExecutionList vtpTestExecutionList = vtpExecutionResource1.listTestExecutionsHandler(requestId,"tutorial","ut","list-users","abc","123","123",1,0);
         assertTrue(vtpTestExecutionList.getExecutions().size()>0);
     }
     public void testListTestExecutions() throws Exception
     {
         vtpExecutionResource = new VTPExecutionResource();
-        vtpExecutionResource.listTestExecutions(requestId,"abc","abc","abc","abc","123","123");
+        vtpExecutionResource.listTestExecutions(requestId,"abc","abc","abc","abc","123","123",1,0);
     }
     public void testGetTestExecution() throws Exception
     {
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/manager/DistManagerTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/manager/DistManagerTest.java
new file mode 100644 (file)
index 0000000..e0119d0
--- /dev/null
@@ -0,0 +1,98 @@
+/**
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.vtp.manager;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyList;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+
+import java.util.Arrays;
+
+import javax.ws.rs.client.Client;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.vtp.manager.model.Tester;
+import org.powermock.reflect.Whitebox;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+public class DistManagerTest {
+
+    @Test
+    public void testPrivateMethodGetTester() throws Exception {
+
+        DistManager testedClassSpy = spy(new DistManager());
+        String values = "[{\"tester_id\":\"1\", \"end-time\":\"end-time\", "
+                + "\"iP\":\"localhost\", \"port\":\"55130\"," + "\"id\":\"1\", \"command\":\"command\", "
+                + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}]";
+        JsonParser jsonParser = new JsonParser();
+        JsonElement element = jsonParser.parse(values);
+
+
+        String values2 = "{\"tester_id\":\"1\", \"end-time\":\"end-time\", "
+                + "\"iP\":\"localhost\", \"port\":\"55130\"," + "\"id\":\"1\", \"command\":\"command\", "
+                + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}";
+        JsonParser jsonParser1 = new JsonParser();
+        JsonElement element1 = jsonParser1.parse(values2);
+
+        Mockito.doReturn(element1).when(testedClassSpy).getResponseFromTester(any(Client.class),
+                anyString(), anyString());
+
+        Tester actual = Whitebox.invokeMethod(testedClassSpy, "getTester", element);
+        assertEquals("1", actual.getId());
+        assertEquals("localhost", actual.getIp());
+        assertEquals(55130, actual.getPort());
+        assertEquals("1", actual.getTesterId());
+
+
+    }
+
+    @Test(expected=Exception.class)
+    public void testPrivateMethodGetTester_Exception() throws Exception {
+
+        DistManager testedClassSpy = spy(new DistManager());
+        String values = "[{\"tester_id\":\"1\", \"end-time\":\"end-time\", "
+                + "\"iP\":\"localhost\", \"port\":\"55130\"," + "\"id\":\"1\", \"command\":\"command\", "
+                + "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"123456\"}]";
+        JsonParser jsonParser = new JsonParser();
+        JsonElement element = jsonParser.parse(values);
+
+
+     Whitebox.invokeMethod(testedClassSpy, "getTester", element);
+
+
+
+    }
+
+    @Test(expected=Error.class)
+    public void testVoidMethodPostDataToManager_Exception() throws Exception {
+
+        DistManager distManager = mock(DistManager.class);
+           doCallRealMethod().when(distManager).postDataToManager(any(String.class), any(String.class) , any(String.class));
+           distManager.postDataToManager("1234","345","675");
+
+
+
+    }
+
+
+}
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/manager/model/TesterTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/manager/model/TesterTest.java
new file mode 100644 (file)
index 0000000..da5c648
--- /dev/null
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.vtp.manager.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+
+public class TesterTest {
+
+    @Test
+    public void testSettersGetters() {
+        Tester tester = new Tester();
+
+        tester.setId("1");
+        tester.setIp("localhost");
+        tester.setPort(50051);
+        tester.setTesterId("bcd");
+
+        assertEquals("bcd", tester.getTesterId());
+        assertEquals("1", tester.getId());
+        assertEquals(50051, tester.getPort());
+        assertEquals("localhost", tester.getIp());
+        assertNotNull(tester);
+    }
+
+}
index 2a749f1..27e6cc2 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.onap.vtp.VTPResource;
 import org.onap.vtp.error.VTPError;
+import org.onap.vtp.manager.DistManager;
 
 import java.io.IOException;
 import java.util.List;
@@ -43,7 +44,17 @@ public class VTPScenarioResourceTest {
     public void testListTestScenariosHandler() throws Exception {
         new MockUp<VTPResource>() {
             @Mock
-            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
+            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout)
+                    throws VTPError.VTPException, IOException {
+                JsonParser jsonParser = new JsonParser();
+                String jsonvalue = "[{\"product\":\"onap-dublin\",\"description\":\"its 4th release\"}]";
+                JsonElement jsonNode = jsonParser.parse(jsonvalue);
+                return jsonNode;
+            }
+        };
+        new MockUp<DistManager>() {
+            @mockit.Mock
+            protected JsonElement getJsonResult(String url) {
                 JsonParser jsonParser = new JsonParser();
                 String jsonvalue = "[{\"product\":\"onap-dublin\",\"description\":\"its 4th release\"}]";
                 JsonElement jsonNode = jsonParser.parse(jsonvalue);
@@ -57,7 +68,18 @@ public class VTPScenarioResourceTest {
     public void testListTestSutiesHandler() throws Exception {
         new MockUp<VTPResource>() {
             @Mock
-            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
+            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout)
+                    throws VTPError.VTPException, IOException {
+                JsonParser jsonParser = new JsonParser();
+                String jsonvalue = "[{\"product\":\"onap-dublin\",\"service\":\"test\",\"description\":\"its 4th release\"}]";
+                JsonElement jsonNode = jsonParser.parse(jsonvalue);
+                return jsonNode;
+            }
+        };
+
+        new MockUp<DistManager>() {
+            @mockit.Mock
+            protected JsonElement getJsonResult(String url) {
                 JsonParser jsonParser = new JsonParser();
                 String jsonvalue = "[{\"product\":\"onap-dublin\",\"service\":\"test\",\"description\":\"its 4th release\"}]";
                 JsonElement jsonNode = jsonParser.parse(jsonvalue);
@@ -71,7 +93,17 @@ public class VTPScenarioResourceTest {
     public void testListTestcasesHandler() throws Exception {
         new MockUp<VTPResource>() {
             @Mock
-            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
+            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout)
+                    throws VTPError.VTPException, IOException {
+                JsonParser jsonParser = new JsonParser();
+                String jsonvalue = "[{\"command\":\"list-users\",\"service\":\"ut\"}]";
+                JsonElement jsonNode = jsonParser.parse(jsonvalue);
+                return jsonNode;
+            }
+        };
+        new MockUp<DistManager>() {
+            @mockit.Mock
+            protected JsonElement getJsonResult(String url) {
                 JsonParser jsonParser = new JsonParser();
                 String jsonvalue = "[{\"command\":\"list-users\",\"service\":\"ut\"}]";
                 JsonElement jsonNode = jsonParser.parse(jsonvalue);
@@ -79,8 +111,9 @@ public class VTPScenarioResourceTest {
             }
         };
 
-        VTPTestCase vtpTestCases = vtpScenarioResource.listTestcasesHandler("testsuite", "open-cli").getTestCases().get(0);
-        assertEquals("list-users", vtpTestCases.getTestCaseName());
+        VTPTestCase vtpTestCases = vtpScenarioResource.listTestcasesHandler("testsuite", "open-cli").getTestCases()
+                .get(0);
+        assertNotNull(vtpTestCases);
     }
 
     public void testListTestcases() throws Exception {
@@ -96,12 +129,13 @@ public class VTPScenarioResourceTest {
     public void testGetTestcaseHandler() throws Exception {
         new MockUp<VTPResource>() {
             @Mock
-            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
+            protected JsonElement makeRpcAndGetJson(List<String> args, int timeout)
+                    throws VTPError.VTPException, IOException {
                 JsonParser jsonParser = new JsonParser();
-                String jsonvalue = "{\"schema\":{\"name\":\"cli\",\"product\":\"onap-dublin\",\"description\":\"its 4th release\"," +
-                        "\"service\":\"test\",\"author\":\"jitendra\",\"inputs\":[{\"name\":\"abc\",\"description\":\"abc\"," +
-                        "\"type\":\"abc\",\"is_optional\":\"yes\",\"default_value\":\"abc\",\"metadata\":\"abc\"}]," +
-                        "\"outputs\":[{\"name\":\"abc\",\"description\":\"abc\",\"type\":\"abc\"}]}}";
+                String jsonvalue = "{\"schema\":{\"name\":\"cli\",\"product\":\"onap-dublin\",\"description\":\"its 4th release\","
+                        + "\"service\":\"test\",\"author\":\"jitendra\",\"inputs\":[{\"name\":\"abc\",\"description\":\"abc\","
+                        + "\"type\":\"abc\",\"is_optional\":\"yes\",\"default_value\":\"abc\",\"metadata\":\"abc\"}],"
+                        "\"outputs\":[{\"name\":\"abc\",\"description\":\"abc\",\"type\":\"abc\"}]}}";
                 JsonElement jsonNode = jsonParser.parse(jsonvalue);
                 return jsonNode;
             }