Skip to content

Commit 3007a1f

Browse files
authored
Justere CustomMetadataDao (#3)
Omstrukturere pakken (mer spesifikt splitte tester ut i egen mappestruktur) Justering av testene sånn at de ikke er avhengig av en faktisk custom metadata type
1 parent 7e1f95d commit 3007a1f

File tree

6 files changed

+113
-99
lines changed

6 files changed

+113
-99
lines changed

sfdx-project.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414
{
1515
"path": "src/platform-admin",
1616
"package": "platform-admin",
17-
"versionNumber": "0.0.0.NEXT",
18-
"definitionFile": "config/project-scratch-def.json"
17+
"versionNumber": "0.0.0.NEXT"
1918
},
2019
{
21-
"path": "src/platform-utility",
22-
"package": "platform-utility",
2320
"versionName": "ver 0.1",
2421
"versionNumber": "0.1.0.NEXT",
22+
"path": "src/platform-utility/CustomMetadataDAO",
2523
"default": false,
26-
"versionDescription": "Package containing the core platform utilities.",
27-
"definitionFile": "config/project-scratch-def.json"
24+
"package": "custom-metadata-dao",
25+
"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."
2826
}
2927
],
3028
"packageAliases": {
31-
"platform-data-model": "0HoKB00000000010AA"
29+
"platform-data-model": "0HoKB00000000010AA",
30+
"custom-metadata-dao": "0HoKB000000000B0AQ"
3231
}
3332
}

src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls renamed to src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @example
1616
* List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO()
1717
* .getCustomMetadataRecords(
18-
* 'SELECT MasterLable, CustomField__c ' +
18+
* 'SELECT MasterLabel, CustomField__c ' +
1919
* 'FROM CustomMetadata__mdt ' +
2020
* 'WHERE DeveloperName = \'Name\''
2121
* );
@@ -25,8 +25,7 @@
2525
* name = nameCMList[0];
2626
* }
2727
*/
28-
@SuppressWarnings('PMD.ApexSharingViolations')
29-
public class CustomMetadataDAO {
28+
public inherited sharing class CustomMetadataDAO {
3029
/**
3130
* @description Is used to set the Custom Metadata Records in Unit Tests.
3231
*
@@ -42,7 +41,7 @@ public class CustomMetadataDAO {
4241
* @author Kenneth Soerensen ([email protected]), NAV
4342
* @since 0.1.0, August 2024
4443
* @param query The SOQL query string to fetch the Custom Metadata Records.
45-
* @returns Return a list of Custom Metadata Records as `List<SObject>`
44+
* @return Return a list of Custom Metadata Records as `List<SObject>`
4645
* @example
4746
* List<CustomMetadata__mdt> nameCMList = (List<CustomMetadata__mdt>) new CustomMetadataDAO()
4847
* .getCustomMetadataRecords(
@@ -57,14 +56,10 @@ public class CustomMetadataDAO {
5756
* }
5857
*/
5958
public List<SObject> getCustomMetadataRecords(String query) {
60-
System.debug(LoggingLevel.DEBUG, 'query: ' + query);
61-
System.debug(
62-
LoggingLevel.DEBUG,
63-
'customMetadataRecordsMap: ' + customMetadataRecordsMap
64-
);
6559
if (!customMetadataRecordsMap.containsKey(query)) {
6660
customMetadataRecordsMap.put(query, Database.query(query));
6761
}
62+
6863
return customMetadataRecordsMap.get(query);
6964
}
7065
}

src/platform-utility/CustomMetadataDAO/CustomMetadataDAO.cls-meta.xml renamed to src/platform-utility/CustomMetadataDAO/main/classes/CustomMetadataDAO.cls-meta.xml

File renamed without changes.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

src/platform-utility/CustomMetadataDAO/CustomMetadataDAOTest.cls-meta.xml renamed to src/platform-utility/CustomMetadataDAO/test/classes/CustomMetadataDAOTest.cls-meta.xml

File renamed without changes.

0 commit comments

Comments
 (0)