mocked testcases added 87/58887/2
authorGanesh Chandrasekaran <ganesh.c@samsung.com>
Fri, 3 Aug 2018 09:06:37 +0000 (18:06 +0900)
committerGanesh Chandrasekaran <ganesh.c@samsung.com>
Fri, 3 Aug 2018 09:30:15 +0000 (18:30 +0900)
Issue-ID: CCSDK-433

Change-Id: I9961df9131b4672a99601b31ed2e9312d0840d6b
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
sshapi-call-node/provider/ReadMe.md
sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java
sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java

index 6c31ae0..beaadcd 100644 (file)
@@ -20,9 +20,9 @@ ResponsePrefix    ->    Optional    ->    location the response will be written
 listName[i]    ->    Optional    ->    Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details
 
 Output:
-"'ResponsePrefix'.sshApi.call.node.status"   ->    SSH Exit status code is set in here.
-"'ResponsePrefix'.sshApi.call.node.stdout"   ->    SSH command execution result is put in here. 
-"'ResponsePrefix'.sshApi.call.node.stderr"   ->    SSH execution failure message is put in here. 
+"sshApi.call.node.status"   ->    SSH Exit status code is set in here.
+"sshApi.call.node.stdout"   ->    SSH command execution result is put in here. 
+"sshApi.call.node.stderr"   ->    SSH execution failure message is put in here. 
 
 
 
index 10635be..452d947 100644 (file)
@@ -65,11 +65,29 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      */
     private String PARAM_OUT_stderr = "sshApi.call.node.stderr";
 
+    /**
+     * Testing parameter - content of mocked SSH.
+     */
+    private boolean PARAM_TEST_MODE = false;
+    private String PARAM_OUT_MESSAGE = "TestOut";
+
+
     /**
      * Default success status.
      */
     private int DEF_SUCCESS_STATUS = 0;
 
