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
Sandeep TechAffinitySandeep TechAffinity 

How to write test class to HTTP Post class?

Hi Guys, This is my class can you help me to write a test class please.

@RestResource(urlMapping='/ChatMessages/*')
global class SlicktextWebhook {
    
    @HttpPost
    global static String doPost(){
        
        List<string> responseObjList = new List<string>();
        String MessVal;
        string myString ;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        if (req.headers.get('X-Slicktext-Signature') != 'ed290b9b5391112c7825deb6244f7972') {
            // If the webhook secret doesnt match what slicktext has, then dont do anything
            
        }
        ChatMessageParse responseObj = ChatMessageParse.parse(req.params.get('data'));
        // Get phone number from chat thread
        String phoneNumber = responseObj.ChatMessage.FromNumber;
        String ChatTrheadId = responseObj.ChatThread.ChatThreadId;
        String ChatMessageId = responseObj.ChatMessage.ChatMessageId;
        String MessageBody = responseObj.ChatMessage.Body;
        String TextwordId = responseObj.Textwords.get(0).TextwordId;
        String Textword = responseObj.Textwords.get(0).Textword;
        
        String sObjId;
        // Remove prefix
        phoneNumber = phoneNumber.replace('+1', '');
        List<Lead> listLead = [Select id, Name, Phone, isConverted from lead where isConverted = false and Phone =: phoneNumber LIMIT 1];
        If(listLead.size() > 0){
            sObjId = listLead.get(0).id;
        }else{
            List<contact> listContact = [Select id, Name, Phone from contact where Phone =: phoneNumber LIMIT 1];
            If(listContact.size() > 0){
                sObjId = listContact.get(0).id;
            }
            
        }
        
        responseObjList.add(ChatTrheadId);
        responseObjList.add(ChatMessageId);
        responseObjList.add(phoneNumber);
        responseObjList.add(MessageBody);
        responseObjList.add(TextwordId);
        responseObjList.add(Textword);
        responseObjList.add(sObjId);
        
        String SoId = String.valueOf(sObjId);
        String sub = SoId.substring(0, 3);
        List<ChatThread__c> chatThrd = [select id, Lead__c,Lead__r.Phone, WithNumber__c 
                                        from ChatThread__c 
                                        where WithNumber__c != null and name =: ChatTrheadId LIMIT 1];
        If(chatThrd.size() > 0){
            ChatMessage__c newMessage = new ChatMessage__c();
            If(sub == '00Q'){newMessage.Lead__c = sObjId;}
            else If(sub == '003'){newMessage.Contact__c = sObjId;}
            newMessage.FromNumber__c = phoneNumber;
            newMessage.Name = ChatMessageId;
            newMessage.ChatThread__c = chatThrd.get(0).id;
            newMessage.Body__c = MessageBody;
            newMessage.Action__c = 'RECEIVED';
            //newMessage.Lead__c = sObjId;
            insert newMessage;
        }else{
            If(sub == '00Q'){
                ChatMessage__c cm = new ChatMessage__c(Name = ChatMessageId, Action__c = 'RECEIVED', 
                                                       Body__c = MessageBody, Lead__c = sObjId,
                                                       FromNumber__c = phoneNumber);
                ChatThread__c ct = new ChatThread__c(Name = ChatTrheadId);
                cm.ChatThread__r = ct;
                ChatThread__c ctp = new ChatThread__c(Name = ChatTrheadId, WithNumber__c = phoneNumber, Lead__c = sObjId);
                Database.SaveResult[] results = Database.insert(new SObject[] {
                    ctp, cm });
            }
            
        }
        return ChatTrheadId;
        
    }
    
}

Thank you
Sandeep.
PriyaPriya (Salesforce Developers) 

Hi Sandeep,

Kindly refer the similar ask and modify yours accordingly :- 

https://salesforce.stackexchange.com/questions/330952/help-understanding-how-to-write-test-class-for-apex-http-post

If it helps, please mark it as best answer so that it can help other as well.

Regards,

Priya Ranjan

Sandeep TechAffinitySandeep TechAffinity

Hi Priya,

I am not getting the response JSON body. Because it's coming from 3rd part application.
Error:
System.NullPointerException: null input to JSON parser

Class.System.JSON.deserialize: line 15, column 1
Class.ChatMessageParse.parse: line 32, column 1
Class.SlicktextWebhook.doPost: line 16, column 1
Class.SlicktextWebhook_Test.testSlickTextWebhook: line 35, column 1

can you share any other example please

Thank you
Sandeep.