Browse Source

Unit test and other improvements

master
Matt Comer 2 months ago
parent
commit
783b53f733
4 changed files with 74 additions and 37 deletions
  1. +28
    -3
      force-app/main/default/classes/MassTransactorAction.cls
  2. +1
    -34
      force-app/main/default/classes/MassTransactorBatchHelper.cls
  3. +40
    -0
      force-app/main/default/classes/MassTransactorBatchHelperTest.cls
  4. +5
    -0
      force-app/main/default/classes/MassTransactorBatchHelperTest.cls-meta.xml

+ 28
- 3
force-app/main/default/classes/MassTransactorAction.cls View File

@@ -21,6 +21,8 @@ global with sharing class MassTransactorAction {
@InvocableMethod (label='DML Records Asynchronously')
public static List<InvocableApexAsyncBatchResponse> InvocableApexCreateAsyncController(List<InvocableApexAsyncBatchRequest> RequestList){
// NOTE that this action is currently not bulkified due to the nature of what it does.
// Creating and submitting multiple independent batch jobs would not properly scale.
InvocableApexAsyncBatchRequest request = RequestList[0];
ValidationObject vo = ValidateRequestObject(request);

@@ -32,9 +34,9 @@ global with sharing class MassTransactorAction {
Integer batchSize = request.BatchSizeIsNull() ? DEFAULT_BATCH_SIZE : request.BatchSize;
MassTransactorBatchHelper bh;
if (request.EmailToAddress != null) {
MassTransactorBatchHelper.SimpleEmailOnFinishAction fa = new MassTransactorBatchHelper.SimpleEmailOnFinishAction(request.EmailToAddress,
request.EmailSubject,
request.EmailBody);
SimpleEmailOnFinishAction fa = new SimpleEmailOnFinishAction(request.EmailToAddress,
request.EmailSubject,
request.EmailBody);
bh = new MassTransactorBatchHelper(request.RequestList,
request.OperationType,
fa);
@@ -196,4 +198,27 @@ global with sharing class MassTransactorAction {
public Integer ResponseCode;
}

public class SimpleEmailOnFinishAction implements MassTransactorBatchHelper.OnFinishAction {
private String emailTo;
private String subject;
private String body;
public SimpleEmailOnFinishAction(String emailTo, String subject, String body) {
this.emailTo = emailTo;
this.subject = subject;
this.body = body;
}
public void onFinish(Id finishedJobId) {
If(this.emailTo != null && this.subject != null && this.body != null) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new List<String>();
toAddresses.add(this.emailTo);
mail.setToAddresses(toAddresses);
mail.setSubject(this.subject);
mail.setPlainTextBody(this.body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}

}

+ 1
- 34
force-app/main/default/classes/MassTransactorBatchHelper.cls View File

@@ -50,18 +50,7 @@ public with sharing class MassTransactorBatchHelper implements Database.Batchabl

public MassTransactorBatchHelper(List<SObject> records, String actionIn) {
this.records = records;
String a = actionIn.toUpperCase();

if (a == 'CREATE') {
this.action = OperationType.DO_INSERT;
} else if (a == 'UPDATE') {
this.action = OperationType.DO_UPDATE;
} else if (a == 'UPSERT') {
this.action = OperationType.DO_UPSERT;
} else if (a == 'DELETE') {
this.action = OperationType.DO_DELETE;
}
this(records, actionIn, null);
}

public MassTransactorBatchHelper(List<SObject> records, String actionIn, onFinishAction fa) {
@@ -125,26 +114,4 @@ public with sharing class MassTransactorBatchHelper implements Database.Batchabl
void onFinish(Id finishedJobId);
}

public class SimpleEmailOnFinishAction implements OnFinishAction {
private String emailTo;
private String subject;
private String body;
public SimpleEmailOnFinishAction(String emailTo, String subject, String body) {
this.emailTo = emailTo;
this.subject = subject;
this.body = body;
}
public void onFinish(Id finishedJobId) {
If(this.emailTo != null && this.subject != null && this.body != null) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new List<String>();
toAddresses.add(this.emailTo);
mail.setToAddresses(toAddresses);
mail.setSubject(this.subject);
mail.setPlainTextBody(this.body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
}

+ 40
- 0
force-app/main/default/classes/MassTransactorBatchHelperTest.cls View File

@@ -0,0 +1,40 @@
@IsTest
public with sharing class MassTransactorBatchHelperTest {

public static Boolean onFinishCalled = false;

@IsTest
public static void testIterable() {
Contact c1 = new Contact(LastName='Contact 1');
Contact c2 = new Contact(LastName='Contact 2');
List<Contact> contacts = new List<Contact>{c1, c2};
MassTransactorBatchHelper.ListIterable itbl = new MassTransactorBatchHelper.ListIterable(contacts);
Iterator<SObject> it = itbl.Iterator();

System.assert(it.hasNext());
System.assert(((Contact)it.next()).LastName == 'Contact 1');
System.assert(it.hasNext());
System.assert(((Contact)it.next()).LastName == 'Contact 2');
System.assert(!it.hasNext());
}

@IsTest
public static void testCallsFinishAction() {
Contact c1 = new Contact(LastName='Contact 1');
Contact c2 = new Contact(LastName='Contact 2');
List<Contact> contacts = new List<Contact>{c1, c2};
SimpleFinishAction action = new SimpleFinishAction();
MassTransactorBatchHelper bh = new MassTransactorBatchHelper(contacts, 'create', action);
Test.startTest();
Database.executeBatch(bh);
Test.stopTest();
System.assert(onFinishCalled);
}

public class SimpleFinishAction implements MassTransactorBatchHelper.OnFinishAction {
public void onFinish(Id jobId) {
MassTransactorBatchHelperTest.onFinishCalled = true;
}
}

}

+ 5
- 0
force-app/main/default/classes/MassTransactorBatchHelperTest.cls-meta.xml View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>49.0</apiVersion>
<status>Active</status>
</ApexClass>

Loading…
Cancel
Save