Skip to content

Commit

Permalink
Fixed some testing in LoggerEmailSender_Tests to ensure tests pass in…
Browse files Browse the repository at this point in the history
… orgs with & without email deliverability enabled
  • Loading branch information
jongpie committed Sep 19, 2023
1 parent 57e40f0 commit 9a6b32f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ public without sharing class LoggerEmailSender {
}

@TestVisible
private static Boolean IS_EMAIL_DELIVERABILITY_ENABLED {
private static Boolean IS_EMAIL_DELIVERABILITY_AVAILABLE {
get {
if (IS_EMAIL_DELIVERABILITY_ENABLED == null) {
if (IS_EMAIL_DELIVERABILITY_AVAILABLE == null) {
try {
System.Messaging.reserveSingleEmailCapacity(1);
IS_EMAIL_DELIVERABILITY_ENABLED = true;
IS_EMAIL_DELIVERABILITY_AVAILABLE = true;
} catch (System.NoAccessException e) {
// Exception thrown when email deliverability is disabled
IS_EMAIL_DELIVERABILITY_ENABLED = false;
IS_EMAIL_DELIVERABILITY_AVAILABLE = false;
} catch (System.HandledException handledException) {
// Exception thrown when org limits are reached
IS_EMAIL_DELIVERABILITY_ENABLED = false;
IS_EMAIL_DELIVERABILITY_AVAILABLE = false;
}
}
return IS_EMAIL_DELIVERABILITY_ENABLED;
return IS_EMAIL_DELIVERABILITY_AVAILABLE;
}
set;
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public without sharing class LoggerEmailSender {

private static void sendEmail(Messaging.SingleEmailMessage message) {
SENT_EMAILS.add(message);
if (IS_EMAIL_DELIVERABILITY_ENABLED == true) {
if (IS_EMAIL_DELIVERABILITY_AVAILABLE == true) {
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>{ message };
List<Messaging.SendEmailResult> emailResults = Messaging.sendEmail(messages);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,46 @@
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. //
//------------------------------------------------------------------------------------------------//

@SuppressWarnings('PMD.MethodNamingConventions')
@SuppressWarnings('PMD.MethodNamingConventions, PMD.PropertyNamingConventions')
@IsTest(IsParallel=true)
private class LoggerEmailSender_Tests {
private static final Boolean IS_EMAIL_DELIVERABILITY_ENABLED {
get {
if (IS_EMAIL_DELIVERABILITY_ENABLED == null) {
try {
System.Messaging.reserveSingleEmailCapacity(1);
IS_EMAIL_DELIVERABILITY_ENABLED = true;
} catch (System.NoAccessException e) {
IS_EMAIL_DELIVERABILITY_ENABLED = false;
}
}
return IS_EMAIL_DELIVERABILITY_ENABLED;
}
set;
}

@IsTest
static void it_should_indicate_email_deliverability_is_based_on_email_deliverability_when_org_limits_not_exceeded() {
System.OrgLimit singleEmailOrgLimit = OrgLimits.getMap().get('SingleEmail');
System.Assert.isTrue(singleEmailOrgLimit.getValue() < singleEmailOrgLimit.getLimit());

System.Assert.areEqual(IS_EMAIL_DELIVERABILITY_ENABLED, LoggerEmailSender.IS_EMAIL_DELIVERABILITY_AVAILABLE);
}

@IsTest
static void it_should_indicate_email_deliverability_is_disabled_when_org_limits_exceeded() {
static void it_should_indicate_email_deliverability_is_not_available_when_org_limits_exceeded() {
// No need to fail the test if it's running in an org that does not have email deliverability enabled
if (IS_EMAIL_DELIVERABILITY_ENABLED == false) {
return;
}

System.OrgLimit singleEmailOrgLimit = OrgLimits.getMap().get('SingleEmail');

// Reserve all of the available single email capacity, which is the effectively
// the same as the email limit being exceeded in the org
System.Messaging.reserveSingleEmailCapacity(singleEmailOrgLimit.getLimit());
System.Messaging.reserveSingleEmailCapacity(singleEmailOrgLimit.getLimit() - singleEmailOrgLimit.getValue() - 1);

System.Assert.isFalse(LoggerEmailSender.IS_EMAIL_DELIVERABILITY_ENABLED);
System.Assert.isFalse(LoggerEmailSender.IS_EMAIL_DELIVERABILITY_AVAILABLE);
}

@IsTest
Expand Down Expand Up @@ -55,7 +83,7 @@ private class LoggerEmailSender_Tests {
LoggerEmailSender.SENT_EMAILS.get(0).getHtmlBody().contains(saveResultsWithErrors.get(0).errors.get(0).getMessage()),
'Email message should contain SaveResult error message'
);
if (LoggerEmailSender.IS_EMAIL_DELIVERABILITY_ENABLED == true) {
if (LoggerEmailSender.IS_EMAIL_DELIVERABILITY_AVAILABLE == true) {
System.Assert.areEqual(1, System.Limits.getEmailInvocations(), 'Email should have been sent');
} else {
System.Assert.areEqual(0, System.Limits.getEmailInvocations(), 'Deliverability is not currently enabled');
Expand Down Expand Up @@ -106,7 +134,7 @@ private class LoggerEmailSender_Tests {
LoggerEmailSender.SENT_EMAILS.get(0).getHtmlBody().contains(upsertResultsWithErrors.get(0).errors.get(0).getMessage()),
'Email message should contain UpsertResult error message'
);
if (LoggerEmailSender.IS_EMAIL_DELIVERABILITY_ENABLED == true) {
if (LoggerEmailSender.IS_EMAIL_DELIVERABILITY_AVAILABLE == true) {
System.Assert.areEqual(1, System.Limits.getEmailInvocations(), 'Email should have been sent');
} else {
System.Assert.areEqual(0, System.Limits.getEmailInvocations(), 'Deliverability is not currently enabled');
Expand Down

0 comments on commit 9a6b32f

Please sign in to comment.