Skip to content

Commit

Permalink
Added tests for execute methods in LoggerSObjectHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
jongpie committed Mar 16, 2022
1 parent 9a75da3 commit fe39523
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@ public without sharing abstract class LoggerSObjectHandler {
private static final Map<Schema.SObjectType, List<LoggerSObjectHandler>> SOBJECT_TYPE_TO_EXECUTED_HANDLERS = new Map<Schema.SObjectType, List<LoggerSObjectHandler>>();
private static final Map<Schema.SObjectType, List<LoggerPlugin__mdt>> SOBJECT_TYPE_TO_MOCK_PLUGIN_CONFIGURATIONS = new Map<Schema.SObjectType, List<LoggerPlugin__mdt>>();

@TestVisible
private TriggerOperation triggerOperationType;
@TestVisible
private List<SObject> triggerNew;
@TestVisible
private Map<Id, SObject> triggerNewMap;
@TestVisible
private Map<Id, SObject> triggerOldMap;
private SObjectHandlerInput input;
private List<LoggerPlugin__mdt> pluginConfigurations;
private List<LoggerPlugin__mdt> pluginConfigurations = new List<LoggerPlugin__mdt>();
private List<LoggerSObjectHandlerPlugin> executedApexPlugins = new List<LoggerSObjectHandlerPlugin>();

/**
* @description Default constructor
*/
public LoggerSObjectHandler() {
this.createSObjectHandlerInput();
this.triggerOperationType = Trigger.operationType;
this.triggerNew = Trigger.new;
this.triggerNewMap = Trigger.newMap;
this.triggerOldMap = Trigger.oldMap;

this.queryPluginConfigurations();
}

Expand Down Expand Up @@ -61,6 +73,8 @@ public without sharing abstract class LoggerSObjectHandler {
return;
}

this.createSObjectHandlerInput();

switch on this.input.triggerOperationType {
when BEFORE_INSERT {
this.executeBeforeInsert(this.input.triggerNew);
Expand Down Expand Up @@ -129,11 +143,10 @@ public without sharing abstract class LoggerSObjectHandler {

// Trigger variables for Apex Developers
input.sobjectType = this.getSObjectType();
input.triggerOperationType = Trigger.operationType;
input.triggerNew = Trigger.new;
input.triggerNewMap = Trigger.newMap;
input.triggerOld = Trigger.old;
input.triggerOldMap = Trigger.oldMap;
input.triggerOperationType = triggerOperationType;
input.triggerNew = triggerNew;
input.triggerNewMap = triggerNewMap;
input.triggerOldMap = triggerOldMap;

// Additional invocable variables for Flow Builders (and Apex Developers too, if they want to use them)
input.sobjectTypeName = this.getSObjectType().getDescribe().getName();
Expand All @@ -149,8 +162,8 @@ public without sharing abstract class LoggerSObjectHandler {

input.triggerRecords.add(recordInput);
}
} else if (input.triggerOld != null) {
for (SObject record : input.triggerOld) {
} else if (input.triggerOldMap != null) {
for (SObject record : input.triggerOldMap.values()) {
SObjectHandlerRecordInput recordInput = new SObjectHandlerRecordInput();
recordInput.triggerRecordOld = record;

Expand Down Expand Up @@ -221,7 +234,13 @@ public without sharing abstract class LoggerSObjectHandler {
apexPlugin.execute(configuration, this.input);

// TODO Legacy approach, remove in a future release
apexPlugin.execute(this.input.triggerOperationType, this.input.triggerNew, this.input.triggerNewMap, this.input.triggerOld, this.input.triggerOldMap);
apexPlugin.execute(
this.input.triggerOperationType,
this.input.triggerNew,
this.input.triggerNewMap,
this.input.triggerOldMap?.values(),
this.input.triggerOldMap
);

if (Test.isRunningTest() == true) {
this.executedApexPlugins.add(apexPlugin);
Expand All @@ -236,7 +255,7 @@ public without sharing abstract class LoggerSObjectHandler {
// TODO Legacy approach, remove in a future release
flowInputs.put('triggerOperationType', this.input.triggerOperationType?.name());
flowInputs.put('triggerNew', this.input.triggerNew);
flowInputs.put('triggerOld', this.input.triggerOld);
flowInputs.put('triggerOld', this.input.triggerOldMap?.values());

try {
Flow.Interview flowPlugin = Flow.Interview.createInterview(configuration.PluginApiName__c, flowInputs);
Expand All @@ -254,10 +273,10 @@ public without sharing abstract class LoggerSObjectHandler {
}
}

// Instance test helper methods
// Instance test helper methods, used for unit tests
@TestVisible
private void setMockInput(SObjectHandlerInput input) {
this.input = input;
private List<LoggerPlugin__mdt> getPluginConfigurations() {
return this.pluginConfigurations;
}

@TestVisible
Expand All @@ -270,7 +289,7 @@ public without sharing abstract class LoggerSObjectHandler {
return this.executedApexPlugins;
}

// Static test helper methods
// Static test helper methods, used for integration tests
@TestVisible
private static Map<Schema.SObjectType, List<LoggerSObjectHandler>> getExecutedHandlers() {
return SOBJECT_TYPE_TO_EXECUTED_HANDLERS;
Expand All @@ -292,7 +311,6 @@ public without sharing abstract class LoggerSObjectHandler {
public TriggerOperation triggerOperationType;
public List<SObject> triggerNew;
public Map<Id, SObject> triggerNewMap;
public List<SObject> triggerOld;
public Map<Id, SObject> triggerOldMap;

@InvocableVariable(label='SObject Type Name')
Expand Down
Loading

0 comments on commit fe39523

Please sign in to comment.