Add setter method to fflib_SObjects2 plus unit-test

Signed-off-by: WimVelzeboer <wimvelzeboer@protonmail.com>
This commit is contained in:
WimVelzeboer
2021-11-04 17:18:46 +00:00
parent 6eee1acdb6
commit 1f45c8d4c2
2 changed files with 120 additions and 4 deletions
@@ -387,6 +387,16 @@ public virtual class fflib_SObjects2
return result;
}
/**
* @return Returns only the first SObject contained in the domain.
*/
public SObject getFirstSObject()
{
if (isEmpty()) return null;
return getRecords().get(0);
}
/**
* Get a map with the values of two Id fields
* Key fields containing null values are omitted
@@ -751,4 +761,51 @@ public virtual class fflib_SObjects2
new fflib_SObjects2(getRecords(criteria), getSObjectType())
.setFieldValue(sObjectField, value);
}
/**
* Sets a value to the given field only when key field Id value is provided in the given map
*
* @param sObjectIdFieldToCheck The SObject Id Field to match the key against in the provided map
* @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field
* @param values Map of values to store by the sObjectIdFieldToCheck fields value
*/
@TestVisible
protected virtual void setFieldValue(
Schema.SObjectField sObjectIdFieldToCheck,
Schema.SObjectField sObjectFieldToUpdate,
Map<Id, Object> values)
{
for (SObject record : getRecords())
{
Id keyValue = (Id) record.get(sObjectIdFieldToCheck);
if (values?.containsKey(keyValue))
{
record.put(sObjectFieldToUpdate, values.get(keyValue));
}
}
}
/**
* Sets a value to the given field only when key field String value is provided in the given map
*
* @param sObjectStringFieldToCheck The SObject String Field to match the key against in the provided map
* @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field
* @param values Map of values to store by the sObjectIdFieldToCheck fields value
*/
@TestVisible
protected virtual void setFieldValue(
Schema.SObjectField sObjectStringFieldToCheck,
Schema.SObjectField sObjectFieldToUpdate,
Map<String, Object> values)
{
for (SObject record : getRecords())
{
String keyValue = (String) record.get(sObjectStringFieldToCheck);
if (values?.containsKey(keyValue))
{
record.put(sObjectFieldToUpdate, values.get(keyValue));
}
}
}
}
@@ -89,11 +89,21 @@ private class fflib_SObjects2Test
{
fflib_Criteria criteria = new fflib_Criteria().equalTo(Account.Name, 'A');
DomainAccounts domain = generateDomain();
DomainAccounts domain = new DomainAccounts(
new List<Account>
{
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'A', ShippingCountry = 'USA', AnnualRevenue = 5, BillingLatitude = 121.0),
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'B', ShippingCountry = 'Ireland', AnnualRevenue = 3),
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'C', ShippingCountry = 'UK', AnnualRevenue = 2),
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'D', ShippingCountry = '', AnnualRevenue = 1),
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'E'),
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), BillingLatitude = 121.0 )
}
);
domain.getDateTimeFieldValues(Account.LastActivityDate);
domain.getDateTimeFieldValues(Account.CreatedDate);
domain.getFieldByField(Account.Name, Account.AccountNumber);
domain.getFieldsByField(Account.Id, Account.AccountNumber);
domain.getDateTimeFieldValues(Account.CreatedDate, criteria);
domain.getFieldByField(Account.Name, Account.AnnualRevenue);
domain.getFieldsByField(Account.Id, Account.AnnualRevenue);
domain.getFieldByField(Account.Id, Account.Id);
domain.getIdFieldByIdField(Account.Id, Account.Id);
domain.getIdFieldsByIdField(Account.Id, Account.Id);
@@ -101,6 +111,7 @@ private class fflib_SObjects2Test
domain.getIdFieldValues(Account.Id, criteria);
domain.getIntegerFieldValues(Account.NumberOfEmployees);
domain.getIntegerFieldValues(Account.NumberOfEmployees, criteria);
domain.getDecimalFieldValues(Account.BillingLatitude, criteria);
domain.getDoubleFieldValues(Account.BillingLatitude);
domain.getDoubleFieldValues(Account.BillingLatitude, criteria);
domain.getSObjectByIdField(Account.Id);
@@ -327,6 +338,54 @@ private class fflib_SObjects2Test
System.assert(domain.getDecimalFieldValues(Account.AnnualRevenue).contains((Decimal) 5));
}
@IsTest
static void itShouldSetFieldByIdField()
{
Id accountId = fflib_IDGenerator.generate(Account.SObjectType);
Account record = new Account(Id = accountId, Name = '');
fflib_SObjects2 domain = new fflib_SObjects2(new List<Account>{ record });
domain.setFieldValue(
Schema.Account.Id,
Schema.Account.Name,
new Map<Id, String>
{
accountId => 'Hello'
}
);
System.assert(domain.getFirstSObject().get('Name') == 'Hello');
}
@IsTest
static void itShouldSetFieldByStringField()
{
Account record = new Account(Name = 'Hello', Rating = 'Cold');
fflib_SObjects2 domain = new fflib_SObjects2(new List<Account>{ record });
domain.setFieldValue(
Schema.Account.Name,
Schema.Account.Rating,
new Map<String, String>
{
'Hello' => 'Warm'
}
);
System.assert(domain.getFirstSObject().get('Rating') == 'Warm');
}
@IsTest
static void itShouldSetField()
{
Account record = new Account(Name = 'Hello', Rating = 'Cold');
fflib_SObjects2 domain = new fflib_SObjects2(new List<Account>{ record });
domain.setFieldValue(Account.Name, 'Hello World', new fflib_Criteria().equalTo(Account.Rating, 'Cold'));
System.assert(domain.getFirstSObject().get('Name') == 'Hello World');
}
private static DomainAccounts generateDomain()
{
DomainAccounts domain = new DomainAccounts(