Skip to content

Commit

Permalink
variable & refactoring test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
anh-bolt committed Aug 23, 2024
1 parent 851affc commit 0c8f586
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 63 deletions.
25 changes: 24 additions & 1 deletion sftp-connector-demo/config/variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,27 @@
# please add a 'variables.yaml' in the sub directory '_<environment>'.
#
Variables:
#myVariable: value
com.axonivy.connector.sftp.server:
dummy:
# The host name to the SFTP server
host: 'localhost'

# The port number to the SFTP server
port: 22

# The username to the SFTP server
username: 'usr'

# Auth type to the SFPT server
# [enum: password, ssh]
auth: 'ssh'

# The password to the SFTP server
# [password]
password: ''

# The path of ssh key file to SFTP server
sshkeyFilePath: 'C:\NonInstall\RebexTinySftpServer-Binaries-Latest\sshkeyBK\rsa4096new'

# The ssh key passphrase
sshPassphraseSecret: '123456'
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.axonivy.connector.sftp.test;

import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.environment.IvyTest;

@IvyTest
public class BaseTest {
protected static final String TEST_SFTP_SERVER_NAME = "dummy";
protected static final String TEST_SFTP_SSH_SERVER_NAME = "dummy_ssh";

protected static final BpmProcess TEST_HELPER_PROCESS = BpmProcess.path("Sftp/SftpHelper");
protected static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile");
protected static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile");

protected static final String PREFIX = "com.axonivy.connector.sftp.server";
protected static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf";
protected static final long TEST_FILE_SIZE = 207569L;

protected static void setVarForSFTPName(String sftpServerName, String username, String auth, String password, String sshKeyFilePath, String sshpassphrase) {
setVar(sftpServerName, "host", "localhost");
setVar(sftpServerName, "username", username);
setVar(sftpServerName, "auth", auth);
setVar(sftpServerName, "password", password);
setVar(sftpServerName, "sshkeyFilePath", sshKeyFilePath);
setVar(sftpServerName, "sshPassphraseSecret", sshpassphrase);
}

private static void setVar(String sftpServerName, String var, String value) {
Ivy.var().set(String.format("%s.%s.%s", PREFIX, sftpServerName, var), value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -13,38 +11,29 @@

import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.Ivy;


/**
* This SftpMultiConnectionTest creates 2 sFTP connections
*/
@IvyProcessTest(enableWebServer = true)
public class SftpMultiConnectionTest {
public class SftpMultiConnectionTest extends BaseTest {

private static final String PREFIX = "com.axonivy.connector.sftp.server.";

private static final String SFTP_NAME = "dummy";
private static final String SFTP_SSH_NAME = "dummy_ssh";
private static final String SFTP_SSH_NAME_VAR = SFTP_SSH_NAME + ".";


@BeforeEach
public void preInit() throws Exception {
// set-up variables for sftp connector with SSH key pair
Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"host", "localhost");
Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"username", "usr2ssh");
Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"auth", "ssh");
Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"password", "");
String keyString = Files.readString(Paths.get(SftpProcessSSHTest.class.getResource("sftptest").toURI()));
Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"secret.sshkey", keyString);
Ivy.var().set(PREFIX+SFTP_SSH_NAME_VAR+"secret.sshpassphrase", "123456");
setVarForSFTPName(TEST_SFTP_SERVER_NAME, "usr", "password", "pwd", "", "");
String keyPath = SftpProcessSSHTest.class.getResource("sftptest").getPath();
setVarForSFTPName(TEST_SFTP_SSH_SERVER_NAME, "usr2ssh", "ssh", "", keyPath, "123456");
}

@Test
public void callOpenConnection(BpmClient bpmClient) throws IOException {
SftpClientService sftpClient = new SftpClientService(SFTP_NAME);
SftpClientService sftpSSHClient = new SftpClientService(SFTP_SSH_NAME);

assertThat(sftpClient).isNotNull();
assertThat(sftpSSHClient).isNotNull();
sftpClient.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess;
import ch.ivyteam.ivy.bpm.engine.client.sub.SubProcessCallResult;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.objects.File;


Expand All @@ -39,28 +38,12 @@
* </p>
*/
@IvyProcessTest(enableWebServer = true)
public class SftpProcessSSHTest {

private static final BpmProcess TEST_HELPER_PROCESS = BpmProcess.path("Sftp/SftpHelper");
private static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile");
private static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile");

private static final String TEST_SFTP_NAME = "dummy";
private static final String TEST_SFTP_NAME_VAR = TEST_SFTP_NAME + ".";
private static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf";
private static final long TEST_FILE_SIZE = 207569L;

private static final String PREFIX = "com.axonivy.connector.sftp.server.";
public class SftpProcessSSHTest extends BaseTest {

@BeforeAll
public static void init() throws Exception {
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"username", "usr2ssh");
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"auth", "ssh");
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"password", "");

String keyString = Files.readString(Paths.get(SftpProcessSSHTest.class.getResource("sftptest").toURI()));
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"secret.sshkey", keyString);
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"secret.sshpassphrase", "123456");
setVarForSFTPName(TEST_SFTP_SSH_SERVER_NAME, "usr2ssh", "ssh", "", keyString, "123456");
}

