From ee0a8f4ff75704c792c4b5ee82cfb5a161c44392 Mon Sep 17 00:00:00 2001 From: Jonathan Gillespie Date: Sat, 14 Sep 2024 16:38:03 -0400 Subject: [PATCH] [WIP] --- .../logger-engine/classes/CallableLogger.cls | 12 +- .../classes/CallableLogger_Tests.cls | 117 +++++++++--------- 2 files changed, 69 insertions(+), 60 deletions(-) diff --git a/nebula-logger/core/main/logger-engine/classes/CallableLogger.cls b/nebula-logger/core/main/logger-engine/classes/CallableLogger.cls index 4b0f78b48..2a554e126 100644 --- a/nebula-logger/core/main/logger-engine/classes/CallableLogger.cls +++ b/nebula-logger/core/main/logger-engine/classes/CallableLogger.cls @@ -24,6 +24,7 @@ global without sharing class CallableLogger implements System.Callable { private static final String ARGUMENT_RECORD = 'record'; private static final String ARGUMENT_RECORD_LIST = 'recordList'; private static final String ARGUMENT_RECORD_MAP = 'recordMap'; + private static final String ARGUMENT_SAVE_LOG = 'saveLog'; private static final String ARGUMENT_SAVE_METHOD_NAME = 'saveMethodName'; private static final String ARGUMENT_TAGS = 'tags'; private static final String ARGUMENT_TRANSACTION_ID = 'transactionId'; @@ -36,6 +37,9 @@ global without sharing class CallableLogger implements System.Callable { private static final String OUTPUT_ARGUMENT_CALL_EXCEPTION_TYPE = 'exceptionType'; private static final String OUTPUT_ARGUMENT_CALL_IS_SUCCESS = 'isSuccess'; + @TestVisible + private static Boolean returnLogEntryEventBuilderInOutput = false; + /** * @description The one method required by the interface `System.Callable` description. It provides a `String`-based way to dynamically call Nebula Logger's code. * @param action The `String` name of the `Logger` method to call. The supported actions are @@ -87,7 +91,13 @@ global without sharing class CallableLogger implements System.Callable { } // Methods for adding & saving log entries when 'newEntry' { - output.put(ARGUMENT_LOG_ENTRY_EVENT_BUILDER, this.newEntry(input)); + LogEntryEventBuilder builder = this.newEntry(input); + if (returnLogEntryEventBuilderInOutput) { + output.put(ARGUMENT_LOG_ENTRY_EVENT_BUILDER, builder); + } + if (input.get(ARGUMENT_SAVE_LOG) == true) { + this.saveLog(input); + } } when 'saveLog' { this.saveLog(input); diff --git a/nebula-logger/core/tests/logger-engine/classes/CallableLogger_Tests.cls b/nebula-logger/core/tests/logger-engine/classes/CallableLogger_Tests.cls index d89fedda2..68ed9bbc1 100644 --- a/nebula-logger/core/tests/logger-engine/classes/CallableLogger_Tests.cls +++ b/nebula-logger/core/tests/logger-engine/classes/CallableLogger_Tests.cls @@ -6,6 +6,10 @@ @SuppressWarnings('PMD.ApexDoc, PMD.ApexAssertionsShouldIncludeMessage, PMD.MethodNamingConventions') @IsTest(IsParallel=true) private class CallableLogger_Tests { + static { + CallableLogger.returnLogEntryEventBuilderInOutput = true; + } + @IsTest static void it_returns_exception_message_for_unsupported_action_when_using_standard_approach() { String fakeActionName = 'some-action-that-will-never-exist-i-hope'; @@ -226,7 +230,7 @@ private class CallableLogger_Tests { } @IsTest - static void it_adds_new_entry_using_standard_approach() { + static void it_adds_new_entry_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; @@ -244,7 +248,7 @@ private class CallableLogger_Tests { } @IsTest - static void it_adds_new_entry_using_omnistudio_approach() { + static void it_adds_new_entry_when_using_omnistudio_approach() { String loggingLevelName = System.LoggingLevel.FINE.name(); String message = 'some log entry message'; Map omnistudioInput = new Map{ 'loggingLevel' => loggingLevelName, 'message' => message }; @@ -260,19 +264,22 @@ private class CallableLogger_Tests { System.Assert.areEqual(message, logEntryEvent.Message__c); } + // For the other newEntry() tests, just test the standard approach - the omnistudio approach will use the same parameters/inputs @IsTest - static void it_adds_new_entry_with_exception() { + static void it_adds_new_entry_with_exception_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; System.Exception apexException = new System.DmlException('whoops'); System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance - .call('newEntry', new Map{ 'exception' => apexException, 'loggingLevel' => loggingLevel, 'message' => message }); + Map returnedOutput = (Map) callableLoggerInstance.call( + 'newEntry', + new Map{ 'exception' => apexException, 'loggingLevel' => loggingLevel, 'message' => message } + ); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); System.Assert.areEqual(apexException.getMessage(), logEntryEvent.ExceptionMessage__c); @@ -280,90 +287,92 @@ private class CallableLogger_Tests { } @IsTest - static void it_adds_new_entry_with_record_id() { + static void it_adds_new_entry_with_record_id_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; Id recordId = System.UserInfo.getUserId(); System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance - .call('newEntry', new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'recordId' => recordId }); + Map returnedOutput = (Map) callableLoggerInstance.call( + 'newEntry', + new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'recordId' => recordId } + ); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); System.Assert.areEqual(recordId, logEntryEvent.RecordId__c); } @IsTest - static void it_adds_new_entry_with_record() { + static void it_adds_new_entry_with_record_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; SObject record = new Schema.User(Id = System.UserInfo.getUserId()); System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance + Map returnedOutput = (Map) callableLoggerInstance .call('newEntry', new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'record' => record }); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); System.Assert.areEqual(System.JSON.serializePretty(record), logEntryEvent.RecordJson__c); } @IsTest - static void it_adds_new_entry_with_record_list() { + static void it_adds_new_entry_with_record_list_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; List recordList = new List{ new Schema.User(Id = System.UserInfo.getUserId()) }; System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance + Map returnedOutput = (Map) callableLoggerInstance .call('newEntry', new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'recordList' => recordList }); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); System.Assert.areEqual(System.JSON.serializePretty(recordList), logEntryEvent.RecordJson__c); } @IsTest - static void it_adds_new_entry_with_record_map() { + static void it_adds_new_entry_with_record_map_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; Map recordMap = new Map{ System.UserInfo.getUserId() => new Schema.User(Id = System.UserInfo.getUserId()) }; System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance + Map returnedOutput = (Map) callableLoggerInstance .call('newEntry', new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'recordMap' => recordMap }); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); System.Assert.areEqual(System.JSON.serializePretty(recordMap), logEntryEvent.RecordJson__c); } @IsTest - static void it_adds_new_entry_with_tags() { + static void it_adds_new_entry_with_tags_when_using_standard_approach() { System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; List tags = new List{ 'some-tag', 'another-tag' }; System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance + Map returnedOutput = (Map) callableLoggerInstance .call('newEntry', new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'tags' => tags }); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); tags.sort(); // Tags are auto-sorted by LogEntryEventBuilder @@ -371,20 +380,24 @@ private class CallableLogger_Tests { } @IsTest - static void it_adds_new_entry_when_using_omnistudio_approach() { - // TODO revisit / delete? - String loggingLevelName = System.LoggingLevel.FINE.name(); + static void it_adds_new_entry_and_saves_when_using_standard_approach() { + System.LoggingLevel loggingLevel = System.LoggingLevel.FINE; String message = 'some log entry message'; - Map omnistudioInput = new Map{ 'loggingLevel' => loggingLevelName, 'message' => message }; + Boolean saveLog = true; + System.Assert.areEqual(0, Logger.getBufferSize()); + System.Assert.isNull(Logger.lastSaveMethodNameUsed); System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance.call('newEntry', new Map{ 'input' => omnistudioInput }); + Map returnedOutput = (Map) callableLoggerInstance + .call('newEntry', new Map{ 'loggingLevel' => loggingLevel, 'message' => message, 'saveLog' => saveLog }); - System.Assert.isNotNull(returnedValue); - System.Assert.isInstanceOfType(returnedValue, LogEntryEventBuilder.class); - LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedValue).getLogEntryEvent(); - System.Assert.areEqual(loggingLevelName, logEntryEvent.LoggingLevel__c); + System.Assert.areEqual(2, returnedOutput.size()); + System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + LogEntryEvent__e logEntryEvent = ((LogEntryEventBuilder) returnedOutput.get('logEntryEventBuilder')).getLogEntryEvent(); + System.Assert.areEqual(loggingLevel.name(), logEntryEvent.LoggingLevel__c); System.Assert.areEqual(message, logEntryEvent.Message__c); + System.Assert.areEqual(0, Logger.getBufferSize()); + System.Assert.areEqual(Logger.getUserSettings().DefaultSaveMethod__c, Logger.lastSaveMethodNameUsed); } @IsTest @@ -398,6 +411,7 @@ private class CallableLogger_Tests { System.Assert.areEqual(1, returnedOutput.size()); System.Assert.areEqual(true, returnedOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + returnedOutput); + System.Assert.areEqual(0, Logger.getBufferSize()); System.Assert.areEqual(Logger.getUserSettings().DefaultSaveMethod__c, Logger.lastSaveMethodNameUsed); } @@ -413,6 +427,7 @@ private class CallableLogger_Tests { System.Assert.areEqual(1, omnistudioOutput.size(), omnistudioOutput.toString()); System.Assert.areEqual(true, omnistudioOutput.get('isSuccess'), 'Expected isSuccess == true. Output received: ' + omnistudioOutput); + System.Assert.areEqual(0, Logger.getBufferSize()); System.Assert.areEqual(Logger.getUserSettings().DefaultSaveMethod__c, Logger.lastSaveMethodNameUsed); } @@ -453,20 +468,4 @@ private class CallableLogger_Tests { System.Assert.areEqual(0, Logger.getBufferSize()); System.Assert.areEqual(targetSaveMethodName, Logger.lastSaveMethodNameUsed); } - - @IsTest - static void it_adds_and_saves_new_entry_when_using_omnistudio_approach() { - // TODO revisit / delete? - System.Assert.isNull(Logger.lastSaveMethodNameUsed); - String loggingLevelName = System.LoggingLevel.FINE.name(); - String message = 'some log entry message'; - Map omnistudioInput = new Map{ 'loggingLevel' => loggingLevelName, 'message' => message, 'saveLog' => true }; - - System.Callable callableLoggerInstance = (System.Callable) System.Type.forName('CallableLogger').newInstance(); - Object returnedValue = callableLoggerInstance.call('newEntry', new Map{ 'input' => omnistudioInput }); - - System.Assert.isNull(returnedValue, 'No return value expected for Logger.saveLog(), received ' + returnedValue); - System.Assert.areEqual(0, Logger.getBufferSize()); - System.Assert.areEqual(Logger.getUserSettings().DefaultSaveMethod__c, Logger.lastSaveMethodNameUsed); - } }