+ Start a Discussion
sumit dsumit d 

test class for contentDocumentLink trigger helper

Hi All,
       My Helper class is given below:-
   /*
 *  Trigger Helper Class to parse image response
 *  Parse Image response From social post
 * */
public without sharing class ContentDocumentLinkTriggerHelper {
    public static List<ContentDocumentLink> newList;
    public static List<ContentDocumentLink> oldList;
    public static Map<Id, ContentDocumentLink> newMap;
    public static Map<Id, ContentDocumentLink> oldMap;
    
    public static Boolean runTrigger = true;
    // method to Parse Image response from social Post
    public static void parseImageResponse(){
        Set<Id> socialPostIds = new Set<Id>(newMap.keySet());
        List<Predictions__c> pdcList = new List<Predictions__c>();
        Set<Id> setLinkId = new Set<Id>();
        for(contentdocumentlink cdl : newList){
            if(String.valueOf((cdl.LinkedEntityId)).startsWith('0ST') && !setLinkId.contains(cdl.LinkedEntityId)){
                String jsonResponse = EinsteenPredictionService.getImagePrediction('test');
                EinsteenPredictionService ep= EinsteenPredictionService.parseImagePrediction(jsonResponse);
                for(EinsteenPredictionService.cls_probabilities result: ep.probabilities) {
                    Predictions__c    pc = new Predictions__c();
                    pc.Label__c = result.label;
                    pc.Probability__c = result.probability;
                    pc.Social_Post__c = cdl.LinkedEntityId;
                    pdcList.add(pc);
                }
                setLinkId.add(cdl.LinkedEntityId);
            }
        }
        //Check Prediction list Has value or not
        if(pdcList.size() > 0){
            insert pdcList; 
        }
    }
}
how to write test class for this helper class?
Any suggestions?
Saurav PrasadSaurav Prasad
Taking this as reference you can write the test class

https://gist.github.com/xgeek-net/b96fa1794899f4c31428
 
Kamal ThakurKamal Thakur

Hi Sumit,

You can start with below code and customize as per your logic:

@isTest
private class contentDocumentLinkTriggerTest{
@isTest
static void itShould()
{
    Account acc = new Account(name='test acc');
    insert acc;
    Contact con = new Contact(lastname='test cont',accountid=acc.id);
    insert con;
    Opportunity opp = new Opportunity(name='testoppty',AccountId=acc.id,stageName='Qualification',closedate=system.today());
    insert opp;

    ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
    insert content;
    ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=acc.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'V';
        test.starttest();
    insert contentlink;
}


Let me know if it helps. If it does, kindly mark this question as Best. 

Thanks,

Kamal

sumit dsumit d
Hi kamal,
its giving this error:-System.UnexpectedException: ContentPublication Limit exceeded.