-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from navikt/addPlatformUtility
Adding Custom Metadata Utility
- Loading branch information
Showing
6 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @description This Custom Metadata Data Access Object class is used to get access to | ||
* Custom Metadata objects, and at the same time make it easier to test the | ||
* various paths the code can take based on the values in the Custom Metadata. | ||
* It will also help to get 100% test coverage of the code and reduce the need | ||
* for use of "Test.isRunningTest()" in the code. | ||
* <br><br> | ||
* Inspiration for this way of solving the problem is taken from the article | ||
* "Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions". | ||
* | ||
* @author Kenneth Soerensen ([email protected]), NAV | ||
* @since 0.1.0, August 2024 | ||
* @group Custom Metadata DAO | ||
* @see [Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions](https://www.avenga.com/magazine/salesforce-custom-metadata/) | ||
* @example | ||
* List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO() | ||
* .getCustomMetadataRecords( | ||
* 'SELECT MasterLable, CustomField__c ' + | ||
* 'FROM CustomMetadata__mdt ' + | ||
* 'WHERE DeveloperName = \'Name\'' | ||
* ); | ||
* | ||
* CustomMetadata__mdt name; | ||
* if (nameCMList.size() > 0) { | ||
* name = nameCMList[0]; | ||
* } | ||
*/ | ||
@SuppressWarnings('PMD.ApexSharingViolations') | ||
public class CustomMetadataDAO { | ||
/** | ||
* @description Is used to set the Custom Metadata Records in Unit Tests. | ||
* | ||
* @author Kenneth Soerensen ([email protected]), NAV | ||
* @since 0.1.0, August 2024 | ||
*/ | ||
@TestVisible | ||
static private Map<String, List<SObject>> customMetadataRecordsMap = new Map<String, List<SObject>>(); | ||
|
||
/** | ||
* @description Get the Custom Metadata Records based on the SOQL query string provided. | ||
* | ||
* @author Kenneth Soerensen ([email protected]), NAV | ||
* @since 0.1.0, August 2024 | ||
* @param query The SOQL query string to fetch the Custom Metadata Records. | ||
* @returns Return a list of Custom Metadata Records as `List<SObject>` | ||
* @example | ||
* List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO() | ||
* .getCustomMetadataRecords( | ||
* 'SELECT MasterLable, CustomField__c ' + | ||
* 'FROM CustomMetadata__mdt ' + | ||
* 'WHERE DeveloperName = \'Name\'' | ||
* ); | ||
* | ||
* CustomMetadata__mdt name; | ||
* if (nameCMList.size() > 0) { | ||
* name = nameCMList[0]; | ||
* } | ||
*/ | ||
public List<SObject> getCustomMetadataRecords(String query) { | ||
System.debug(LoggingLevel.DEBUG, 'query: ' + query); | ||
System.debug( | ||
LoggingLevel.DEBUG, | ||
'customMetadataRecordsMap: ' + customMetadataRecordsMap | ||
); | ||
if (!customMetadataRecordsMap.containsKey(query)) { | ||
customMetadataRecordsMap.put(query, Database.query(query)); | ||
} | ||
return customMetadataRecordsMap.get(query); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
83 changes: 83 additions & 0 deletions
83
src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @description This is the test class for the Custom Metadata Data Access Object class. | ||
* <br><br> | ||
* Inspiration for this way of solving the problem is taken form the article | ||
* "Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions". | ||
* | ||
* @author Kenneth Soerensen ([email protected]), NAV | ||
* @since 0.1.0, August 2024 | ||
* @group Custom Metadata DAO | ||
* @see CustomMetadataDAO | ||
* @see [Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions](https://www.avenga.com/magazine/salesforce-custom-metadata/) | ||
* @example | ||
* CustomMetadataDAOTest.setMetadata( | ||
* 'SELECT MasterLable, CustomField__c ' + | ||
* 'FROM CustomMetadata__mdt ' + | ||
* 'WHERE DeveloperName = \'Name\'', | ||
* (List<CustomMetadata__mdt>) JSON.deserialize('[{"CustomField__c":"Value"}]', List<CustomMetadata__mdt>.class) | ||
* ); | ||
* | ||
* List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO() | ||
* .getCustomMetadataRecords( | ||
* 'SELECT MasterLable, CustomField__c ' + | ||
* 'FROM CustomMetadata__mdt ' + | ||
* 'WHERE DeveloperName = \'Name\'' | ||
* ); | ||
* | ||
* CustomMetadata__mdt name; | ||
* if (nameCMList.size() > 0) { | ||
* name = nameCMList[0]; | ||
* } | ||
*/ | ||
@IsTest | ||
public class CustomMetadataDAOTest { | ||
/** | ||
* @description Simple test for the getMetadata method. | ||
*/ | ||
@IsTest | ||
static void testGetMetadata() { | ||
List<SObject> customMetadataRecords; | ||
System.Test.startTest(); | ||
customMetadataRecords = new CustomMetadataDAO() | ||
.getCustomMetadataRecords( | ||
'SELECT MasterLabel FROM API_Base_Configuration__mdt' | ||
); | ||
System.Test.stopTest(); | ||
System.assertEquals( | ||
[SELECT MasterLabel FROM API_Base_Configuration__mdt].size(), | ||
customMetadataRecords.size(), | ||
'Size should match' | ||
); | ||
} | ||
|
||
/** | ||
* @description A utility method to set custom metadata records for the tests. | ||
* | ||
* @author Kenneth Soerensen ([email protected]), NAV | ||
* @since 0.1.0, August 2024 | ||
* @param query The SOQL query string to fetch the Custom Metadata Records. | ||
* @param records Set Custom Metadata Records for the tests. | ||
* @example | ||
* CustomMetadataDAOTest.setMetadata( | ||
* 'SELECT MasterLable, CustomField__c ' + | ||
* 'FROM CustomMetadata__mdt ' + | ||
* 'WHERE DeveloperName = \'Name\'', | ||
* (List<CustomMetadata__mdt>) JSON.deserialize('[{"CustomField__c":"Value"}]', List<CustomMetadata__mdt>.class) | ||
* ); | ||
* | ||
* List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO() | ||
* .getCustomMetadataRecords( | ||
* 'SELECT MasterLable, CustomField__c ' + | ||
* 'FROM CustomMetadata__mdt ' + | ||
* 'WHERE DeveloperName = \'Name\'' | ||
* ); | ||
* | ||
* CustomMetadata__mdt name; | ||
* if (nameCMList.size() > 0) { | ||
* name = nameCMList[0]; | ||
* } | ||
*/ | ||
public static void setMetadata(String query, List<SObject> records) { | ||
CustomMetadataDAO.customMetadataRecordsMap.put(query, records); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# platform-utility | ||
|
||
Package containing the core platform utilities. | ||
|
||
The package can only contain utilities that are used by the sf-platform, and can be used by any other re. | ||
|
||
## Maintainer | ||
|
||
Maintained by Team Platforce. | ||
|
||
### Henvendelser | ||
|
||
Spørsmål knyttet til koden eller prosjektet kan stilles som issues her på GitHub. | ||
|
||
### For NAV-ansatte | ||
|
||
Interne henvendelser kan sendes via Slack i kanalen #platforce. |