Skip to content

Commit

Permalink
Increase test coverage (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmather-c authored Nov 15, 2023
1 parent 2df6806 commit 1169f89
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 31 deletions.
50 changes: 50 additions & 0 deletions sfdx/force-app/main/default/classes/ConfigExportVFCont_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,54 @@ public with sharing class ConfigExportVFCont_Test {
System.assertEquals('testProduct2Filter', (String)resultsMap.get('product_filter'));
System.assertEquals('testPricebookEntryFilter', (String)resultsMap.get('pricebook_entry_filter'));
}

@IsTest
static public void TestGetConfigWithRecords() {
Map<String,Object> testRecordsMap = test_restService.makeData(false);
Opportunity opportunity = (Opportunity)testRecordsMap.get('opportunity');
Product2 product = (Product2)testRecordsMap.get('product');
Sync_Record__c sr = new Sync_Record__c(
Primary_Object_Type__c = 'Opportunity',
Primary_Record_ID__c = opportunity.Id,
Secondary_Object_Type__c = 'Product2',
Secondary_Record_ID__c = product.Id,
Resolution_Message__c = 'Test',
Resolution_Status__c = 'Error'
);
insert sr;

Setup_Connection_Data__mdt testSetupData = test_setupAssistant.getTestStripeConnectionKey();
test_setupAssistant.setTestGlobalKey(testSetupData);
test_setupAssistant.insertTestConnectedRecord();
Test.setMock(HttpCalloutMock.class, new test_setupAssistant.UnifiedConfigMock());

Test.startTest();
PageReference pageRef = Page.DownloadForSupportConfig;
pageRef.getParameters().put('recordId', sr.Id);
Test.setCurrentPage(Page.DownloadForSupportConfig);
Test.setCurrentPageReference(pageRef);
ConfigExportVFController controller = new ConfigExportVFController();
String response = controller.getConfig();
Map<String, Object> resultsMap = (Map<String, Object>)JSON.deserializeUntyped(response);
Test.stopTest();

Map<String, Object> allMappingConfigurations = (Map<String, Object>)resultsMap.get('allMappingConfigurations');
Map<String, Object> default_mappings = (Map<String, Object>)allMappingConfigurations.get('default_mappings');
Map<String, Object> field_defaults = (Map<String, Object>)allMappingConfigurations.get('field_defaults');
Map<String, Object> field_mappings = (Map<String, Object>)allMappingConfigurations.get('field_mappings');
Map<String, Object> required_mappings = (Map<String, Object>)allMappingConfigurations.get('required_mappings');

System.assertNotEquals(allMappingConfigurations, null);
System.assertEquals(default_mappings, null);
System.assertEquals(required_mappings, null);
System.assertNotEquals(field_defaults, null);
System.assertNotEquals(field_mappings, null);
System.assertEquals('2 days', (String)resultsMap.get('sync_record_retention'));
System.assertEquals('yesterday', (String)resultsMap.get('sync_start_date'));

System.assertEquals('testOrderFilter', (String)resultsMap.get('order_filter'));
System.assertEquals('testAccountFilter', (String)resultsMap.get('account_filter'));
System.assertEquals('testProduct2Filter', (String)resultsMap.get('product_filter'));
System.assertEquals('testPricebookEntryFilter', (String)resultsMap.get('pricebook_entry_filter'));
}
}
2 changes: 1 addition & 1 deletion sfdx/force-app/main/default/classes/test_restService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public with sharing class test_restService {
System.assertEquals('MAPPING_ERROR', parsedReqBody.get('error_type'));
}

