Please reload

Recent Posts
Featured Posts

Test Class (100% code coverage) for Rest API web service class

February 21, 2016

In this post, I'll be posting a sample test class I wrote that has all CRUD operations and includes attachements in POST. It's built on a custom object Individual_KYC__c that has all the fields. 

 

The class.

 

@RestResource(urlMapping='/Demo/IndividualKYCManager/*')
global with sharing class IndividualKYCManager {
   
    global class KYCRequest {
        public String name {get; set;}
        public String email {get; set;}
        public String idnum {get; set;}
        public String phone{get; set;}
        public Boolean termsandconditions {get; set;}
        public String address1 {get; set;}
        public String address2 {get; set;}
        public String suburb {get; set;}
        public String nationality {get; set;}
        public String postcode {get; set;}
        public String summary {get; set;}
        public String uniqueid{get; set;}
        
        public List<KYCRequestAttachment> attachments {get; set;}
    }

    global class KYCRequestAttachment {
        public String type {get; set;}
        public String base64 {get; set;}
    }
    
    //Create
    @HttpPost
    global static String IndividualKYCManager (KYCRequest request){
    
    Individual_KYC__c ikyc=new Individual_KYC__c();
    
    ikyc.Name = request.name;
    ikyc.Email__c = request.email;
    ikyc.Phone__c =request.phone;
    ikyc.ID_Number__c = request.idnum;  
    ikyc.Terms_and_Conditions__c = request.termsandconditions;
    ikyc.Address_Line_1__c = request.address1;
    ikyc.Address_Line_2_Optional__c = request.address2;
    ikyc.Suburb__c = request.suburb;
    ikyc.Nationality__c = request.nationality;
    ikyc.Post_Code__c = request.postcode;
    ikyc.Profile_Summary__c = request.summary;
   
    
    insert ikyc;
    
    String ikycid=ikyc.id;
    
    Attachment[] attachments = new Attachment[] {};
    
        for (KYCRequestAttachment att : request.attachments) {
        attachments.add(new Attachment(
            Name = att.type,
            
            Body = EncodingUtil.base64Decode(att.base64),
            //Body = Blob.valueOf(att.base64),            
            ContentType = 'application/vnd.ms-excel',
            ParentId = ikyc.id
        ));
        }
        insert attachments;
        return ikycid;    
    }
        
      
    
   //Update
    @HttpPatch
    global static String updateAccount(String name, String photo,String email,string idnum, String phone, Boolean termsandconditions, String address1, String address2, String suburb, String nationality, String postcode, String summary){
      
      RestRequest req = RestContext.request;
      RestResponse res = RestContext.response;
      String keyID= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);    
    
      Individual_KYC__c  kyc= [select Id, Photo__c, Name,Phone__c,Terms_and_Conditions__c ,Address_Line_1__c ,Address_Line_2_Optional__c,Suburb__c,
      Nationality__c,Post_Code__c,Profile_Summary__c from Individual_KYC__c  where id =: keyID];
     
        kyc.Name=name;
        kyc.Email__c=email;
        kyc.ID_Number__c=idnum;                
        kyc.Phone__c=phone;
        kyc.Terms_and_Conditions__c = termsandconditions;
        kyc.Address_Line_1__c = address1;
        kyc.Address_Line_2_Optional__c= address2;
        kyc.Suburb__c= suburb;
        kyc.Nationality__c= nationality;
        kyc.Post_Code__c= postcode;
        kyc.Photo__c = photo;
        kyc.Profile_Summary__c= summary ;     
      
      update kyc;
      return 'Record updated';
    }    
   //Retrieve 
    @HttpGet
    global static Individual_KYC__c getIndividualKYCById() {
    
        RestRequest req = RestContext.request; 
        RestResponse res = RestContext.response;       
        String merchId = req.requestURI.substring(
                                  req.requestURI.lastIndexOf('/')+1);
        Individual_KYC__c result = 
                       [SELECT Name,Email__c,ID_Number__c,Photo__c,Phone__c, Address_Line_1__c ,Address_Line_2_Optional__c,Suburb__c,Nationality__c,Post_Code__c,Profile_Summary__c
                        FROM Individual_KYC__c 
                        WHERE Id = :merchId];
        return result;
    }
    
    //Delete
    @HttpDelete
    global static String doDelete() {
    
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String kycID = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Individual_KYC__c kyc = [SELECT Id FROM Individual_KYC__c WHERE Id = :kycID];
        delete kyc;
        return 'Record deleted';
    }    

 

