Skip to content

Commit df50c24

Browse files
committed
Implemented TestDataFactory and tests for Title Normalization. Solved issues in LeadTrigger and LeadTriggerHandler
1 parent 75c0944 commit df50c24

File tree

6 files changed

+140
-16
lines changed

6 files changed

+140
-16
lines changed

force-app/main/default/classes/LeadTriggerHandler.cls

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,32 @@ public with sharing class LeadTriggerHandler {
3232
*/
3333
public static void handleTitleNormalization(List<Lead> leadsToNormalize) {
3434
for (Lead ld : leadsToNormalize) {
35-
if (ld.title == 'vp' || ld.title.contains('v.p.') || ld.title.contains('vice president')) {
35+
if (String.isBlank(ld.Title)) {
36+
continue;
37+
}
38+
if (
39+
ld.title.containsIgnoreCase('vp') ||
40+
ld.title.containsIgnoreCase('v.p.') ||
41+
ld.title.containsIgnoreCase('vice president')
42+
) {
3643
ld.Title = 'Vice President';
3744
} else if (
38-
ld.title.contains('mgr') ||
39-
ld.title.contains('manage') ||
40-
ld.title.contains('head of department')
45+
ld.title.containsIgnoreCase('mgr') ||
46+
ld.title.containsIgnoreCase('manage') ||
47+
ld.title.containsIgnoreCase('head of department')
4148
) {
4249
ld.Title = 'Manager';
43-
} else if (ld.title.contains('exec') || ld.title == 'chief' || ld.title.contains('head')) {
50+
} else if (
51+
ld.title.containsIgnoreCase('exec') ||
52+
ld.title.containsIgnoreCase('chief') ||
53+
ld.title.containsIgnoreCase('head')
54+
) {
4455
ld.Title = 'Executive';
45-
} else if (ld.title.contains('assist') || ld.title.contains('deputy') || ld.title == 'jr') {
56+
} else if (
57+
ld.title.containsIgnoreCase('assist') ||
58+
ld.title.containsIgnoreCase('deputy') ||
59+
ld.title.containsIgnoreCase('jr')
60+
) {
4661
ld.Title = 'Assistant';
4762
}
4863
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* This class contains unit tests for validating the behavior of
3+
* Apex class LeadTriggerHandler
4+
* and trigger LeadTrigger.
5+
*
6+
* Implemented by Oxana Suvorova
7+
*/
8+
@isTest
9+
private class LeadTriggerHandlerTest {
10+
11+
/*
12+
* LeadTriggerHandler.handleTitleNormalization insert test
13+
*/
14+
@isTest
15+
static void testHandleTitleNormalization_insert() {
16+
// Prepare the test data
17+
List<String> titles = new List<String>{
18+
'vp', 'v.p.', 'vice president', 'VPOTUS', 'V.P.', 'vice person', // 5 'Vice President'
19+
'mgr', 'manage', 'head of department', '', 'Manager', // 4 'Manager'
20+
'exec', 'chief', 'head', 'Chief Executive', null, // 4 'Executive'
21+
'assist', 'deputy', 'jr', 'DEPUTY', 'junior'}; // 4 'Assistant'
22+
23+
List<Lead> leadsToInsert = TestDataFactory.createLeadsByTitle(titles, false);
24+
25+
// Perform the test
26+
Test.startTest();
27+
Database.insert(leadsToInsert);
28+
Test.stopTest();
29+
30+
// Retrieve the count of Leads grouped by Title
31+
List<AggregateResult> groupedLeads = [
32+
SELECT
33+
Title,
34+
COUNT(Name) aggCount
35+
FROM Lead
36+
GROUP BY Title
37+
];
38+
39+
Map<String, Integer> countsByTitle = new Map<String, Integer>();
40+
Integer totalRecords = 0;
41+
for (AggregateResult ar : groupedLeads) {
42+
totalRecords += (Integer) ar.get('aggCount');
43+
countsByTitle.put((String) ar.get('Title'), (Integer) ar.get('aggCount'));
44+
}
45+
46+
// Assert that the Title have been correctly changed
47+
Assert.isTrue(countsByTitle.get('Vice President') == 5, 'Expected 5 Leads with Title \'Vice President\'');
48+
Assert.isTrue(countsByTitle.get('Manager') == 4, 'Expected 4 Leads with Title \'Manager\'');
49+
Assert.isTrue(countsByTitle.get('Executive') == 4, 'Expected 4 Leads with Title \'Executive\'');
50+
Assert.isTrue(countsByTitle.get('Assistant') == 4, 'Expected 4 Leads with Title \'Assistant\'');
51+
Assert.areEqual(21, totalRecords, 'Expected 21 Lead records');
52+
}
53+
54+
/*
55+
* LeadTriggerHandler.handleTitleNormalization update test
56+
*/
57+
@isTest
58+
static void testHandleTitleNormalization_update() {
59+
// Prepare the test data
60+
List<String> titles = new List<String>{'asist', 'depty', 'junior', null, ''};
61+
List<Lead> leadsToUpdate = TestDataFactory.createLeadsByTitle(titles, true);
62+
63+
for (Lead lead : leadsToUpdate) {
64+
lead.Title = 'jr';
65+
}
66+
// Perform the test
67+
Test.startTest();
68+
Database.update(leadsToUpdate);
69+
Test.stopTest();
70+
71+
// Retrieve updated Leads
72+
List<Lead> leadsAfterUpdate = [
73+
SELECT
74+
Title
75+
FROM Lead
76+
WHERE Id IN :leadsToUpdate
77+
];
78+
79+
// Assert that the Title have been correctly updated
80+
Assert.isTrue(leadsAfterUpdate.size() == 5, 'Expected 5 Lead records');
81+
for (Lead lead : leadsAfterUpdate) {
82+
Assert.areEqual('Assistant', lead.Title, 'Expected Title \'Assistant\'');
83+
}
84+
}
85+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* TestDataFactory contain methods that can be called by test methods to perform useful tasks
3+
*/
4+
@isTest
5+
public class TestDataFactory {
6+
7+
public static List<Lead> createLeadsByTitle(List<String> titles, Boolean doInsert) {
8+
List<Lead> leadsToInsert = new List<Lead>();
9+
for (String title : titles) {
10+
leadsToInsert.add(
11+
new Lead(LastName = 'Test Lead ' + title,
12+
Company = 'Test Company',
13+
Title = title));
14+
}
15+
if (doInsert) {
16+
Database.insert(leadsToInsert);
17+
}
18+
19+
return leadsToInsert;
20+
}
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

force-app/main/default/triggers/LeadTrigger.trigger

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
* - It's essential to test the trigger thoroughly after making any changes to ensure its correct functionality.
1616
* - Debugging skills will be tested, so students should look out for discrepancies between the expected and actual behavior.
1717
*/
18-
trigger LeadTrigger on Lead(before insert) {
18+
trigger LeadTrigger on Lead(before insert, before update, after insert, after update) {
1919
switch on Trigger.operationType {
20-
when BEFORE_INSERT {
20+
when BEFORE_INSERT, BEFORE_UPDATE {
2121
LeadTriggerHandler.handleTitleNormalization(Trigger.new);
2222
LeadTriggerHandler.handleAutoLeadScoring(Trigger.new);
2323
}
24-
when BEFORE_UPDATE {
25-
LeadTriggerHandler.handleTitleNormalization(Trigger.new);
26-
LeadTriggerHandler.handleAutoLeadScoring(Trigger.new);
27-
}
28-
when AFTER_INSERT {
29-
LeadTriggerHandler.handleLeadAutoConvert(Trigger.new);
30-
}
31-
when AFTER_UPDATE {
24+
when AFTER_INSERT, AFTER_UPDATE {
3225
LeadTriggerHandler.handleLeadAutoConvert(Trigger.new);
3326
}
3427
}

0 commit comments

Comments
 (0)