-
-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathRelatedLogEntriesController_Tests.cls
More file actions
134 lines (117 loc) · 5.96 KB
/
RelatedLogEntriesController_Tests.cls
File metadata and controls
134 lines (117 loc) · 5.96 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
//------------------------------------------------------------------------------------------------//
// This file is part of the Nebula Logger project, released under the MIT License. //
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. //
//------------------------------------------------------------------------------------------------//
@SuppressWarnings('PMD.ApexDoc, PMD.CyclomaticComplexity, PMD.ExcessiveParameterList, PMD.MethodNamingConventions, PMD.NcssMethodCount')
@IsTest(IsParallel=true)
private class RelatedLogEntriesController_Tests {
private static final Id TARGET_RECORD_ID = System.UserInfo.getUserId();
private static final Integer TOTAL_LOG_ENTRIES = 10;
private static final Integer TOTAL_RELATED_LOG_ENTRIES = 7;
static {
// Don't use the org's actual custom metadata records when running tests
LoggerConfigurationSelector.useMocks();
}
@TestSetup
static void setupData() {
LoggerSObjectHandler.shouldExecute(false);
Log__c log = (Log__c) LoggerMockDataCreator.createDataBuilder(Schema.Log__c.SObjectType).populateRequiredFields().getRecord();
insert log;
List<LogEntry__c> logEntries = new List<LogEntry__c>();
for (Integer i = 0; i < TOTAL_LOG_ENTRIES; i++) {
LogEntry__c logEntry = new LogEntry__c(Log__c = log.Id, Message__c = 'test ' + i);
if (i <= TOTAL_RELATED_LOG_ENTRIES) {
logEntry.RecordId__c = TARGET_RECORD_ID;
}
LoggerMockDataCreator.createDataBuilder(logEntry).populateRequiredFields().getRecord();
logEntries.add(logEntry);
}
insert logEntries;
}
@IsTest
static void it_should_return_matching_logEntryQueryResult_when_no_search_term_provided() {
String fieldSetName = getFieldSetName();
Integer rowLimit = 10;
String sortByFieldName = 'Name';
String sortDirection = 'ASC';
String searchTerm = null;
List<LogEntry__c> matchingLogEntries = [SELECT Id FROM LogEntry__c WHERE RecordId__c = :TARGET_RECORD_ID];
// TODO revisit how SOSL is tested - having to use setFixedSearchResults() means we're not really truly testing
// that the SOSL search query is working (other than confirming that the query string can be executed successfully),
// the test will just return the records specified
System.Test.setFixedSearchResults(new List<Id>(new Map<Id, SObject>(matchingLogEntries).keySet()));
RelatedLogEntriesController.LogEntryQueryResult result = RelatedLogEntriesController.getQueryResult(
TARGET_RECORD_ID,
fieldSetName,
rowLimit,
sortByFieldName,
sortDirection,
searchTerm,
false
);
System.Assert.areEqual(fieldSetName, result.fieldSet.name);
System.Assert.areEqual(matchingLogEntries.size(), result.records.size());
System.Assert.areEqual(new Map<Id, SObject>(matchingLogEntries).keySet(), new Map<Id, SObject>(result.records).keySet());
}
@IsTest
static void it_should_return_matching_logEntryQueryResult_when_search_term_provided() {
String fieldSetName = getFieldSetName();
Integer rowLimit = 10;
String sortByFieldName = 'Name';
String sortDirection = 'ASC';
String searchTerm = 'my searchTerm term';
Log__c log = [SELECT Id FROM Log__c];
LogEntry__c logEntryWithSearchTerm = new LogEntry__c(Log__c = log.Id, Message__c = searchTerm, RecordId__c = TARGET_RECORD_ID);
insert logEntryWithSearchTerm;
// TODO revisit how SOSL is tested - having to use setFixedSearchResults() means we're not really truly testing
// that the SOSL search query is working (other than confirming that the query string can be executed successfully),
// the test will just return the records specified
System.Test.setFixedSearchResults(new List<Id>{ logEntryWithSearchTerm.Id });
RelatedLogEntriesController.LogEntryQueryResult result = RelatedLogEntriesController.getQueryResult(
TARGET_RECORD_ID,
fieldSetName,
rowLimit,
sortByFieldName,
sortDirection,
searchTerm,
false
);
System.Assert.areEqual(fieldSetName, result.fieldSet.name);
System.Assert.areEqual(1, result.records.size());
System.Assert.areEqual(logEntryWithSearchTerm.Id, result.records[0].Id);
}
@IsTest
static void it_should_return_no_matching_logEntryQueryResult_when_search_term_provided_and_strict_mode_enabled() {
String fieldSetName = getFieldSetName();
Integer rowLimit = 10;
String sortByFieldName = 'Name';
String sortDirection = 'ASC';
String searchTerm = 'my searchTerm term';
Log__c log = [SELECT Id FROM Log__c];
LogEntry__c logEntryWithSearchTerm = new LogEntry__c(Log__c = log.Id, Message__c = searchTerm, RecordId__c = TARGET_RECORD_ID);
insert logEntryWithSearchTerm;
// TODO revisit how SOSL is tested - having to use setFixedSearchResults() means we're not really truly testing
// that the SOSL search query is working (other than confirming that the query string can be executed successfully),
// the test will just return the records specified
System.Test.setFixedSearchResults(new List<Id>{ logEntryWithSearchTerm.Id });
Boolean shouldEnableStrictSearch = true;
RelatedLogEntriesController.LogEntryQueryResult result = RelatedLogEntriesController.getQueryResult(
// explicitly don't pass the same recordId to ensure that strict search is performed in WHERE clause
LoggerMockDataCreator.createId(Schema.User.SObjectType),
fieldSetName,
rowLimit,
sortByFieldName,
sortDirection,
searchTerm,
shouldEnableStrictSearch
);
System.Assert.areEqual(fieldSetName, result.fieldSet.name);
System.Assert.areEqual(0, result.records.size());
}
static String getFieldSetName() {
Schema.FieldSet fieldSet = Schema.SObjectType.LogEntry__c.fieldSets.getMap().values().get(0);
String fieldSetNamespacePrefix = String.isBlank(fieldSet.getNamespace()) ? '' : fieldSet.getNamespace() + '__';
String fieldSetName = fieldSetNamespacePrefix + fieldSet.getName();
return fieldSetName;
}
}