Skip to content

Commit

Permalink
Merge pull request #2 from navikt/addPlatformUtility
Browse files Browse the repository at this point in the history
Adding Custom Metadata Utility
  • Loading branch information
Triopticon authored Oct 31, 2024
2 parents 57cae1f + 0834ce1 commit d85e45e
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@
{
"path": "src/platform-admin",
"package": "platform-admin",
"versionNumber": "0.0.0.NEXT"
"versionNumber": "0.0.0.NEXT",
"definitionFile": "config/project-scratch-def.json"
},
{
"path": "src/platform-utility",
"package": "platform-utility",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT",
"default": false,
"versionDescription": "Package containing the core platform utilities.",
"definitionFile": "config/project-scratch-def.json"

}
],
"packageAliases": {
Expand Down
70 changes: 70 additions & 0 deletions src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls
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);
}
}
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 src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls
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);
}
}
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>
17 changes: 17 additions & 0 deletions src/platform-utility/README.md
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.

0 comments on commit d85e45e

Please sign in to comment.