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
RaffusRaffus 

How to write test test method for below code

@AuraEnabled
    public static String loadData(String strCaseInfo) {
        System.debug('caseInfo ' + strCaseInfo);
        CaseInfoWrapper caseInfo = (CaseInfoWrapper)JSON.deserialize(strCaseInfo, CaseInfoWrapper.class);
        //rand_App_Customer__c customer = getCustomerInfo(caseInfo.partyId, caseInfo.mobile);
        rand_App_Customer__c customer = getCustomerInfo2(caseInfo.emailId);
        Case cse = new Case();
        cse.subject = 'tan One Problem Report';
        cse.Origin = rand_APP;
        cse.Status = 'Awaiting Approval';
        cse.Priority = 'Medium';
        cse.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get(rand_APP).getRecordTypeId();
        // cse.Hex_Service_Request__c = 'System Update';
        // cse.Hex_Service_Request_new__c = 'System Update';
        // cse.ProblemCode__c = 'NOC for Name Addition - HO';
        cse.Description = caseInfo.description;
        //RN Comment
        //cse.Current_Location__Latitude__s = caseInfo.latitude;
        //cse.Current_Location__Longitude__s = caseInfo.longitude;
        cse.rand_App_Customer__c = customer?.Id;
        //cse.LocationName__c = caseInfo.locationName; */
        cse.Issue_category__c = caseInfo.category;
        cse.Issue_Sub_Category__c = caseInfo.subCategory;
        insert cse;
        // for skip the triggers of ContentDocumentLink && ContentDocument
        ContentDocumentLinkTriggerUtil.skipTrigger = true;
        ContentDocumentTriggerUtil.skipTrigger = true;
        List<ContentVersion> cVersions = new List<ContentVersion>();
        for (FileInfoWrapper file : caseInfo.files) {
            System.debug('fileInfo ' + file);
            if (String.isNotBlank(file.base64Data)) {
                ContentVersion cv = new ContentVersion();
                cv.ContentLocation = 'S';
                cv.PathOnClient = file.fileName;
                cv.VersionData = EncodingUtil.base64Decode(file.base64Data);
                cv.Title = file.fileName;
                cVersions.add(cv);
            }
        }
        List<Id> contentDocumentIds = new List<Id>(caseInfo.contentDocumentIds);
        if (cVersions.size() > 0) {
            insert cVersions;
            List<ContentDistribution> ContentDistributionList = new List<ContentDistribution>();
            for(ContentVersion ctv : cVersions){
                ContentDistribution cd = new ContentDistribution();
                cd.Name = ctv.Title;
                cd.ContentVersionId = ctv.Id;
                cd.PreferencesAllowViewInBrowser = true;
                cd.PreferencesLinkLatestVersion = true;
                cd.PreferencesNotifyOnVisit = false;
                cd.PreferencesPasswordRequired = false;
                cd.PreferencesAllowOriginalDownload = true;
                ContentDistributionList.add(cd);
            }
            if(ContentDistributionList.size() > 0){
                INSERT ContentDistributionList;
            }
            Set<Id> cvIds = new Map<Id, ContentVersion>(cVersions).keySet();
            for (ContentVersion cVersion : [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id IN:cvIds ORDER BY CreatedDate ASC]) {
                contentDocumentIds.add(cVersion.ContentDocumentId);
            }
        }
        List<ContentDocumentLink> links = new List<ContentDocumentLink>();
        for (Id contentDocumentId : contentDocumentIds) {
            ContentDocumentLink link = new ContentDocumentLink();
            link.LinkedEntityId = cse.Id;
            link.ContentDocumentId = contentDocumentId;
            link.ShareType = 'V';
            links.add(link);
        }
        insert links;
        return [SELECT CaseNumber FROM Case WHERE Id = :cse.Id]?.CaseNumber;
    }
Please help me to write a test method for the above method.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi ,

This class contains refernece of some other class like CaseInfoWrapper and getCustomerInfo2 method.

Can you share those so can try the test class for the same.

Thanks,
 
RaffusRaffus
CaseInfoWrapper class - 
public class CaseInfoWrapper {
        @AuraEnabled
        public List<FileInfoWrapper> files { get; set; }

        @AuraEnabled
        public String description { get; set; }

        @AuraEnabled
        public String category { get; set; }

        @AuraEnabled
        public String subCategory { get; set; }

        @AuraEnabled
        public Decimal latitude { get; set; }

        @AuraEnabled
        public Decimal longitude { get; set; }

        @AuraEnabled
        public String locationName { get; set; }

        @AuraEnabled
        public List<Id> contentDocumentIds { get; set; }

        @AuraEnabled
        public String partyId { get; set; }

        @AuraEnabled
        public String mobile { get; set; }

        @AuraEnabled
        public String emailid { get; set; }

        @AuraEnabled
        public String empid { get; set; }

        @AuraEnabled
        public Id randcustomerstart { get; set; }

    }

getCustomerInfo2  Method-
@AuraEnabled
    public static rand_App_Customer__c getCustomerInfo2(String emailId) {
        try {

            List<rand_App_Customer__c> customers = [SELECT Id, Customer_Points__c, Customer_Email__c,Last_Quarter_Points__c, Last_Quarter_Rank__c, Customer_Photo_Base64__c FROM rand_App_Customer__c WHERE Customer_Email__c= : emailId];
            if (!customers.isEmpty()) {
                System.debug('customers ' + customers);
                return customers[0]; 
            }
            return null;
        }
        catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }


 
