function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Akshay JuwaAkshay Juwa 

How to test this Apex class for the following?

Hello Everyone, I haven't really worked with creating a test class yet but I'd like to know how to write a test class for the following Apex class. If you could guide on how to write a test class that'd be awesome.

public class OptOutPreference {
    public static void OptOutPreferenceLink(){
        //Now these encryption keys are obtained via custom metadata, updated API version as well
        List<SFMC_Encrypt_Setting__mdt> encrypt_settings = SFMC_Encrypt_Setting__mdt.getAll().values();
        SFMC_Encrypt_Setting__mdt setting = encrypt_settings[0];
        String initializationvector = setting.InitVector__c;
        String base64key = setting.Base64Key__c;
        String encrypttype = setting.EncryptType__c;
        
        Blob exampleIv = Blob.valueOf(initializationvector);
        Blob key = Blob.valueOf(base64key);
        
        String bxurl = 'http://cloud.mail.buxomcosmetics.com/bx_unsubscribe?&EA=';
        String bmurl = 'http://cloud.email.bareminerals.com/bm_unsubscribe?&EA=';
        String lmurl = 'http://cloud.email.lauramercier.com/AuRevoirPage?&EA=';
        String cpburl = 'http://cloud.email.cledepeaubeauteus.com/cpb_unsubscribe?&EA=';
        String shurl = 'http://cloud.email.shiseidousa.com/shi_unsubscribe?EA=';
        String narsurl = 'http://cloud.email.narscosmetics.com/nars_unsubscribe?&EA=';
        String narscaurl = 'http://cloud.email.narscosmetics.ca/en_ca_nars_unsubscribe?&EA=';
        String shcaurl = 'http://cloud.email.shiseido.ca/en_ca_sh_unsubscribe?&EA=';
        String cpbcaurl = 'http://cloud.email.cledepeaubeaute.ca/en_ca_cpb_unsubscribe?&EA=';
        String lmcaurl = 'http://cloud.email.lauramercier.ca/en_ca_lm_unsubscribe?&EA=';
        String deurl = 'https://cloud.cheers.drunkelephant.com/unsubscribe?EA=';
        String decaurl = 'https://cloud.hello.drunkelephant.ca/unsubscribe?EA=' ;
        
        List<Account> OptOutEmail =[SELECT Id, PersonEmail, Brand__c, CreatedDate, Opt_Out_Preference__c FROM Account WHERE CreatedDate < 2022-09-15T16:00:53.000+0000 AND PersonEmail !=NULL AND Brand__c !=NULL AND Opt_Out_Preference__c = NULL];
        System.debug('AJ' + OptOutEmail);
        List<Account> UpdateAccounts = new List<Account>();
        
        for (Account a : OptOutEmail){
            Blob data = Blob.valueOf(a.PersonEmail);
            Blob encrypted = Crypto.encrypt(encrypttype, key, exampleIv, data);
            String s = EncodingUtil.urlEncode(EncodingUtil.base64Encode(encrypted),'UTF-8');
            if (a.Brand__c == 'Buxom')
            {
                a.Opt_Out_Preference__c = bxurl + s;
            }
            if (a.Brand__c == 'bareMinerals')
            {
                a.Opt_Out_Preference__c = bmurl + s;
            }
            if (a.Brand__c == 'NARS')
            {
                a.Opt_Out_Preference__c = narsurl + s;
            }
            if (a.Brand__c == 'Laura Mercier')
            {
                a.Opt_Out_Preference__c = lmurl + s;
            }
            if (a.Brand__c == 'Shiseido')
            {
                a.Opt_Out_Preference__c = shurl + s;
            }
            if (a.Brand__c == 'Cle de Peau Beaute')
            {
                a.Opt_Out_Preference__c = cpburl + s;
            }
            if (a.Brand__c == 'NARS CA')
            {
                a.Opt_Out_Preference__c = narscaurl + s;
            }
            if (a.Brand__c == 'Laura Mercier CA')
            {
                a.Opt_Out_Preference__c = lmcaurl + s;
            }
            if (a.Brand__c == 'Shiseido CA')
            {
                a.Opt_Out_Preference__c = shcaurl + s;
            }
            if (a.Brand__c == 'Cle de Peau Beaute CA')
            {
                a.Opt_Out_Preference__c = cpbcaurl + s;
            }
            if (a.Brand__c == 'Drunk Elephant')
            {
                a.Opt_Out_Preference__c = deurl + s;
            }
            if (a.Brand__c == 'Drunk Elephant CA')
            {
                a.Opt_Out_Preference__c = decaurl + s;
            }
            UpdateAccounts.add(a);
        }
        update UpdateAccounts;
    }
    
}
SwethaSwetha (Salesforce Developers) 
HI Akshay,
The code provided in question does not highlight anu uncovered lines. Since this code requires an understanding of your implementation, it might not be possible to provide exact suggestions. However, the below articles give a good insight into how coverage can be improved
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 
 
Examples of crypto test classes:
https://salesforce.stackexchange.com/questions/25636/how-to-encrypt-and-decrypt-a-string-in-apex-using-the-crypto-class
https://salesforce.stackexchange.com/questions/347236/how-can-i-encrypt-a-string-in-apex-in-a-way-that-an-external-system-can-decrypt
 
If this information helps, please mark the answer as best. Thank you