Skip to content

Commit

Permalink
Added testing and documentation to SobjectUtils method
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Dec 9, 2021
1 parent 31cf34d commit 0f64657
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 3 deletions.
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Introduce ortoo_Selector - make it secure by default

Test:
ortoo_Exception.regenerateStackTraceString
SobjectUtils.getSObjectDescribeResult
SobjectUtils.isCreateable
SobjectUtils.isUpdateable
SobjectUtils.isDeletable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* 1 - Make the code in the app that performs these checks simpler and easier to read.
* 2 - Provide the ability to make performance improvements as and when the
* relative performance of different methods change.
* This may include the ability to cache certain results.
*
* These methods should always be used to ask questions of SObjects.
*
Expand Down Expand Up @@ -48,9 +49,16 @@ public inherited sharing class SobjectUtils
return ((SObject)Type.forName( sobjectName )?.newInstance())?.getSObjectType();
}

public static Schema.SobjectType getSobjectType( Sobject record )
/**
* Given an instance of an SObject, will return its SobjectType
*
* @param Sobject The SObject for which to return the SobjectType
* @return SobjectType The SobjectType of the SObject
*/
public static SobjectType getSobjectType( Sobject record )
{
Contract.requires( record != null, 'Attempted to get the type for a null record' );
Contract.requires( record != null, 'getSobjectType called with a null record' );

return record.getSObjectType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,46 @@ private without sharing class SobjectUtilsTest
Amoss_Asserts.assertContains( 'Attempted to get the name of a null SObject', exceptionMessage, 'getSobjectType, when given a null Sobject, will throw an exception' );
}

@isTest
private static void getSobjectType_whenGivenARecord_willReturnItsType() // NOPMD: Test method name format
{
SobjectType expectedType = Contact.getSObjectType();
SobjectType actualType = SobjectUtils.getSobjectType( new Contact() );

System.assertEquals( expectedType, actualType, 'getSobjectType, when given an SObject record, will its type' );
}

@isTest
private static void getSobjectType_whenGivenARecordHeldInAGenericVariable_willReturnItsType() // NOPMD: Test method name format
{
SobjectType expectedType = Contact.getSObjectType();

Sobject contactRecord = new Contact();
SobjectType actualType = SobjectUtils.getSobjectType( contactRecord );

System.assertEquals( expectedType, actualType, 'getSobjectType, when given an SObject record held in a generic variable, will its type' );
}

@isTest
private static void getSobjectType_whenPassedANullRecord_willThrowAnException() // NOPMD: Test method name format
{
Sobject nullRecord = null;

Test.startTest();
String exceptionMessage;
try
{
SobjectUtils.getSobjectType( nullRecord );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'getSobjectType called with a null record', exceptionMessage, 'getSobjectType, when passed a null record, will throw an exception' );
}

@isTest
private static void getSobjectName_whenGivenAnSobject_willReturnTheApiNameOfIt() // NOPMD: Test method name format
{
Expand Down Expand Up @@ -95,4 +135,91 @@ private without sharing class SobjectUtilsTest

Amoss_Asserts.assertContains( 'Attempted to get the name of a null SObjectType', exceptionMessage, 'getSobjectName, when given a null SobjectType, will throw an exception' );
}

@isTest
private static void isCreateable_whenCalled_willReturnIsCreatableOfThatSobject() // NOPMD: Test method name format
{
SObject record = new Contact();

Boolean expectedIsCreateable = record.getSObjectType().getDescribe().isCreateable();
Boolean actualIsCreateable = SobjectUtils.isCreateable( record );

System.assertEquals( expectedIsCreateable, actualIsCreateable, 'isCreatable, when called with an SObject, will return if that SObject Type is createable by the current user' );
}

@isTest
private static void isCreateable_whenGivenANullSobject_willThrowAnException() // NOPMD: Test method name format
{
Test.startTest();
String exceptionMessage;
try
{
SobjectUtils.isCreateable( null );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'isCreateable called with a null record', exceptionMessage, 'isCreateable, when given a null record, will throw an exception' );
}

@isTest
private static void isUpdateable_whenCalled_willReturnIsUpdateableOfThatSobject() // NOPMD: Test method name format
{
SObject record = new Contact();

Boolean expectedIsUpdateable = record.getSObjectType().getDescribe().isUpdateable();
Boolean actualIsUpdateable = SobjectUtils.isUpdateable( record );

System.assertEquals( expectedIsUpdateable, actualIsUpdateable, 'isUpdateable, when called with an SObject, will return if that SObject Type is updateable by the current user' );
}

@isTest
private static void isUpdateable_whenGivenANullSobject_willThrowAnException() // NOPMD: Test method name format
{
Test.startTest();
String exceptionMessage;
try
{
SobjectUtils.isUpdateable( null );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'isUpdateable called with a null record', exceptionMessage, 'isUpdateable, when given a null record, will throw an exception' );
}

@isTest
private static void isDeletable_whenCalled_willReturnIsUpdateableOfThatSobject() // NOPMD: Test method name format
{
SObject record = new Contact();

Boolean expectedIsDeletable = record.getSObjectType().getDescribe().isDeletable();
Boolean actualIsDeletable = SobjectUtils.isDeletable( record );

System.assertEquals( expectedIsDeletable, actualIsDeletable, 'isDeletable, when called with an SObject, will return if that SObject Type is deletable by the current user' );
}

@isTest
private static void isDeletable_whenGivenANullSobject_willThrowAnException() // NOPMD: Test method name format
{
Test.startTest();
String exceptionMessage;
try
{
SobjectUtils.isDeletable( null );
}
catch ( Contract.RequiresException e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'isDeletable called with a null record', exceptionMessage, 'isDeletable, when given a null record, will throw an exception' );
}
}

0 comments on commit 0f64657

Please sign in to comment.