-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBulkPatterns.cls
More file actions
171 lines (148 loc) · 6.35 KB
/
TestBulkPatterns.cls
File metadata and controls
171 lines (148 loc) · 6.35 KB
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
// TestBulkPatterns.initTestObjects
public class TestBulkPatterns {
// Prepare the specified number of Opportunities with Contact Roles on each.
// The Contact Roles are distributed evenly among the number of Contacts specified.
public static void initTestObjects (
List<Opportunity> newOpportunities,
Integer numberOfOpportunities,
Integer numberOfOtherOpportunities,
Integer contactRolesPerOpp,
Integer numberOfContacts) {
if(numberOfContacts < contactRolesPerOpp)
numberOfContacts = contactRolesPerOpp;
List<Contact> cts = new List<Contact>();
for(Integer x=0; x<numberOfContacts; x++) {
cts.add(new Contact(LastName = 'cttest_' + String.valueOf(x)));
}
insert cts;
/* The code for creating Contacts and Opportunities shows a common Test Pattern to
* create a specified number of objects with different names or other field values.
*/
newOpportunities.clear();
for(Integer x=0; x<numberOfOpportunities; x++) {
newOpportunities.add(
new Opportunity(
CloseDate = Date.Today().addDays(5),
Name = 'optest_' + String.valueOf(x),
StageName = 'Prospecting'
)
);
}
insert newOpportunities;
List<Opportunity> otherOpportunities = new List<Opportunity>();
for(Integer x=0; x<numberOfOtherOpportunities; x++) {
otherOpportunities.add(
new Opportunity(
CloseDate = Date.Today().addDays(5),
Name = 'optest_' + String.valueOf(x + numberOfOpportunities),
StageName = 'Prospecting'
)
);
}
insert otherOpportunities;
// Combine newOpportunities and otherOpportunities for creating OpportunityContactRoles
otherOpportunities.addAll(newOpportunities);
// Now insert Contact Roles
List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();
Integer contactNumber = 0;
for(Opportunity op: otherOpportunities) {
for(Integer ocrNumber = 0; ocrNumber < contactRolesPerOpp; ocrNumber++) {
ocrList.add(
new OpportunityContactRole(
OpportunityId = op.id,
ContactId = cts[contactNumber].id
)
);
contactNumber++;
if(contactNumber >= numberOfContacts)
contactNumber = 0;
}
}
insert ocrList;
}
/* Test utility function that takes a list of Opportunity Objects and makes sure that each
* one has a primary Contact.
*/
public static void validateOCRs(List<Opportunity> opps) {
// get map for IDs
Map<ID, Opportunity> oppMap = new Map<ID, Opportunity>(opps);
// Query for primary Contacts
List<OpportunityContactRole> ocrs =
[SELECT ID, OpportunityID
FROM OpportunityContactRole
WHERE OpportunityId IN :oppMap.keyset()
AND isPrimary = TRUE];
// Create OpportunityIDs Set with Primary Contacts
Set<ID> opportunitiesWithPrimaryContact = new Set<ID>();
for(OpportunityContactRole ocr: ocrs) {
opportunitiesWithPrimaryContact.add(ocr.OpportunityID);
}
// Now make sure every Opportunity has a Primary Contact Role
for(Opportunity opp: opps) {
System.Assert(opportunitiesWithPrimaryContact.contains(op.id));
}
}
// EOMethod initTestObjects
}
// This functional test demonstrates how easy it is to tie the utility functions together.
static testMethod void bulkOpportunityTest() {
List<Opportunity> opps = new List<Opportunity>();
/* (you may need to adjust these numbers)
* List<Opportunity> newOpportunities,
numberOfOpportunities,
numberOfOtherOpportunities,
contactRolesPerOpp,
numberOfContacts
*/
initTestObjects(opps, 100, 15, 15, 40);
/* The bulkOpportunityTest test and utility functions validate the part of the
* requirements which ensure that a Primary Contact exists for Opportunities already
* associated with Contacts.
*/
Test.StartTest();
for(Opportunity opp: opps) {
opp.StageName = 'Qualification';
}
update opps;
Test.StopTest();
validateOCRs(opps);
} // EOMethod bulkOpportunityTest
static testMethod void createTaskTest() {
Integer numberOfOpportunities = 100;
List<Opportunity> opps = new List<Opportunity>();
for(Integer x=0; x<numberOfOpportunities; x++) {
opps.add(new Opportunity(
CloseDate = Date.Today().addDays(5),
Name = 'optest_' + String.valueOf(x),
StageName = 'Prospecting')
);
}
insert opps;
/* The createTaskTest method tests the condition where there are no Contacts
* associated with an Opportunity.
*/
Test.StartTest();
for(Opportunity opp: opps) {
opp.StageName = 'Qualification';
}
update opps;
Test.StopTest();
List<Task> tasks = [
SELECT ID, OwnerID, WhatID, Status, Subject, Type
FROM Task
WHERE OwnerID = :UserInfo.getUserID()
AND TYPE = 'Other'
AND IsClosed = FALSE
AND Subject = 'Assign Primary Contact'
];
system.assertEquals(numberOfOpportunities, tasks.size());
} // EOMethod createTaskTest
public static void afterUpdateOpportunityCommon(
List<Opportunity> newList,
Map<ID, Opportunity> newMap,
Map<ID, Opportunity> oldMap
) {
// Pattern 2 - Straightforward common implementation
Set<ID> opportunityIDsWithStagenameChanges = new Set<ID>();
}
} // EOClass TestBulkPatterns