Skip to content

Commit

Permalink
Add tests for new LWC Wizard Apex
Browse files Browse the repository at this point in the history
  • Loading branch information
aheber committed Apr 22, 2024
1 parent 72769f1 commit 6c83b96
Show file tree
Hide file tree
Showing 14 changed files with 651 additions and 3 deletions.
68 changes: 68 additions & 0 deletions dlrs/main/classes/AsyncApexJobsSelectorTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@isTest
private class AsyncApexJobsSelectorTest {
@isTest
static void testGetScheduledInstancesOfType() {
List<AsyncApexJob> jobs = new AsyncApexJobsSelector().getAllScheduledJobs();
Integer jobCountStart = jobs.size();
if (jobCountStart > 98) {
System.debug('not enough capacity to schedule new, exiting early');
return;
}

// we don't get database isolation here
// ensure tests here don't break if extra jobs are scheduled
String jobId1 = System.schedule(
'TestJob112233',
'0 0 * * * ? 2100',
new RollupJob()
);
String jobId2 = System.schedule(
'TestJob998877',
'0 0 * * * ? 2101',
new RollupJob()
);

jobs = new AsyncApexJobsSelector().getAllScheduledJobs();
Assert.areEqual(
2,
jobs.size() - jobCountStart,
'Expected total scheduled jobs to have increased by two'
);

jobs = new List<AsyncApexJob>(
new AsyncApexJobsSelector().getScheduledInstancesOfType(RollupJob.class)
);

Assert.isTrue(
jobs.size() >= 2,
'Exepcted at least 2 jobs, found ' + jobs.size()
);

Boolean hasJob1 = false;
Boolean hasJob2 = false;

for (AsyncApexJob job : jobs) {
if (job.CronTriggerId == (Id) jobId1) {
hasJob1 = true;
}
if (job.CronTriggerId == (Id) jobId2) {
hasJob2 = true;
}
}

Assert.isTrue(
hasJob1,
'Expected ' + jobId1 + ' to be included in jobs list:' + jobs
);
Assert.isTrue(
hasJob2,
'Expected ' + jobId2 + ' to be included in jobs list:' + jobs
);

// clear jobs
System.abortJob(jobId1);
System.abortJob(jobId2);

Assert.isTrue(jobs.size() >= 2, 'Expected at least two jobs scheduled');
}
}
5 changes: 5 additions & 0 deletions dlrs/main/classes/AsyncApexJobsSelectorTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 2 additions & 0 deletions dlrs/main/classes/CustomMetadataService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public class CustomMetadataService {
);
}

