Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S-115548 : zos Create Overthere sshHostZosUnix composite connection #324

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ public SshConnectionBuilder(String type, ConnectionOptions options, AddressPortM
case INTERACTIVE_SUDO:
connection = new SshInteractiveSudoConnection(type, options, mapper);
break;
case ZOS_UNIX_SSH:
connection = new SshZOSConnectionBuilder(type, options, mapper).getSshConnection();
break;
default:
throw new IllegalArgumentException("Unknown SSH connection type " + sshConnectionType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ public enum SshConnectionType {
*/
INTERACTIVE_SUDO,

/**
* ZOS SSH connection which is a composite class that has both SCP and SFTP connection to transfer files, to a ZOS host
*/
ZOS_UNIX_SSH,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.xebialabs.overthere.ssh;


import com.xebialabs.overthere.ConnectionOptions;
import com.xebialabs.overthere.OverthereConnection;
import com.xebialabs.overthere.spi.AddressPortMapper;
import com.xebialabs.overthere.spi.OverthereConnectionBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE;

public class SshZOSConnectionBuilder implements OverthereConnectionBuilder {

public static final String ZOS_CONNECTION_TYPE = "zosConnectionType";
private static final Logger log = LoggerFactory.getLogger(SshZOSConnectionBuilder.class);

private SshConnection connection;

public SshZOSConnectionBuilder(String type, ConnectionOptions options, AddressPortMapper mapper) {
SshZosConnectionType zosSshConnectionType = options.getEnum(ZOS_CONNECTION_TYPE, SshZosConnectionType.class);
if (zosSshConnectionType == SshZosConnectionType.SFTP) {
log.debug("Creating SFTP connection inside ZOSConnectionBuilder");
options.set(CONNECTION_TYPE, SshConnectionType.SFTP);
connection = new SshSftpUnixConnection(type, options, mapper);
} else if (zosSshConnectionType == SshZosConnectionType.SCP) {
log.debug("Creating SCP connection inside ZOSConnectionBuilder");
options.set(CONNECTION_TYPE, SshConnectionType.SCP);
connection = new SshScpConnection(type, options, mapper);
} else {
throw new IllegalArgumentException("Unknown SSH connection type " + zosSshConnectionType);
}
}

SshConnection getSshConnection() {
return connection;
}

@Override
public OverthereConnection connect() {
connection.connect();
return connection;
}

@Override
public String toString() {
return connection.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.xebialabs.overthere.ssh;

public enum SshZosConnectionType {
/**
* An SSH connection that uses SFTP to transfer files, to a Unix host.
*/
SFTP,

/**
* An SSH connection that uses SCP to transfer files, to a Unix host.
*/
SCP,
}
62 changes: 62 additions & 0 deletions src/main/java/com/xebialabs/overthere/ssh/ZosConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.xebialabs.overthere.ssh;

import static com.xebialabs.overthere.ssh.SshConnectionBuilder.CONNECTION_TYPE;
import static com.xebialabs.overthere.ssh.SshConnectionType.SCP;
import static com.xebialabs.overthere.ssh.SshConnectionType.SFTP;

import com.xebialabs.overthere.ConnectionOptions;
import com.xebialabs.overthere.Overthere;
import com.xebialabs.overthere.OverthereConnection;
import com.xebialabs.overthere.OverthereConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZosConnection {
private static final Logger logger = LoggerFactory.getLogger(OverthereConnector.class);

private SshConnection sshScpZosConnection;
private SshConnection sshSftpZosConnection;


public ZosConnection(ConnectionOptions options) {
initializeConnections(options);
}

private void initializeConnections(ConnectionOptions targetOptions) {
try {
targetOptions.set(CONNECTION_TYPE, SCP);
this.sshScpZosConnection = (SshConnection) Overthere.getConnection("ssh", targetOptions);
} catch (Exception e) {
logger.warn("OverThere:zosConnection:SCP Failed", e);
this.sshScpZosConnection = null;
}

try {
targetOptions.set(CONNECTION_TYPE, SFTP);
this.sshSftpZosConnection = (SshConnection) Overthere.getConnection("ssh", targetOptions);
} catch (Exception e) {
logger.warn("OverThere:zosConnection:SFTP Failed", e);
this.sshSftpZosConnection = null;
}
}

public OverthereConnection getConnectionForScp() { return sshScpZosConnection; }

public OverthereConnection getConnectionForSftp() {
return sshSftpZosConnection;
}

public OverthereConnection getConnection(SshConnectionType connectionType) {
if (connectionType.equals(SFTP)) {
return sshSftpZosConnection;
} else if (connectionType.equals(SCP)) {
return sshScpZosConnection;
} else {
throw new IllegalArgumentException("Unsupported connection type: " + connectionType);
}
}

public ZosConnection getConnection() {
return this;
}
}
74 changes: 74 additions & 0 deletions src/test/java/com/xebialabs/overthere/ssh/ZosConnectionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.xebialabs.overthere.ssh;

import com.xebialabs.overthere.ConnectionOptions;

import org.mockito.Mockito;

import static com.xebialabs.overthere.ConnectionOptions.*;
import static com.xebialabs.overthere.OperatingSystemFamily.UNIX;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ZosConnectionTest {

private SshConnection mockScpConnection;
private SshConnection mockSftpConnection;
private ZosConnection zosConnection;

@BeforeClass
public void setUp() {
ConnectionOptions options = new ConnectionOptions();
options.set(PORT, 22);
options.set(OPERATING_SYSTEM, UNIX);
options.set(ADDRESS, "host");
options.set(USERNAME, "username");
options.set(PASSWORD, "password");
mockScpConnection = mock(SshConnection.class);
mockSftpConnection = mock(SshConnection.class);

zosConnection = Mockito.spy(new ZosConnection(options));
doReturn(mockScpConnection).when(zosConnection).getConnectionForScp();
doReturn(mockSftpConnection).when(zosConnection).getConnectionForSftp();

doReturn(mockScpConnection).when(zosConnection).getConnection(SshConnectionType.SCP);
doReturn(mockSftpConnection).when(zosConnection).getConnection(SshConnectionType.SFTP);
}

@Test
public void testGetConnectionForScp() {
assertThat(mockScpConnection, equalTo(zosConnection.getConnectionForScp()));
}

@Test
public void testGetConnectionForSftp() {
assertThat(mockSftpConnection, equalTo(zosConnection.getConnectionForSftp()));
}

@Test
public void testGetConnection_Scp() {
assertThat(zosConnection.getConnection(SshConnectionType.SCP), equalTo(mockScpConnection));
}

@Test
public void testGetConnection_Sftp() {
assertThat(zosConnection.getConnection(SshConnectionType.SFTP), equalTo(mockSftpConnection));
}

@Test(expectedExceptions = NullPointerException.class)
public void testGetConnection_NullPointer() {
zosConnection.getConnection(null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testGetConnection_Unsupported() {
zosConnection.getConnection(SshConnectionType.SFTP_CYGWIN);
}


}