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
RajnisfRajnisf 

Rest Callout test Class help

public class projectrequestnew { 
              
   @future(callout=true)
    public static void getCalloutResponseContents() {
        String result = null;
        //String result_post = null;  
        
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('endpoint');
    
    req.setMethod('GET');
    HttpResponse res = h.send(req);
        if(res.getStatusCode() == 200){
            result = res.getBody();
        } 
        
      
       // to store soci projects    
        List<ResultClass> WrapperProjectList = new List<ResultClass>();
        
        // to map soci projects
        Map<String, Object> projectResult = new Map<String, Object>();
        
        Map<String, Object> mapResult = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
        List<Object> resultList = (List<Object>)(mapResult.get('projects'));
        if(resultList != null){
        for (Object entry : resultList) {
             WrapperProjectList.add((ResultClass)(JSON.deserialize(JSON.serialize(entry), ResultClass.class)));             
             Map<String, Object> pResult = (Map<String, Object>) entry;
             projectResult.put((String)pResult.get('id'),pResult.get('meta_idx'));
             
        }   
        }     
        
               Set<String> accNames = new Set<String>();
        for (ResultClass f : WrapperProjectList) {
            accNames.add(f.name);
        }

    // map Soci project name with Account name
        Map<String, Account> accounts = new Map<String, Account>();
        for (Account a : [
                select Id, Name
                from Account
                where Name in :accNames
                ]) {
            accounts.put(a.Name, a);
        }
        for (ResultClass f : WrapperProjectList) {
            Account a;
            if (accounts.containsKey(f.name)) {
                a = accounts.get(f.name);
            } else {
                a = new Account(Name = f.name);
                accounts.put(f.name, a);
            }
            a.SOCi_Id__c = f.id;
        }
        upsert accounts.values();
            
            
// create or insert channel     
            
            Set<String> accIds = new Set<String>();
            for (ResultClass f : WrapperProjectList) {
                accIds.add(f.id);
            }
            Map<String, Channel__c> channels = new Map<String, Channel__c>();
            for (Channel__c ch : [
                    select Id, Name , Soci_Channel_ID__c
                    from Channel__c
                    where Account__r.SOCi_Id__c in :accIds
                    ]) {
                channels.put(ch.Soci_Channel_ID__c, ch);
            }
            
                 
   
        for(Integer i = 0; i<WrapperProjectList.size(); i++){              
            ID acid = [SELECT id from Account where SOCi_Id__c =: WrapperProjectList.get(i).id LIMIT 1].id; 
           
            System.debug('The channel sfdc List value is: ' + channels);
            for (ChannelClass f : WrapperProjectList.get(i).networks) {
                Channel__c ch;
                if (channels.containsKey(f.id)) {
                    ch = channels.get(f.id);
                } else {
                    ch = new Channel__c(Soci_Channel_ID__c = f.id, Account__c = acid);
                    channels.put(f.id, ch);
                }
                ch.Name= WrapperProjectList.get(i).name +' '+ f.network;
                
          
                 Map<String, Object> metaResult = (Map<String, Object>) projectResult.get(WrapperProjectList.get(i).id);
                
                if(f.network == 'twitter')
                {
           
                    ch.Link_to_Channel__c = (String) metaResult.get('Twitter');
                }
                else if(f.network == 'LinkedIn')
                {
                 
                    ch.Link_to_Channel__c = (String) metaResult.get('LinkedIn');
                }
                
                else
                {
                    ch.Link_to_Channel__c = '';
                }
                
                ch.Type_of_Channel__c = f.network;                
                }
                    
            System.debug('The channel List value is: ' + channels);                        
       }          
          upsert channels.values();   

              
  }
  
  
    class ResultClass {
        public String id;
        public String account_name;
        public String name;
        public String account_id;
        public MetaidxClass meta_idx;
        public List<ChannelClass> networks;          
    }  
    class MetaidxClass {
        public String LinkedIn;        
        public String Twitter; 
    }

    class ChannelClass {
        public String id;
        public String network;
        public String name;
        public String picture;
        
    }    
    
}
RajnisfRajnisf
@isTest
private class testprojectrequestnew {


     @testSetup static void testCallout() {   
                          
           Account acc = new Account(name = 'testing', Soci_ID__c = '8896');
            insert acc;
            
            acc.name = 'test2';
            
            update acc;
            
           Channel__c ch = new Channel__c();
                ch.Name = 'test';
                ch.Account__c = acc.id;
                ch.Link_to_Channel__c = 'https://www.twitter.com/';
                ch.Type_of_Channel__c = 'twitter';
                ch.Soci_Channel_ID__c = '1234';
                                
         insert ch;
         
         
         
    }      
                  
   @isTest static void testCalloutt(){
   
   Account acc1 = [Select Name, id,Soci_ID__c  from Account Limit 1 ];
   Channel__c c1 = [Select Id, Name,Type_of_Channel__c  FROM channel__c WHERE Name = 'test' Limit 1];
        
   Test.setMock(HttpCalloutMock.class, new MocksociAccount()); 
   
                 Test.startTest();
               projectrequestnew.getCalloutResponseContents();
                           
                Test.stopTest(); 
                c1 = [SELECT Id,Account__c , Name,Type_of_Channel__c FROM Channel__c where id =: c1.Id];
             
             System.assertEquals('tesst', c1.Name ); 
               System.assertEquals('Facebook', c1.Type_of_Channel__c ); 
           
         System.assertEquals('test2', acc1.Name ); 
           
 
}
      
}

covers only 40%