RaffusRaffus
This is what I wrote - 
@isTest
public static void loadDataTest(){

    rand_App_Customer__c customer = new rand_App_Customer();
    randCustomer.Customer_ID__c = '1234567';
    randCustomer.Customer_Email__c = 'tannow@tan.ae';

    Case caseInfo = new Case();

    caseInfo.subject = 'tan One Problem Report';
    caseInfo.Status = 'Awaiting Approval';
    caseInfo.Priority = 'Medium';
    // cse.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get(rand_APP).getRecordTypeId();
    caseInfo.description = 'test';
    caseInfo.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('rand_App').getRecordTypeId();
    insert caseInfo;

    ContentVersion contentVersion = new ContentVersion(
    Title = 'Penguins',
    PathOnClient = 'Penguins.jpg',
    VersionData = Blob.valueOf('Test Content'),
    IsMajorVersion = true
    );
    insert contentVersion;    
    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

    //create ContentDocumentLink  record 
    ContentDocumentLink cdl = New ContentDocumentLink();
    cdl.LinkedEntityId = caseInfo.id;
    cdl.ContentDocumentId = documents[0].Id;
    cdl.shareType = 'V';
    insert cdl;

    ContentDistribution cd = new ContentDistribution();
    cd.Name = 'Test';
    cd.ContentVersionId = contentVersion.id;
    cd.PreferencesAllowViewInBrowser= true;
    cd.PreferencesLinkLatestVersion=true;
    cd.PreferencesNotifyOnVisit=false;
    cd.PreferencesPasswordRequired=false;
    cd.PreferencesAllowOriginalDownload= true;
    insert cd;
       
    test.startTest();
        {
            randAppController.uploadData('tan One Problem Report');
        }
    test.stopTest();
}

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Which part of code is not covering or is test class failing.

Thanks,
 
RaffusRaffus
I made some changes and written this code so far-
@isTest
    public static void loadDataTest(){


        rand_App_Customer__c customer = new rand_App_Customer__c();
        customer.Customer_ID__c = '1234567';
        customer.Customer_Email__c = 'tannow@tan.ae';
        insert customer;

        Case caseInfo = new Case();
  
        caseInfo.subject = 'tan One Problem Report';
        caseInfo.Status = 'Awaiting Approval';
        caseInfo.Priority = 'Medium';
        // cse.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get(rand_APP).getRecordTypeId();
        caseInfo.description = 'test';
        caseInfo.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('rand_App').getRecordTypeId();
        insert caseInfo;

        ContentVersion contentVersion = new ContentVersion(
        Title = 'Penguins',
        PathOnClient = 'Penguins.jpg',
        VersionData = Blob.valueOf('Test Content'),
        IsMajorVersion = true,
        ContentLocation = 'S'
        );
        insert contentVersion;    
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = caseInfo.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;

        ContentDistribution cd = new ContentDistribution();
        cd.Name = 'Test';
        cd.ContentVersionId = contentVersion.id;
        cd.PreferencesAllowViewInBrowser= true;
        cd.PreferencesLinkLatestVersion=true;
        cd.PreferencesNotifyOnVisit=false;
        cd.PreferencesPasswordRequired=false;
        cd.PreferencesAllowOriginalDownload= true;
        insert cd;

        String jsoninput = '{"category":"fountain show","subCategory":"fountain","mobile":"1234567890","emailid":"test@gmail.com", "partyId":"12133"}';
        string str = randAppController.uploadData(jsoninput);

        // randAppController.uploadData(str);
         
        Test.startTest();
        try
       {
         randAppController.uploadData(str);
       }
       catch(exception e)
         {
        }     
         Test.stopTest();
     }

but from this for loop code is not covered-
for (FileInfoWrapper file : caseInfo.files) {
            System.debug('fileInfo ' + file);
            if (String.isNotBlank(file.base64Data)) {
                ContentVersion cv = new ContentVersion();
                cv.ContentLocation = 'S';
                cv.PathOnClient = file.fileName;
                cv.VersionData = EncodingUtil.base64Decode(file.base64Data);
                cv.Title = file.fileName;
                cVersions.add(cv);
            }
        }
        List<Id> contentDocumentIds = new List<Id>(caseInfo.contentDocumentIds);
        if (cVersions.size() > 0) {
            insert cVersions;
            List<ContentDistribution> ContentDistributionList = new List<ContentDistribution>();
            for(ContentVersion ctv : cVersions){
                ContentDistribution cd = new ContentDistribution();
                cd.Name = ctv.Title;
                cd.ContentVersionId = ctv.Id;
                cd.PreferencesAllowViewInBrowser = true;
                cd.PreferencesLinkLatestVersion = true;
                cd.PreferencesNotifyOnVisit = false;
                cd.PreferencesPasswordRequired = false;
                cd.PreferencesAllowOriginalDownload = true;
                ContentDistributionList.add(cd);
            }
            if(ContentDistributionList.size() > 0){
                INSERT ContentDistributionList;
            }
            Set<Id> cvIds = new Map<Id, ContentVersion>(cVersions).keySet();
            for (ContentVersion cVersion : [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id IN:cvIds ORDER BY CreatedDate ASC]) {
                contentDocumentIds.add(cVersion.ContentDocumentId);
            }
        }
        List<ContentDocumentLink> links = new List<ContentDocumentLink>();
        for (Id contentDocumentId : contentDocumentIds) {
            ContentDocumentLink link = new ContentDocumentLink();
            link.LinkedEntityId = cse.Id;
            link.ContentDocumentId = contentDocumentId;
            link.ShareType = 'V';
            links.add(link);
        }
        insert links;
        return [SELECT CaseNumber FROM Case WHERE Id = :cse.Id]?.CaseNumber;
    }

please help