Examples of using product through code
Example for writing tests for FTP2SF methods
Below is an example class and test class on how to mock the responses for the FTP2SF functions/methods. Please note that these mock responses are exposed from version 1.38 onwards.
Example Class
public class Files {
//Download/Get a File
public static Blob getFile(string site, string fileName){
Blob b = FTP2SF.FTP.get(site, fileName);
return b;
}
//List Files at the specified path
public static FTP2SF.FTP.Find listFiles(string site, string folderPath){
FTP2SF.FTP.Find findResult = FTP2SF.FTP.find(site, folderPath);
for(FTP2SF.FTP.File f : findResult.items){
System.debug(f.filename);
}
return findResult;
}
//Upload/put a File
public static FTP2SF.FTP.Put uploadFile(string site, string fileName, Blob content){
FTP2SF.FTP.Put putResult = FTP2SF.FTP.put(site, fileName, content);
return putResult;
}
//Ping site
public static FTP2SF.FTP.Response pingSite(string site, Integer timeout){
FTP2SF.FTP.Response pingResult = FTP2SF.FTP.ping(site, timeout);
return pingResult;
}
//Check if File exists on the server
public static FTP2SF.FTP.Response fileExists(string site, string filePath){
FTP2SF.FTP.Response existResult = FTP2SF.FTP.exists(site, filePath);
return existResult;
}
//Delete a File
public static FTP2SF.FTP.Response deleteFile(string site, string filePath){
FTP2SF.FTP.Response deleteResult = FTP2SF.FTP.del(site, filePath);
return deleteResult;
}
//Rename a File
public static FTP2SF.FTP.Response renameFile(string site, string oldFilePath, string newFilePath){
FTP2SF.FTP.Response renameResult = FTP2SF.FTP.ren(site, oldFilePath, newFilePath);
return renameResult;
}
//Append data to a existing File
public static FTP2SF.FTP.Append appendDataToFile(string site, string fileName, Blob content){
FTP2SF.FTP.Append appendResult = FTP2SF.FTP.Append(site, fileName, content);
return appendResult;
}
//Create a directory
public static FTP2SF.FTP.Response createDirectory(string site, string folderPath){
FTP2SF.FTP.Response createDirResult = FTP2SF.FTP.mkdir(site, folderPath);
return createDirResult;
}
//Delete a directory
public static FTP2SF.FTP.Response deleteDirectory(string site, string folderPath){
FTP2SF.FTP.Response removeDirResult = FTP2SF.FTP.rmdir(site, folderPath);
return removeDirResult;
}
//Transfer file
public static FTP2SF.FTP.Response transferFile(string fromSite, string fromFilePath, string toSite, string toFilePath){
FTP2SF.FTP.Response trnResult = FTP2SF.FTP.trn(fromSite, fromFilePath, toSite, toFilePath);
return trnResult;
}
//Retrieve a large File
public static void rtrFile(string site, string filePath, string requestId, string terminator, integer maxSize, string endPointurl){
FTP2SF.FTP.Rtr(site, filePath, requestId, terminator, maxSize, endPointurl);
}
public static void processMyFiles(FTP2SF.FTP.File file){
//Your code must cater for filenames filetest.0 .... filetest.9 while running the test method e.g.:
if(Test.isRunningTest()){
//Your code here
}
System.debug(file.filename);
}
public static void nextActions(Boolean isSuccess){
if(isSuccess){
//Do something
}
}
public static void processData(Blob data){
if(data.size() > 1){
//Do something
}
}
}
//Download/Get a File
public static Blob getFile(string site, string fileName){
Blob b = FTP2SF.FTP.get(site, fileName);
return b;
}
//List Files at the specified path
public static FTP2SF.FTP.Find listFiles(string site, string folderPath){
FTP2SF.FTP.Find findResult = FTP2SF.FTP.find(site, folderPath);
for(FTP2SF.FTP.File f : findResult.items){
System.debug(f.filename);
}
return findResult;
}
//Upload/put a File
public static FTP2SF.FTP.Put uploadFile(string site, string fileName, Blob content){
FTP2SF.FTP.Put putResult = FTP2SF.FTP.put(site, fileName, content);
return putResult;
}
//Ping site
public static FTP2SF.FTP.Response pingSite(string site, Integer timeout){
FTP2SF.FTP.Response pingResult = FTP2SF.FTP.ping(site, timeout);
return pingResult;
}
//Check if File exists on the server
public static FTP2SF.FTP.Response fileExists(string site, string filePath){
FTP2SF.FTP.Response existResult = FTP2SF.FTP.exists(site, filePath);
return existResult;
}
//Delete a File
public static FTP2SF.FTP.Response deleteFile(string site, string filePath){
FTP2SF.FTP.Response deleteResult = FTP2SF.FTP.del(site, filePath);
return deleteResult;
}
//Rename a File
public static FTP2SF.FTP.Response renameFile(string site, string oldFilePath, string newFilePath){
FTP2SF.FTP.Response renameResult = FTP2SF.FTP.ren(site, oldFilePath, newFilePath);
return renameResult;
}
//Append data to a existing File
public static FTP2SF.FTP.Append appendDataToFile(string site, string fileName, Blob content){
FTP2SF.FTP.Append appendResult = FTP2SF.FTP.Append(site, fileName, content);
return appendResult;
}
//Create a directory
public static FTP2SF.FTP.Response createDirectory(string site, string folderPath){
FTP2SF.FTP.Response createDirResult = FTP2SF.FTP.mkdir(site, folderPath);
return createDirResult;
}
//Delete a directory
public static FTP2SF.FTP.Response deleteDirectory(string site, string folderPath){
FTP2SF.FTP.Response removeDirResult = FTP2SF.FTP.rmdir(site, folderPath);
return removeDirResult;
}
//Transfer file
public static FTP2SF.FTP.Response transferFile(string fromSite, string fromFilePath, string toSite, string toFilePath){
FTP2SF.FTP.Response trnResult = FTP2SF.FTP.trn(fromSite, fromFilePath, toSite, toFilePath);
return trnResult;
}
//Retrieve a large File
public static void rtrFile(string site, string filePath, string requestId, string terminator, integer maxSize, string endPointurl){
FTP2SF.FTP.Rtr(site, filePath, requestId, terminator, maxSize, endPointurl);
}
public static void processMyFiles(FTP2SF.FTP.File file){
//Your code must cater for filenames filetest.0 .... filetest.9 while running the test method e.g.:
if(Test.isRunningTest()){
//Your code here
}
System.debug(file.filename);
}
public static void nextActions(Boolean isSuccess){
if(isSuccess){
//Do something
}
}
public static void processData(Blob data){
if(data.size() > 1){
//Do something
}
}
}
Test Class for Example Class
@isTest
private class FilesTest {
@TestSetup
private static void setup(){
FTP2SF.FTPTest.Registration();
}
@isTest
static void testGetFile() {
Test.startTest();
//This mock will return blob.valueOf('Test')
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Get));
// Call your method to test
Blob fileContent = Files.getFile('AWS', 'testFileName.txt');
Files.processData(fileContent);
System.debug(String.ValueOf(fileContent));
Test.stopTest();
}
@isTest
static void testListFiles() {
Test.startTest();
//This mock will return ten files - testfile.0 .... testfile.9
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Find));
// Call your method to test
FTP2SF.FTP.Find fileListing = Files.listFiles('AWS', '');
for(FTP2SF.FTP.File file : fileListing.items){
Files.processMyFiles(file);
System.debug(file.fileName);
}
Test.stopTest();
}
@isTest
static void testPutFile() {
Test.startTest();
//This mock will return http response with values for filePut.error, filePut.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Put));
// Call your method to test
FTP2SF.FTP.Put filePut = Files.uploadFile('AWS', 'testFileName.txt', Blob.ValueOf('Testing'));
Files.nextActions(filePut.success);
System.debug(filePut.success);
Test.stopTest();
}
@isTest
static void testPing(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Ping));
// Call your method to test
FTP2SF.FTP.Response result = Files.pingSite('AWS', 30);
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testFileExists(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Exist));
// Call your method to test
FTP2SF.FTP.Response result = Files.fileExists('AWS', 'test.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testDeleteFile(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Del));
// Call your method to test
FTP2SF.FTP.Response result = Files.deleteFile('AWS', 'test.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testRenameFile(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Ren));
// Call your method to test
FTP2SF.FTP.Response result = Files.renameFile('AWS', 'oldPath.txt', 'newPath.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testAppendFile(){
Test.startTest();
//This mock will return http response with values for fileAppend.error, fileAppend.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Append));
// Call your method to test
FTP2SF.FTP.Append fileAppend = Files.appendDataToFile('AWS', 'filePath.txt', Blob.valueOf('Sample string'));
Files.nextActions(fileAppend.success);
System.debug(fileAppend.success);
Test.stopTest();
}
@isTest
static void testCreateDirectory(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.MkDir));
// Call your method to test
FTP2SF.FTP.Response result = Files.createDirectory('AWS', 'TestFolder');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testDeleteDirectory(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.RmDir));
// Call your method to test
FTP2SF.FTP.Response result = Files.deleteDirectory('AWS', 'TestFolder');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testRetrieveFile(){
Test.startTest();
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Retrieves));
// Call your method to test
Files.rtrFile('AWS', 'filePath.txt', 'requestId', 'terminator', 3, 'https://test.com/getFile');
Test.stopTest();
}
@isTest
static void testTransferFile(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Trn));
// Call your method to test
FTP2SF.FTP.Response result = Files.transferFile('AWS', 'TestFolder/test1.txt', 'AWS', 'test2/test2.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
}
private class FilesTest {
@TestSetup
private static void setup(){
FTP2SF.FTPTest.Registration();
}
@isTest
static void testGetFile() {
Test.startTest();
//This mock will return blob.valueOf('Test')
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Get));
// Call your method to test
Blob fileContent = Files.getFile('AWS', 'testFileName.txt');
Files.processData(fileContent);
System.debug(String.ValueOf(fileContent));
Test.stopTest();
}
@isTest
static void testListFiles() {
Test.startTest();
//This mock will return ten files - testfile.0 .... testfile.9
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Find));
// Call your method to test
FTP2SF.FTP.Find fileListing = Files.listFiles('AWS', '');
for(FTP2SF.FTP.File file : fileListing.items){
Files.processMyFiles(file);
System.debug(file.fileName);
}
Test.stopTest();
}
@isTest
static void testPutFile() {
Test.startTest();
//This mock will return http response with values for filePut.error, filePut.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Put));
// Call your method to test
FTP2SF.FTP.Put filePut = Files.uploadFile('AWS', 'testFileName.txt', Blob.ValueOf('Testing'));
Files.nextActions(filePut.success);
System.debug(filePut.success);
Test.stopTest();
}
@isTest
static void testPing(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Ping));
// Call your method to test
FTP2SF.FTP.Response result = Files.pingSite('AWS', 30);
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testFileExists(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Exist));
// Call your method to test
FTP2SF.FTP.Response result = Files.fileExists('AWS', 'test.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testDeleteFile(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Del));
// Call your method to test
FTP2SF.FTP.Response result = Files.deleteFile('AWS', 'test.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testRenameFile(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Ren));
// Call your method to test
FTP2SF.FTP.Response result = Files.renameFile('AWS', 'oldPath.txt', 'newPath.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testAppendFile(){
Test.startTest();
//This mock will return http response with values for fileAppend.error, fileAppend.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Append));
// Call your method to test
FTP2SF.FTP.Append fileAppend = Files.appendDataToFile('AWS', 'filePath.txt', Blob.valueOf('Sample string'));
Files.nextActions(fileAppend.success);
System.debug(fileAppend.success);
Test.stopTest();
}
@isTest
static void testCreateDirectory(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.MkDir));
// Call your method to test
FTP2SF.FTP.Response result = Files.createDirectory('AWS', 'TestFolder');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testDeleteDirectory(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.RmDir));
// Call your method to test
FTP2SF.FTP.Response result = Files.deleteDirectory('AWS', 'TestFolder');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
@isTest
static void testRetrieveFile(){
Test.startTest();
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Retrieves));
// Call your method to test
Files.rtrFile('AWS', 'filePath.txt', 'requestId', 'terminator', 3, 'https://test.com/getFile');
Test.stopTest();
}
@isTest
static void testTransferFile(){
Test.startTest();
//This mock will return http response with values for result.error, result.success
FTP2SF.FTPMock.setMock(new FTP2SF.FTPMock(FTP2SF.FTPMock.MockType.Trn));
// Call your method to test
FTP2SF.FTP.Response result = Files.transferFile('AWS', 'TestFolder/test1.txt', 'AWS', 'test2/test2.txt');
Files.nextActions(result.success);
System.debug(result.success);
Test.stopTest();
}
}
Test Results
Test results on the client org would look like below.