diff --git a/sfdx-project.json b/sfdx-project.json index d5981d7..0c55dc3 100644 --- a/sfdx-project.json +++ b/sfdx-project.json @@ -14,20 +14,19 @@ { "path": "src/platform-admin", "package": "platform-admin", - "versionNumber": "0.0.0.NEXT", - "definitionFile": "config/project-scratch-def.json" + "versionNumber": "0.0.0.NEXT" }, { - "path": "src/platform-utility", - "package": "platform-utility", "versionName": "ver 0.1", "versionNumber": "0.1.0.NEXT", + "path": "src/platform-utility/CustomMetadataDAO", "default": false, - "versionDescription": "Package containing the core platform utilities.", - "definitionFile": "config/project-scratch-def.json" + "package": "custom-metadata-dao", + "versionDescription": "Custom Metadata Data Access Object class 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." } ], "packageAliases": { - "platform-data-model": "0HoKB00000000010AA" + "platform-data-model": "0HoKB00000000010AA", + "custom-metadata-dao": "0HoKB000000000B0AQ" } } diff --git a/src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls b/src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls deleted file mode 100644 index ce44f27..0000000 --- a/src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls +++ /dev/null @@ -1,83 +0,0 @@ -/** - * @description This is the test class for the Custom Metadata Data Access Object class. - *

- * 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 (kenneth.sorensen@nav.com), 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) JSON.deserialize('[{"CustomField__c":"Value"}]', List.class) - * ); - * - * List nameCMList = (List) 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 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 (kenneth.sorensen@nav.com), 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) JSON.deserialize('[{"CustomField__c":"Value"}]', List.class) - * ); - * - * List nameCMList = (List) 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 records) { - CustomMetadataDAO.customMetadataRecordsMap.put(query, records); - } -} diff --git a/src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls b/src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls similarity index 85% rename from src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls rename to src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls index a1c713c..032924c 100644 --- a/src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls +++ b/src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls @@ -15,7 +15,7 @@ * @example * List nameCMList = (List) new CustomMetadataDAO() * .getCustomMetadataRecords( - * 'SELECT MasterLable, CustomField__c ' + + * 'SELECT MasterLabel, CustomField__c ' + * 'FROM CustomMetadata__mdt ' + * 'WHERE DeveloperName = \'Name\'' * ); @@ -25,8 +25,7 @@ * name = nameCMList[0]; * } */ -@SuppressWarnings('PMD.ApexSharingViolations') -public class CustomMetadataDAO { +public inherited sharing class CustomMetadataDAO { /** * @description Is used to set the Custom Metadata Records in Unit Tests. * @@ -42,7 +41,7 @@ public class CustomMetadataDAO { * @author Kenneth Soerensen (kenneth.sorensen@nav.com), 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` + * @return Return a list of Custom Metadata Records as `List` * @example * List nameCMList = (List) new CustomMetadataDAO() * .getCustomMetadataRecords( @@ -57,14 +56,10 @@ public class CustomMetadataDAO { * } */ public List 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); } } diff --git a/src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls-meta.xml b/src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls-meta.xml similarity index 100% rename from src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls-meta.xml rename to src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls-meta.xml diff --git a/src/platform-utility/CustomMetadataDAO/test/classes/CustomMetadataDAOTest.cls b/src/platform-utility/CustomMetadataDAO/test/classes/CustomMetadataDAOTest.cls new file mode 100644 index 0000000..16b396d --- /dev/null +++ b/src/platform-utility/CustomMetadataDAO/test/classes/CustomMetadataDAOTest.cls @@ -0,0 +1,103 @@ +/** + * @description This is the test class for the Custom Metadata Data Access Object class. + *

+ * 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 (kenneth.sorensen@nav.com), NAV + * @since 0.1.0, August 2024 + * + * @author Tor HÃ¥kon Sigurdsen, Nav + * @since 2025-01-14 - Made tests independent from Custom Metadata types in the orgs + * + * @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 MasterLabel ' + + * 'FROM CustomMetadata__mdt ' + + * 'WHERE DeveloperName = \'Name\'', + * (List) JSON.deserialize( + * '[{"attributes": {"type": "CustomMetadata__mdt"},"MasterLabel":"Value"}]', + * List.class) + * ); + * + * List nameCMList = (List) new CustomMetadataDAO() + * .getCustomMetadataRecords( + * 'SELECT MasterLable, CustomField__c ' + + * 'FROM CustomMetadata__mdt ' + + * 'WHERE DeveloperName = \'Name\'' + * ); + * + * CustomMetadata__mdt name; + * if (nameCMList.size() > 0) { + * name = nameCMList[0]; + * } + */ +@IsTest(IsParallel=true) +public class CustomMetadataDAOTest { + /** + * @description Simple test for the getMetadata method. + */ + @IsTest + private static void testGetMetadata() { + List customMetadataRecords; + String query = + 'SELECT MasterLabel ' + + 'FROM CustomMetadata__mdt ' + + 'WHERE DeveloperName = \'Name\''; + + // We set the type to Account in order to not be dependent on any Custom Metadata types + CustomMetadataDAOTest.setMetadata( + query, + (List) JSON.deserialize( + '[{"attributes": {"type": "Account"},"MasterLabel":"Label"}]', + List.class + ) + ); + + System.Test.startTest(); + customMetadataRecords = new CustomMetadataDAO() + .getCustomMetadataRecords(query); + System.Test.stopTest(); + System.Assert.areEqual( + 1, + customMetadataRecords.size(), + 'Size should match' + ); + } + + /** + * @description A utility method to set custom metadata records for the tests. + * + * @author Kenneth Soerensen (kenneth.sorensen@nav.com), 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 MasterLabel ' + + * 'FROM CustomMetadata__mdt ' + + * 'WHERE DeveloperName = \'Name\'', + * (List) JSON.deserialize( + * '[{"attributes": {"type": "CustomMetadata__mdt"},"MasterLabel":"Value"}]', + * List.class) + * ); + * + * List nameCMList = (List) new CustomMetadataDAO() + * .getCustomMetadataRecords( + * 'SELECT MasterLabel ' + + * 'FROM CustomMetadata__mdt ' + + * 'WHERE DeveloperName = \'Name\'' + * ); + * + * CustomMetadata__mdt name; + * if (nameCMList.size() > 0) { + * name = nameCMList[0]; + * } + */ + public static void setMetadata(String query, List records) { + CustomMetadataDAO.customMetadataRecordsMap.put(query, records); + } +} diff --git a/src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls-meta.xml b/src/platform-utility/CustomMetadataDAO/test/classes/CustomMetadataDAOTest.cls-meta.xml similarity index 100% rename from src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls-meta.xml rename to src/platform-utility/CustomMetadataDAO/test/classes/CustomMetadataDAOTest.cls-meta.xml