Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions source/classes/DatabaseLayer.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
*/
@SuppressWarnings('PMD.AvoidGlobalModifier')
global class DatabaseLayer {
public static final DatabaseLayerUtils UTILS;
private static final DatabaseLayer INSTANCE;
private static final DatabaseLayer INSTANCE = new DatabaseLayer();

private Dml currentDml;
private SoqlProvider currentSoql;

static {
INSTANCE = new DatabaseLayer();
UTILS = new DatabaseLayerUtils(INSTANCE);
}

private DatabaseLayer() {
this.currentDml = new Dml(this);
this.currentSoql = new SoqlProvider(this);
Expand Down Expand Up @@ -41,6 +35,19 @@ global class DatabaseLayer {
}
}

/**
* @description Provides access to utility methods for database operations and testing.
* @return Singleton instance of the DatabaseLayerUtils class
*/
@SuppressWarnings('PMD.PropertyNamingConventions')
public static final DatabaseLayerUtils Utils {
get {
DatabaseLayer.Utils = DatabaseLayer.Utils ?? new DatabaseLayerUtils(INSTANCE);
return DatabaseLayer.Utils;
}
private set;
}

/**
* @description Switches the DML provider to use mock implementation for testing.
* @return The mock DML instance that was configured
Expand Down
30 changes: 27 additions & 3 deletions source/classes/DatabaseLayerTestUtils.cls
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* @description Test utility class providing common functionality and spy implementations for framework testing.
*/
@SuppressWarnings('PMD.AvoidGlobalModifier')
@IsTest
public class DatabaseLayerTestUtils {
global class DatabaseLayerTestUtils {
// Note: These properties do not use traditional naming conventions to support a 'namespace-like' API
@SuppressWarnings('PMD.FieldNamingConventions')
public static final PreAndPostProcessorSpy DmlPluginSpy = new PreAndPostProcessorSpy();
Expand All @@ -11,14 +12,37 @@ public class DatabaseLayerTestUtils {

/**
* @description Creates a mock DatabaseLayerSetting__mdt for testing and injects it into the metadata selector.
* @param setting The custom metadata record being injected.
* @return The created test settings object
*/
public static DatabaseLayerSetting__mdt initSettings() {
DatabaseLayerSetting__mdt setting = new DatabaseLayerSetting__mdt();
global static DatabaseLayerSetting__mdt initSettings(DatabaseLayerSetting__mdt setting) {
DatabaseLayer.Utils.MetadataSelector.settings = new List<DatabaseLayerSetting__mdt>{ setting };
DatabaseLayer.Dml.initPlugins();
DatabaseLayer.Soql.initPlugins();
return setting;
}

/**
* @description Creates an empty DatabaseLayerSetting__mdt for testing and injects it into the metadata selector.
* @return The created test settings object
*/
global static DatabaseLayerSetting__mdt initSettings() {
return DatabaseLayerTestUtils.initSettings(new DatabaseLayerSetting__mdt());
}

/**
* @description Creates a DatabaseLayerSetting__mdt for testing with both DML and SOQL plugins configured to use the same class.
* @param className The name of the class to use for both DML and SOQL pre/post processing
* @return The created test settings object with plugin configuration
*/
global static DatabaseLayerSetting__mdt initDmlAndSoqlPlugins(String className) {
DatabaseLayerSetting__mdt setting = new DatabaseLayerSetting__mdt(
DmlPreAndPostProcessor__c = className,
SoqlPreAndPostProcessor__c = className
);
return DatabaseLayerTestUtils.initSettings(setting);
}

// **** INNER **** //
/**
* @description Spy implementation for tracking plugin method invocations during testing.
Expand Down
27 changes: 3 additions & 24 deletions source/classes/DmlTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ private class DmlTest {
// Custom metadata actually defined in the org should not interfere with this test:
DatabaseLayer.Utils.MetadataSelector.settings = new List<DatabaseLayerSetting__mdt>{};
// Initialize plugins
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
DatabaseLayer.Dml.initPlugins();
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(DatabaseLayerTestUtils.SamplePlugin.class.getName());
}

// **** TESTS **** //
Expand Down Expand Up @@ -874,10 +872,6 @@ private class DmlTest {

@IsTest
static void shouldRunPreAndPostProcessorPluginLogic() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
// Re-initialize the plugins, now that the field has been set:
DatabaseLayer.Dml.initPlugins();
Account account = DmlTest.initAccount();

Test.startTest();
Expand All @@ -896,10 +890,6 @@ private class DmlTest {

@IsTest
static void shouldRunPluginErrorLogic() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
// Re-initialize the plugins, now that the field has been set:
DatabaseLayer.Dml.initPlugins();
Account account = DmlTest.initAccount();

Test.startTest();
Expand All @@ -925,10 +915,7 @@ private class DmlTest {

@IsTest
static void shouldNotRunProcessorLogicIfInvalidApexClassDefined() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = 'an obviously invalid apex class name';
// Re-initialize the plugins, now that the field has been set:
DatabaseLayer.Dml.initPlugins();
DatabaseLayerTestUtils.initDmlAndSoqlPlugins('An obviously invalid apex class name');
Account account = DmlTest.initAccount();

Test.startTest();
Expand All @@ -947,9 +934,7 @@ private class DmlTest {

@IsTest
static void shouldNotRunProcessorLogicIfNoValueDefined() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = null;
DatabaseLayer.Dml.initPlugins();
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(null);
Account account = DmlTest.initAccount();

Test.startTest();
Expand Down Expand Up @@ -1044,12 +1029,6 @@ private class DmlTest {
return leadToConvert;
}

private static DatabaseLayerSetting__mdt initSettings() {
DatabaseLayerSetting__mdt settings = new DatabaseLayerSetting__mdt();
DatabaseLayer.Utils.MetadataSelector.settings?.add(settings);
return settings;
}

// **** INNER **** //
private class DeleteCallback extends DataSource.AsyncDeleteCallback {
public override void processDelete(Database.DeleteResult result) {
Expand Down
21 changes: 3 additions & 18 deletions source/classes/MockDmlTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ private class MockDmlTest {
// Use Mock DML:
DatabaseLayer.useMockDml();
// Initialize plugins:
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
DatabaseLayer.Dml.initPlugins();
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(DatabaseLayerTestUtils.SamplePlugin.class.getName());
}

// **** TESTS **** //
Expand Down Expand Up @@ -905,10 +903,6 @@ private class MockDmlTest {

@IsTest
static void shouldRunPreAndPostProcessorPluginLogic() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
// Re-initialize the plugins, now that the field has been set:
DatabaseLayer.Dml.initPlugins();
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);

Test.startTest();
Expand All @@ -927,10 +921,6 @@ private class MockDmlTest {

@IsTest
static void shouldRunPluginErrorLogic() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
// Re-initialize the plugins, now that the field has been set:
DatabaseLayer.Dml.initPlugins();
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);
MockDml.shouldFail();

Expand All @@ -957,10 +947,7 @@ private class MockDmlTest {

@IsTest
static void shouldNotRunProcessorLogicIfInvalidApexClassDefined() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = 'an obviously invalid apex class name';
// Re-initialize the plugins, now that the field has been set:
DatabaseLayer.Dml.initPlugins();
DatabaseLayerTestUtils.initDmlAndSoqlPlugins('An obviously invalid apex class name');
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);

Test.startTest();
Expand All @@ -979,9 +966,7 @@ private class MockDmlTest {

@IsTest
static void shouldNotRunProcessorLogicIfNoValueDefined() {
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
settings.DmlPreAndPostProcessor__c = null;
DatabaseLayer.Dml.initPlugins();
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(null);
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);

Test.startTest();
Expand Down
Loading