______________________________________________________________________

Test Class.

 

@isTest
public class IndividualKYCManagerTest{

    @testSetup static void setup() {
        Individual_KYC__c ikyc = new Individual_KYC__c();
        ikyc.Name = 'Test';
        ikyc.Email__c = 'test@gmail.com';
        ikyc.Phone__c = '+22 678 456 345';
        ikyc.Terms_and_Conditions__c = false;
        ikyc.Address_Line_1__c = '23 West Street';
        insert ikyc;
    }
     
    static testMethod void testCreateIndividualKYCManager() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        IndividualKYCManager.KYCRequest kycReq = new IndividualKYCManager.KYCRequest();
        kycReq.name = 'name';
        kycReq.email = 'email@domain.com';
        kycReq.idnum = 'id number';
        kycReq.phone = '+23 567 890';
        kycReq.termsandconditions = true;
        kycReq.address1 = 'address 1';
        kycReq.address2 = 'address 2';
        kycReq.suburb = 'suburb';
        kycReq.nationality = 'SA';
        kycReq.postcode = '1234';
        kycReq.summary = 'summary';
        kycReq.uniqueid = 'unique id';
        kycReq.attachments = new List<IndividualKYCManager.KYCRequestAttachment>();
        
        IndividualKYCManager.KYCRequestAttachment attach1 = new IndividualKYCManager.KYCRequestAttachment();
        attach1.type = 'MyAttach1.txt';
        attach1.base64 = EncodingUtil.base64Encode(Blob.valueOf('attachment 1'));
        kycReq.attachments.add(attach1);
        
        IndividualKYCManager.KYCRequestAttachment attach2 = new IndividualKYCManager.KYCRequestAttachment();
        attach2.type = 'MyAttach2.txt';
        attach2.base64 = EncodingUtil.base64Encode(Blob.valueOf('attachment 2'));
        kycReq.attachments.add(attach2);
        
        String jsonMsg = JSON.serialize(kycReq );

        // pass the req and resp objects to the method     
        req.requestURI = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Demo/IndividualKYCManager';  
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(jsonMsg);
        RestContext.request = req;
        RestContext.response = res;
        
        String ikycId = IndividualKYCManager.IndividualKYCManager(kycReq);
        Individual_KYC__c ikyc = [SELECT Id, Name, Email__c, Phone__c, ID_Number__c, Terms_and_Conditions__c, Address_Line_1__c, Address_Line_2_Optional__c,
                                    Suburb__c, Nationality__c, Post_Code__c, Profile_Summary__c FROM Individual_KYC__c WHERE Id = :ikycId];
        System.assertEquals(ikyc.Name, 'name');
        System.assertEquals(ikyc.Email__c, 'email@domain.com');
        System.assertEquals(ikyc.Phone__c, '+23 567 890');
        System.assertEquals(ikyc.ID_Number__c, 'id number');
        System.assertEquals(ikyc.Terms_and_Conditions__c , true);
        System.assertEquals(ikyc.Address_Line_1__c, 'address 1');
        System.assertEquals(ikyc.Address_Line_2_Optional__c, 'address 2');
        System.assertEquals(ikyc.Suburb__c, 'suburb');
        System.assertEquals(ikyc.Nationality__c, 'SA');
        System.assertEquals(ikyc.Post_Code__c, '1234');
        System.assertEquals(ikyc.Profile_Summary__c, 'summary');
        