@TestVisible
class ApexMdApiDeployCallback implements Metadata.DeployCallback {
public void handleResult(
Metadata.DeployResult result,
Expand All @@ -286,6 +287,7 @@ public class CustomMetadataService {
}
}

@TestVisible
class DeleteMetadataQueueable implements Queueable, Database.AllowsCallouts {
SObjectType qualifiedMetadataType;
List<String> customMetadataFullNames;
Expand Down
127 changes: 127 additions & 0 deletions dlrs/main/classes/CustomMetadataServiceTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,131 @@

@IsTest
private class CustomMetadataServiceTest {
@IsTest
static void testInitiateMetadataSave() {
try {
CustomMetadataService.initiateMetadataSave(
new List<LookupRollupSummary2__mdt>{
new LookupRollupSummary2__mdt(
DeveloperName = 'TestRec',
Label = 'Test Rec',
Active__c = true,
RowLimit__c = 100
)
}
);
Assert.fail('Expected to fail starting the deployment');
} catch (System.AsyncException e) {
Assert.areEqual(
'Metadata cannot be deployed from within a test',
e.getMessage()
);
}
}

@IsTest
static void testDeleteMetadataAsync() {
Test.startTest();
Test.setMock(
WebServiceMock.class,
new MetadataServiceDeleteSuccessCalloutMock()
);
CustomMetadataService.deleteMetadataAsync(
LookupRollupSummary2__mdt.getSObjectType(),
new List<String>{ 'LookupRollupSummary2__mdt.Test123' }
);
Test.stopTest();
}

@IsTest
static void testDeleteMetadataAsyncFailed() {
Test.startTest();
Test.setMock(
WebServiceMock.class,
new MetadataServiceDeleteFailureCalloutMock()
);
CustomMetadataService.deleteMetadataAsync(
LookupRollupSummary2__mdt.getSObjectType(),
new List<String>{ 'LookupRollupSummary2__mdt.Test123' }
);
Test.stopTest();
}

@IsTest
static void testApexMdApiDeployCallback() {
CustomMetadataService.ApexMdApiDeployCallback cb = new CustomMetadataService.ApexMdApiDeployCallback();
Metadata.DeployResult result = new Metadata.DeployResult();
result.status = Metadata.DeployStatus.Succeeded;
Metadata.DeployMessage messageObj = new Metadata.DeployMessage();
messageObj.changed = true;
messageObj.success = true;
messageObj.fullName = 'TestRec';
messageObj.componentType = 'CustomMetadata';
messageObj.fullName = 'LookupRollupSummary2__mdt.TestRec';
Metadata.DeployDetails deployDetailsObj = new Metadata.DeployDetails();
deployDetailsObj.componentSuccesses.add(messageObj);
result.details = deployDetailsObj;
Metadata.DeployCallbackContext context = new Metadata.DeployCallbackContext();

// Invoke the callback's handleResult method.
cb.handleResult(result, context);

// expected Platform Event publishing DML
Assert.areEqual(1, Limits.getDmlStatements());
Assert.areEqual(1, Limits.getDmlRows());
}

public class MetadataServiceDeleteSuccessCalloutMock implements WebServiceMock {
public void doInvoke(
Object stub,
Object request,
Map<String, Object> response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType
) {
System.debug(request);
MetadataService.deleteMetadataResponse_element responseElement = new MetadataService.deleteMetadataResponse_element();
// MetadataService.createMetadataResponse_element responseElement = new MetadataService.createMetadataResponse_element();
MetadataService.DeleteResult res = new MetadataService.DeleteResult();
res.success = true;
res.fullName = 'myTestResult';
responseElement.result = new List<MetadataService.DeleteResult>{ res };

response.put('response_x', responseElement);
}
}

public class MetadataServiceDeleteFailureCalloutMock implements WebServiceMock {
public void doInvoke(
Object stub,
Object request,
Map<String, Object> response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType
) {
System.debug(request);
MetadataService.deleteMetadataResponse_element responseElement = new MetadataService.deleteMetadataResponse_element();
// MetadataService.createMetadataResponse_element responseElement = new MetadataService.createMetadataResponse_element();
MetadataService.DeleteResult res = new MetadataService.DeleteResult();
res.success = false;
res.errors = new List<MetadataService.Error>();
MetadataService.Error err = new MetadataService.Error();
err.message = 'Test Error';
err.statusCode = 'Error Code';
err.fields = new List<String>{ 'Field__1', 'Field_2' };
res.errors.add(err);
res.fullName = 'myTestResult';
responseElement.result = new List<MetadataService.DeleteResult>{ res };

response.put('response_x', responseElement);
}
}
}
106 changes: 106 additions & 0 deletions dlrs/main/classes/LookupRollupStatusCheckControllerTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
@IsTest
public with sharing class LookupRollupStatusCheckControllerTest {
@IsTest
static void testGetOutstandingScheduledItemsForLookup() {
List<LookupRollupSummary2__mdt> summaries = [
SELECT Id
FROM LookupRollupSummary2__mdt
LIMIT 1
];
if (summaries.isEmpty()) {
return;
}
Integer val = LookupRollupStatusCheckController.getOutstandingScheduledItemsForLookup(
summaries[0].Id
);
Assert.isTrue(val >= 0, 'Expected a value, even zero');
}

@IsTest
static void testgetScheduledFullCalculates() {
List<LookupRollupSummary2__mdt> summaries = [
SELECT Id, DeveloperName
FROM LookupRollupSummary2__mdt
LIMIT 1
];
if (summaries.isEmpty()) {
return;
}

// Build the CRON string
// Kickoff the calculate job for this lookup

String uniqueNameForJob =
'rollup_' +
summaries[0].DeveloperName +
'(' +
summaries[0].Id.to15() +
')';

String jobId;
try {
jobId = System.schedule(
uniqueNameForJob,
'0 0 * * * ? 2100',
new RollupCalculateJobSchedulable(summaries[0].Id, '')
);
} catch (Exception e) {
System.debug(
'Failed to schedule, probably because it is already scheduled:' +
e.getMessage()
);
}

Datetime nextRunDate = LookupRollupStatusCheckController.getScheduledFullCalculates(
summaries[0].Id
);
Assert.isNotNull(nextRunDate);
if (String.isNotBlank(jobId)) {
System.abortJob(jobId);
}
}

@IsTest
static void testHasChildTriggerDeployed() {
// limited to records in the org, would have to mock things
LookupRollupStatusCheckController.hasChildTriggerDeployed(null);

List<LookupRollupSummary2__mdt> rec = [
SELECT Id
FROM LookupRollupSummary2__mdt
WHERE Active__c = TRUE AND CalculationMode__c IN ('Realtime', 'Scheduled')
LIMIT 1
];
if (rec.size() == 0) {
return;
}

Boolean isDeployed = LookupRollupStatusCheckController.hasChildTriggerDeployed(
rec[0].Id
);
// Assume anything active and needing a trigger should have one
// this test could fail for silly reasons
Assert.areEqual(
true,
isDeployed,
'Expected a rollup that requires a trigger to have one, could be false-positive'
);
}

@IsTest
static void testGetScheduledJobs() {
Integer startVal = LookupRollupStatusCheckController.getScheduledJobs();
String jobId = System.schedule(
'Test Job 2000',
'0 0 * * * ? 2100',
new RollupJob()
);
Integer endVal = LookupRollupStatusCheckController.getScheduledJobs();
Assert.areEqual(
1,
endVal - startVal,
'Expected class to report an additional scheduled job'
);
System.abortJob(jobId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion dlrs/main/classes/ObjectSelectorController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public with sharing class ObjectSelectorController {
return objects;
}

class SObjectInfo {
public class SObjectInfo {
@AuraEnabled
public String fullName;
@AuraEnabled
Expand Down
20 changes: 20 additions & 0 deletions dlrs/main/classes/ObjectSelectorControllerTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@IsTest
public with sharing class ObjectSelectorControllerTest {
@IsTest
static void testGetParentObjList() {
List<ObjectSelectorController.SObjectInfo> objects = ObjectSelectorController.getParentObjList();
Assert.isFalse(objects.isEmpty());

Set<String> expected = new Set<String>{
'Account',
'Opportunity',
Schema.LookupRollupSummary2__mdt.getSObjectType().getDescribe().getName()
};
for (ObjectSelectorController.SObjectInfo sobj : objects) {
expected.remove(sobj.fullName);
}

// expected items should have been removed
Assert.isTrue(expected.isEmpty());
}
}
5 changes: 5 additions & 0 deletions dlrs/main/classes/ObjectSelectorControllerTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading

0 comments on commit 6c83b96

Please sign in to comment.