|
| 1 | +/** |
| 2 | + * @description This is the test class for the Custom Metadata Data Access Object class. |
| 3 | + * <br><br> |
| 4 | + * Inspiration for this way of solving the problem is taken form the article |
| 5 | + * "Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions". |
| 6 | + * |
| 7 | + * @author Kenneth Soerensen ([email protected]), NAV |
| 8 | + * @since 0.1.0, August 2024 |
| 9 | + * |
| 10 | + * @author Tor Håkon Sigurdsen, Nav |
| 11 | + * @since 2025-01-14 - Made tests independent from Custom Metadata types in the orgs |
| 12 | + * |
| 13 | + * @group Custom Metadata DAO |
| 14 | + * @see CustomMetadataDAO |
| 15 | + * @see [Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions](https://www.avenga.com/magazine/salesforce-custom-metadata/) |
| 16 | + * @example |
| 17 | + * CustomMetadataDAOTest.setMetadata( |
| 18 | + * 'SELECT MasterLabel ' + |
| 19 | + * 'FROM CustomMetadata__mdt ' + |
| 20 | + * 'WHERE DeveloperName = \'Name\'', |
| 21 | + * (List<CustomMetadata__mdt>) JSON.deserialize( |
| 22 | + * '[{"attributes": {"type": "CustomMetadata__mdt"},"MasterLabel":"Value"}]', |
| 23 | + * List<CustomMetadata__mdt>.class) |
| 24 | + * ); |
| 25 | + * |
| 26 | + * List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO() |
| 27 | + * .getCustomMetadataRecords( |
| 28 | + * 'SELECT MasterLable, CustomField__c ' + |
| 29 | + * 'FROM CustomMetadata__mdt ' + |
| 30 | + * 'WHERE DeveloperName = \'Name\'' |
| 31 | + * ); |
| 32 | + * |
| 33 | + * CustomMetadata__mdt name; |
| 34 | + * if (nameCMList.size() > 0) { |
| 35 | + * name = nameCMList[0]; |
| 36 | + * } |
| 37 | + */ |
| 38 | +@IsTest(IsParallel=true) |
| 39 | +public class CustomMetadataDAOTest { |
| 40 | + /** |
| 41 | + * @description Simple test for the getMetadata method. |
| 42 | + */ |
| 43 | + @IsTest |
| 44 | + private static void testGetMetadata() { |
| 45 | + List<SObject> customMetadataRecords; |
| 46 | + String query = |
| 47 | + 'SELECT MasterLabel ' + |
| 48 | + 'FROM CustomMetadata__mdt ' + |
| 49 | + 'WHERE DeveloperName = \'Name\''; |
| 50 | + |
| 51 | + // We set the type to Account in order to not be dependent on any Custom Metadata types |
| 52 | + CustomMetadataDAOTest.setMetadata( |
| 53 | + query, |
| 54 | + (List<SObject>) JSON.deserialize( |
| 55 | + '[{"attributes": {"type": "Account"},"MasterLabel":"Label"}]', |
| 56 | + List<SObject>.class |
| 57 | + ) |
| 58 | + ); |
| 59 | + |
| 60 | + System.Test.startTest(); |
| 61 | + customMetadataRecords = new CustomMetadataDAO() |
| 62 | + .getCustomMetadataRecords(query); |
| 63 | + System.Test.stopTest(); |
| 64 | + System.Assert.areEqual( |
| 65 | + 1, |
| 66 | + customMetadataRecords.size(), |
| 67 | + 'Size should match' |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @description A utility method to set custom metadata records for the tests. |
| 73 | + * |
| 74 | + * @author Kenneth Soerensen ([email protected]), NAV |
| 75 | + * @since 0.1.0, August 2024 |
| 76 | + * @param query The SOQL query string to fetch the Custom Metadata Records. |
| 77 | + * @param records Set Custom Metadata Records for the tests. |
| 78 | + * @example |
| 79 | + * CustomMetadataDAOTest.setMetadata( |
| 80 | + * 'SELECT MasterLabel ' + |
| 81 | + * 'FROM CustomMetadata__mdt ' + |
| 82 | + * 'WHERE DeveloperName = \'Name\'', |
| 83 | + * (List<CustomMetadata__mdt>) JSON.deserialize( |
| 84 | + * '[{"attributes": {"type": "CustomMetadata__mdt"},"MasterLabel":"Value"}]', |
| 85 | + * List<CustomMetadata__mdt>.class) |
| 86 | + * ); |
| 87 | + * |
| 88 | + * List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO() |
| 89 | + * .getCustomMetadataRecords( |
| 90 | + * 'SELECT MasterLabel ' + |
| 91 | + * 'FROM CustomMetadata__mdt ' + |
| 92 | + * 'WHERE DeveloperName = \'Name\'' |
| 93 | + * ); |
| 94 | + * |
| 95 | + * CustomMetadata__mdt name; |
| 96 | + * if (nameCMList.size() > 0) { |
| 97 | + * name = nameCMList[0]; |
| 98 | + * } |
| 99 | + */ |
| 100 | + public static void setMetadata(String query, List<SObject> records) { |
| 101 | + CustomMetadataDAO.customMetadataRecordsMap.put(query, records); |
| 102 | + } |
| 103 | +} |
0 commit comments