-
Notifications
You must be signed in to change notification settings - Fork 4
/
EmployeeBonusManagerTest.cls
164 lines (129 loc) · 5.1 KB
/
EmployeeBonusManagerTest.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
@isTest
private class EmployeeBonusManagerTest {
@TestSetup
static void setup(){
Account testAccount = TestData.createAccounts(1)[0];
testAccount.Name = 'Apex Testing DF16 Co.';
insert testAccount;
}
private static Account testAccount {
get {
if (testAccount == null) {
testAccount = [ SELECT Id, Name FROM Account WHERE Name = 'Apex Testing DF16 Co.' LIMIT 1];
}
return testAccount;
}
set;
}
// test employee with some open opps and some closed opps
// they should get a bonus
@isTest
static void testAwardBonus() {
// set up data
User employee = TestData.standardUser;
List<Opportunity> opps = TestData.createOpportunities(testAccount, 3);
opps[0].Amount = 1000;
opps[0].StageName = 'Closed Won';
opps[1].Amount = 10000;
opps[1].StageName = 'Prospecting';
opps[2].Amount = 100000;
opps[2].StageName = 'Closed Won';
insert opps;
// execute the logic we're testing
EmployeeBonusManager.updateEmployeeBonuses(employee.Id);
// query for updated record
employee = queryForUser(employee.Id);
// assert expected results
System.assertEquals(1010, employee.Bonus__c, 'Employee has have bonus for $101,000 in opps');
}
// test employee who should get the maximum bonus
static void testAwardMaximumBonus() {
// set up data
User employee = TestData.standardUser;
List<Opportunity> opps = TestData.createOpportunities(testAccount, 1);
opps[0].Amount = 60000000;
opps[0].StageName = 'Closed Won';
insert opps;
// execute the logic we're testing
EmployeeBonusManager.updateEmployeeBonuses(employee.Id);
// query for updated record
employee = queryForUser(employee.Id);
// assert expected results
System.assertEquals(25000, employee.Bonus__c, 'Employee should be awarded the maximum bonus');
}
// test employee with no opps
// they shouldn't get a bonus
@isTest
static void testNoBonusNoOpps(){
// set up data
User employee = TestData.standardUser;
// execute the logic we're testing
EmployeeBonusManager.updateEmployeeBonuses(employee.Id);
// query for updated record
employee = queryForUser(employee.Id);
// assert expected results
System.assertEquals(null, employee.Bonus__c, 'Employee has no opportunities and should have no bonus');
}
// test employee with only open opps
// they shouldn't get a bonus
@isTest
static void testNoBonusOnlyOpenOpps(){
// set up data
User employee = TestData.standardUser;
List<Opportunity> opps = TestData.createOpportunities(testAccount, 3);
for (Opportunity opp : opps) {
opp.StageName = 'Prospecting';
}
insert opps;
// execute the logic we're testing
EmployeeBonusManager.updateEmployeeBonuses(employee.Id);
// query for updated record
employee = queryForUser(employee.Id);
// assert expected results
System.assertEquals(null, employee.Bonus__c, 'Employee has only open opportunities and should have no bonus');
}
// test negative total opp amount
// this should throw an exception
@isTest
static void testNegativeOppTotal(){
// set up data
User employee = TestData.standardUser;
List<Opportunity> opps = TestData.createOpportunities(testAccount, 3);
for (Opportunity opp : opps) {
opp.StageName = 'Closed Won';
opp.Amount = -5;
}
insert opps;
Boolean exceptionThrown = false;
try {
// execute the logic we're testing
EmployeeBonusManager.updateEmployeeBonuses(employee.Id);
} catch (Exception ex) {
exceptionThrown = true;
System.assert(ex instanceOf EmployeeBonusManager.BonusException, 'Thrown exception should be a Bonus Exception');
}
// assert expected results
System.assert(exceptionThrown, 'An exception should have been thrown');
}
// test employee bonus with several opps
@isTest
static void testBonusBulk(){
// set up data
User employee = TestData.standardUser;
List<Opportunity> opps = TestData.createOpportunities(testAccount, 200);
for (Opportunity opp : opps) {
opp.Amount = 10000;
opp.StageName = 'Closed Won';
}
insert opps;
// execute the logic we're testing
EmployeeBonusManager.updateEmployeeBonuses(employee.Id);
// query for updated record
employee = queryForUser(employee.Id);
// assert expected results
System.assertEquals(25000, employee.Bonus__c, 'Employee should be awarded the maximum bonus');
}
private static User queryForUser(Id userId) {
return [SELECT Id, Bonus__c FROM User WHERE Id = :userId];
}
}