+    /**
+     * Used for jUnit test and testing interface
+     */
+    public SshApiCallNode(boolean mode) {
+        PARAM_TEST_MODE = mode;
+    }
+
+    public SshApiCallNode() {
+        PARAM_TEST_MODE = false;
+    }
+
     private SshAdapter sshAdapter;
 
     public void setSshAdapter(SshAdapter sshAdapter) {
@@ -117,23 +135,33 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
         Parameters p = parser.getParameters(params);
         logger.debug("=> Connecting to SSH server...");
         SshConnection sshConnection = null;
+        int status = -1;
+        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
+        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
+        String stdoutRes = "", stderrRes = "";
         try {
-            sshConnection = getSshConnection(p);
-            sshConnection.connect();
-            logger.debug("=> Connected to SSH server...");
-            logger.debug("=> Running SSH command...");
-            sshConnection.setExecTimeout(p.sshExecTimeout);
-            ByteArrayOutputStream stdout = new ByteArrayOutputStream();
-            ByteArrayOutputStream stderr = new ByteArrayOutputStream();
-            int status;
-            if (withPty) {
-                status = sshConnection.execCommandWithPty(parser.makeCommand(params), stdout);
-                stderr = stdout;
+            if (!PARAM_TEST_MODE) {
+                sshConnection = getSshConnection(p);
+                sshConnection.connect();
+                logger.debug("=> Connected to SSH server...");
+                logger.debug("=> Running SSH command...");
+                sshConnection.setExecTimeout(p.sshExecTimeout);
+                if (withPty) {
+                    status = sshConnection.execCommandWithPty(parser.makeCommand(params), stdout);
+                    stderr = stdout;
+                }
+                else
+                    status = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr);
+                stdoutRes = stdout.toString();
+                stderrRes = stderr.toString();
+
+            } else {
+                if (params.get("TestFail").equalsIgnoreCase("true"))
+                    status = 202;
+                else
+                    status = DEF_SUCCESS_STATUS;
+                stdoutRes = params.get(PARAM_OUT_MESSAGE);
             }
-            else
-                status = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr);
-            String stdoutRes = stdout.toString();
-            String stderrRes = stderr.toString();
             logger.debug("=> executed SSH command");
 
             if(status == DEF_SUCCESS_STATUS) {
@@ -206,12 +234,10 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
 
     public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
         execCommand(params, ctx);
-        ParseParam parser = new ParseParam();
-        String responsePrefix = parser.getStringParameters(params, parser.SSH_ResponsePrefix);
-        parseResponse(ctx, responsePrefix);
+        parseResponse(ctx);
     }
 
-    private void parseResponse (SvcLogicContext ctx, String responsePrefix) throws SvcLogicException {
+    private void parseResponse (SvcLogicContext ctx) throws SvcLogicException {
         int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
         if(status != DEF_SUCCESS_STATUS) {
             StringBuilder errmsg = new StringBuilder();
index 3992dff..544057a 100644 (file)
@@ -225,4 +225,111 @@ public class TestSshApiCallNode {
         params.put("ResponseType", "json");
         adapter.execWithStatusCheck(params, svcContext);
     }
+
+    @Test
+    public void testExecCommandResponse_validJSON() throws SvcLogicException,
+            IllegalStateException, IllegalArgumentException {
+
+        params.put("Url", "test");
+        params.put("Port", "10");
+        params.put("User", "test");
+        params.put("Password", "test");
+        params.put("Cmd", "test");
+        params.put("AuthType", "basic");
+        params.put("ResponseType", "json");
+        params.put("TestOut", "{\"equipment-data\":\"boo\"}");
+        params.put("TestFail", "false");
+        adapter = new SshApiCallNode(true);
+        adapter.execWithStatusCheck(params, svcContext);
+        assertEquals("boo", svcContext.getAttribute("equipment-data"));
+    }
+
+    @Test
+    public void testExecCommandResponse_validXML() throws SvcLogicException,
+            IllegalStateException, IllegalArgumentException {
+
+        params.put("Url", "test");
+        params.put("Port", "10");
+        params.put("User", "test");
+        params.put("Password", "test");
+        params.put("Cmd", "test");
+        params.put("AuthType", "basic");
+        params.put("ResponseType", "xml");
+        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
+        params.put("TestFail", "false");
+        adapter = new SshApiCallNode(true);
+        adapter.execWithStatusCheck(params, svcContext);
+        assertEquals("4.0.0", svcContext.getAttribute("modelVersion"));
+    }
+
+    @Test
+    public void testExecCommandResponse_validJSONPrefix() throws SvcLogicException,
+            IllegalStateException, IllegalArgumentException {
+
+        params.put("Url", "test");
+        params.put("Port", "10");
+        params.put("User", "test");
+        params.put("Password", "test");
+        params.put("Cmd", "test");
+        params.put("AuthType", "basic");
+        params.put("ResponseType", "json");
+        params.put("TestOut", "{\"equipment-data\":\"boo\"}");
+        params.put("ResponsePrefix", "test");
+        params.put("TestFail", "false");
+        adapter = new SshApiCallNode(true);
+        adapter.execWithStatusCheck(params, svcContext);
+        assertEquals("boo", svcContext.getAttribute("test.equipment-data"));
+    }
+
+    @Test
+    public void testExecCommandResponse_validXMLPrefix() throws SvcLogicException,
+            IllegalStateException, IllegalArgumentException {
+
+        params.put("Url", "test");
+        params.put("Port", "10");
+        params.put("User", "test");
+        params.put("Password", "test");
+        params.put("Cmd", "test");
+        params.put("AuthType", "basic");
+        params.put("ResponseType", "xml");
+        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
+        params.put("ResponsePrefix", "test");
+        params.put("TestFail", "false");
+        adapter = new SshApiCallNode(true);
+        adapter.execWithStatusCheck(params, svcContext);
+        assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
+    }
+
+    @Test(expected = SvcLogicException.class)
+    public void testExecCommandResponse_validXMLFail() throws SvcLogicException,
+            IllegalStateException, IllegalArgumentException {
+
+        params.put("Url", "test");
+        params.put("Port", "10");
+        params.put("User", "test");
+        params.put("Password", "test");
+        params.put("Cmd", "test");
+        params.put("AuthType", "basic");
+        params.put("ResponseType", "xml");
+        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
+        params.put("TestFail", "true");
+        params.put("ResponsePrefix", "test");
+        adapter = new SshApiCallNode(true);
+        adapter.execWithStatusCheck(params, svcContext);
+    }
+
+    @Test(expected = SvcLogicException.class)
+    public void testExecCommandResponse_validXMLPrefixKey() throws SvcLogicException,
+            IllegalStateException, IllegalArgumentException {
+        params = new HashMap<>();
+        params.put("Url", "test");
+        params.put("Port", "10");
+        params.put("SshKey", "test");
+        params.put("Cmd", "test");
+        params.put("ResponseType", "xml");
+        params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
+        params.put("ResponsePrefix", "test");
+        adapter.execWithStatusCheck(params, svcContext);
+        assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
+    }
 }