-
Notifications
You must be signed in to change notification settings - Fork 22
/
DMLMock.cls
197 lines (167 loc) · 5.71 KB
/
DMLMock.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
public virtual class DMLMock extends DML {
public static List<SObject> InsertedRecords = new List<SObject>();
public static List<SObject> UpsertedRecords = new List<SObject>();
public static List<SObject> UpdatedRecords = new List<SObject>();
public static List<SObject> DeletedRecords = new List<SObject>();
public static List<SObject> UndeletedRecords = new List<SObject>();
public static List<SObject> PublishedRecords = new List<SObject>();
public override List<Database.SaveResult> doInsert(List<SObject> records) {
TestingUtils.generateIds(records);
InsertedRecords.addAll(records);
return (List<Database.SaveResult>) createDatabaseResults(Database.SaveResult.class, records);
}
public override List<Database.SaveResult> doUpdate(List<SObject> records) {
UpdatedRecords.addAll(records);
return (List<Database.SaveResult>) createDatabaseResults(Database.SaveResult.class, records);
}
public override List<Database.UpsertResult> doUpsert(List<SObject> records) {
TestingUtils.generateIds(records);
UpsertedRecords.addAll(records);
return (List<Database.UpsertResult>) createDatabaseResults(Database.UpsertResult.class, records);
}
public override List<Database.UpsertResult> doUpsert(List<SObject> records, Schema.SObjectField field) {
return this.doUpsert(records);
}
public override List<Database.UndeleteResult> doUndelete(List<SObject> records) {
UndeletedRecords.addAll(records);
return (List<Database.UndeleteResult>) createDatabaseResults(Database.UndeleteResult.class, records);
}
public override List<Database.DeleteResult> doDelete(List<SObject> records) {
if (records?.isEmpty() == false) {
DeletedRecords.addAll(records);
}
return (List<Database.DeleteResult>) createDatabaseResults(Database.DeleteResult.class, records);
}
public override List<Database.DeleteResult> doHardDelete(List<SObject> records) {
return this.doDelete(records);
}
public override Database.SaveResult publish(SObject event) {
PublishedRecords.add(event);
return (Database.SaveResult) createDatabaseResult(Database.SaveResult.class, event);
}
public override List<Database.SaveResult> publish(List<SObject> events) {
PublishedRecords.addAll(events);
return (List<Database.SaveResult>) createDatabaseResults(Database.SaveResult.class, events);
}
public static RecordsWrapper Inserted {
get {
return new RecordsWrapper(InsertedRecords);
}
}
public static RecordsWrapper Upserted {
get {
return new RecordsWrapper(UpsertedRecords);
}
}
public static RecordsWrapper Updated {
get {
return new RecordsWrapper(UpdatedRecords);
}
}
public static RecordsWrapper Deleted {
get {
return new RecordsWrapper(DeletedRecords);
}
}
public static RecordsWrapper Undeleted {
get {
return new RecordsWrapper(UndeletedRecords);
}
}
public static RecordsWrapper Published {
get {
return new RecordsWrapper(PublishedRecords);
}
}
public class RecordsWrapper {
List<SObject> recordList;
private RecordsWrapper(List<SObject> recordList) {
this.recordList = recordList;
}
public RecordsWrapper ofType(Schema.SObjectType sObjectType) {
return new RecordsWrapper(this.getRecordsMatchingType(recordList, sObjectType));
}
public RecordsWrapper Accounts {
get {
return this.ofType(Schema.Account.SObjectType);
}
}
public RecordsWrapper Leads {
get {
return this.ofType(Schema.Lead.SObjectType);
}
}
public RecordsWrapper Contacts {
get {
return this.ofType(Schema.Contact.SObjectType);
}
}
public RecordsWrapper Opportunities {
get {
return this.ofType(Schema.Opportunity.SObjectType);
}
}
public RecordsWrapper Tasks {
get {
return this.ofType(Schema.Task.SObjectType);
}
}
public List<SObject> Records {
get {
return recordList;
}
}
public Boolean hasId(Id recordId) {
return this.hasId(recordId, 'Id');
}
public Boolean hasId(Id relatedId, Schema.SObjectField idField) {
return this.hasId(relatedId, idField.getDescribe().getName());
}
private Boolean hasId(Id relatedId, String idFieldName) {
for (SObject record : this.recordList) {
if (record.get(idFieldName) == relatedId) {
return true;
}
}
return false;
}
public Integer size() {
return this.recordList.size();
}
public SObject singleOrDefault {
get {
if (recordList.size() > 1) {
throw new IllegalArgumentException('More than one value in records list');
}
return recordList.size() == 0 ? null : recordList[0];
}
}
public SObject firstOrDefault {
get {
if (recordList.size() > 0) {
return recordList[0];
}
return null;
}
}
private List<SObject> getRecordsMatchingType(List<SObject> records, Schema.SObjectType sObjectType) {
List<SObject> matchingRecords = new List<SObject>();
for (SObject record : records) {
if (record.getSObjectType() == sObjectType) {
matchingRecords.add(record);
}
}
return matchingRecords;
}
}
private static List<Object> createDatabaseResults(Type clazz, List<SObject> records) {
List<Object> results = (List<Object>) Type.forName('List<' + clazz.getName() + '>').newInstance();
for (SObject record : records) {
results.add(createDatabaseResult(clazz, record));
}
return results;
}
private static Object createDatabaseResult(Type clazz, SObject record) {
return JSON.deserialize('{"success": true, "id": "' + record.Id + '"}', clazz);
}
}