diff options
3 files changed, 155 insertions, 22 deletions
diff --git a/sshapi-call-node/provider/ReadMe.md b/sshapi-call-node/provider/ReadMe.md index 6c31ae0c..beaadcda 100644 --- a/sshapi-call-node/provider/ReadMe.md +++ b/sshapi-call-node/provider/ReadMe.md @@ -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. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java index 10635be7..452d9478 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java @@ -66,10 +66,28 @@ 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(); diff --git a/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java b/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java index 3992dffe..544057a6 100644 --- a/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java +++ b/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java @@ -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")); + } } |