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
Susmitha T 5Susmitha T 5 

How to write test class for document uplaodapi using restapi


Susmitha T 5
I need to add case with attachment in salesforce from our website, In web-to-case that is not possible directly.. so workaround is 
using apis like first ineed to create case then attach files to it.

Iwrite code for file uploading like
@RestResource(urlMapping='/DocumentUpload/*')
global class fileuploaderapi{
 
    @HttpPost
global static void uploadDocument()
{
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
Id recId = req.params.get('recordId');
string contenttype = req.params.get('contenttype');
string name = req.params.get('name');
Blob myBlob=Blob.valueOf(name);
try
{
        //Insert ContentVersion
        ContentVersion cVersion = new ContentVersion();
        cVersion.PathOnClient = name;//File name with extention
        cVersion.Title = name;//Name of the file
        cVersion.VersionData = req.requestBody;//File content
        Insert cVersion;

        //After saved the Content Verison, get the ContentDocumentId
        Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;

        //Insert ContentDocumentLink
        ContentDocumentLink cDocLink = new ContentDocumentLink();
        cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId
        cDocLink.LinkedEntityId = recId;//Add attachment parentId
        Insert cDocLink;

        RestContext.response.addHeader('Content-Type', 'application/json');
        RestContext.response.responseBody = Blob.valueOf(generateJSON('Success',conDocument,''));

        }
        catch(Exception e)
        {
        RestContext.response.addHeader('Content-Type', 'application/json');
        RestContext.response.responseBody = Blob.valueOf(generateJSON('Error','',e.getMessage()));
        }

        }
        // To generate JSON response
        private static string generateJSON(String Status,String Content,String error){
        JSONGenerator jsGen = JSON.createGenerator(true);
        jsGen.writeStartObject();
        jsGen.writeStringField('Status',Status);
        jsGen.writeStringField('ContentID', Content);
        jsGen.writeStringField('Error', error);
        jsGen.writeEndObject();
        return jsGen.getAsString();
        }
      }
    

It is successfull but i'm getting error for testclass code coverage
My test class is 
@isTest
public class fileuploadapitestclass {
     static testMethod void testMethod1() 
     {
    // Account acc = [ SELECT Id FROM Account where LastModifiedById!=null LIMIT 1 ];
    Case c=new Case();
        c.Subject='test';
        insert c;
       StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'GetcaseResource' LIMIT 1];
String body =  EncodingUtil.base64Encode(sr.Body);
        
        
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/DocumentUpload/' ;
        req.httpMethod = 'POST';
        req.addHeader('Content-Type', 'application/json');
         req.addHeader('Content-Type','image/jpeg');
        RestContext.request = req;
        RestContext.response= res;
        Id recId =c.Id;
        string contenttype = 'contenttype';
        string name = 'testname';
        //Blob myBlob=Blob.valueOf(name);
       
        ContentVersion cVersion = new ContentVersion();
        cVersion.Title = name;//Name of the file 
        cVersion.PathOnClient = 'testname.jpg';//File name with extention
       
        cVersion.VersionData =Blob.valueOf(body );//File content
        Insert cVersion;

        //After saved the Content Verison, get the ContentDocumentId
        Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;

        //Insert ContentDocumentLink
        ContentDocumentLink cDocLink = new ContentDocumentLink();
        cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId
        cDocLink.LinkedEntityId = recId;//Add attachment parentId
        Insert cDocLink;

       
     Test.setMock(HttpCalloutMock.class, new SMSCalloutMock());
      
       fileuploaderapi.uploadDocument();
        string Status;
        string Content;
        string error;
         JSONGenerator jsGen = JSON.createGenerator(true);
        jsGen.writeStartObject();
        jsGen.writeStringField('Status',Status);
        jsGen.writeStringField('ContentID', Content);
        jsGen.writeStringField('Error', error);
        jsGen.writeEndObject();
        Test.stopTest();
    }  

}
--------------------
I also tried not using static resource but no luck
Can any one help me solving my issue