diff --git a/TODO.txt b/TODO.txt index 87f695726c4..124043bfb25 100644 --- a/TODO.txt +++ b/TODO.txt @@ -10,7 +10,6 @@ Introduce ortoo_Selector - make it secure by default Test: ortoo_Exception.regenerateStackTraceString - SobjectUtils.getSObjectDescribeResult SobjectUtils.isCreateable SobjectUtils.isUpdateable SobjectUtils.isDeletable diff --git a/framework/default/ortoo-core/default/classes/utils/SobjectUtils.cls b/framework/default/ortoo-core/default/classes/utils/SobjectUtils.cls index 3d237abced6..04ef72522d1 100644 --- a/framework/default/ortoo-core/default/classes/utils/SobjectUtils.cls +++ b/framework/default/ortoo-core/default/classes/utils/SobjectUtils.cls @@ -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. * @@ -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(); } diff --git a/framework/default/ortoo-core/default/classes/utils/tests/SobjectUtilsTest.cls b/framework/default/ortoo-core/default/classes/utils/tests/SobjectUtilsTest.cls index 61b13985147..cafe13595ac 100644 --- a/framework/default/ortoo-core/default/classes/utils/tests/SobjectUtilsTest.cls +++ b/framework/default/ortoo-core/default/classes/utils/tests/SobjectUtilsTest.cls @@ -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 { @@ -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' ); + } } \ No newline at end of file