@Test
Expand All @@ -70,7 +53,7 @@ public void callOpenConnection(BpmClient bpmClient) throws Exception {

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_SFTP_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_SSH_SERVER_NAME) // Callable sub process input arguments
.subResult();

SftpClientService sftpClient = result.param("sftpClient", SftpClientService.class);
Expand All @@ -89,7 +72,7 @@ public void callUploadFile(BpmClient bpmClient) {

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_SFTP_NAME,fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_SSH_SERVER_NAME,fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments
.subResult();

Boolean isSuccess = result.param("isSuccess", Boolean.class);
Expand All @@ -110,7 +93,7 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException {

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_SFTP_NAME, ivyFile) // Callable sub process input arguments
.execute(TEST_SFTP_SSH_SERVER_NAME, ivyFile) // Callable sub process input arguments
.subResult();

Boolean isSuccess = result.param("isSuccess", Boolean.class);
Expand All @@ -124,7 +107,7 @@ public void callListAllFiles(BpmClient bpmClient) {

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_SFTP_NAME, ".") // Callable sub process input arguments
.execute(TEST_SFTP_SSH_SERVER_NAME, ".") // Callable sub process input arguments
.subResult();
List<FileData> listFiles = result.param("listFiles", List.class);
assertThat(listFiles.size()).isGreaterThanOrEqualTo(1);
Expand All @@ -138,7 +121,7 @@ public void callDownloadFile(BpmClient bpmClient) {

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_SFTP_NAME, TEST_FILE_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_SSH_SERVER_NAME, TEST_FILE_NAME) // Callable sub process input arguments
.subResult();
java.io.File downloadedFile = result.param("toFile", java.io.File.class);
assertThat(downloadedFile.length()).isEqualTo(TEST_FILE_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.engine.client.element.BpmElement;
import ch.ivyteam.ivy.bpm.engine.client.element.BpmProcess;
import ch.ivyteam.ivy.bpm.engine.client.sub.SubProcessCallResult;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.scripting.objects.File;
Expand All @@ -35,15 +34,7 @@
* </p>
*/
@IvyProcessTest(enableWebServer = true)
public class SftpProcessTest {

private static final BpmProcess TEST_HELPER_PROCESS = BpmProcess.path("Sftp/SftpHelper");
private static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile");
private static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile");

private static final String TEST_SFTP_SERVER_NAME = "dummy";
private static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf";
private static final long TEST_FILE_SIZE = 207569L;
public class SftpProcessTest extends BaseTest {


@Test
Expand Down
13 changes: 6 additions & 7 deletions sftp-connector/config/variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ Variables:
com.axonivy.connector.sftp.server:
dummy:
# The host name to the SFTP server
host: 'localhost'
host: ''

# The port number to the SFTP server
port: 22

# The username to the SFTP server
username: 'usr'
username: ''

# Auth type to the SFPT server
# [enum: password, ssh]
auth: 'password'

# The password to the SFTP server
# [password]
password: pwd
password: ''

# The ssh key string to SFTP server
# [secret private key]
secret.sshkey: ''
# The path of ssh key file to SFTP server
sshkeyFilePath: ''

# The ssh key passphrase
secret.sshpassphrase: ''
sshPassphraseSecret: ''
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -35,8 +37,8 @@ public class SftpClientService implements AutoCloseable {
private static final String SFTP_VAR = "com.axonivy.connector.sftp.server";
private static final String HOST_VAR = "host";
private static final String PORT_VAR = "port";
private static final String SECRET_SSHPASSPHRASE_VAR = "secret.sshpassphrase";
private static final String SECRET_SSHKEY_VAR = "secret.sshkey";
private static final String SECRET_SSHPASSPHRASE_VAR = "sshPassphraseSecret";
private static final String SSHKEY_FILEPATH_VAR = "sshkeyFilePath";
private static final String AUTH_VAR = "auth";
private static final String PASSWORD_VAR = "password";
private static final String USERNAME_VAR = "username";
Expand All @@ -61,7 +63,7 @@ public SftpClientService(String sftpName) throws IOException {
String username = getUsername(sftpName);
String password = getVar(sftpName, PASSWORD_VAR);
String auth = getVar(sftpName, AUTH_VAR);
String secretSSHkey = getVar(sftpName, SECRET_SSHKEY_VAR);
String sshKeyFilePath = getVar(sftpName, SSHKEY_FILEPATH_VAR);
String secretSSHpassphrase = getVar(sftpName, SECRET_SSHPASSPHRASE_VAR);

int port = 22;
Expand All @@ -80,8 +82,9 @@ public SftpClientService(String sftpName) throws IOException {
if (StringUtils.isEmpty(auth) || PASSWORD.name().equalsIgnoreCase(auth)) {
session.setPassword(password);
} else {
byte[] sshKeyBytes = Files.readAllBytes(Paths.get(sshKeyFilePath));
session.setConfig("PreferredAuthentications", "publickey");
jsch.addIdentity(null, secretSSHkey.getBytes(), null, secretSSHpassphrase.getBytes());
jsch.addIdentity(null, sshKeyBytes, null, secretSSHpassphrase.getBytes());
}
session.setConfig("StrictHostKeyChecking", "no");
// 10 seconds session timeout
Expand Down

0 comments on commit 0c8f586

Please sign in to comment.