Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Commit

Permalink
Added logging support for Database.MergeResult (#13)
Browse files Browse the repository at this point in the history
* Added methods for logging Database.MergeResult instances

* Fixed invalid type name from copy/paste

* Added test method for single instance of MergeResult

* Use correct variable name

* Remember that indexes start at zero you absolute noob

* Test to see if we get a null error

* Added null check in formatDatabaseErrors

* Added test for multiple Database.MergeResult instances

* Forgot to add @istest to test method

* Removed really long classes rule
  • Loading branch information
davidsbond authored Jan 25, 2019
1 parent 4d1dc21 commit 5d59c87
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 6 deletions.
6 changes: 0 additions & 6 deletions pmd.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Default ruleset used by the CodeClimate Engine for Salesforce.com Apex" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Default ruleset used by the Code Climate Engine for Salesforce.com Apex</description>
<rule ref="category/apex/design.xml/ExcessiveClassLength" message="Avoid really long classes (lines of code)">
<priority>3</priority>
<properties>
<property name="minimum" value="1000" />
</properties>
</rule>
<rule ref="category/apex/design.xml/ExcessiveParameterList" message="Avoid long parameter lists">
<priority>3</priority>
<properties>
Expand Down
67 changes: 67 additions & 0 deletions src/ForceLog.cls
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,15 @@ public with sharing class ForceLog {
return this.withResult('result', res);
}

/**
* @description Adds a single instance of Database.MergeResult to the log
* @param {Database.MergeResult} res The merge result to log
* @return {Logger} The current instance of the logger, for method chaining.
*/
public Logger withResult(Database.MergeResult res) {
return this.withResult('result', res);
}

/**
* @description Adds a single instance of Database.DeleteResult to the log under
* a given key
Expand Down Expand Up @@ -664,6 +673,26 @@ public with sharing class ForceLog {
return this.withField(key, entry);
}

/**
* @description Adds a single instance of Database.MergeResult to the log under
* a given key
* @param {String} key The key where the save result will be nested in the log.
* @param {Database.MergeResult} res The database result to log
* @return {Logger} The current instance of the logger, for method chaining.
*/
public Logger withResult(String key, Database.MergeResult res) {
Map<String, Object> entry = new Map<String, Object>{
'id' => res.getId(),
'errors' => this.formatDatabaseErrors(res.getErrors()),
'success' => res.isSuccess(),
'merged_record_ids' => res.getMergedRecordIds(),
'updated_related_ids' => res.getUpdatedRelatedIds(),
'type' => 'merge'
};

return this.withField(key, entry);
}

/**
* @description Adds a single instance of Messaging.SendEmailResult to the log
* @param {String} key The key where the save result will be nested in the log.
Expand Down Expand Up @@ -707,6 +736,15 @@ public with sharing class ForceLog {
return this.withResults('results', results);
}

/**
* @description Adds a list of Database.MergeResult to the log data.
* @param {List<Database.MergeResult>} results The database results to log
* @return {Logger} The current instance of the logger, for method chaining.
*/
public Logger withResults(List<Database.MergeResult> results) {
return this.withResults('results', results);
}

/**
* @description Adds a list of Messaging.SendEmailResult to the log data.
* @param {List<Messaging.SendEmailResult>} results The email results to log
Expand Down Expand Up @@ -736,6 +774,28 @@ public with sharing class ForceLog {
return this.withField(key, entry);
}

/**
* @description Adds a list of Database.MergeResult to the log data.
* @param {List<Database.MergeResult>} results The database results to log
* @return {Logger} The current instance of the logger, for method chaining.
*/
public Logger withResults(String key, List<Database.MergeResult> results) {
List<Object> entry = new List<Object>();

for (Database.MergeResult res : results) {
entry.add(new Map<String, Object>{
'id' => res.getId(),
'errors' => this.formatDatabaseErrors(res.getErrors()),
'success' => res.isSuccess(),
'merged_record_ids' => res.getMergedRecordIds(),
'updated_related_ids' => res.getUpdatedRelatedIds(),
'type' => 'merge'
});
}

return this.withField(key, entry);
}

/**
* @description Adds a list of Database.UpsertResult to the log data under
* a given key.
Expand Down Expand Up @@ -811,6 +871,13 @@ public with sharing class ForceLog {
private List<Object> formatDatabaseErrors(List<Database.Error> errs) {
List<Object> output = new List<Object>();

// All the other Database.*Result classes return an empty list of
// errors. However, MergeResult returns null, so we need a null
// check here now.
if (errs == null) {
return output;
}

for (Database.Error err : errs) {
output.add(new Map<String, Object> {
'fields' => err.getFields(),
Expand Down
66 changes: 66 additions & 0 deletions src/tests/ForceLogTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,71 @@ private class ForceLogTest {
}

@isTest
private static void itShouldAddMergeResults() {
TestLogger logger = new TestLogger('test');
logger.expect('level', 'info');
logger.expect('message', 'test');
logger.expect('timestamp', '*');
logger.expect('name', 'test');

// Create two accounts to act as a duplicate
List<Account> accounts = new List<Account> {
new Account(Name='Test Account 1'),
new Account(Name='Test Account 2')
};

insert accounts;

// Create a contact to be related to a single account
Contact con = new Contact(FirstName='Test', LastName='McTest', AccountId=accounts.get(0).Id);
insert con;

Database.MergeResult result = Database.merge(accounts.get(0), accounts.get(1));

logger.expect('results', new List<Map<String, Object>> {
new Map<String, Object> {
'id' => result.getId(),
'errors' => new List<Object>(),
'success' => result.isSuccess(),
'merged_record_ids' => result.getMergedRecordIds(),
'updated_related_ids' => result.getUpdatedRelatedIds(),
'type' => 'merge'
}
});

logger.withResults(new List<Database.MergeResult> { result }).info('test');
}

@isTest
private static void itShouldAddAMergeResult() {
TestLogger logger = new TestLogger('test');

// Create two accounts to act as a duplicate
List<Account> accounts = new List<Account> {
new Account(Name='Test Account 1'),
new Account(Name='Test Account 2')
};

insert accounts;

// Create a contact to be related to a single account
Contact con = new Contact(FirstName='Test', LastName='McTest', AccountId=accounts.get(0).Id);
insert con;

Database.MergeResult result = Database.merge(accounts.get(0), accounts.get(1));

logger.expect('result', new Map<String, Object> {
'id' => result.getId(),
'errors' => new List<Object>(),
'success' => result.isSuccess(),
'merged_record_ids' => result.getMergedRecordIds(),
'updated_related_ids' => result.getUpdatedRelatedIds(),
'type' => 'merge'
});

logger.withResult(result).info('test');
}

private static void itShouldAddSOQLQueries() {
TestLogger logger = new TestLogger('test');
Database.QueryLocator ql = Database.getQueryLocator([
Expand All @@ -896,6 +961,7 @@ private class ForceLogTest {
logger.expect('message', 'test');
logger.expect('timestamp', '*');
logger.expect('name', 'test');

logger.expect('query', ql.getQuery());

logger.withQuery(ql).info('test');
Expand Down

0 comments on commit 5d59c87

Please sign in to comment.