+ Start a Discussion
AntonPavlovAntonPavlov 

Help to write Test class apex

This is code apex 
public class displayImageController {
   @AuraEnabled  
    public static List<String> getImage(Id CurrentId){
          List<String> files =new List<String>();
        List<ContentDocumentLink> cdLinks =[SELECT Id,ContentDocument.LatestPublishedVersionID,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:CurrentId];
        for(ContentDocumentLink cdl: cdLinks){
             String FileExtension =[SELECT FileExtension FROM ContentVersion WHERE ContentDocumentId=:cdl.ContentDocumentId].get(0).FileExtension;
            if(Pattern.matches('(png|jpg|jpeg|gif|svg)', FileExtension)){
                  files.add(URL.getSalesforceBaseUrl().toExternalForm() +'/sfc/servlet.shepherd/version/download/'+cdl.ContentDocument.LatestPublishedVersionID);  
            }
        }
        return files;
    }
}

Apex test I don't know what write next

@isTest
public class displayImageControllerTest {
    private static testMethod void testCreate() {
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        insert testAccount;
        
        Contact cont = new Contact ();
        cont.FirstName = 'FirstName';
        cont.LastName = 'LastName';
        cont.Email='email233@email.com';
        cont.phone='12345678';
        insert cont;
        
        Account acct = new Account(Name='TEST');
        insert acct;
        
        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];
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = acct.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
  }
}
Best Answer chosen by AntonPavlov
Maharajan CMaharajan C
Hi,

Just you have to call the method and need to pass the account id.

Please use the below updated one:
 
@isTest
public class displayImageControllerTest {
    private static testMethod void testCreate() {
        
        Account acct = new Account(Name='TEST');
        insert acct;
        
        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];
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = acct.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
        
        test.startTest();
        	displayImageController.getImage(acct.Id);
        test.stopTest();
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi,

Just you have to call the method and need to pass the account id.

Please use the below updated one:
 
@isTest
public class displayImageControllerTest {
    private static testMethod void testCreate() {
        
        Account acct = new Account(Name='TEST');
        insert acct;
        
        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];
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = acct.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
        
        test.startTest();
        	displayImageController.getImage(acct.Id);
        test.stopTest();
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
AntonPavlovAntonPavlov
Hi Maharajan.C, Thanks you