|
| 1 | +/** |
| 2 | + * @description a factory class for generating test data. |
| 3 | + * This class auto populates required fields and, by default, generates a plausible but fake ID for the record. |
| 4 | + * You can use this class directly, or use one of the associated builder classes for generating |
| 5 | + * complex data structures. |
| 6 | + * |
| 7 | + * This class has the ability to set default values for fields in 3, hierarchical ways. |
| 8 | + * 1. The default values passed into the SObjectFactory via the sObject prototype. |
| 9 | + * 2. The default values defined by the custom default class specified by the usingDefaultsClassName parameter. |
| 10 | + * 3. The default values found in the 'org-wide' defaults class defined in the SObjectFactoryDefaults' subclasses. |
| 11 | + * |
| 12 | + * It's important to note that *nothing* overwrites field values specified in the SObject prototype. |
| 13 | + * |
| 14 | + * Some profiling information: |
| 15 | + * | Action | Avg of 10 test executions | |
| 16 | + * |-----------|------------------------| |
| 17 | + * | Create 10 Single SObject w/ fake Id | 58ms | |
| 18 | + * | Create 10 Single SObjects w/ fake Id and Custom Defaults | 73ms | |
| 19 | + * | Create 10 Single SObjects & insert them | 550ms | |
| 20 | + * | Create list of 200 SObjects w/ fake Id | 115ms | |
| 21 | + * | Create list of 200 SObjects & insert them | 1216ms | |
| 22 | + */ |
| 23 | +@IsTest |
| 24 | +public with sharing class SObjectFactory { |
| 25 | + /** |
| 26 | + * @description Use the FieldDefaults interface to set up field/value keys you want to routinely impose on your |
| 27 | + * factory generated objects. |
| 28 | + */ |
| 29 | + public interface FieldDefaults { |
| 30 | + /** |
| 31 | + * @description Interface used by implementing classes to define defaults. |
| 32 | + * @return `Map<Schema.SObjectField, Object>` |
| 33 | + */ |
| 34 | + Map<Schema.SObjectField, Object> getFieldDefaults(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @description convenience method allowing you to specify just the prototype |
| 39 | + * @param prototype SObject any SObject |
| 40 | + * @return A created SObject with required fields populated |
| 41 | + */ |
| 42 | + public static SObject createSObject(SObject prototype) { |
| 43 | + return createSObject(prototype, null, false); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @description Convenience method for creating SObjects using just the prototype and a custom defaults class |
| 48 | + * @param prototype SObject - any constructed SObject |
| 49 | + * @param usingDefaultsClassName String - the name of the class to use for custom default values |
| 50 | + * @return A created SObject with required fields populated |
| 51 | + */ |
| 52 | + public static SObject createSObject( |
| 53 | + SObject prototype, |
| 54 | + String usingDefaultsClassName |
| 55 | + ) { |
| 56 | + return createSObject(prototype, usingDefaultsClassName, false); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @description Creates an SObject with the given prototype and forceInsert flag. |
| 61 | + * |
| 62 | + * @param prototype The prototype SObject. |
| 63 | + * @param forceInsert Flag indicating whether to force insertion. |
| 64 | + * |
| 65 | + * @return The created SObject. |
| 66 | + */ |
| 67 | + public static SObject createSObject(SObject prototype, Boolean forceInsert) { |
| 68 | + return createSObject(prototype, null, forceInsert); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @description Creates an SObject with the given prototype, using defaults from the specified class, and optionally inserts it into the database. |
| 73 | + * |
| 74 | + * @param prototype The prototype SObject to be created. |
| 75 | + * @param usingDefaultsClassName The name of the class providing default values for the SObject. |
| 76 | + * @param forceInsert Indicates whether to insert the SObject into the database. |
| 77 | + * |
| 78 | + * @return The created SObject. |
| 79 | + */ |
| 80 | + public static SObject createSObject( |
| 81 | + SObject prototype, |
| 82 | + String usingDefaultsClassName, |
| 83 | + Boolean forceInsert |
| 84 | + ) { |
| 85 | + prototype = internalCreateSObject(prototype, usingDefaultsClassName); |
| 86 | + if (forceInsert) { |
| 87 | + Database.insert(prototype, AccessLevel.SYSTEM_MODE); |
| 88 | + } else { |
| 89 | + prototype.Id = IdFactory.get(prototype); |
| 90 | + } |
| 91 | + return prototype; |
| 92 | + } |
| 93 | + |
| 94 | + private static SObject internalCreateSObject( |
| 95 | + SObject prototype, |
| 96 | + String usingDefaultsClassName |
| 97 | + ) { |
| 98 | + // ensure the defaults class is not null |
| 99 | + String functionalDefaultsClassName = String.isNotEmpty( |
| 100 | + usingDefaultsClassName |
| 101 | + ) |
| 102 | + ? usingDefaultsClassName |
| 103 | + : ''; |
| 104 | + // First the specified defaults class |
| 105 | + prototype = SObjectFactoryHelper.applyCustomDefaults( |
| 106 | + prototype, |
| 107 | + functionalDefaultsClassName |
| 108 | + ); |
| 109 | + // then the 'org-wide' defaults |
| 110 | + prototype = SObjectFactoryHelper.applyOrgWideObjectDefaults(prototype); |
| 111 | + return prototype; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @description Creates a list of SObjects based on the provided prototype and count. |
| 116 | + * |
| 117 | + * @param prototype The SObject prototype to base the created SObjects on. |
| 118 | + * @param count The number of SObjects to create. |
| 119 | + * |
| 120 | + * @return A list of created SObjects. |
| 121 | + */ |
| 122 | + public static List<SObject> createSObjects(SObject prototype, Integer count) { |
| 123 | + return createSObjects(prototype, count, null, false); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @description Creates a list of SObjects based on the provided prototype, count, and defaults class name. |
| 128 | + * |
| 129 | + * @param prototype The prototype SObject to base the new SObjects on. |
| 130 | + * @param count The number of SObjects to create. |
| 131 | + * @param usingDefaultsClassName The name of the class to use for default values. |
| 132 | + * |
| 133 | + * @return A list of created SObjects. |
| 134 | + */ |
| 135 | + public static List<SObject> createSObjects( |
| 136 | + SObject prototype, |
| 137 | + Integer count, |
| 138 | + String usingDefaultsClassName |
| 139 | + ) { |
| 140 | + return createSObjects(prototype, count, usingDefaultsClassName, false); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @description Creates a list of sObjects based on the provided prototype, count, and forceInsert flag. |
| 145 | + * |
| 146 | + * @param prototype The prototype SObject to base the created objects on. |
| 147 | + * @param count The number of SObjects to create. |
| 148 | + * @param forceInsert Flag indicating whether to force insertion of the created SObjects. |
| 149 | + * |
| 150 | + * @return List of created SObjects. |
| 151 | + */ |
| 152 | + public static List<SObject> createSObjects( |
| 153 | + SObject prototype, |
| 154 | + Integer count, |
| 155 | + Boolean forceInsert |
| 156 | + ) { |
| 157 | + return createSObjects(prototype, count, null, forceInsert); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @description Creates a list of SObjects based on a prototype, count, defaults class, and force insert flag. |
| 162 | + * |
| 163 | + * @param prototype The prototype SObject to clone. |
| 164 | + * @param count The number of SObjects to create. |
| 165 | + * @param usingDefaultsClassName The name of the defaults class to use. |
| 166 | + * @param forceInsert Whether to force insert the created SObjects. |
| 167 | + * |
| 168 | + * @return A list of created SObjects. |
| 169 | + */ |
| 170 | + @SuppressWarnings('PMD.ExcessiveParameterList') |
| 171 | + public static List<SObject> createSObjects( |
| 172 | + SObject prototype, |
| 173 | + Integer count, |
| 174 | + String usingDefaultsClassName, |
| 175 | + Boolean forceInsert |
| 176 | + ) { |
| 177 | + List<SObject> createdSObjects = new List<SObject>(); |
| 178 | + SObject constructedFromPrototype = internalCreateSObject( |
| 179 | + prototype, |
| 180 | + usingDefaultsClassName |
| 181 | + ); |
| 182 | + for ( |
| 183 | + Integer iterationCounter = 0; iterationCounter < count; iterationCounter++ |
| 184 | + ) { |
| 185 | + SObject clonedSObject = constructedFromPrototype.clone(false, true); |
| 186 | + createdSObjects.add( |
| 187 | + SObjectFactoryHelper.mutateCloneToRespectNameAndAutonumberRules( |
| 188 | + clonedSObject, |
| 189 | + !forceInsert, |
| 190 | + iterationCounter |
| 191 | + ) |
| 192 | + ); |
| 193 | + } |
| 194 | + SObjectFactoryHelper.insertIfForced(createdSObjects, forceInsert); |
| 195 | + return createdSObjects; |
| 196 | + } |
| 197 | +} |
0 commit comments