diff --git a/rflib/main/default/classes/rflib_GlobalSettings.cls b/rflib/main/default/classes/rflib_GlobalSettings.cls index 8bf8521d..181b42e9 100644 --- a/rflib/main/default/classes/rflib_GlobalSettings.cls +++ b/rflib/main/default/classes/rflib_GlobalSettings.cls @@ -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); } } diff --git a/rflib/test/default/classes/rflib_GlobalSettingsTest.cls b/rflib/test/default/classes/rflib_GlobalSettingsTest.cls index fd798d46..93a505cd 100644 --- a/rflib/test/default/classes/rflib_GlobalSettingsTest.cls +++ b/rflib/test/default/classes/rflib_GlobalSettingsTest.cls @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); + } }