Skip to content

Commit

Permalink
Moved LoggerTestUtils, added public methods for mocking all CMDT records
Browse files Browse the repository at this point in the history
  • Loading branch information
jongpie committed Mar 27, 2022
1 parent 54b1c0f commit 5bc2e2e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. //
//------------------------------------------------------------------------------------------------//

@SuppressWarnings('PMD.MethodNamingConventions, PMD.PropertyNamingConventions')
/**
* @group Configuration
* @description Utilities for Apex tests for commonly used data & settings.
* These methods can be used by plugins' tests.
*/
@SuppressWarnings('PMD.CyclomaticComplexity, PMD.ExcessivePublicCount, PMD.MethodNamingConventions, PMD.PropertyNamingConventions')
@IsTest
public class LoggerTestUtils {
private static final String FAKE_DATA_STRING = 'someFakeData';
Expand Down Expand Up @@ -449,6 +454,55 @@ public class LoggerTestUtils {
return loggerQueue;
}

/**
* @description Loads the mock `LogEntryDataMaskRule__mdt` during test execution
* @param mock The mock instance of `LogEntryDataMaskRule__mdt` to load
*/
public static void setMock(LogEntryDataMaskRule__mdt mock) {
LogEntryEventBuilder.setMockDataMaskRule(mock);
}

/**
* @description Loads the mock `LogEntryTagRule__mdt` during test execution
* @param mock The mock instance of `LogEntryTagRule__mdt` to load
*/
public static void setMock(LogEntryTagRule__mdt mock) {
LogEntryEventHandler.TAG_ASSIGNMENT_RULES.add(mock);
}

/**
* @description Loads the mock `LoggerParameter__mdt` during test execution
* @param mock The mock instance of `LoggerParameter__mdt` to load
*/
public static void setMock(LoggerParameter__mdt mock) {
LoggerParameter.setMock(mock);
}

/**
* @description Loads the mock `LoggerPlugin__mdt` during test execution
* @param mock The mock instance of `LoggerPlugin__mdt` to load
*/
public static void setMock(LoggerPlugin__mdt mock) {
LoggerPlugin.setMock(mock);
}

/**
* @description Loads the mock `LogScenarioRule__mdt` during test execution
* @param mock The mock instance of `LogScenarioRule__mdt` to load
*/
public static void setMock(LogScenarioRule__mdt mock) {
Logger.setMockScenarioRule(mock);
LogHandler.setMockScenarioRule(mock);
}

/**
* @description Loads the mock `LogStatus__mdt` during test execution
* @param mock The mock instance of `LogStatus__mdt` to load
*/
public static void setMock(LogStatus__mdt mock) {
LogHandler.setMockLogStatus(mock);
}

// Helper methods
private static void assignPermissionSet(Id userId, PermissionSet permissionSet) {
PermissionSetAssignment permissionSetAssignment = new PermissionSetAssignment(AssigneeId = userId, PermissionSetId = permissionSet.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
@SuppressWarnings('PMD.AvoidGlobalModifier')
global with sharing class LogBatchPurger implements Database.Batchable<SObject>, Database.Stateful {
private static final List<LoggerPlugin__mdt> MOCK_PLUGIN_CONFIGURATIONS = new List<LoggerPlugin__mdt>();
@TestVisible
private static final String NO_DELETE_ACCESS_EXCEPTION_MESSAGE = 'User does not have access to delete logs';

Expand Down
6 changes: 3 additions & 3 deletions nebula-logger/core/main/log-management/classes/LogHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
@SuppressWarnings('PMD.ApexCrudViolation, PMD.CognitiveComplexity, PMD.CyclomaticComplexity')
public without sharing class LogHandler extends LoggerSObjectHandler {
private static final Map<String, LogStatus__mdt> MOCK_LOG_STATUS_LABEL_TO_STATUS = new Map<String, LogStatus__mdt>();
private static final Map<String, LogStatus__mdt> MOCK_LOG_STATUS_TO_STATUS = new Map<String, LogStatus__mdt>();
private static final Map<String, LogScenarioRule__mdt> MOCK_SCENARIO_TO_SCENARIO_RULE = new Map<String, LogScenarioRule__mdt>();

@TestVisible
Expand Down Expand Up @@ -205,7 +205,7 @@ public without sharing class LogHandler extends LoggerSObjectHandler {

if (System.Test.isRunningTest() == true) {
logStatusNameToStatus.clear();
logStatusNameToStatus.putAll(MOCK_LOG_STATUS_LABEL_TO_STATUS);
logStatusNameToStatus.putAll(MOCK_LOG_STATUS_TO_STATUS);
}

return logStatusNameToStatus;
Expand Down Expand Up @@ -254,7 +254,7 @@ public without sharing class LogHandler extends LoggerSObjectHandler {

@TestVisible
private static void setMockLogStatus(LogStatus__mdt logStatus) {
MOCK_LOG_STATUS_LABEL_TO_STATUS.put(logStatus.MasterLabel, logStatus);
MOCK_LOG_STATUS_TO_STATUS.put(logStatus.MasterLabel, logStatus);
}

@TestVisible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public without sharing class LoggerSettingsController {
private static final String CUSTOM_LOG_PURGE_ACTION_PREFIX = 'CustomLogPurgeAction';
@TestVisible
private static final String DELETE_LOG_PURGE_ACTION = 'Delete';
private static final Map<String, LoggerParameter__mdt> MOCK_CUSTOM_LOG_PURGE_ACTION_PARAMETERS = new Map<String, LoggerParameter__mdt>();
private static final Map<String, LoggerParameter__mdt> MOCK_CUSTOM_SAVE_METHOD_PARAMETERS = new Map<String, LoggerParameter__mdt>();

// Data methods
/**
Expand Down
6 changes: 3 additions & 3 deletions nebula-logger/core/main/logger-engine/classes/Logger.cls
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ global with sharing class Logger {
private static final String CURRENT_VERSION_NUMBER = 'v4.7.1';
private static final LoggingLevel DEFAULT_LOGGING_LEVEL = LoggingLevel.DEBUG;
private static final List<LogEntryEventBuilder> LOG_ENTRIES_BUFFER = new List<LogEntryEventBuilder>();
private static final Map<String, LogScenarioRule__mdt> SCENARIO_TO_MOCK_SCENARIO_RULE = new Map<String, LogScenarioRule__mdt>();
private static final Map<String, LogScenarioRule__mdt> MOCK_SCENARIO_TO_SCENARIO_RULE = new Map<String, LogScenarioRule__mdt>();
private static final String TRANSACTION_ID = setTransactionId();
private static final Quiddity TRANSACTION_QUIDDITY = setTransactionQuiddity();

Expand Down Expand Up @@ -2490,7 +2490,7 @@ global with sharing class Logger {
}

if (System.Test.isRunningTest() == true) {
matchingScenarioRule = SCENARIO_TO_MOCK_SCENARIO_RULE.get(scenario);
matchingScenarioRule = MOCK_SCENARIO_TO_SCENARIO_RULE.get(scenario);
}

if (matchingScenarioRule != null) {
Expand All @@ -2500,7 +2500,7 @@ global with sharing class Logger {

@TestVisible
private static void setMockScenarioRule(LogScenarioRule__mdt scenarioRule) {
SCENARIO_TO_MOCK_SCENARIO_RULE.put(scenarioRule.Scenario__c, scenarioRule);
MOCK_SCENARIO_TO_SCENARIO_RULE.put(scenarioRule.Scenario__c, scenarioRule);
}

/**
Expand Down

0 comments on commit 5bc2e2e

Please sign in to comment.