        List<Attachment> attachments = [SELECT Id, Name FROM Attachment WHERE ParentId = :ikycId];
        System.assertEquals(attachments.size(), 2);
    }
    
    static testMethod void testUpdateAccount() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        Individual_KYC__c ikyc = [SELECT Id, Name FROM Individual_KYC__c WHERE Name = 'Test'];

        // pass the req and resp objects to the method     
        req.requestURI = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Demo/IndividualKYCManager/' + ikyc.Id;  
        req.httpMethod = 'PATCH';
        RestContext.request = req;
        RestContext.response = res;
        
        String result = IndividualKYCManager.updateAccount('Test', null, 'test@gmail.com', 'id number', '+23 456 213', true, 'address 1', 'optional', null, 'SA', '9999', 'summary');
        System.assertEquals(result, 'Record updated');
        
        Individual_KYC__c ikycAfterUpd = [SELECT Id, Name, Photo__c, Email__c, ID_Number__c, Phone__c, Terms_and_Conditions__c, Address_Line_1__c, Address_Line_2_Optional__c, 
                                            Suburb__c, Nationality__c, Post_Code__c, Profile_Summary__c FROM Individual_KYC__c WHERE Id = :ikyc.Id];
        System.assertEquals(ikycAfterUpd.Name, 'Test');
        System.assertEquals(ikycAfterUpd.Photo__c, null);
        System.assertEquals(ikycAfterUpd.Email__c, 'test@gmail.com');
        System.assertEquals(ikycAfterUpd.ID_Number__c, 'id number');
        System.assertEquals(ikycAfterUpd.Phone__c, '+23 456 213');
        System.assertEquals(ikycAfterUpd.Terms_and_Conditions__c, true);
        System.assertEquals(ikycAfterUpd.Address_Line_1__c, 'address 1');
        System.assertEquals(ikycAfterUpd.Address_Line_2_Optional__c, 'optional');
        System.assertEquals(ikycAfterUpd.Suburb__c, null);
        System.assertEquals(ikycAfterUpd.Nationality__c, 'SA');
        System.assertEquals(ikycAfterUpd.Post_Code__c, '9999');
        System.assertEquals(ikycAfterUpd.Profile_Summary__c, 'summary');
    }

    static testMethod void testGetIndividualKYCById() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        Individual_KYC__c ikyc = [SELECT Id, Name FROM Individual_KYC__c WHERE Name = 'Test'];

        // pass the req and resp objects to the method     
        req.requestURI = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Demo/IndividualKYCManager/' + ikyc.Id;  
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;
        
        Individual_KYC__c result = IndividualKYCManager.getIndividualKYCById();
        
        System.assertEquals(result.Name, 'Test');
        System.assertEquals(result.Email__c, 'test@gmail.com');
        System.assertEquals(result.ID_Number__c, null);
        System.assertEquals(result.Photo__c, null);
        System.assertEquals(result.Phone__c, '+22 678 456 345');
        System.assertEquals(result.Address_Line_1__c, '23 West Street');
        System.assertEquals(result.Address_Line_2_Optional__c, null);
        System.assertEquals(result.Suburb__c, null);
        System.assertEquals(result.Nationality__c, null);
        System.assertEquals(result.Post_Code__c, null);
        System.assertEquals(result.Profile_Summary__c, null);
    }
    
    static testMethod void testDoDelete() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        Individual_KYC__c ikyc = [SELECT Id, Name FROM Individual_KYC__c WHERE Name = 'Test'];

        // pass the req and resp objects to the method     
        req.requestURI = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Demo/IndividualKYCManager/' + ikyc.Id;  
        req.httpMethod = 'DELETE';
        RestContext.request = req;
        RestContext.response = res;
        
        String result = IndividualKYCManager.doDelete();
        System.assertEquals(result, 'Record deleted');
        
        List<Individual_KYC__c> ikycs = [SELECT Id, Name FROM Individual_KYC__c WHERE Id = :ikyc.Id];
        System.assertEquals(ikycs.size(), 0);
    }
}

 


 

Please reload

Follow Me
Please reload

Search By Tags
Please reload

Archive
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square

Data Visualization with Chart.JS in Lightning Components

October 3, 2019

1/10
Please reload