Skip to content

Instantly share code, notes, and snippets.

@aidan-harding
Created November 16, 2021 15:57
Show Gist options
  • Save aidan-harding/9e19f17dabd66cdb27279b60b70ab4bb to your computer and use it in GitHub Desktop.
Save aidan-harding/9e19f17dabd66cdb27279b60b70ab4bb to your computer and use it in GitHub Desktop.
Tests for a CMDT-driven Apex object transformation library
/**
* @author aidan@nebulaconsulting.co.uk
* @date 15/11/2021
*/
@IsTest
private class TransformationTest {
@IsTest
static void passThroughSObjectToOtherSObject() {
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'FirstName'),
new Transformation_Field__mdt(Source_Field__c = 'LastName', Target_Field__c = 'LastName')
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Contact.class);
Lead theLead = new Lead(FirstName = 'A', LastName = 'B');
Contact newContact = (Contact)thisTransformation.call(theLead);
System.assertEquals(theLead.FirstName, newContact.FirstName);
System.assertEquals(theLead.LastName, newContact.LastName);
}
@IsTest
static void nullInputValues() {
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'FirstName'),
new Transformation_Field__mdt(Source_Field__c = 'LastName', Target_Field__c = 'LastName')
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Contact.class);
Lead theLead = new Lead();
Contact newContact = (Contact)thisTransformation.call(theLead);
System.assertEquals(null, newContact.FirstName);
System.assertEquals(null, newContact.LastName);
}
@IsTest
static void multipleDestinations() {
String description = 'Generated by transform';
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'FirstName'),
new Transformation_Field__mdt(Source_Field__c = 'LastName', Target_Field__c = 'LastName'),
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'Description')
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Contact.class);
Lead theLead = new Lead(FirstName = 'A', LastName = 'B');
Contact newContact = (Contact)thisTransformation.call(theLead);
System.assertEquals(theLead.FirstName, newContact.FirstName);
System.assertEquals(theLead.LastName, newContact.LastName);
System.assertEquals(theLead.FirstName, newContact.Description);
}
@IsTest
static void generateValues() {
String description = 'Generated by transform';
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'FirstName'),
new Transformation_Field__mdt(Source_Field__c = 'LastName', Target_Field__c = 'LastName'),
new Transformation_Field__mdt(
Target_Field__c = 'Description',
Apex_Class__c = StringConstant.class.getName(),
Apex_Class_Parameters__c = JSON.serialize(new Map<String, Object>{'value' => description}))
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Contact.class);
Lead theLead = new Lead(FirstName = 'A', LastName = 'B');
Contact newContact = (Contact)thisTransformation.call(theLead);
System.assertEquals(theLead.FirstName, newContact.FirstName);
System.assertEquals(theLead.LastName, newContact.LastName);
System.assertEquals(description, newContact.Description);
}
@IsTest
static void transformSObjectToMap() {
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'first_name'),
new Transformation_Field__mdt(Source_Field__c = 'Birthdate', Target_Field__c = 'date_of_birth', Apex_Class__c = 'nebc.JsonSerialize')
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Map<String, Object>.class);
Contact theContact = new Contact(FirstName = 'A', Birthdate = Date.today());
Map<String, Object> newMap = (Map<String, Object>)thisTransformation.call(theContact);
System.assertEquals(theContact.FirstName, newMap.get('first_name'));
System.assertEquals(JSON.serialize(theContact.Birthdate), newMap.get('date_of_birth'));
}
@IsTest
static void sObjectToMapRoundTrip() {
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'first_name'),
new Transformation_Field__mdt(Source_Field__c = 'LastName', Target_Field__c = 'last_name')
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Map<String, Object>.class);
Contact theContact = new Contact(FirstName = 'A', Birthdate = Date.today());
Map<String, Object> newMap = (Map<String, Object>)thisTransformation.call(theContact);
System.assertEquals(theContact.FirstName, newMap.get('first_name'));
System.assertEquals(theContact.LastName, newMap.get('last_name'));
Transformation reverseTransformation = thisTransformation.getReverse(Contact.class);
Contact roundTripContact = (Contact)reverseTransformation.call(newMap);
System.assertEquals(theContact.FirstName, roundTripContact.FirstName);
System.assertEquals(theContact.LastName, roundTripContact.LastName);
}
@IsTest
static void transformSObjectToDeepMap() {
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'FirstName', Target_Field__c = 'person.first_name'),
new Transformation_Field__mdt(Source_Field__c = 'LastName', Target_Field__c = 'person.last_name')
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Map<String, Object>.class);
Contact theContact = new Contact(FirstName = 'A', LastName = 'B');
Map<String, Object> newMap = (Map<String, Object>)thisTransformation.call(theContact);
Map<String, Object> person = (Map<String, Object>)newMap.get('person');
System.assertEquals(theContact.FirstName, person.get('first_name'));
System.assertEquals(theContact.LastName, person.get('last_name'));
}
@IsTest
static void transformDeepMapToSObject() {
List<Transformation_Field__mdt> transformationFieldMetadata = new List<Transformation_Field__mdt> {
new Transformation_Field__mdt(Source_Field__c = 'person.first_name', Target_Field__c = 'FirstName' ),
new Transformation_Field__mdt(Source_Field__c = 'person.last_name', Target_Field__c = 'LastName' )
};
Transformation thisTransformation = new Transformation(transformationFieldMetadata, Lead.class);
Map<String, Object> theMap = new Map<String, Object>{'person' => new Map<String, Object> { 'FirstName' => 'A', 'LastName' => 'B'} };
Lead newLead = (Lead)thisTransformation.call(theMap);
Map<String, Object> person = (Map<String, Object>)theMap.get('person');
System.assertEquals(person.get('first_name'), newLead.FirstName);
System.assertEquals(person.get('last_name'), newLead.LastName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment