Skip to content

Commit

Permalink
Merge pull request apex-enterprise-patterns#33 from OrtooApps/feature…
Browse files Browse the repository at this point in the history
…/add-dynamic-uow

Added factory methods for the Dynamic Unit of Work
  • Loading branch information
rob-baillie-ortoo committed Apr 25, 2022
2 parents 01ca71e + 93d5fff commit 3dc4fcf
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 2 deletions.
19 changes: 17 additions & 2 deletions framework/default/ortoo-core/default/classes/Application.cls
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ public inherited sharing class Application {

public class InvalidApplicationConfigurationException extends Exceptions.DeveloperException {}

public final static ortoo_UnitOfWorkFactory UNIT_OF_WORK {
get {
public final static ortoo_UnitOfWorkFactory UNIT_OF_WORK
{
get
{
if ( UNIT_OF_WORK == null )
{
UNIT_OF_WORK = new ortoo_UnitOfWorkFactory(); // is created with no default since that needs to be defined by the consumer
Expand All @@ -18,6 +20,19 @@ public inherited sharing class Application {
set;
}

public final static ortoo_DynamicUnitOfWorkFactory DYNAMIC_UNIT_OF_WORK
{
get
{
if ( DYNAMIC_UNIT_OF_WORK == null )
{
DYNAMIC_UNIT_OF_WORK = new ortoo_DynamicUnitOfWorkFactory();
}
return DYNAMIC_UNIT_OF_WORK;
}
set;
}

public static ortoo_SelectorFactory SELECTOR; // NOPMD: want to give the impression that this is final, even though it isn't.
public static ortoo_DomainFactory DOMAIN; // NOPMD: want to give the impression that this is final, even though it isn't.
public static ortoo_ServiceFactory SERVICE; // NOPMD: want to give the impression that this is final, even though it isn't.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ public inherited sharing class ApplicationMockRegistrar
return mockUowController;
}

/**
* Generates a mock Dynamic Unit of Work controller and registers its double against the Application
* Then returns the controller for the mock so that expectations can be set against it
*
* @return Amoss_Instance The controller for the mock Unit of Work
*/
public static Amoss_Instance registerMockDynamicUnitOfWork()
{
Amoss_Instance mockUowController = new Amoss_Instance( ortoo_DynamicSObjectUnitOfWork.class );
Application.DYNAMIC_UNIT_OF_WORK.setMock( (ortoo_DynamicSObjectUnitOfWork)mockUowController.generateDouble() );
return mockUowController;
}

/**
* Generates a mock Service controller and registers its double against the Application
* Then returns the controller for the mock so that expectations can be set against it
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public inherited sharing class ortoo_DynamicUnitOfWorkFactory // NOPMD: specified a mini-namespace to differentiate from fflib versions
{
private static fflib_SobjectUnitOfWork.Idml defaultIdml = new SecureDml();
protected fflib_ISObjectUnitOfWork mockUow;

/**
* Returns a new SObjectUnitOfWork configured with the
* SObjectType list provided in the constructor, returns a Mock implementation
* if set via the setMock method
**/
public fflib_ISObjectUnitOfWork newInstance()
{
return newInstance( defaultIdml );
}

/**
* Returns a new SObjectUnitOfWork configured with the
* SObjectType list provided in the constructor, returns a Mock implementation
* if set via the setMock method
**/
public fflib_ISObjectUnitOfWork newInstance( fflib_SObjectUnitOfWork.Idml dml )
{
if ( this.mockUow != null )
{
return this.mockUow;
}
return new ortoo_DynamicSObjectUnitOfWork( dml );
}

@TestVisible
private void setMock( fflib_ISObjectUnitOfWork mockUow )
{
this.mockUow = mockUow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@isTest
private without sharing class ortoo_DynamicUnitOfWorkFactoryTest // NOPMD: mini-namespace to distinguich from fflib version
{
@isTest
private static void newInstance_whenGivenNoIdml_willReturnAnInstanceWithASecureDml() // NOPMD: Test method name format
{
ortoo_DynamicUnitOfWorkFactory factory = new ortoo_DynamicUnitOfWorkFactory();
ortoo_DynamicSobjectUnitOfWork uow = (ortoo_DynamicSobjectUnitOfWork)factory.newInstance();

System.assert( uow.getIdml() instanceOf SecureDml, 'newInstance, when given no dml, will create an instance with a secure dml' );
}

@isTest
private static void newInstance_whenGivenAnIdml_willCreateAnInstanceWithThatIdml() // NOPMD: Test method name format
{
ortoo_DynamicUnitOfWorkFactory factory = new ortoo_DynamicUnitOfWorkFactory();

RegisterableIdml IDml = new RegisterableIdml();
ortoo_DynamicSobjectUnitOfWork uow = (ortoo_DynamicSobjectUnitOfWork)factory.newInstance( IDml );

System.assertEquals( IDml, uow.getIdml(), 'newInstance, when given no object order and an IDml, will create an instance with the IDml' );
}

@isTest
private static void setMock_whenNewInstanceIsCalledWithoutAnIdml_willReturnTheMock() // NOPMD: Test method name format
{
ortoo_DynamicSobjectUnitOfWork mockUow = (ortoo_DynamicSobjectUnitOfWork)new Amoss_Instance( ortoo_DynamicSobjectUnitOfWork.class ).getDouble();
ortoo_DynamicUnitOfWorkFactory factory = new ortoo_DynamicUnitOfWorkFactory();

Test.startTest();
factory.setMock( mockUow );
ortoo_DynamicSobjectUnitOfWork returnedUow = (ortoo_DynamicSobjectUnitOfWork)factory.newInstance();
Test.stopTest();

System.assertEquals( mockUow, returnedUow, 'setMock, when new instance is called without a DML, will return the mock' );
}

@isTest
private static void setMock_whenNewInstanceIsCalledWithAnIdml_willReturnTheMock() // NOPMD: Test method name format
{
ortoo_DynamicSobjectUnitOfWork mockUow = (ortoo_DynamicSobjectUnitOfWork)new Amoss_Instance( ortoo_DynamicSobjectUnitOfWork.class ).getDouble();
ortoo_DynamicUnitOfWorkFactory factory = new ortoo_DynamicUnitOfWorkFactory();

Test.startTest();
factory.setMock( mockUow );
RegisterableIdml IDml = new RegisterableIdml();
ortoo_DynamicSobjectUnitOfWork returnedUow = (ortoo_DynamicSobjectUnitOfWork)factory.newInstance( IDml );
Test.stopTest();

System.assertEquals( mockUow, returnedUow, 'setMock, when new instance is called without an order, will return the mock' );
}

private without sharing class RegisterableIdml implements fflib_SObjectUnitOfWork.IDML
{
public void dmlInsert(List<SObject> objList) {} // NOPMD: empty implementation for tests
public void dmlUpdate(List<SObject> objList) {} // NOPMD: empty implementation for tests
public void dmlDelete(List<SObject> objList) {} // NOPMD: empty implementation for tests
public void eventPublish(List<SObject> objList) {} // NOPMD: empty implementation for tests
public void emptyRecycleBin(List<SObject> objList) {} // NOPMD: empty implementation for tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
@isTest
private without sharing class ApplicationMockRegistrarTest
{
@isTest
private static void registerMockUnitOfWork_willGenerateAMockAndReturnIt() // NOPMD: Test method name format
{
Test.startTest();
Amoss_Instance mockController = ApplicationMockRegistrar.registerMockUnitOfWork();
Test.stopTest();

System.assertEquals( mockController.getDouble(), Application.UNIT_OF_WORK.newInstance(), 'registerMockUnitOfWork, will register is as a mock UOW so that its double is returned by the factory' );
}

@isTest
private static void registerMockDynamicUnitOfWork_willGenerateAMockAndReturnIt() // NOPMD: Test method name format
{
Test.startTest();
Amoss_Instance mockController = ApplicationMockRegistrar.registerMockDynamicUnitOfWork();
Test.stopTest();

System.assertEquals( mockController.getDouble(), Application.DYNAMIC_UNIT_OF_WORK.newInstance(), 'registerMockDynamicUnitOfWork, will register is as a mock UOW so that its double is returned by the factory' );
}

@isTest
private static void registerMockService_whenGivenAServiceType_willGenerateAMockAndReturnIt() // NOPMD: Test method name format
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ private without sharing class ApplicationTest
System.assertNotEquals( null, Application.MESSAGE_RENDERER , 'MESSAGE_RENDERER, when referenced, will exist' );
}

@isTest
private static void unitOfWork_whenReferenced_isAUnitOfWorkFactory() // NOPMD: Test method name format
{
System.assertNotEquals(null, Application.UNIT_OF_WORK, 'unitOfWork, when referenced, will be a unit of work factory' );
}

@isTest
private static void dynamicUnitOfWork_whenReferenced_isAUnitOfWorkFactory() // NOPMD: Test method name format
{
System.assertNotEquals(null, Application.DYNAMIC_UNIT_OF_WORK, 'dynamicUnitOfWork, when referenced, will be a dynamic unit of work factory' );
}

@isTest
private static void selector_whenReferenced_willBeConfiguredBasedOnMetadata() // NOPMD: Test method name format
{
Expand Down

0 comments on commit 3dc4fcf

Please sign in to comment.