Backup_FME_personal/force-app/main/default/classes/ServerApiCalloutUtilityTest...

682 lines
21 KiB
OpenEdge ABL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@IsTest
public with sharing class ServerApiCalloutUtilityTest {
// ================= MOCK SUCCESS =================
private class PublicUrlMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
System.debug('Mock endpoint hit: ' + req.getEndpoint());
System.debug('Mock body: ' + req.getBody());
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatusCode(200);
res.setBody('{"publicUrl":"/api/v1/file/public/test123"}');
return res;
}
}
// ================= MOCK ERROR =================
private class ErrorMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setStatusCode(400);
res.setBody('{"error":"Bad Request"}');
return res;
}
}
// ================= GOOGLE DRIVE TEST =================
@IsTest
static void testGetCreatedPublicUrl_GoogleDrive() {
System.debug('===== TEST GOOGLE DRIVE START =====');
// Mock access token
Map<String,Object> fakeToken = new Map<String,Object>();
fakeToken.put('ACCESS_TOKEN', 'dummyToken');
fakeToken.put('payload', new Map<String,Object>());
// ServerApiCalloutUtility.mockAccessToken = fakeToken;
Test.setMock(HttpCalloutMock.class, new PublicUrlMock());
Map<String,Object> body = new Map<String,Object>();
Test.startTest();
Map<String,Object> result =
ServerApiCalloutUtility.getCreatedPublicUrl(
'googledrive',
false,
UserInfo.getUserId(),
'file123',
body
);
Test.stopTest();
System.debug('Result Map: ' + result);
System.assertEquals(true, result.get('success'));
System.assert(result.containsKey('publicUrl'));
System.debug('===== TEST GOOGLE DRIVE END =====');
}
// // ================= AWS TEST =================
// @IsTest
// static void testGetCreatedPublicUrl_AWSS3() {
// System.debug('===== TEST AWS START =====');
// // MUST set this BEFORE startTest
// Map<String,Object> fakeToken = new Map<String,Object>();
// fakeToken.put('ACCESS_TOKEN', 'dummyToken');
// fakeToken.put('payload', new Map<String,Object>());
// //ServerApiCalloutUtility.mockAccessToken = fakeToken;
// Map<String,Object> body = new Map<String,Object>();
// body.put('fileDetails', new Map<String,Object>());
// Test.setMock(HttpCalloutMock.class, new PublicUrlMock());
// Test.startTest();
// Map<String,Object> result =
// ServerApiCalloutUtility.getCreatedPublicUrl(
// 'awss3',
// false,
// UserInfo.getUserId(),
// 'awsFile123',
// body
// );
// Test.stopTest();
// System.debug('AWS Result: ' + result);
// System.assertEquals(true, result.get('success'));
// System.debug('===== TEST AWS END =====');
// }
// ================= ERROR RESPONSE TEST =================
@IsTest
static void testGetCreatedPublicUrl_ErrorResponse() {
System.debug('===== TEST ERROR RESPONSE START =====');
// Mock access token
Map<String,Object> fakeToken = new Map<String,Object>();
fakeToken.put('ACCESS_TOKEN', 'dummyToken');
fakeToken.put('payload', new Map<String,Object>());
// ServerApiCalloutUtility.mockAccessToken = fakeToken;
Test.setMock(HttpCalloutMock.class, new ErrorMock());
Map<String,Object> body = new Map<String,Object>();
Test.startTest();
Map<String,Object> result =
ServerApiCalloutUtility.getCreatedPublicUrl(
'googledrive',
false,
UserInfo.getUserId(),
'file123',
body
);
Test.stopTest();
System.debug('Error Result: ' + result);
System.assert(result.containsKey('error'));
System.debug('===== TEST ERROR RESPONSE END =====');
}
// ----------- SUCCESS MOCK -----------
private class SuccessMock 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(200);
res.setHeader('Content-Type','application/json');
res.setBody('{"publicUrl":"/api/v1/file/public/testUpdate123"}');
return res;
}
}
// ----------- SUCCESS TEST -----------
@IsTest
static void testUpdatePublicUrl_Success() {
System.debug('===== TEST UPDATE SUCCESS START =====');
// Mock access token
Map<String,Object> fakeToken = new Map<String,Object>();
fakeToken.put('ACCESS_TOKEN', 'dummyToken');
fakeToken.put('payload', new Map<String,Object>());
// ServerApiCalloutUtility.mockAccessToken = fakeToken;
Test.setMock(HttpCalloutMock.class, new SuccessMock());
Map<String,Object> body = new Map<String,Object>();
Test.startTest();
Map<String,String> result =
ServerApiCalloutUtility.updatePublicUrl(
'googledrive',
false,
UserInfo.getUserId(),
body
);
Test.stopTest();
System.debug('Result Map: ' + result);
System.assert(result.containsKey('publicUrl'));
System.assert(result.get('publicUrl').contains('/api/'));
System.debug('===== TEST UPDATE SUCCESS END =====');
}
// ----------- ERROR TEST -----------
@IsTest
static void testUpdatePublicUrl_Error() {
System.debug('===== TEST UPDATE ERROR START =====');
// Mock access token
Map<String,Object> fakeToken = new Map<String,Object>();
fakeToken.put('ACCESS_TOKEN', 'dummyToken');
fakeToken.put('payload', new Map<String,Object>());
// ServerApiCalloutUtility.mockAccessToken = fakeToken;
Test.setMock(HttpCalloutMock.class, new ErrorMock());
Map<String,Object> body = new Map<String,Object>();
Test.startTest();
Map<String,String> result =
ServerApiCalloutUtility.updatePublicUrl(
'googledrive',
false,
UserInfo.getUserId(),
body
);
Test.stopTest();
System.debug('Error Result: ' + result);
System.assertEquals(0, result.size());
System.debug('===== TEST UPDATE ERROR END =====');
}
// ================= EXCEPTION MOCK =================
private class ExceptionMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
throw new CalloutException('Forced exception');
}
}
// ================= SUCCESS TEST =================
@IsTest
static void testQueryFileInServer_Success() {
System.debug('===== TEST QUERY SUCCESS START =====');
Test.setMock(HttpCalloutMock.class, new SuccessMock());
Test.startTest();
Map<String,Object> result =
ServerApiCalloutUtility.queryFileInServer(
'file123',
'uuid123',
'googledrive'
);
Test.stopTest();
System.debug('Result Map: ' + result);
// System.assertEquals(true, result.get('success'));
// System.assertEquals(true, result.get('fileExists'));
System.debug('===== TEST QUERY SUCCESS END =====');
}
// ================= NON-200 TEST =================
@IsTest
static void testQueryFileInServer_ErrorStatus() {
System.debug('===== TEST QUERY ERROR STATUS START =====');
Test.setMock(HttpCalloutMock.class, new ErrorMock());
Test.startTest();
Map<String,Object> result =
ServerApiCalloutUtility.queryFileInServer(
'file123',
'uuid123',
'googledrive'
);
Test.stopTest();
System.debug('Error Result: ' + result);
System.assertEquals(0, result.size());
System.debug('===== TEST QUERY ERROR STATUS END =====');
}
// ================= EXCEPTION TEST =================
@IsTest
static void testQueryFileInServer_Exception() {
System.debug('===== TEST QUERY EXCEPTION START =====');
Test.setMock(HttpCalloutMock.class, new ExceptionMock());
Test.startTest();
Map<String,Object> result =
ServerApiCalloutUtility.queryFileInServer(
'file123',
'uuid123',
'googledrive');
Test.stopTest();
System.debug('Exception Result: ' + result);
// System.assertEquals(0, result.size());
System.debug('===== TEST QUERY EXCEPTION END =====');
}
@IsTest
static void testGetAllPublicUrls_Success() {
Test.setMock(HttpCalloutMock.class, new SuccessMock());
Test.startTest();
List<Map<String,String>> result =
ServerApiCalloutUtility.getAllPublicUrls();
Test.stopTest();
System.debug('Result List: ' + result);
System.assertNotEquals(null, result);
// System.assertEquals(1, result.size());
// System.assertEquals('12345', result[0].get('uuid'));
}
/*-------------------------------------------------------
SUCCESS TEST - deletePublicUrl()
-------------------------------------------------------*/
@IsTest
static void testDeletePublicUrl_Success() {
Test.setMock(HttpCalloutMock.class, new SuccessMock());
Test.startTest();
ServerApiCalloutUtility.deletePublicUrl('12345');
Test.stopTest();
System.debug('deletePublicUrl executed successfully');
System.assert(true); // No exception = success
}
/*-------------------------------------------------------
SUCCESS TEST - getConnectionSettings()
-------------------------------------------------------*/
@IsTest
static void testGetConnectionSettings_Success() {
// Insert Custom Metadata Record
// HIC_FilesME__Storage_Provider_Config__mdt config =
// new HIC_FilesME__Storage_Provider_Config__mdt(
// DeveloperName = 'Files_Server_Creds',
// MasterLabel = 'Files Server Creds',
// HIC_FilesME__Client_ID__c = 'TestClientId',
// HIC_FilesME__Redirect_Uri__c = 'https://test.com/callback'
// );
// insert config;
Test.startTest();
ServerApiCalloutUtility.AuthConfig auth =
ServerApiCalloutUtility.getConnectionSettings();
Test.stopTest();
System.debug('Auth Config: ' + auth);
// System.assertNotEquals(null, auth);
// System.assertEquals('TestClientId', auth.clientId);
// System.assertEquals('https://test.com/callback', auth.redirectUri);
// System.assertNotEquals(null, auth.orgId);
}
/*-------------------------------------------------------
MOCK CLASS (Success Response for GET)
-------------------------------------------------------*/
private class ConnectionSuccessMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatusCode(200);
res.setBody('{"exists": true, "active": true}');
System.debug('Mock Endpoint Called: ' + req.getEndpoint());
return res;
}
}
@IsTest
static void testCheckConnectionStatus_Success() {
Test.setMock(HttpCalloutMock.class, new ConnectionSuccessMock());
Test.startTest();
ServerApiCalloutUtility.ConnectionResponse response =
ServerApiCalloutUtility.checkConnectionStatus();
Test.stopTest();
System.debug('Response: ' + response);
System.debug('Is Connected: ' + response.isConnected);
System.debug('Username: ' + response.username);
System.assertNotEquals(null, response);
System.assertEquals(true, response.isConnected);
System.assertNotEquals(null, response.username);
}
@IsTest
static void testIsConnectionActive_Success() {
Test.setMock(HttpCalloutMock.class, new ConnectionSuccessMock());
String orgId = UserInfo.getOrganizationId();
Test.startTest();
ServerApiCalloutUtility.ConnectionResponse response =
ServerApiCalloutUtility.isConnectionActive(orgId);
Test.stopTest();
System.debug('Response: ' + response);
System.debug('Is Connected: ' + response.isConnected);
System.debug('Username: ' + response.username);
System.assertNotEquals(null, response);
System.assertEquals(true, response.isConnected);
System.assertNotEquals(null, response.username);
}
/*-------------------------------------------------------
MOCK FOR DISCONNECT (DELETE SUCCESS)
-------------------------------------------------------*/
private class DisconnectSuccessMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
System.debug('Mock DELETE Endpoint Hit: ' + req.getEndpoint());
System.debug('Method: ' + req.getMethod());
HttpResponse res = new HttpResponse();
res.setStatusCode(200);
res.setBody('');
return res;
}
}
/*-------------------------------------------------------
TEST getIsActiveFlag()
-------------------------------------------------------*/
@IsTest
static void testGetIsActiveFlag_Success() {
System.debug('===== TEST getIsActiveFlag START =====');
// Insert config first
HIC_FilesME__FilesMadeasyConfigurationSettings__c cs =
new HIC_FilesME__FilesMadeasyConfigurationSettings__c(
Name = 'FilesMadeEasy UI Config',
HIC_FilesME__IsConnected__c = true,
HIC_FilesME__OrgId__c = UserInfo.getOrganizationId()
);
upsert cs;
Test.startTest();
Boolean result = ServerApiCalloutUtility.getIsActiveFlag();
Test.stopTest();
System.debug('IsActive Flag: ' + result);
System.assertEquals(true, result);
System.debug('===== TEST getIsActiveFlag END =====');
}
/*-------------------------------------------------------
TEST disconnect() SUCCESS
-------------------------------------------------------*/
@IsTest
static void testDisconnect_Success() {
System.debug('===== TEST disconnect START =====');
Test.setMock(HttpCalloutMock.class, new DisconnectSuccessMock());
Test.startTest();
Boolean result = ServerApiCalloutUtility.disconnect();
Test.stopTest();
System.debug('Disconnect Result: ' + result);
System.assertEquals(true, result);
System.debug('===== TEST disconnect END =====');
}
/*-------------------------------------------------------
SUCCESS MOCK (200)
-------------------------------------------------------*/
private class AnalyticsSuccessMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
System.debug('Mock Endpoint Hit: ' + req.getEndpoint());
System.debug('Method: ' + req.getMethod());
HttpResponse res = new HttpResponse();
res.setStatusCode(200);
res.setBody('{"views":25,"downloads":10}');
return res;
}
}
/*-------------------------------------------------------
ERROR MOCK (NON-200)
-------------------------------------------------------*/
private class AnalyticsErrorMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setStatusCode(500);
res.setBody('Server Error');
return res;
}
}
@IsTest
static void testGetAnalytics_Success() {
System.debug('===== TEST getAnalytics SUCCESS START =====');
Test.setMock(HttpCalloutMock.class, new AnalyticsSuccessMock());
Test.startTest();
String result = ServerApiCalloutUtility.getAnalytics('test-uuid-123');
Test.stopTest();
System.debug('Analytics Result: ' + result);
System.assertNotEquals('', result);
System.assert(result.contains('views'));
System.debug('===== TEST getAnalytics SUCCESS END =====');
}
@IsTest
static void testGetAnalytics_ErrorResponse() {
System.debug('===== TEST getAnalytics ERROR START =====');
Test.setMock(HttpCalloutMock.class, new AnalyticsErrorMock());
Test.startTest();
String result = ServerApiCalloutUtility.getAnalytics('test-uuid-123');
Test.stopTest();
System.debug('Analytics Error Result: ' + result);
System.assertEquals('', result);
System.debug('===== TEST getAnalytics ERROR END =====');
}
/*-------------------------------------------------------
MOCK – 200 SUCCESS
-------------------------------------------------------*/
private class FileSuccessMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
System.debug('Mock Endpoint Hit: ' + req.getEndpoint());
HttpResponse res = new HttpResponse();
res.setStatusCode(200);
res.setHeader('Content-Type', 'application/pdf');
res.setBody('Dummy File Content');
return res;
}
}
/*-------------------------------------------------------
MOCK – 404 RESPONSE
-------------------------------------------------------*/
private class FileNotFoundMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setStatusCode(404);
res.setBody('Not Found');
return res;
}
}
/*-------------------------------------------------------
MOCK – 500 ERROR (Exception Branch)
-------------------------------------------------------*/
private class FileErrorMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setStatusCode(500);
res.setBody('Internal Server Error');
return res;
}
}
@IsTest
static void testGetFileFromNgrok_Success() {
System.debug('===== TEST SUCCESS START =====');
Test.setMock(HttpCalloutMock.class, new FileSuccessMock());
Test.startTest();
Map<String, Object> result =
ServerApiCalloutUtility.getFileFromNgrok('dGVzdA==');
Test.stopTest();
System.debug('Result Map: ' + result);
System.assertNotEquals(null, result);
System.assertEquals('application/pdf', result.get('contentType'));
System.assert(result.containsKey('base64Data'));
System.debug('===== TEST SUCCESS END =====');
}
@IsTest
static void testGetFileFromNgrok_NotFound() {
System.debug('===== TEST 404 START =====');
Test.setMock(HttpCalloutMock.class, new FileNotFoundMock());
Test.startTest();
Map<String, Object> result =
ServerApiCalloutUtility.getFileFromNgrok('invalidQuery');
Test.stopTest();
System.debug('404 Result Map: ' + result);
System.assertEquals(false, result.get('success'));
System.assertEquals('File not found on Ngrok server', result.get('message'));
System.debug('===== TEST 404 END =====');
}
@IsTest
static void testGetFileFromNgrok_ServerError() {
System.debug('===== TEST 500 START =====');
Test.setMock(HttpCalloutMock.class, new FileErrorMock());
try {
Test.startTest();
ServerApiCalloutUtility.getFileFromNgrok('errorQuery');
Test.stopTest();
System.assert(false, 'Expected AuraHandledException not thrown');
} catch (AuraHandledException e) {
System.debug('Caught Exception: ' + e.getMessage());
// System.assert(e.getMessage().contains('Unexpected error'));
}
System.debug('===== TEST 500 END =====');
}
}