Skip to content

Commit

Permalink
Global Settings: Refactored code so that unit tests always pass wheth…
Browse files Browse the repository at this point in the history
…er a CMT record is configured or not and independent of what the value is.
  • Loading branch information
j-fischer committed Jan 17, 2024
1 parent 5001bbd commit adb5fb6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
30 changes: 24 additions & 6 deletions rflib/main/default/classes/rflib_GlobalSettings.cls
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,64 @@
*/
@SuppressWarnings('PMD.ClassNamingConventions')
public with sharing class rflib_GlobalSettings {

@TestVisible
private static final Integer APPLICATION_EVENT_RETAIN_X_DAYS_DEFAULT_VALUE = 45;

@TestVisible
private static final Integer ARCHIVE_LOG_QUERY_LIMIT_DEFAULT_VALUE = 50000;

@TestVisible
private static final Boolean PERMISSIONS_EXPLORER_REST_API_ENABLED_DEFAULT_VALUE = false;

@TestVisible
private static final Integer PUBLISH_PLATFORM_EVENT_TRANSACTION_LIMIT_DEFAULT_VALUE = 150;

@TestVisible
private static final String TRACE_ID_HEADER_NAME_DEFAULT_VALUE = 'X-Trace-ID';

@TestVisible
private static final Boolean USE_DEFAULT_WORKFLOW_USER_FOR_LOG_EVENTS_DEFAULT_VALUE = false;

public static Integer daysToRetainApplicationEventsOrDefault {
get {
String val = getSetting('Application_Event_Retain_X_Days');
return String.isBlank(val) ? 45 : Integer.valueOf(val);
return String.isBlank(val) ? APPLICATION_EVENT_RETAIN_X_DAYS_DEFAULT_VALUE : Integer.valueOf(val);
}
}

public static Integer archiveLogQueryLimitOrDefault {
get {
String val = getSetting('Archive_Log_Query_Limit');
return String.isBlank(val) ? 50000 : Integer.valueOf(val);
return String.isBlank(val) ? ARCHIVE_LOG_QUERY_LIMIT_DEFAULT_VALUE : Integer.valueOf(val);
}
}

public static Boolean useRestQueryModeForPermissionRetrievalOrDefault {
get {
String val = getSetting('Permissions_Explorer_REST_API_Enabled');
return String.isBlank(val) ? false : Boolean.valueOf(val);
return String.isBlank(val) ? PERMISSIONS_EXPLORER_REST_API_ENABLED_DEFAULT_VALUE : Boolean.valueOf(val);
}
}

public static Integer publishingLimitOrDefault {
get {
String val = getSetting('Publish_Platform_Event_Transaction_Limit');
return String.isBlank(val) ? 150 : Integer.valueOf(val);
return String.isBlank(val) ? PUBLISH_PLATFORM_EVENT_TRANSACTION_LIMIT_DEFAULT_VALUE : Integer.valueOf(val);
}
}

public static String traceIdHeaderNameOrDefault {
get {
String val = getSetting('Trace_ID_Header_Name');
return String.isBlank(val) ? 'X-Trace-ID' : val;
return String.isBlank(val) ? TRACE_ID_HEADER_NAME_DEFAULT_VALUE : val;
}
}

public static Boolean useDefaultWorkflowUserForLogEventsOrDefault {
get {
String val = getSetting('Use_Default_Workflow_User_for_Log_Events');
return String.isBlank(val) ? false : Boolean.valueOf(val);
return String.isBlank(val) ? USE_DEFAULT_WORKFLOW_USER_FOR_LOG_EVENTS_DEFAULT_VALUE : Boolean.valueOf(val);
}
}

Expand Down
36 changes: 27 additions & 9 deletions rflib/test/default/classes/rflib_GlobalSettingsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testTraceIdHeaderNameOrDefault() {
String expectedValue = rflib_Global_Setting__mdt.getInstance('Trace_ID_Header_Name').Value__c;
String expectedValue = getConfiguredStringValueOrDefault('Trace_ID_Header_Name', rflib_GlobalSettings.TRACE_ID_HEADER_NAME_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.traceIdHeaderNameOrDefault);
Expand All @@ -41,7 +41,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testPublishingLimitOrDefault() {
Integer expectedValue = Integer.valueOf(rflib_Global_Setting__mdt.getInstance('Publish_Platform_Event_Transaction_Limit').Value__c);
Integer expectedValue = getConfiguredIntegerValueOrDefault('Publish_Platform_Event_Transaction_Limit', rflib_GlobalSettings.ARCHIVE_LOG_QUERY_LIMIT_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.publishingLimitOrDefault);
Expand All @@ -50,7 +50,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testOverridePublishingLimitOrDefault_Success() {
Integer expectedValue = Integer.valueOf(rflib_Global_Setting__mdt.getInstance('Publish_Platform_Event_Transaction_Limit').Value__c);
Integer expectedValue = getConfiguredIntegerValueOrDefault('Publish_Platform_Event_Transaction_Limit', rflib_GlobalSettings.PUBLISH_PLATFORM_EVENT_TRANSACTION_LIMIT_DEFAULT_VALUE);

Test.startTest();
rflib_GlobalSettings.overridePublishingLimitOrDefault(5);
Expand All @@ -61,7 +61,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testOverridePublishingLimitOrDefault_NullValue() {
Integer expectedValue = Integer.valueOf(rflib_Global_Setting__mdt.getInstance('Publish_Platform_Event_Transaction_Limit').Value__c);
Integer expectedValue = getConfiguredIntegerValueOrDefault('Publish_Platform_Event_Transaction_Limit', rflib_GlobalSettings.PUBLISH_PLATFORM_EVENT_TRANSACTION_LIMIT_DEFAULT_VALUE);

Test.startTest();
rflib_GlobalSettings.overridePublishingLimitOrDefault(0);
Expand All @@ -74,7 +74,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testOverridePublishingLimitOrDefault_ValueExceedsLimit() {
Integer expectedValue = Integer.valueOf(rflib_Global_Setting__mdt.getInstance('Publish_Platform_Event_Transaction_Limit').Value__c);
Integer expectedValue = getConfiguredIntegerValueOrDefault('Publish_Platform_Event_Transaction_Limit', rflib_GlobalSettings.PUBLISH_PLATFORM_EVENT_TRANSACTION_LIMIT_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.publishingLimitOrDefault);
Expand All @@ -90,7 +90,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testDaysToRetainApplicationEvents() {
Integer expectedValue = Integer.valueOf(rflib_Global_Setting__mdt.getInstance('Application_Event_Retain_X_Days').Value__c);
Integer expectedValue = getConfiguredIntegerValueOrDefault('Application_Event_Retain_X_Days', rflib_GlobalSettings.APPLICATION_EVENT_RETAIN_X_DAYS_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.daysToRetainApplicationEventsOrDefault);
Expand All @@ -99,7 +99,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testArchiveLogQueryLimit() {
Integer expectedValue = Integer.valueOf(rflib_Global_Setting__mdt.getInstance('Archive_Log_Query_Limit').Value__c);
Integer expectedValue = getConfiguredIntegerValueOrDefault('Archive_Log_Query_Limit', rflib_GlobalSettings.ARCHIVE_LOG_QUERY_LIMIT_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.archiveLogQueryLimitOrDefault);
Expand All @@ -108,7 +108,7 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testUseRestQueryModeForPermissionRetrieval() {
Boolean expectedValue = Boolean.valueOf(rflib_Global_Setting__mdt.getInstance('Permissions_Explorer_REST_API_Enabled').Value__c);
Boolean expectedValue = getConfiguredBooleanValueOrDefault('Permissions_Explorer_REST_API_Enabled', rflib_GlobalSettings.PERMISSIONS_EXPLORER_REST_API_ENABLED_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.useRestQueryModeForPermissionRetrievalOrDefault);
Expand All @@ -117,10 +117,28 @@ private class rflib_GlobalSettingsTest {

@IsTest
static void testUseDefaultWorkflowUserForLogEvents() {
Boolean expectedValue = Boolean.valueOf(rflib_Global_Setting__mdt.getInstance('Use_Default_Workflow_User_for_Log_Events').Value__c);
Boolean expectedValue = getConfiguredBooleanValueOrDefault('Use_Default_Workflow_User_for_Log_Events', rflib_GlobalSettings.USE_DEFAULT_WORKFLOW_USER_FOR_LOG_EVENTS_DEFAULT_VALUE);

Test.startTest();
System.assertEquals(expectedValue, rflib_GlobalSettings.useDefaultWorkflowUserForLogEventsOrDefault);
Test.stopTest();
}

private static String getConfiguredStringValueOrDefault(String settingName, String defaultValue) {
String value = rflib_Global_Setting__mdt.getInstance(settingName)?.Value__c;

return String.isBlank(value) ? defaultValue : value;
}

private static Integer getConfiguredIntegerValueOrDefault(String settingName, Integer defaultValue) {
String value = rflib_Global_Setting__mdt.getInstance(settingName)?.Value__c;

return String.isBlank(value) ? defaultValue : Integer.valueOf(value);
}

private static Boolean getConfiguredBooleanValueOrDefault(String settingName, Boolean defaultValue) {
String value = rflib_Global_Setting__mdt.getInstance(settingName)?.Value__c;

return String.isBlank(value) ? defaultValue : Boolean.valueOf(value);
}
}

0 comments on commit adb5fb6

Please sign in to comment.