Skip to content

Commit

Permalink
Adds mocking capabilities for the publishing of platform events
Browse files Browse the repository at this point in the history
  • Loading branch information
jamessimone committed Sep 6, 2023
1 parent 428a94c commit 626cbf0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions force-app/dml/DML.cls
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public virtual class DML implements IDML {
return results;
}

public virtual Database.SaveResult publish(SObject event) {
return EventBus.publish(event);
}
public virtual List<Database.SaveResult> publish(List<SObject> events) {
return EventBus.publish(events);
}

public DML setOptions(Database.DMLOptions options, System.AccessLevel accessLevel) {
if (options != null) {
this.options = options;
Expand Down
16 changes: 16 additions & 0 deletions force-app/dml/DMLMock.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public virtual class DMLMock extends DML {
public static List<SObject> UpdatedRecords = new List<SObject>();
public static List<SObject> DeletedRecords = new List<SObject>();
public static List<SObject> UndeletedRecords = new List<SObject>();
public static List<SObject> PublishedRecords = new List<SObject>();

public override List<Database.SaveResult> doInsert(List<SObject> records) {
TestingUtils.generateIds(records);
Expand Down Expand Up @@ -41,6 +42,15 @@ public virtual class DMLMock extends DML {
return this.doDelete(records);
}

public override Database.SaveResult publish(SObject event) {
PublishedRecords.add(event);
return (Database.SaveResult) createDatabaseResult(Database.SaveResult.class, event);
}
public override List<Database.SaveResult> publish(List<SObject> events) {
PublishedRecords.addAll(events);
return (List<Database.SaveResult>) createDatabaseResults(Database.SaveResult.class, events);
}

public static RecordsWrapper Inserted {
get {
return new RecordsWrapper(InsertedRecords);
Expand Down Expand Up @@ -71,6 +81,12 @@ public virtual class DMLMock extends DML {
}
}

public static RecordsWrapper Published {
get {
return new RecordsWrapper(PublishedRecords);
}
}

public class RecordsWrapper {
List<SObject> recordList;
private RecordsWrapper(List<SObject> recordList) {
Expand Down
13 changes: 13 additions & 0 deletions force-app/dml/DMLTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,17 @@ private class DMLTests {

System.assert(true, 'Should make it here');
}

@IsTest
static void publishesEvents() {
BatchApexErrorEvent first = new BatchApexErrorEvent();
BatchApexErrorEvent second = new BatchApexErrorEvent();

IDML dml = new DML();
Database.SaveResult firstResult = dml.publish(first);
Database.SaveResult secondResult = dml.publish(new List<SObject>{ second }).get(0);

Assert.areEqual(true, firstResult.isSuccess());
Assert.areEqual(true, secondResult.isSuccess());
}
}
3 changes: 3 additions & 0 deletions force-app/dml/IDML.cls
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public interface IDML {
Database.DeleteResult doHardDelete(SObject record);
List<Database.DeleteResult> doHardDelete(List<SObject> recordList);

Database.SaveResult publish(SObject platformEvent);
List<Database.SaveResult> publish(List<SObject> platformEvent);

IDML setOptions(Database.DMLOptions options, System.AccessLevel accessLevel);
}
7 changes: 7 additions & 0 deletions force-app/repository/Repository.cls
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ public virtual without sharing class Repository implements IRepository {
return this.dml.doHardDelete(records);
}

public Database.SaveResult publish(SObject event) {
return this.dml.publish(event);
}
public List<Database.SaveResult> publish(List<SObject> events) {
return this.dml.publish(events);
}

public IDML setOptions(Database.DMLOptions options, System.AccessLevel accessLevel) {
this.accessLevel = accessLevel;
return this.dml.setOptions(options, accessLevel);
Expand Down
5 changes: 5 additions & 0 deletions force-app/repository/RepositoryTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ private class RepositoryTests {
repo.doHardDelete(acc);
repo.doHardDelete(accs);
System.assertEquals(acc, DMLMock.Deleted.Accounts.firstOrDefault);

BatchApexErrorEvent event = new BatchApexErrorEvent();
repo.publish(event);
repo.publish(new List<SObject>{ event });
System.assertEquals(event, DMLMock.Published.firstOrDefault);
}

private class GroupMemberRepo extends Repository {
Expand Down

0 comments on commit 626cbf0

Please sign in to comment.