Skip to content

Instantly share code, notes, and snippets.

@NikiforovAll
Created August 22, 2017 15:39
Show Gist options
  • Save NikiforovAll/a4172080f57eb91a0b0df26d63be112b to your computer and use it in GitHub Desktop.
Save NikiforovAll/a4172080f57eb91a0b0df26d63be112b to your computer and use it in GitHub Desktop.
@isTest
public class ObjectHierarchyManagerTest {
@isTest
private static void testBasicHierarchyInsert(){
insertObjectMappingSettings(1);
Account acc = new Account(Name = 'Test');
Account acc2 = new Account(Name = 'Test2');
List<Contact> contactList = new List<Contact>{
new Contact(LastName = 'Contact1'),
new Contact(LastName = 'Contact2'),
new Contact(LastName = 'Contact3'),
new Contact(LastName = 'Contact4')
};
List<SObjectContainer> testData = new List<SObjectContainer>{
new SObjectContainer('Account')
.setData(acc)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[0])
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[1])
),
new SObjectContainer('Account')
.setData(acc2)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[2])
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[3])
)
};
ObjectHierarchyManager manager = new ObjectHierarchyManager('Test');
manager.commitObjects(testData);
List<Account> accountResult = [SELECT Id, (SELECT ID FROM Contacts) FROM Account];
List<Contact> contactResult = [SELECT Id, AccountId FROM Contact];
system.debug('accountResult' + accountResult);
system.debug('contactResult' + contactResult);
system.assertEquals(2, accountResult.size());
system.assertEquals(4, contactResult.size());
System.assertEquals(2, accountResult[0].Contacts.size());
System.assertEquals(2, accountResult[1].Contacts.size());
}
@isTest
private static void testAdvancedHierarchyInsert(){
insertObjectMappingSettings(2);
Account acc = new Account(Name = 'Test');
Account acc2 = new Account(Name = 'Test2');
List<Contact> contactList = new List<Contact>{
new Contact(LastName = 'Contact1'),
new Contact(LastName = 'Contact2'),
new Contact(LastName = 'Contact3'),
new Contact(LastName = 'Contact4')
};
List<Wallet__c> walletList = new List<Wallet__c>{
new Wallet__c(WalletId__c = 'W1'),
new Wallet__c(WalletId__c = 'W2'),
new Wallet__c(WalletId__c = 'W3'),
new Wallet__c(WalletId__c = 'W4')
};
List<SObjectContainer> testData = new List<SObjectContainer>{
new SObjectContainer('Account')
.setData(acc)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[0])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[0])
)
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[1])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[1])
)
),
new SObjectContainer('Account')
.setData(acc2)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[2])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[2])
)
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[3])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[3])
)
)
};
ObjectHierarchyManager manager = new ObjectHierarchyManager('Test');
manager.commitObjects(testData);
List<Account> accountResult = [SELECT Id, (SELECT ID FROM Contacts) FROM Account];
List<Contact> contactResult = [SELECT Id, (SELECT Id FROM Wallets__r) AccountId FROM Contact];
List<Wallet__c> walletResult = [SELECT Id, Contact__c FROM Wallet__c];
system.debug('accountResult' + accountResult);
system.debug('contactResult' + contactResult);
system.debug('walletResult' + walletResult);
system.assertEquals(2, accountResult.size());
system.assertEquals(4, contactResult.size());
system.assertEquals(4, walletResult.size());
}
@isTest
private static void test_upsert(){
insertObjectMappingSettings(3);
Account acc = new Account(Name = 'Test');
Account acc2 = new Account(Name = 'Test2');
List<Contact> contactList = new List<Contact>{
new Contact(LastName = 'Contact1'),
new Contact(LastName = 'Contact2'),
new Contact(LastName = 'Contact3'),
new Contact(LastName = 'Contact4')
};
List<Wallet__c> walletList = new List<Wallet__c>{
new Wallet__c(WalletId__c = 'W1'),
new Wallet__c(WalletId__c = 'W2'),
new Wallet__c(WalletId__c = 'W3'),
new Wallet__c(WalletId__c = 'W4')
};
insert walletList;
for(Wallet__c wallet : walletList){
wallet.Amount__c = 10;
wallet.Id = null;
}
List<SObjectContainer> testData = new List<SObjectContainer>{
new SObjectContainer('Account')
.setData(acc)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[0])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[0])
)
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[1])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[1])
)
),
new SObjectContainer('Account')
.setData(acc2)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[2])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[2])
)
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[3])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[3])
)
)
};
ObjectHierarchyManager manager = new ObjectHierarchyManager('Test');
manager.commitObjects(testData);
List<Account> accountResult = [SELECT Id, (SELECT ID FROM Contacts) FROM Account];
List<Contact> contactResult = [SELECT Id, (SELECT Id FROM Wallets__r) AccountId FROM Contact];
List<Wallet__c> walletResult = [SELECT Id, Amount__c, Contact__c FROM Wallet__c];
system.debug('accountResult' + accountResult);
system.debug('contactResult' + contactResult);
system.debug('walletResult' + walletResult);
system.assertEquals(2, accountResult.size());
system.assertEquals(4, contactResult.size());
system.assertEquals(4, walletResult.size());
system.assertEquals(10, walletResult[0].Amount__c);
}
@isTest
private static void test_update() {
insertObjectMappingSettings(4);
Account acc = new Account(Name = 'Test');
Account acc2 = new Account(Name = 'Test2');
List<Contact> contactList = new List<Contact>{
new Contact(LastName = 'Contact1'),
new Contact(LastName = 'Contact2'),
new Contact(LastName = 'Contact3'),
new Contact(LastName = 'Contact4')
};
List<Wallet__c> walletList = new List<Wallet__c>{
new Wallet__c(WalletId__c = 'W1'),
new Wallet__c(WalletId__c = 'W2'),
new Wallet__c(WalletId__c = 'W3'),
new Wallet__c(WalletId__c = 'W4')
};
insert walletList;
for(Wallet__c wallet : walletList){
wallet.Amount__c = 10;
}
List<SObjectContainer> testData = new List<SObjectContainer>{
new SObjectContainer('Account')
.setData(acc)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[0])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[0])
)
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[1])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[1])
)
),
new SObjectContainer('Account')
.setData(acc2)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[2])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[2])
)
)
.addChildContainer(
new SObjectContainer('Contact')
.setData(contactList[3])
.addChildContainer(
new SObjectContainer('Wallet')
.setData(walletList[3])
)
)
};
ObjectHierarchyManager manager = new ObjectHierarchyManager('Test');
manager.commitObjects(testData);
List<Account> accountResult = [SELECT Id, (SELECT ID FROM Contacts) FROM Account];
List<Contact> contactResult = [SELECT Id, (SELECT Id FROM Wallets__r) AccountId FROM Contact];
List<Wallet__c> walletResult = [SELECT Id, Amount__c, Contact__c FROM Wallet__c];
system.debug('accountResult' + accountResult);
system.debug('contactResult' + contactResult);
system.debug('walletResult' + walletResult);
system.assertEquals(2, accountResult.size());
system.assertEquals(4, contactResult.size());
system.assertEquals(4, walletResult.size());
system.assertEquals(10, walletResult[0].Amount__c);
}
private static void insertObjectMappingSettings(Integer mockNumber){
if(mockNumber == 1){
insert new List<Mapper_Object_Settings__c>{
new Mapper_Object_Settings__c(Name = 'Test1', Mode__c = 'Insert', Parent_Container_Id__c = 'Account',Parent_Relationship_Name__c ='AccountId', Mapping_Name__c = 'Test', Container_Id__c = 'Contact')
};
}
if(mockNumber == 2){
insert new List<Mapper_Object_Settings__c>{
new Mapper_Object_Settings__c(Name = 'Test1', Mode__c = 'Insert', Parent_Container_Id__c = 'Account',Parent_Relationship_Name__c ='AccountId', Mapping_Name__c = 'Test', Container_Id__c = 'Contact'),
new Mapper_Object_Settings__c(Name = 'Test2', Mode__c = 'Insert', Parent_Container_Id__c = 'Contact', Parent_Relationship_Name__c ='Contact__c', Mapping_Name__c = 'Test', Container_Id__c = 'Wallet')
};
}
if(mockNumber == 3){
insert new List<Mapper_Object_Settings__c>{
new Mapper_Object_Settings__c(Name = 'Test1', Mode__c = 'Insert', Parent_Container_Id__c = 'Account',Parent_Relationship_Name__c ='AccountId', Mapping_Name__c = 'Test', Container_Id__c = 'Contact'),
new Mapper_Object_Settings__c(Name = 'Test2', Mode__c = 'Upsert', Parent_Container_Id__c = 'Contact', Parent_Relationship_Name__c ='Contact__c', Mapping_Name__c = 'Test', Container_Id__c = 'Wallet', External_Id__c = 'WalletId__c')
};
}
if(mockNumber == 4){
insert new List<Mapper_Object_Settings__c>{
new Mapper_Object_Settings__c(Name = 'Test1', Mode__c = 'Insert', Parent_Container_Id__c = 'Account',Parent_Relationship_Name__c ='AccountId', Mapping_Name__c = 'Test', Container_Id__c = 'Contact'),
new Mapper_Object_Settings__c(Name = 'Test2', Mode__c = 'Update', Parent_Container_Id__c = 'Contact', Parent_Relationship_Name__c ='Contact__c', Mapping_Name__c = 'Test', Container_Id__c = 'Wallet')
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment