1 package jtest.org.onap.ccsdk.sli.plugins.sshapicall;
 
   3 import org.junit.After;
 
   4 import org.junit.Before;
 
   6 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
   7 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
   8 import org.onap.ccsdk.sli.plugins.sshapicall.SshApiCallNode;
 
   9 import org.onap.ccsdk.sli.plugins.sshapicall.model.XmlParser;
 
  10 import org.slf4j.Logger;
 
  11 import org.slf4j.LoggerFactory;
 
  13 import java.io.BufferedReader;
 
  14 import java.io.InputStreamReader;
 
  15 import java.util.HashMap;
 
  16 import java.util.HashSet;
 
  20 import static org.junit.Assert.assertEquals;
 
  22 public class TestSshApiCallNode {
 
  24     private static final Logger log = LoggerFactory.getLogger(TestSshApiCallNode.class);
 
  26     private SshApiCallNode adapter;
 
  27     private String TestId;
 
  28     private boolean testMode = true;
 
  29     private Map<String, String> params;
 
  30     private SvcLogicContext svcContext;
 
  34     public void setup() throws IllegalArgumentException {
 
  36         svcContext = new SvcLogicContext();
 
  37         adapter = new SshApiCallNode();
 
  39         params = new HashMap<>();
 
  40         params.put("AgentUrl", "https://192.168.1.1");
 
  41         params.put("User", "test");
 
  42         params.put("Password", "test");
 
  46     public void tearDown() {
 
  53     @Test(expected = SvcLogicException.class)
 
  54     public void testExecCommand_noUrlFailed() throws SvcLogicException,
 
  55             IllegalStateException, IllegalArgumentException {
 
  57         params.put("HostName", "test");
 
  58         params.put("Port", "10");
 
  59         params.put("User", "test");
 
  60         params.put("Password", "test");
 
  61         params.put("Test", "fail");
 
  62         adapter.execCommand(params, svcContext);
 
  65     @Test(expected = SvcLogicException.class)
 
  66     public void testExecCommandPty_noUrlFailed() throws SvcLogicException,
 
  67             IllegalStateException, IllegalArgumentException {
 
  69         params.put("HostName", "test");
 
  70         params.put("Port", "10");
 
  71         params.put("User", "test");
 
  72         params.put("Password", "test");
 
  73         params.put("Test", "fail");
 
  74         adapter.execCommandWithPty(params, svcContext);
 
  77     @Test(expected = SvcLogicException.class)
 
  78     public void testExecCommandResponse_noUrlFailed() throws SvcLogicException,
 
  79             IllegalStateException, IllegalArgumentException {
 
  81         params.put("HostName", "test");
 
  82         params.put("Port", "10");
 
  83         params.put("User", "test");
 
  84         params.put("Password", "test");
 
  85         params.put("Test", "fail");
 
  86         adapter.execWithStatusCheck(params, svcContext);
 
  89     @Test(expected = SvcLogicException.class)
 
  90     public void testExecCommand_noPortFailed() throws SvcLogicException,
 
  91             IllegalStateException, IllegalArgumentException {
 
  93         params.put("Url", "test");
 
  94         params.put("User", "test");
 
  95         params.put("Password", "test");
 
  96         params.put("Test", "fail");
 
  97         adapter.execCommand(params, svcContext);
 
 100     @Test(expected = SvcLogicException.class)
 
 101     public void testExecCommandPty_noPortFailed() throws SvcLogicException,
 
 102             IllegalStateException, IllegalArgumentException {
 
 104         params.put("Url", "test");
 
 105         params.put("User", "test");
 
 106         params.put("Password", "test");
 
 107         params.put("Test", "fail");
 
 108         adapter.execCommandWithPty(params, svcContext);
 
 111     @Test(expected = SvcLogicException.class)
 
 112     public void testExecCommandResponse_noPortFailed() throws SvcLogicException,
 
 113             IllegalStateException, IllegalArgumentException {
 
 115         params.put("Url", "test");
 
 116         params.put("User", "test");
 
 117         params.put("Password", "test");
 
 118         params.put("Test", "fail");
 
 119         adapter.execWithStatusCheck(params, svcContext);
 
 122     @Test(expected = SvcLogicException.class)
 
 123     public void testExecCommand_noCmdFailed() throws SvcLogicException,
 
 124             IllegalStateException, IllegalArgumentException {
 
 126         params.put("Url", "test");
 
 127         params.put("Port", "10");
 
 128         adapter.execCommand(params, svcContext);
 
 131     @Test(expected = SvcLogicException.class)
 
 132     public void testExecCommandPty_noCmdFailed() throws SvcLogicException,
 
 133             IllegalStateException, IllegalArgumentException {
 
 135         params.put("Url", "test");
 
 136         params.put("Port", "10");
 
 137         adapter.execCommandWithPty(params, svcContext);
 
 140     @Test(expected = SvcLogicException.class)
 
 141     public void testExecCommandResponse_noCmdFailed() throws SvcLogicException,
 
 142             IllegalStateException, IllegalArgumentException {
 
 144         params.put("Url", "test");
 
 145         params.put("Port", "10");
 
 146         adapter.execWithStatusCheck(params, svcContext);
 
 149     @Test(expected = SvcLogicException.class)
 
 150     public void testExecCommandResponse_noSSHBasicFailed() throws SvcLogicException,
 
 151             IllegalStateException, IllegalArgumentException {
 
 153         params.put("Url", "test");
 
 154         params.put("Port", "10");
 
 155         params.put("User", "test");
 
 156         params.put("Password", "test");
 
 157         params.put("AuthType", "basic");
 
 158         params.put("Cmd", "test");
 
 159         adapter.execWithStatusCheck(params, svcContext);
 
 162     @Test(expected = SvcLogicException.class)
 
 163     public void testExecCommandResponse_noSSHKeyFailed() throws SvcLogicException,
 
 164             IllegalStateException, IllegalArgumentException {
 
 166         params.put("Url", "test");
 
 167         params.put("Port", "10");
 
 168         params.put("User", "test");
 
 169         params.put("Password", "test");
 
 170         params.put("AuthType", "key");
 
 171         params.put("Cmd", "test");
 
 172         adapter.execWithStatusCheck(params, svcContext);
 
 175     @Test(expected = SvcLogicException.class)
 
 176     public void testExecCommandResponse_noSSHNoneFailed() throws SvcLogicException,
 
 177             IllegalStateException, IllegalArgumentException {
 
 179         params.put("Url", "test");
 
 180         params.put("Port", "10");
 
 181         params.put("User", "test");
 
 182         params.put("Password", "test");
 
 183         params.put("AuthType", "none");
 
 184         params.put("Cmd", "test");
 
 185         params.put("ResponseType", "xml");
 
 186         adapter.execWithStatusCheck(params, svcContext);
 
 189     @Test(expected = SvcLogicException.class)
 
 190     public void testExecCommandResponse_noSSHFailed() throws SvcLogicException,
 
 191             IllegalStateException, IllegalArgumentException {
 
 193         params.put("Url", "test");
 
 194         params.put("Port", "10");
 
 195         params.put("User", "test");
 
 196         params.put("Password", "test");
 
 197         params.put("Cmd", "test");
 
 198         params.put("ResponseType", "json");
 
 199         adapter.execWithStatusCheck(params, svcContext);
 
 202     @Test(expected = IllegalArgumentException.class)
 
 203     public void testExecCommandResponse_noSSHInvalidParam() throws SvcLogicException,
 
 204             IllegalStateException, IllegalArgumentException {
 
 206         params.put("Url", "test");
 
 207         params.put("Port", "10");
 
 208         params.put("User", "test");
 
 209         params.put("Password", "test");
 
 210         params.put("Cmd", "test");
 
 211         params.put("ResponseType", "txt");
 
 212         adapter.execWithStatusCheck(params, svcContext);
 
 215     @Test(expected = IllegalArgumentException.class)
 
 216     public void testExecCommandResponse_noSSHInvalidAuthParam() throws SvcLogicException,
 
 217             IllegalStateException, IllegalArgumentException {
 
 219         params.put("Url", "test");
 
 220         params.put("Port", "10");
 
 221         params.put("User", "test");
 
 222         params.put("Password", "test");
 
 223         params.put("Cmd", "test");
 
 224         params.put("AuthType", "spring");
 
 225         params.put("ResponseType", "json");
 
 226         adapter.execWithStatusCheck(params, svcContext);
 
 230     public void testExecCommandResponse_validJSON() throws SvcLogicException,
 
 231             IllegalStateException, IllegalArgumentException {
 
 233         params.put("Url", "test");
 
 234         params.put("Port", "10");
 
 235         params.put("User", "test");
 
 236         params.put("Password", "test");
 
 237         params.put("Cmd", "test");
 
 238         params.put("AuthType", "basic");
 
 239         params.put("ResponseType", "json");
 
 240         params.put("TestOut", "{\"equipment-data\":\"boo\"}");
 
 241         params.put("TestFail", "false");
 
 242         adapter = new SshApiCallNode(true);
 
 243         adapter.execWithStatusCheck(params, svcContext);
 
 244         assertEquals("boo", svcContext.getAttribute("equipment-data"));
 
 248     public void testExecCommandResponse_validXML() throws SvcLogicException,
 
 249             IllegalStateException, IllegalArgumentException {
 
 251         params.put("Url", "test");
 
 252         params.put("Port", "10");
 
 253         params.put("User", "test");
 
 254         params.put("Password", "test");
 
 255         params.put("Cmd", "test");
 
 256         params.put("AuthType", "basic");
 
 257         params.put("ResponseType", "xml");
 
 258         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
 
 259         params.put("TestFail", "false");
 
 260         adapter = new SshApiCallNode(true);
 
 261         adapter.execWithStatusCheck(params, svcContext);
 
 262         assertEquals("4.0.0", svcContext.getAttribute("modelVersion"));
 
 266     public void testExecCommandResponse_validJSONPrefix() throws SvcLogicException,
 
 267             IllegalStateException, IllegalArgumentException {
 
 269         params.put("Url", "test");
 
 270         params.put("Port", "10");
 
 271         params.put("User", "test");
 
 272         params.put("Password", "test");
 
 273         params.put("Cmd", "test");
 
 274         params.put("AuthType", "basic");
 
 275         params.put("ResponseType", "json");
 
 276         params.put("TestOut", "{\"equipment-data\":\"boo\"}");
 
 277         params.put("ResponsePrefix", "test");
 
 278         params.put("TestFail", "false");
 
 279         adapter = new SshApiCallNode(true);
 
 280         adapter.execWithStatusCheck(params, svcContext);
 
 281         assertEquals("boo", svcContext.getAttribute("test.equipment-data"));
 
 285     public void testExecCommandResponse_validXMLPrefix() throws SvcLogicException,
 
 286             IllegalStateException, IllegalArgumentException {
 
 288         params.put("Url", "test");
 
 289         params.put("Port", "10");
 
 290         params.put("User", "test");
 
 291         params.put("Password", "test");
 
 292         params.put("Cmd", "test");
 
 293         params.put("AuthType", "basic");
 
 294         params.put("ResponseType", "xml");
 
 295         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
 
 296         params.put("ResponsePrefix", "test");
 
 297         params.put("TestFail", "false");
 
 298         adapter = new SshApiCallNode(true);
 
 299         adapter.execWithStatusCheck(params, svcContext);
 
 300         assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));
 
 303     @Test(expected = SvcLogicException.class)
 
 304     public void testExecCommandResponse_validXMLFail() throws SvcLogicException,
 
 305             IllegalStateException, IllegalArgumentException {
 
 307         params.put("Url", "test");
 
 308         params.put("Port", "10");
 
 309         params.put("User", "test");
 
 310         params.put("Password", "test");
 
 311         params.put("Cmd", "test");
 
 312         params.put("AuthType", "basic");
 
 313         params.put("ResponseType", "xml");
 
 314         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
 
 315         params.put("TestFail", "true");
 
 316         params.put("ResponsePrefix", "test");
 
 317         adapter = new SshApiCallNode(true);
 
 318         adapter.execWithStatusCheck(params, svcContext);
 
 321     @Test(expected = SvcLogicException.class)
 
 322     public void testExecCommandResponse_validXMLPrefixKey() throws SvcLogicException,
 
 323             IllegalStateException, IllegalArgumentException {
 
 324         params = new HashMap<>();
 
 325         params.put("Url", "test");
 
 326         params.put("Port", "10");
 
 327         params.put("SshKey", "test");
 
 328         params.put("Cmd", "test");
 
 329         params.put("ResponseType", "xml");
 
 330         params.put("TestOut", "<modelVersion>4.0.0</modelVersion>");
 
 331         params.put("ResponsePrefix", "test");
 
 332         adapter.execWithStatusCheck(params, svcContext);
 
 333         assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion"));