private static Map<String,Object> makeData(Boolean isAmendedOrderTest) {
public static Map<String,Object> makeData(Boolean isAmendedOrderTest) {
Boolean isCpqInstalled = utilities.isCpqEnabled();

//insert all the records we will be checking the JSON response for
Expand Down
96 changes: 94 additions & 2 deletions sfdx/force-app/main/default/classes/test_utilities.cls
Original file line number Diff line number Diff line change
@@ -1,11 +1,103 @@
@isTest
@IsTest
public with sharing class test_utilities {
@isTest
@IsTest
static public void testPackageVersion() {
Test.startTest();
String packageVersion = utilities.getPackageVersion();
Test.stopTest();

System.assert(packageVersion != null);
}

@IsTest
static public void testPackageVersionString() {
Test.startTest();
String packageVersion = utilities.getPackageVersionString();
Test.stopTest();

System.assert(packageVersion != null);
}

@IsTest
static void testGetSobjectByMap() {
Map<String, Object> q = new Map<String, Object> {
'Account__c' => '123'
};

Test.startTest();
SBQQ__Quote__c quote = (SBQQ__Quote__c) utilities.getSobjectByMap(q, 'Quote__c', 'SBQQ__');
Test.stopTest();

System.assertNotEquals(null, quote);
System.assertEquals('123', quote.SBQQ__Account__c);
}

@IsTest
static void testGetConnectionKey() {
Exception ex;
Setup_Connection_Data__mdt testSetupData = test_setupAssistant.getTestStripeConnectionKey();
test_setupAssistant.setTestGlobalKey(testSetupData);

String key = utilities.getStripeConnectionKey();

utilities.setupConfigMetadata.clear();

try {
utilities.getStripeConnectionKey();
} catch (Exception e) {
ex = e;
}

System.assertNotEquals(null, ex, 'Got an exception');
System.assertEquals(testSetupData.Global_Key__c, key, 'Got the right key');
}

@IsTest
static void testGetOAuthSigningKey() {
Setup_Connection_Data__mdt testSetupData = test_setupAssistant.getTestStripeConnectionKey();
test_setupAssistant.setTestGlobalKey(testSetupData);

Blob key = utilities.getOAuthStateSigningKey();

utilities.setupConfigMetadata.clear();

Blob failed = utilities.getOAuthStateSigningKey();

System.assertEquals(null, failed, 'returns null with no record set');
System.assertNotEquals(null, key, 'Returns a value with record set');
}

@IsTest
static void testIsDevModeEnabled() {
Setup_Connection_Data__mdt testSetupData = test_setupAssistant.getTestStripeConnectionKey();
test_setupAssistant.setTestGlobalKey(testSetupData);

testSetupData.Enable_JS_Debug__c = true;

Boolean yep1 = utilities.isDeveloperModeEnabled();
Boolean yepJs = utilities.isJSDebuggingEnabled();
Boolean nopeApex2 = utilities.isApexDebuggingEnabled();

testSetupData.Enable_Apex_Debug__c = true;

Boolean yepAll = utilities.isDeveloperModeEnabled();
Boolean yepJs2 = utilities.isJSDebuggingEnabled();
Boolean yepApex = utilities.isApexDebuggingEnabled();

utilities.setupConfigMetadata.clear();

Boolean nopeAll = utilities.isDeveloperModeEnabled();
Boolean nopeJs = utilities.isJSDebuggingEnabled();
Boolean nopeApex = utilities.isApexDebuggingEnabled();

System.assertEquals(false, nopeAll, 'Nope all');
System.assertEquals(false, nopeJs, 'Nope JS');
System.assertEquals(false, nopeApex, 'Nope Apex');
System.assertEquals(true, yep1, 'Yep all');
System.assertEquals(true, yepJs, 'Yep JS');
System.assertEquals(false, nopeApex2, 'Nope Apex 2');
System.assertEquals(true, yepAll, 'Yep all 2');
System.assertEquals(true, yepJs2, 'Yep JS 2');
System.assertEquals(true, yepApex, 'Yep Apex');
}
}
31 changes: 3 additions & 28 deletions sfdx/force-app/main/default/classes/utilities.cls
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,6 @@ public with sharing class utilities {
public class SetupIsNotCompleteException extends Exception {}
public class HttpCalloutFailedException extends Exception {}

public static Map<String, Object> requestPlatformConfig() {
// make callout to ruby services configuration endpoint to see if the org is connected to Stripe and Salesforce
String route = UriTargets.getConfigEndpoint();
HttpResponse response = utilities.makeCallout(route, 'GET');

/*
if we do not get a 200 status code back we create an Error Log record to show we did not get a success
response from the ruby service and send a status of failed to signify to show an error toast
*/
if(response.getStatusCode() != 200) {
Sentry_Exception e = (Sentry_Exception) Sentry_ExceptionFactory.build(HttpCalloutFailedException.class);
e.setMessage('Failed to get configuration from Ruby service.');
e.context.put('route', route);
e.context.put('response', response.getBody());
e.context.put('response_status', response.getStatus());
e.context.put('response_status_code', response.getStatusCode());
errorLogger.create('requestPlatformConfig', e);
return null;
}

/*
if we get a 200 response code from the ruby service we will check the connection status object
in the payload to determine weather the user successfully connected to stripe or not
*/
return (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
}

public static Boolean isPackagedEnvironment() {
return !String.isEmpty(constants.NAMESPACE_API);
}
Expand Down Expand Up @@ -251,7 +224,9 @@ public with sharing class utilities {
return setupConfigMetadata[0].Global_Key__c;
}

Sentry_Log.logSentry('missing metadata record with global key');
if (Test.isRunningTest() == false) {
Sentry_Log.logSentry('missing metadata record with global key');
}
throw new SetupIsNotCompleteException('Org is not setup correctly, missing custom metadata record with global key');
}

Expand Down

0 comments on commit 1169f89

Please sign in to comment.