@IsTest public with sharing class SFTPFeatureTest { /*--------------------------------------------- MOCK – SUCCESS RESPONSE (200) ----------------------------------------------*/ private class SFTPSuccessMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { System.debug('Mock Endpoint Hit: ' + req.getEndpoint()); System.debug('Mock Request Body: ' + req.getBody()); HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setHeader('Content-Type', 'application/json'); // Simulated flat file list from server res.setBody(JSON.serialize(new List{ new Map{ 'name' => 'rootFolder', 'path' => '/rootFolder', 'type' => 'folder' }, new Map{ 'name' => 'file1.txt', 'path' => '/rootFolder/file1.txt', 'type' => 'file' } })); return res; } } /*--------------------------------------------- TEST METHOD – ROOT LISTING ----------------------------------------------*/ @IsTest static void testListFiles_Success_Root() { System.debug('===== TEST LIST FILES START ====='); Test.setMock(HttpCalloutMock.class, new SFTPSuccessMock()); // Prepare params Map payload = new Map{ 'host' => 'testHost', 'username' => 'testUser' }; Map params = new Map{ 'payload' => payload // No folderId = root listing }; Test.startTest(); // Replace YourClassName with actual class name SFTPFeature service = new SFTPFeature(); OperationResult result = service.listFiles(params); Test.stopTest(); System.debug('OperationResult: ' + result); System.debug('Returned Data: ' + JSON.serializePretty(result.data)); System.assertNotEquals(null, result); System.assertEquals(true, result.success); System.debug('===== TEST LIST FILES END ====='); } /*------------------------------------------------- MOCK – 201 SUCCESS WITH BODY --------------------------------------------------*/ private class CreateFolderSuccessMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { System.debug('Mock Endpoint: ' + req.getEndpoint()); System.debug('Mock Body: ' + req.getBody()); HttpResponse res = new HttpResponse(); res.setStatusCode(201); res.setHeader('Content-Type', 'application/json'); res.setBody(JSON.serialize(new Map{ 'code' => 201, 'message' => 'Folder created successfully', 'path' => '/var/sftp/shared/newFolder' })); return res; } } /*------------------------------------------------- MOCK – 200 SUCCESS EMPTY BODY --------------------------------------------------*/ private class CreateFolderEmptyBodyMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setBody(''); return res; } } /*------------------------------------------------- MOCK – ERROR RESPONSE --------------------------------------------------*/ private class CreateFolderErrorMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(400); res.setBody('Bad Request'); return res; } } /*------------------------------------------------- TEST – SUCCESS WITH BODY --------------------------------------------------*/ @IsTest static void testCreateFolder_SuccessWithBody() { System.debug('===== TEST SUCCESS WITH BODY START ====='); Test.setMock(HttpCalloutMock.class, new CreateFolderSuccessMock()); Map payload = new Map{ 'host' => 'testHost', 'username' => 'testUser' }; Map params = new Map{ 'parentFolderId' => null, 'folderName' => 'newFolder', 'payload' => payload }; Test.startTest(); SFTPFeature service = new SFTPFeature (); // replace with real class name OperationResult result = service.createFolder(params); Test.stopTest(); System.debug('Result: ' + result); System.debug('Returned Data: ' + result.data); System.assertEquals(true, result.success); System.assertNotEquals(null, result.data); System.debug('===== TEST SUCCESS WITH BODY END ====='); } // @IsTest // public static void testStoreSftpCredentials_Success() { // System.debug('===== TEST storeSftpCredentials START ====='); // HIC_FilesME.SFTPFeature.isTestMode = true; // // Prepare payload // Map payload = new Map{ // 'username' => 'testUser', // 'password' => 'testPassword', // 'host' => 'testHost.com' // }; // Test.startTest(); // // Call method // SFTPFeature.storeSftpCredentials(true, payload); // Test.stopTest(); // System.debug('Method executed successfully'); // // No exception = success // System.assert(true, 'Method executed without exception'); // System.debug('===== TEST storeSftpCredentials END ====='); // } private class SFTPDeleteFileMock implements HttpCalloutMock { Integer statusCode; String responseBody; SFTPDeleteFileMock(Integer statusCode, String responseBody) { this.statusCode = statusCode; this.responseBody = responseBody; } public HttpResponse respond(HttpRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(statusCode); res.setBody(responseBody); System.debug('Mock Responding with Status: ' + statusCode); System.debug('Mock Body: ' + responseBody); return res; } } @IsTest static void testDeleteFile_Success() { Map payload = new Map{ 'username' => 'testUser', 'password' => 'testPass', 'host' => 'testHost' }; Map params = new Map{ 'fileId' => '/var/sftp/shared/test.txt', 'isServiceMode' => true, 'payload' => payload }; Test.setMock(HttpCalloutMock.class, new SFTPDeleteFileMock(200, 'File deleted successfully')); Test.startTest(); OperationResult result = new HIC_FilesME.SFTPFeature().deleteFile(params); Test.stopTest(); System.debug('Delete Success Result: ' + result); System.assertNotEquals(null, result); } private class SFTPRenameMock implements HttpCalloutMock { Integer statusCode; String responseBody; SFTPRenameMock(Integer statusCode, String responseBody) { this.statusCode = statusCode; this.responseBody = responseBody; } public HttpResponse respond(HttpRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(statusCode); res.setBody(responseBody); System.debug('Mock Rename Status: ' + statusCode); System.debug('Mock Rename Body: ' + responseBody); return res; } } @IsTest static void testRenameFile_Success200() { Map payload = new Map{ 'username' => 'testUser', 'password' => 'testPass', 'host' => 'testHost' }; Map params = new Map{ 'fileId' => '/folder/old.txt', 'newFileName' => 'new.txt', 'overwrite' => true, 'payload' => payload }; String mockResponse = '{"message":"Rename successful"}'; Test.setMock(HttpCalloutMock.class, new SFTPRenameMock(200, mockResponse)); Test.startTest(); OperationResult result = new HIC_FilesME.SFTPFeature().renameFile(params); Test.stopTest(); System.debug('Rename Success 200 Result: ' + result); System.assertNotEquals(null, result); } private class SFTPMoveMock implements HttpCalloutMock { Integer statusCode; String responseBody; SFTPMoveMock(Integer statusCode, String responseBody) { this.statusCode = statusCode; this.responseBody = responseBody; } public HttpResponse respond(HttpRequest req) { System.debug('Mock Move Endpoint: ' + req.getEndpoint()); System.debug('Mock Move Method: ' + req.getMethod()); System.debug('Mock Move Body: ' + req.getBody()); HttpResponse res = new HttpResponse(); res.setStatusCode(statusCode); res.setBody(responseBody); return res; } } @IsTest static void testMoveFileAndFolder_Success200() { Map payload = new Map{ 'username' => 'testUser', 'password' => 'testPass', 'host' => 'testHost' }; Map params = new Map{ 'sourceKey' => '/folder/old.txt', 'destinationKey' => '/folder/new.txt', 'overwrite' => true, 'payload' => payload }; String mockResponse = '{"message":"Move successful"}'; Test.setMock(HttpCalloutMock.class, new SFTPMoveMock(200, mockResponse)); Test.startTest(); OperationResult result = new HIC_FilesME.SFTPFeature().moveFileAndFolder(params); Test.stopTest(); System.debug('Move 200 Result: ' + result); System.assertNotEquals(null, result); } @isTest static void testLinkFiles() { // Create parent record Account acc = new Account(Name = 'Test Account'); insert acc; System.debug('Account Id >>> ' + acc.Id); // Create Linked Folder record HIC_FilesME__Linked_Folder__c folder = new HIC_FilesME__Linked_Folder__c( Name = 'Test Folder', HIC_FilesME__Linked_Record__c = acc.Id, HIC_FilesME__External_Folder_ID__c = 'EXT123', HIC_FilesME__Folder_Name__c = 'SFTP Folder', HIC_FilesME__Folder_URL__c = 'https://sftp.test.com/folder', HIC_FilesME__Storage_Provider__c = 'SFTP', HIC_FilesME__Access_Level__c = 'Public', HIC_FilesME__Connected_Username__c = 'testuser', HIC_FilesME__Is_Active__c = true ); insert folder; System.debug('Inserted Folder >>> ' + folder); Test.startTest(); // Call method SFTPFeature feature = new SFTPFeature(); OperationResult result = feature.linkFiles(folder.Id); Test.stopTest(); System.debug('OperationResult >>> ' + result); // Assertions System.assert(result != null, 'Result should not be null'); if(result.success){ System.debug('Result Data >>> ' + result.data); System.assert(true, 'Folders fetched successfully'); } else { System.assert(false, 'Operation should be successful'); } } @isTest static void testToLinkFiles() { // Create parent record Account acc = new Account(Name = 'Test Account'); insert acc; System.debug('Account Id >>> ' + acc.Id); // Prepare folder details JSON Map folderMap = new Map(); folderMap.put('id', 'EXT-1001'); folderMap.put('name', 'Test SFTP Folder'); folderMap.put('linkedBy', 'testUser'); folderMap.put('url', 'https://sftp.test.com/folder'); String folderDetails = JSON.serialize(folderMap); System.debug('folderDetails JSON >>> ' + folderDetails); Test.startTest(); // Call method SFTPFeature feature = new SFTPFeature(); OperationResult result = feature.toLinkFiles(acc.Id, folderDetails, 'SFTP'); Test.stopTest(); System.debug('OperationResult >>> ' + result); // Verify record inserted List folders = [ SELECT Id, HIC_FilesME__External_Folder_ID__c, HIC_FilesME__Folder_Name__c, HIC_FilesME__Storage_Provider__c, HIC_FilesME__Connected_Username__c, HIC_FilesME__Folder_URL__c, HIC_FilesME__Is_Active__c FROM HIC_FilesME__Linked_Folder__c WHERE HIC_FilesME__Linked_Record__c = :acc.Id ]; System.debug('Inserted folders >>> ' + folders); // Assertions System.assertEquals(1, folders.size(), 'One folder should be inserted'); System.assertEquals('EXT-1001', folders[0].HIC_FilesME__External_Folder_ID__c); System.assertEquals('Test SFTP Folder', folders[0].HIC_FilesME__Folder_Name__c); System.assertEquals('SFTP', folders[0].HIC_FilesME__Storage_Provider__c); System.assertEquals(true, folders[0].HIC_FilesME__Is_Active__c); } @isTest static void testUnlinkFiles_Success() { // Create parent record Account acc = new Account(Name = 'Test Account'); insert acc; System.debug('Account Id >>> ' + acc.Id); // Create linked folder record HIC_FilesME__Linked_Folder__c folder = new HIC_FilesME__Linked_Folder__c( Name = 'Test Folder', HIC_FilesME__Linked_Record__c = acc.Id, HIC_FilesME__External_Folder_ID__c = 'EXT-2001', HIC_FilesME__Folder_Name__c = 'Test Folder Name', HIC_FilesME__Storage_Provider__c = 'SFTP', HIC_FilesME__Is_Active__c = true ); insert folder; System.debug('Inserted Folder >>> ' + folder); Test.startTest(); SFTPFeature feature = new SFTPFeature(); OperationResult result = feature.unlinkFiles(acc.Id, 'EXT-2001', false); Test.stopTest(); System.debug('OperationResult >>> ' + result); // Verify record deleted List folders = [ SELECT Id FROM HIC_FilesME__Linked_Folder__c WHERE Id = :folder.Id ]; System.debug('Remaining folders >>> ' + folders); System.assertEquals(0, folders.size(), 'Folder should be deleted'); } public class SFTPUploadMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { System.debug('Mock Request Endpoint >>> ' + req.getEndpoint()); System.debug('Mock Request Body >>> ' + req.getBody()); HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody('{"message":"File uploaded successfully"}'); res.setStatusCode(200); return res; } } @isTest static void testUploadFileSuccess() { // Register Mock Test.setMock(HttpCalloutMock.class, new SFTPUploadMock()); String host = 'test.sftp.com'; String username = 'testUser'; String password = 'testPass'; String uploadPath = '/uploads'; String fileName = 'sample.txt'; String base64Data = EncodingUtil.base64Encode(Blob.valueOf('Test File Content')); System.debug('Base64 Data >>> ' + base64Data); Test.startTest(); OperationResult result = SFTPFeature.uploadFile( host, username, password, uploadPath, fileName, base64Data ); Test.stopTest(); System.debug('OperationResult >>> ' + result); System.assert(result != null, 'Result should not be null'); } @isTest static void testStoreSftpCredentials() { // Prepare payload map Map payload = new Map(); payload.put('username', 'testUser'); payload.put('password', 'testPassword'); payload.put('host', 'sftp.test.com'); System.debug('Payload Map >>> ' + payload); Boolean isAdminCred = true; Test.startTest(); // Call the method SFTPFeature.storeSftpCredentials(isAdminCred, payload); Test.stopTest(); System.debug('storeSftpCredentials method executed successfully'); // Assertion just to confirm execution System.assert(true, 'Method executed without any exception'); } }