diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-02-11 18:31:34 +0000 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-02-11 20:04:45 +0000 |
commit | d6524736ae0061154694d3cd182c122c162bee35 (patch) | |
tree | b01063045fed70073d036e06730461f3161b76c4 | |
parent | 2de3ca3f233dc739bb335cbb6f4693cfd4121e61 (diff) |
Test coverage in ssh adapter
Increased coverage of sshadaptersshd and sshconnection classes
Issue-ID: APPC-1416
Change-Id: I706f942f87d4497f35b2584c1360d571b43eba6c
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
2 files changed, 60 insertions, 26 deletions
diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSample.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSample.java index 8feab8404..7ee1edfbc 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSample.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterSample.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,27 +34,27 @@ import org.onap.appc.adapter.ssh.sshd.SshAdapterSshd; public class SshAdapterSample { - public static void main(String[] args) { - String host = "hostname"; - int port = 22; - String username = "user"; - String password = "secret"; - String command = "ls"; + public static void main(String[] args) { + String host = "hostname"; + int port = 22; + String username = "user"; + String password = "secret"; + String command = "ls"; - SshAdapter sshAdapter = new SshAdapterSshd(); - SshConnection sshConnection = sshAdapter.getConnection(host, port, username, password); - sshConnection.connect(); - try { - OutputStream stdout = new ByteArrayOutputStream(); - OutputStream stderr = new ByteArrayOutputStream(); - int status = sshConnection.execCommand(command, stdout, stderr); - if(status == 0) { - System.out.println("Command executed successfully. Output:\n" + stdout.toString()); - } else { - System.err.println("Command returned status " + status + ". Error:\n" + stderr.toString()); - } - } finally { - sshConnection.disconnect(); - } - } + SshAdapter sshAdapter = new SshAdapterSshd(); + SshConnection sshConnection = sshAdapter.getConnection(host, port, username, password); + sshConnection.connect(); + try { + OutputStream stdout = new ByteArrayOutputStream(); + OutputStream stderr = new ByteArrayOutputStream(); + int status = sshConnection.execCommand(command, stdout, stderr); + if(status == 0) { + System.out.println("Command executed successfully. Output:\n" + stdout.toString()); + } else { + System.err.println("Command returned status " + status + ". Error:\n" + stderr.toString()); + } + } finally { + sshConnection.disconnect(); + } + } } diff --git a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterTest.java b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterTest.java index accc6c1a9..569a95507 100644 --- a/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterTest.java +++ b/appc-adapters/appc-ssh-adapter/appc-ssh-adapter-sshd/src/test/java/org/onap/appc/adapter/ssh/sshd/SshAdapterTest.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2019 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +26,6 @@ package org.onap.appc.adapter.ssh.sshd; import org.apache.sshd.server.SshServer; -import org.apache.sshd.common.keyprovider.KeyPairProvider; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.util.OsUtils; import org.apache.sshd.server.command.Command; @@ -36,12 +37,19 @@ import org.apache.sshd.server.session.ServerSession; import org.apache.sshd.server.shell.ProcessShellFactory; import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; import org.hamcrest.CoreMatchers; -import org.junit.*; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; import org.junit.rules.ExpectedException; +import org.mockito.Mockito; import org.onap.appc.adapter.ssh.SshAdapter; import org.onap.appc.adapter.ssh.SshConnection; import org.onap.appc.adapter.ssh.SshException; - +import org.powermock.reflect.Whitebox; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -49,7 +57,6 @@ import java.io.OutputStream; import java.net.BindException; import java.security.PublicKey; import java.util.Collections; -import java.util.EnumSet; //@Ignore public class SshAdapterTest { @@ -125,6 +132,31 @@ public class SshAdapterTest { disconnect(connect(SSH_USERNAME, "WrongPassword")); } + @Test + public void testInstantiateWithKeyFile() { + SshConnection connection = sshAdapter.getConnection(null, 0, null); + assertTrue(connection instanceof SshConnection); + } + + @Test + public void testConnectWithRetry() { + SshConnection connection = Mockito.spy(sshAdapter.getConnection( + SSH_HOST, SSH_PORT, SSH_USERNAME, SSH_PASSWORD)); + connection.setExecTimeout(1); + connection.connectWithRetry(); + Mockito.verify(connection).connect(); + } + + @Test + public void testToString() { + SshConnection connection = Mockito.spy(sshAdapter.getConnection( + SSH_HOST, SSH_PORT, SSH_USERNAME, SSH_PASSWORD)); + assertEquals(SSH_USERNAME + "@" + SSH_HOST, connection.toString()); + String s = null; + Whitebox.setInternalState(connection, "username", s); + assertEquals(SSH_HOST, connection.toString()); + } + @Before public void beforeTest() throws IOException { if (START_SERVER) { |