Skip to content

Commit

Permalink
Changed approach for creating LogScenario__c records - now, only Uniq…
Browse files Browse the repository at this point in the history
…ueId__c is required, and Name is auto-populated (when null) to a truncated version of UniqueId__c
  • Loading branch information
jongpie committed Aug 31, 2022
1 parent d99038b commit be018d2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ public without sharing class LogEntryEventHandler extends LoggerSObjectHandler {
continue;
}

LogScenario__c logScenario = new LogScenario__c(Name = logEntryEvent.Scenario__c);
logScenario.UniqueId__c = LogScenarioHandler.generateUniqueId(logScenario);

LogScenario__c logScenario = new LogScenario__c(UniqueId__c = logEntryEvent.Scenario__c);
SCENARIO_UNIQUE_ID_TO_LOG_SCENARIO.put(logScenario.UniqueId__c, logScenario);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@
* @description Handles trigger events for the `LogScenario__c` object
*/
public without sharing class LogScenarioHandler extends LoggerSObjectHandler {
private static final Map<String, Log__c> TRANSACTION_ID_TO_LOG = new Map<String, Log__c>();

@TestVisible
private List<LogScenario__c> logScenarios;

/**
* @description Generates a unique key for the `LogScenario__c` record, which
* is used to set the field `LogScenario__c.UniqueId__c`
* @param logScenario The `LogScenario__c` record to use for generating a unique ID
* @return The unique key for the record
*/
public static String generateUniqueId(LogScenario__c logScenario) {
return logScenario.Name?.trim();
}

/**
* @description Returns SObject Type that the handler is responsible for processing
* @return The instance of `SObjectType`
Expand All @@ -34,18 +22,16 @@ public without sharing class LogScenarioHandler extends LoggerSObjectHandler {
protected override void executeBeforeInsert(List<SObject> triggerNew) {
this.logScenarios = (List<LogScenario__c>) triggerNew;

this.setUniqueIdField();
}

protected override void executeBeforeUpdate(Map<Id, SObject> triggerNewMap, Map<Id, SObject> triggerOldMap) {
this.logScenarios = (List<LogScenario__c>) triggerNewMap.values();

this.setUniqueIdField();
this.setNameField();
}

private void setUniqueIdField() {
private void setNameField() {
for (LogScenario__c logScenario : this.logScenarios) {
logScenario.UniqueId__c = logScenario.Name;
if (String.isBlank(logScenario.Name) == true && String.isNotBlank(logScenario.UniqueId__c) == true) {
logScenario.Name = logScenario.UniqueId__c;
}
Integer nameFieldMaxLength = Schema.LogScenario__c.Name.getDescribe().getLength();
logScenario.Name = logScenario.UniqueId__c.left(nameFieldMaxLength);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@
</nameField>
<pluralLabel>Log Scenarios</pluralLabel>
<searchLayouts>
<excludedStandardButtons>PrintableListView</excludedStandardButtons>
<excludedStandardButtons>Accept</excludedStandardButtons>
<excludedStandardButtons>PrintableListView</excludedStandardButtons>
<excludedStandardButtons>Import</excludedStandardButtons>
<searchResultsAdditionalFields>UniqueId__c</searchResultsAdditionalFields>
<searchResultsAdditionalFields>OWNER.ALIAS</searchResultsAdditionalFields>
<searchResultsAdditionalFields>CREATED_DATE</searchResultsAdditionalFields>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ListView xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>All</fullName>
<columns>NAME</columns>
<columns>UniqueId__c</columns>
<columns>OWNER.ALIAS</columns>
<columns>CREATEDBY_USER</columns>
<columns>CREATED_DATE</columns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ListView xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>MyAssignedLogScenarios</fullName>
<columns>NAME</columns>
<columns>UniqueId__c</columns>
<columns>OWNER.ALIAS</columns>
<columns>CREATEDBY_USER</columns>
<columns>CREATED_DATE</columns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private class LogEntryEventHandler_Tests {
LoggerTestConfigurator.getSObjectHandlerConfiguration(Schema.Log__c.SObjectType).IsEnabled__c = false;
LoggerTestConfigurator.getSObjectHandlerConfiguration(Schema.LogEntry__c.SObjectType).IsEnabled__c = false;
LogEntryEvent__e logEntryEvent = createLogEntryEvent();
logEntryEvent.Scenario__c = 'some scenario';
logEntryEvent.Scenario__c = '0'.repeat(Schema.LogEntryEvent__e.Scenario__c.getDescribe().getLength());

Database.SaveResult saveResult = LoggerMockDataStore.getEventBus().publishRecord(logEntryEvent);
LoggerMockDataStore.getEventBus().deliver(new LogEntryEventHandler());
Expand All @@ -187,7 +187,7 @@ private class LogEntryEventHandler_Tests {
LoggerTestConfigurator.setupMockSObjectHandlerConfigurations();
LoggerTestConfigurator.getSObjectHandlerConfiguration(Schema.Log__c.SObjectType).IsEnabled__c = false;
LoggerTestConfigurator.getSObjectHandlerConfiguration(Schema.LogEntry__c.SObjectType).IsEnabled__c = false;
LogScenario__c existingLogScenario = new LogScenario__c(Name = 'hello, world');
LogScenario__c existingLogScenario = new LogScenario__c(UniqueId__c = 'hello, world');
insert existingLogScenario;
System.assertEquals(1, [SELECT COUNT() FROM LogScenario__c]);
LogEntryEvent__e firstLogEntryEvent = createLogEntryEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ private class LogScenarioHandler_Tests {
System.assertEquals(Schema.LogScenario__c.SObjectType, new LogScenarioHandler().getSObjectType());
}

@IsTest
static void it_generates_a_unique_id() {
LogScenario__c logScenario = new LogScenario__c(Name = 'Some scenario name');
String expectedUniqueId = logScenario.Name.trim();

String generatedUniqueId = LogScenarioHandler.generateUniqueId(logScenario);

System.assertEquals(expectedUniqueId, generatedUniqueId);
}

@IsTest
static void it_should_not_run_when_disabled_via_configuration() {
LoggerTestConfigurator.setupMockSObjectHandlerConfigurations();
Expand All @@ -39,11 +29,8 @@ private class LogScenarioHandler_Tests {
}

@IsTest
static void it_should_set_unique_id_on_insert() {
LogScenario__c logScenario = (LogScenario__c) LoggerMockDataCreator.createDataBuilder(Schema.LogScenario__c.SObjectType)
.populateRequiredFields()
.getRecord();
logScenario.Name = 'Some log scenario name';
static void it_should_set_name_on_insert_when_null() {
LogScenario__c logScenario = new LogScenario__c(UniqueId__c = 'Some scenario');
LoggerDataStore.getDatabase().insertRecord(logScenario);

System.assertEquals(
Expand All @@ -56,36 +43,36 @@ private class LogScenarioHandler_Tests {
}

@IsTest
static void it_should_set_unique_id_on_update() {
LogScenario__c logScenario = (LogScenario__c) LoggerMockDataCreator.createDataBuilder(Schema.LogScenario__c.SObjectType)
.populateRequiredFields()
.getRecord();
logScenario.Name = 'Some log scenario name';
static void it_should_truncate_name_on_insert_when_name_value_exceeds_name_field_length() {
Integer nameFieldMaxLength = Schema.LogScenario__c.Name.getDescribe().getLength();
LogScenario__c logScenario = new LogScenario__c(Name = '0'.repeat(nameFieldMaxLength + 1), UniqueId__c = 'Some scenario');
System.assertEquals(true, logScenario.Name.length() > nameFieldMaxLength);
LoggerDataStore.getDatabase().insertRecord(logScenario);

System.assertEquals(
2,
LoggerSObjectHandler.getExecutedHandlers().get(Schema.LogScenario__c.SObjectType).size(),
'Handler class should have executed two times - once for BEFORE_INSERT and once for AFTER_INSERT'
);
String expectedTruncatedValue = logScenario.Name.left(nameFieldMaxLength);
logScenario = [SELECT Id, Name, UniqueId__c FROM LogScenario__c WHERE Id = :logScenario.Id];
System.assertEquals(logScenario.Name, logScenario.UniqueId__c);
System.assertEquals(expectedTruncatedValue, logScenario.Name);
}

@IsTest
static void it_should_not_insert_duplicate_tag() {
static void it_should_not_allow_duplicate_scenario_to_be_inserted() {
LogScenario__c logScenario = (LogScenario__c) LoggerMockDataCreator.createDataBuilder(Schema.LogScenario__c.SObjectType)
.populateRequiredFields()
.getRecord();
logScenario.Name = 'Some log scenario name';
LoggerDataStore.getDatabase().insertRecord(logScenario);
LogScenario__c duplicateTag = (LogScenario__c) LoggerMockDataCreator.createDataBuilder(new LogScenario__c(Name = logScenario.Name))
LogScenario__c duplicateScenario = (LogScenario__c) LoggerMockDataCreator.createDataBuilder(new LogScenario__c(Name = logScenario.Name))
.populateRequiredFields()
.getRecord();
Exception thrownException;

try {
insert duplicateTag;
insert duplicateScenario;
System.assert(false, 'Exception expected on previous line');
} catch (Exception ex) {
thrownException = ex;
Expand Down

0 comments on commit be018d2

Please sign in to comment.