From 1d0d3c906e8ecf3373611d44d2db0bc08b07a81d Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Mon, 6 Dec 2021 10:33:38 +0000 Subject: [PATCH] Added tests for the persistence of fabricated SObjects Defend against some invalid values being populated when persisting --- TODO.txt | 10 +++--- .../classes/sfab_FabricatedSObject.cls | 34 +++++++++++++++---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/TODO.txt b/TODO.txt index 4cadf0be22e..12161d3cb0a 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,10 +4,6 @@ Licenses that are needed with the source code and binary: * Amoss - https://github.com/bobalicious/amoss/blob/main/LICENSE * SObject Fabricator - https://github.com/bobalicious/SObjectFabricator/blob/master/LICENSE -TODO: -* Watch recording of the UI for notifications - - SObject Fabricator: * Test opportunity, opportunity line item, product, pricebook, pricebook entry @@ -15,10 +11,12 @@ SObject Fabricator: * Ensure the registration is complete - there are probably gaps * see sfab_FabricatedSObject.addParentChild + * Why do we need the null check in the post-process? + * Unit Test * sfab_FabricatedSObject - * Do an end to end test for the fabricator committing stuff - * Set unsettable fields, and a parent and child + * Add docs to objectdescriber and sfab_FabricatedSObject + * Particularly a reference to the original framework * Add defaulting mechanism * Make all the classes @isTest diff --git a/framework/default/sobject-fabricator/classes/sfab_FabricatedSObject.cls b/framework/default/sobject-fabricator/classes/sfab_FabricatedSObject.cls index 9958d1c5fbc..5f6ba62862b 100644 --- a/framework/default/sobject-fabricator/classes/sfab_FabricatedSObject.cls +++ b/framework/default/sobject-fabricator/classes/sfab_FabricatedSObject.cls @@ -298,14 +298,22 @@ public virtual class sfab_FabricatedSObject { * @return SObject - The built SObject */ public SObject toSObject() { - SObject newObject = (SObject)JSON.deserialize(JSON.serialize(serialize()), sType); - postBuildProcess( newObject ); - return newObject; + return internalToSobject( false ); } + /** + * Builds the SObject that this sfab_FabricatedSObject represents, in a form that means it can be persisted + * + * @return SObject - The built SObject + */ public SObject toPersistableSobject() { - SObject newObject = (SObject)JSON.deserialize(JSON.serialize(serialize(true)), sType); + return internalToSobject( true ); + } + + private Sobject internalToSobject( Boolean persistable ) + { + SObject newObject = (SObject)JSON.deserialize( JSON.serialize( serialize( persistable ) ), sType ); postBuildProcess( newObject ); return newObject; } @@ -316,7 +324,7 @@ public virtual class sfab_FabricatedSObject { */ public void postBuildProcess( SObject objectToProcess ) { for ( String nodeName : nodes.keySet() ) { - nodes.get( nodeName ).postBuildProcess( objectToProcess ); + nodes.get( nodeName )?.postBuildProcess( objectToProcess ); } } @@ -689,8 +697,15 @@ public virtual class sfab_FabricatedSObject { } public Map serialize( Boolean persistable ) { + + Map noNodes = new Map(); + + if ( persistable && getName().equals( 'Id' ) ) { + System.debug( 'Id with the value "' + value + '" stripped from object' ); + return noNodes; // Ids cannot be set when persisting an object - everything is an insert + } if ( fieldIsBlob() ) { - return new Map(); // is handled by the post process instead as deserializing a blob field does not work (API 50) + return noNodes; // is handled by the post process instead as deserializing a blob field does not work (API 50) } else { return new Map { getName() => value }; } @@ -766,9 +781,14 @@ public virtual class sfab_FabricatedSObject { // child objects in same order as the input map, otherwise the Blob field values // will be applied to the wrong objects. As far as we can tell, this is reliable // at the time of writing. If it becomes not the case, this will need re-writing + if ( objectToProcess == null ) + { + return; // TODO: why is this needed - why is this sometimes null? + } + List childSobjects = ((Sobject)objectToProcess).getSObjects( fieldName ); for ( Integer i=0; i