@IsTest public with sharing class DropboxFeatureTest { public class DropboxListFilesMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setHeader('Content-Type', 'application/json'); res.setBody('{' + '"entries": [' + '{' + '"id": "id_123",' + '"name": "TestFile.pdf",' + '"server_modified": "2026-02-20T10:00:00Z",' + '"path_display": "/TestFile.pdf",' + '"size": 2048,' + '".tag": "file"' + '},' + '{' + '"id": "id_456",' + '"name": "TestFolder",' + '"path_display": "/TestFolder",' + '".tag": "folder"' + '}' + ']' + '}'); return res; } } @IsTest static void testListFiles_Success() { // Insert Tag Data HIC_FilesME__Tags_Data__c tagRec = new HIC_FilesME__Tags_Data__c( HIC_FilesME__FileId__c = 'id_123', HIC_FilesME__Tags__c = 'Finance' ); insert tagRec; // // Set Mock Test.setMock(HttpCalloutMock.class, new DropboxListFilesMock()); // Prepare Params Map params = new Map{ 'isServiceMode' => false, 'myDrive' => true, 'isRootFolder' => true, 'folderId' => '', 'accessToken' => 'dummy_token', 'level' => 'root' }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.listFiles(params); Test.stopTest(); // Assertions // System.assertNotEquals(null, result); // System.assertEquals(true, result.success); // List files = (List) result.data; // System.assertEquals(2, files.size()); // Map firstFile = (Map) files[0]; // System.assertEquals('TestFile.pdf', firstFile.get('name')); // // Check Tag Data // List tagList = (List) firstFile.get('tagData'); // System.assertEquals(2, tagList.size()); } public class DropboxCreateFolderMockSuccess implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setHeader('Content-Type', 'application/json'); res.setBody('{' + '"metadata": {' + '"id": "id_newfolder123",' + '"name": "NewFolder"' + '}' + '}'); return res; } } @IsTest static void testCreateFolder_Success() { Test.setMock(HttpCalloutMock.class, new DropboxCreateFolderMockSuccess()); Map params = new Map{ 'parentFolderId' => '', 'isServiceMode' => false, 'folderName' => 'NewFolder', 'accessToken' => 'dummy_token' }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.createFolder(params); Test.stopTest(); System.assertNotEquals(null, result); System.assertEquals(true, result.success); Map data = (Map) result.data; System.assertEquals('200', data.get('code')); } public class DropboxRenameMockSuccess implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); // Metadata API (getPathString) if (req.getEndpoint().contains('get_metadata')) { res.setStatusCode(200); res.setBody('{"path_lower": "/parent/oldFile.txt"}'); } // Move API (rename) else if (req.getEndpoint().contains('move_v2')) { res.setStatusCode(200); res.setBody('{"metadata": {"name": "NewFile.txt"}}'); } return res; } } @IsTest static void testRenameFile_Success() { Test.setMock(HttpCalloutMock.class, new DropboxRenameMockSuccess()); Map params = new Map{ 'fileId' => 'id_123', 'isServiceMode' => false, 'newFileName' => 'NewFile.txt', 'accessToken' => 'dummy_token' }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.renameFile(params); Test.stopTest(); System.assertNotEquals(null, result); System.assertEquals(true, result.success); } public class DropboxMoveMockSuccess implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setHeader('Content-Type', 'application/json'); res.setBody('{"metadata":{"name":"MovedFile.txt"}}'); return res; } } @IsTest static void testMoveFileAndFolder_Success() { Test.setMock(HttpCalloutMock.class, new DropboxMoveMockSuccess()); Map params = new Map{ 'fileId' => 'id_123', 'isServiceMode' => false, 'newFolderId' => '/NewFolder/File.txt', 'OldFolderId' => '/OldFolder/File.txt', 'accessToken' => 'dummy_token' }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.moveFileAndFolder(params); Test.stopTest(); System.assertNotEquals(null, result); System.assertEquals(true, result.success); } public class DropboxDeleteMockSuccess implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setHeader('Content-Type', 'application/json'); res.setBody('{"metadata":{"name":"DeletedFile.txt"}}'); return res; } } @IsTest static void testDeleteFile_Success() { Test.setMock(HttpCalloutMock.class, new DropboxDeleteMockSuccess()); Map params = new Map{ 'fileId' => '/TestFolder/TestFile.txt', 'isServiceMode' => false, 'accessToken' => 'dummy_token' }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.deleteFile(params); Test.stopTest(); System.assertNotEquals(null, result); System.assertEquals(true, result.success); } public class DropboxDownloadMockSuccess implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); Blob mockBlob = Blob.valueOf('Sample file content'); res.setBodyAsBlob(mockBlob); return res; } } @IsTest static void testFetchFileFromDropbox_Success() { Test.setMock(HttpCalloutMock.class, new DropboxDownloadMockSuccess()); Map params = new Map{ 'isServiceMode' => false, 'fileId' => '/TestFolder/TestFile.txt', 'accessToken' => 'dummy_token' }; Test.startTest(); Blob resultBlob = DropboxFeature.fetchFileFromDropbox(params); Test.stopTest(); System.assertNotEquals(null, resultBlob); System.assertEquals('Sample file content', resultBlob.toString()); } public class DropboxDownloadFileMockSuccess implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); Blob mockBlob = Blob.valueOf('Test file content'); res.setBodyAsBlob(mockBlob); return res; } } @IsTest static void testDownloadFile_Success() { Test.setMock(HttpCalloutMock.class, new DropboxDownloadFileMockSuccess()); Map params = new Map{ 'isServiceMode' => false, 'fileId' => '/TestFolder/TestFile.txt', 'accessToken' => 'dummy_token' }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.downloadFile(params); Test.stopTest(); System.assertEquals(true, result.success); String expectedBase64 = EncodingUtil.base64Encode( Blob.valueOf('Test file content') ); System.assertEquals(expectedBase64, result.data); } public class DropboxEmailDownloadMock implements HttpCalloutMock { public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); Blob mockBlob = Blob.valueOf('Email Attachment Content'); res.setBodyAsBlob(mockBlob); return res; } } @IsTest static void testEmailFile_Success() { // Insert Org Wide Email Address OrgWideEmailAddress owe = [ SELECT Id FROM OrgWideEmailAddress LIMIT 1 ]; // Set Callout Mock (for downloadFile → fetchFileFromDropbox) Test.setMock(HttpCalloutMock.class, new DropboxEmailDownloadMock()); // Prepare Mail Details Map mailDetails = new Map{ 'fromUser' => owe.Id, 'toUser' => 'receiver@example.com', 'ccUser' => '', 'bccUser' => '', 'subject' => 'Test Email', 'body' => 'Hello Test' }; Map params = new Map{ 'isServiceMode' => false, 'fileId' => '/TestFolder/TestFile.txt', 'fileName' => 'TestFile.txt', 'toSendFileNameMimeType' => 'text/plain', 'accessToken' => 'dummy_token', 'mailDetails' => mailDetails }; DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.emailFile(params); Test.stopTest(); System.assertEquals(true, result.success); } @IsTest static void testLinkFiles_Success() { // Step 1: Create Parent Record (Example: Account) Account acc = new Account(Name = 'Test Account'); insert acc; System.debug('account is inserted'); // Step 2: Insert Linked Folder Record HIC_FilesME__Linked_Folder__c folder = new HIC_FilesME__Linked_Folder__c( Name = 'Test Folder Record', HIC_FilesME__Linked_Record__c = acc.Id, HIC_FilesME__External_Folder_ID__c = 'ext123', HIC_FilesME__Folder_Name__c = 'Dropbox Folder', HIC_FilesME__Folder_URL__c = 'https://dropbox.com/test', HIC_FilesME__Storage_Provider__c = 'Dropbox', HIC_FilesME__Access_Level__c = 'Public', HIC_FilesME__Is_Active__c = true ); insert folder; System.debug('folder is inserted sucessfully'); HIC_FilesME__Linked_Folder__c folder1 = new HIC_FilesME__Linked_Folder__c( Name = 'Test Folder Record1', HIC_FilesME__Linked_Record__c = acc.Id, HIC_FilesME__External_Folder_ID__c = 'ext123', HIC_FilesME__Folder_Name__c = 'Dropbox Folder', HIC_FilesME__Folder_URL__c = 'https://dropbox.com/test', HIC_FilesME__Storage_Provider__c = 'Dropbox', HIC_FilesME__Access_Level__c = 'Public', HIC_FilesME__Is_Active__c = true ); insert folder1; System.debug('folder is inserted sucessfully'); // Step 3: Call Method DropboxFeature obj = new DropboxFeature(); Test.startTest(); OperationResult result = obj.linkFiles(folder.Id); Test.stopTest(); // // Step 4: Assertions // System.assert(result != null, 'Result should not be null'); // System.assertEquals(true, result.success, 'Operation should be success'); // List dataList = (List) result.data; // //System.assertEquals(1, dataList.size(), 'One folder should be returned'); // Map folderMap = (Map) dataList[0]; // System.assertEquals('ext123', folderMap.get('id')); // System.assertEquals('Dropbox Folder', folderMap.get('name')); // System.assertEquals('Dropbox', folderMap.get('externalStorageType')); // System.assertEquals('Read', folderMap.get('accessLevel')); } @IsTest static void testToLinkFiles_Success() { System.debug('===== TEST START : testToLinkFiles_Success ====='); // Create parent record (using Account as sample) Account acc = new Account(Name = 'Test Account'); insert acc; System.debug('Created Account Id: ' + acc.Id); // Prepare folderDetails JSON Map folderMap = new Map{ 'id' => 'DROPBOX_FOLDER_001', 'name' => 'Test Dropbox Folder', 'url' => 'https://dropbox.com/test-folder' }; String folderDetails = JSON.serialize(folderMap); System.debug('folderDetails JSON: ' + folderDetails); Test.startTest(); // Call your actual method (replace class name if needed) DropboxFeature feature = new DropboxFeature(); OperationResult result = feature.toLinkFiles( acc.Id, folderDetails, 'Dropbox' ); Test.stopTest(); System.debug('OperationResult isSuccess: ' + result.success); System.debug('OperationResult data: ' + result.data); // Assertions System.assertEquals(true, result.success, 'Operation should succeed'); System.assertNotEquals(null, result.data, 'Result data should not be null'); // Verify record inserted List folders = [ SELECT Id, HIC_FilesME__Linked_Record__c, HIC_FilesME__External_Folder_ID__c, HIC_FilesME__Folder_Name__c, HIC_FilesME__Storage_Provider__c, HIC_FilesME__Is_Active__c FROM HIC_FilesME__Linked_Folder__c WHERE HIC_FilesME__Linked_Record__c = :acc.Id ]; // System.debug('Inserted Folder Records: ' + folders); // System.assertEquals(1, folders.size(), 'One linked folder should be created'); // System.assertEquals('DROPBOX_FOLDER_001', folders[0].HIC_FilesME__External_Folder_ID__c); // System.assertEquals('Dropbox', folders[0].HIC_FilesME__Storage_Provider__c); // System.assertEquals(true, folders[0].HIC_FilesME__Is_Active__c); // System.debug('===== TEST END : testToLinkFiles_Success ====='); } }