+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

How to cover a test class for this code ?

Hi ,

Can anyone helps to cover a test class for this class 
public class GD_ProductSearchController {
    public static ApexPages.StandardSetController ssc;
    public static Map<Id,GD_CartProductsWrapper> mapOfProduct = new Map<Id,GD_CartProductsWrapper>();
    //    public static final String  priceListName = 'RPL01';
    public static List<GD_Cart__c> cartListToReturn = new List<GD_Cart__c>();
    @AuraEnabled
    public static InitData getDataInit(){
        InitData data = new InitData();
        data.cartProducts = getExistingProductsInCart();
        data.assignedInventories = getInventoryOrgDetails();
        return data;
    }
    
    @AuraEnabled
    public static List<GD_Cart__c> getProductsToDisplay(String pageSize,String pageNumber,String productSearch,String inventoryOrgId){
        try{
            mapOfProduct = new Map<Id,GD_CartProductsWrapper>();
            cartListToReturn = new List<GD_Cart__c>();
            system.debug('Inventory '+inventoryOrgId);
            List<User> loggedInUser = new List<User>();
            loggedInUser = [SELECT Id,Name,Contact.AccountId FROM User WHERE Id=:userInfo.getUserId()];
            Account acc = [SELECT Id,GD_Customer_Class__c,GD_Status__c,GD_Product_List__c,GD_Price_List__c FROM Account WHERE Id=:loggedInUser.get(0).Contact.AccountId];
            // AND GD_Status__c = 'Active' AND GD_Product_List__c != null
            if(acc.GD_Status__c != 'Active'){
                throw new AuraHandledException('Your account is not Active. Please contact sales rep to activate your account.');    
            }
            if(acc.GD_Product_List__c == null){
                throw new AuraHandledException('Please contact your sales rep with error message - Product list not assigned to customer.');    
            }
            if(acc.GD_Price_List__c == null){
                throw new AuraHandledException('Please contact your sales rep with error message - Price list not assigned to customer.');    
            }
            
            system.debug('GD_Product_List__c '+acc.GD_Product_List__c);
            if(acc.GD_Price_List__c != null){
                String priceListId = acc.GD_Price_List__c; 
                Set<Id> portalItemIds = new Set<Id>();
                // GD_Inventory_Organization__c=:inventoryOrgId AND
                for(GD_Portal_Products__c product : [SELECT Id,Name,GD_Item__c FROM GD_Portal_Products__c WHERE GD_Product_List__c=:acc.GD_Product_List__c]){
                    portalItemIds.add(product.GD_Item__c);
                }
                system.debug('---> portalItemIds '+portalItemIds);
                if(portalItemIds.size() > 0){
                    String query = 'SELECT Id,Name,GD_Item_Description__c,GD_Item__c,GD_Price_List__c,GD_Item__r.GD_Inventory__c, ';
                    query += ' GD_Unit_Price__c,GD_UOM__c,GD_Code_UOM_Formula__c FROM GD_Price_List_Entry__c ';
                    query += ' WHERE GD_Price_List__c = :priceListId  AND GD_Item__r.GD_Inventory__c = :inventoryOrgId';
                    query += ' AND GD_Item__r.GD_Segment_3__c = \'A\' ';
                    query += ' AND GD_Item__c IN :portalItemIds ';
                    system.debug('productSearch '+productSearch);
                    if(productSearch != ''){
                        String searchTerm = '%'+String.escapeSingleQuotes(productSearch) +'%';  // assign the search term to a variable
                        query += ' AND GD_Item_Description__c LIKE :searchTerm';
                    }
                    //        query += ' AND GD_Unit_Price__c > 0  ';  
                    query += ' ORDER BY GD_Item_Description__c ';
                    system.debug('pageSize '+pageSize);
                    system.debug('pageNumber '+pageNumber);
                    system.debug('query '+query);
                    
                    ssc = new ApexPages.StandardSetController( Database.getQueryLocator( query ) );
                    system.debug('ssc '+ssc);
                    
                    //set page size
                    ssc.setpagesize(Integer.valueOf(pageSize));
                    //set page number 
                    ssc.setPageNumber(Integer.valueOf(pageNumber));
                    system.debug(' Set Page number');
                    //      accountList =  (List<Account>) ssc.getRecords();
                    system.debug(' Adding account list');
                    List<GD_Price_List_Entry__c> priceList = new List<GD_Price_List_Entry__c>();
                    priceList =  (List<GD_Price_List_Entry__c>) ssc.getRecords();
                    system.debug('---> priceList '+priceList);
                    system.debug('ssc.getHasNext() '+ssc.getHasNext());
                    system.debug('ssc.getHasPrevious()'+ssc.getHasPrevious());
                    //system.debug();
                    system.debug('ssc.getRecords() '+ssc.getRecords());
                    system.debug('priceList '+priceList);
                    List<GD_CartProductsWrapper> cartProducts = new  List<GD_CartProductsWrapper>();
                    
                    List<GD_CartProductsWrapper> listOfProduct = new List<GD_CartProductsWrapper>();
                    List<GD_Order_Line_Item__c> orderLineItem = new List<GD_Order_Line_Item__c>();
                    Map<Id,List<String>> uomList = new Map<Id,List<String>>();
                    for(GD_Price_List_Entry__c p : priceList){
                        populateWrapper(p,inventoryOrgId);
                    }
                }
            }
            return cartListToReturn;
        }catch(Exception e){
            throw new AuraHandledException('Please contact your sales rep with error message - '+ e.getMessage());    
        }
    }
    
    public static GD_Cart__c populateOrderLine(GD_Price_List_Entry__c p ,String inventoryOrgId){
        GD_Cart__c line = new GD_Cart__c();
        
        line.Name = p.GD_Item_Description__c;
        line.GD_Item__c = p.GD_Item__c;
        line.GD_Quantity__c = 0;
        line.GD_Unit_Price__c = p.GD_Unit_Price__c;
        line.GD_UOM__C = p.GD_UOM__c;
        line.GD_Price_List_Entry__c = p.Id;
        line.GD_Inventory_Organization__c = inventoryOrgId; 
        return line;
    }
    
    public static void populateWrapper(GD_Price_List_Entry__c p,String inventoryOrgId){
        system.debug(p.GD_Item_Description__c);
        system.debug('---> POPULATING'+p.GD_Item__c);
        GD_CartProductsWrapper product = new GD_CartProductsWrapper();
        GD_Cart__c line = populateOrderLine(p,inventoryOrgId);
        cartListToReturn.add(line);
        product.cartItem = line;
        product.unitPrice = p.GD_Unit_Price__c;
        mapOfProduct.put(p.Id, product);
    }
    
    public static List<GD_Inventory_Organization__c> getInventoryOrgDetails(){
        Set<Id> inventoryIds = new Set<Id>();
        for(GD_User_Inventory_OrganizationAssignment__c assignment : [SELECT Id,GD_Inventory_Organization__c,GD_User__c FROM GD_User_Inventory_OrganizationAssignment__c WHERE GD_User__c =:userInfo.getUserId()]){
            inventoryIds.add(assignment.GD_Inventory_Organization__c);
            
        }
        
        List<GD_Inventory_Organization__c> inventoryOrgList = new List<GD_Inventory_Organization__c>();
        inventoryOrgList = [SELECT Id,Name,GD_Alias_Name__c FROM GD_Inventory_Organization__c WHERE  Id IN: inventoryIds ];
        
        return inventoryOrgList;
    }
    
    
    
    @AuraEnabled
    public static GD_Cart__c addProductToCart(GD_Cart__c orderLine){
        try{
            if(orderLine.GD_User__c == null){
                orderLine.GD_User__c = userInfo.getUserId();
            }
            system.debug('orderLine '+orderLine);
            Upsert orderLine;
            return orderLine;
        }catch(Exception e){
            
            throw new AuraHandledException(e.getMessage());    
        }
    }
    //GD_Total_Price_Formula__c
    @AuraEnabled
    public static List<GD_Cart__c> getExistingProductsInCart(){
        try{
            List<GD_Cart__c> cartList = new List<GD_Cart__c>();
            cartList = [SELECT id,Name,GD_Item__r.Name,GD_Unit_Price__c,GD_Total_Price_Formula__c,GD_Total_Price_With_Vat__c,GD_Quantity__c,GD_UOM__c,
                        CreatedDate FROM GD_Cart__c WHERE GD_User__c =:userInfo.getUserId() ORDER BY CreatedDate DESC];
            return cartList;
        }catch(Exception e){
            
            throw new AuraHandledException(e.getMessage());    
        }
    }
    
    // Creating wrapper to send data on init.
    public Class InitData{
        @AuraEnabled
        public List<GD_Cart__c> cartProducts;
        
        @AuraEnabled
        public List<GD_Inventory_Organization__c> assignedInventories;
        
        public InitData(){
            cartProducts = new List<GD_Cart__c>();
            assignedInventories = new  List<GD_Inventory_Organization__c>() ;
        } 
    }
    
}



My test class is covered 70% , follwing method is not covering.
 
@AuraEnabled
    public static List<GD_Cart__c> getProductsToDisplay(String pageSize,String pageNumber,String productSearch,String inventoryOrgId){
        try{
            mapOfProduct = new Map<Id,GD_CartProductsWrapper>();
            cartListToReturn = new List<GD_Cart__c>();
            system.debug('Inventory '+inventoryOrgId);
            List<User> loggedInUser = new List<User>();
            loggedInUser = [SELECT Id,Name,Contact.AccountId FROM User WHERE Id=:userInfo.getUserId()];
            Account acc = [SELECT Id,GD_Customer_Class__c,GD_Status__c,GD_Product_List__c,GD_Price_List__c FROM Account WHERE Id=:loggedInUser.get(0).Contact.AccountId];
            // AND GD_Status__c = 'Active' AND GD_Product_List__c != null
            if(acc.GD_Status__c != 'Active'){
                throw new AuraHandledException('Your account is not Active. Please contact sales rep to activate your account.');    
            }
            if(acc.GD_Product_List__c == null){
                throw new AuraHandledException('Please contact your sales rep with error message - Product list not assigned to customer.');    
            }
            if(acc.GD_Price_List__c == null){
                throw new AuraHandledException('Please contact your sales rep with error message - Price list not assigned to customer.');    
            }
            
            system.debug('GD_Product_List__c '+acc.GD_Product_List__c);
            if(acc.GD_Price_List__c != null){
                String priceListId = acc.GD_Price_List__c; 
                Set<Id> portalItemIds = new Set<Id>();
                // GD_Inventory_Organization__c=:inventoryOrgId AND
                for(GD_Portal_Products__c product : [SELECT Id,Name,GD_Item__c FROM GD_Portal_Products__c WHERE GD_Product_List__c=:acc.GD_Product_List__c]){
                    portalItemIds.add(product.GD_Item__c);
                }
                system.debug('---> portalItemIds '+portalItemIds);
                if(portalItemIds.size() > 0){
                    String query = 'SELECT Id,Name,GD_Item_Description__c,GD_Item__c,GD_Price_List__c,GD_Item__r.GD_Inventory__c, ';
                    query += ' GD_Unit_Price__c,GD_UOM__c,GD_Code_UOM_Formula__c FROM GD_Price_List_Entry__c ';
                    query += ' WHERE GD_Price_List__c = :priceListId  AND GD_Item__r.GD_Inventory__c = :inventoryOrgId';
                    query += ' AND GD_Item__r.GD_Segment_3__c = \'A\' ';
                    query += ' AND GD_Item__c IN :portalItemIds ';
                    system.debug('productSearch '+productSearch);
                    if(productSearch != ''){
                        String searchTerm = '%'+String.escapeSingleQuotes(productSearch) +'%';  // assign the search term to a variable
                        query += ' AND GD_Item_Description__c LIKE :searchTerm';
                    }
                    //        query += ' AND GD_Unit_Price__c > 0  ';  
                    query += ' ORDER BY GD_Item_Description__c ';
                    system.debug('pageSize '+pageSize);
                    system.debug('pageNumber '+pageNumber);
                    system.debug('query '+query);
                    
                    ssc = new ApexPages.StandardSetController( Database.getQueryLocator( query ) );
                    system.debug('ssc '+ssc);
                    
                    //set page size
                    ssc.setpagesize(Integer.valueOf(pageSize));
                    //set page number 
                    ssc.setPageNumber(Integer.valueOf(pageNumber));
                    system.debug(' Set Page number');
                    //      accountList =  (List<Account>) ssc.getRecords();
                    system.debug(' Adding account list');
                    List<GD_Price_List_Entry__c> priceList = new List<GD_Price_List_Entry__c>();
                    priceList =  (List<GD_Price_List_Entry__c>) ssc.getRecords();
                    system.debug('---> priceList '+priceList);
                    system.debug('ssc.getHasNext() '+ssc.getHasNext());
                    system.debug('ssc.getHasPrevious()'+ssc.getHasPrevious());
                    //system.debug();
                    system.debug('ssc.getRecords() '+ssc.getRecords());
                    system.debug('priceList '+priceList);
                    List<GD_CartProductsWrapper> cartProducts = new  List<GD_CartProductsWrapper>();
                    
                    List<GD_CartProductsWrapper> listOfProduct = new List<GD_CartProductsWrapper>();
                    List<GD_Order_Line_Item__c> orderLineItem = new List<GD_Order_Line_Item__c>();
                    Map<Id,List<String>> uomList = new Map<Id,List<String>>();
                    for(GD_Price_List_Entry__c p : priceList){
                        populateWrapper(p,inventoryOrgId);
                    }
                }
            }
            return cartListToReturn;
        }catch(Exception e){
            throw new AuraHandledException('Please contact your sales rep with error message - '+ e.getMessage());    
        }
    }



Thanks in Advance,
Soundar.
Best Answer chosen by Soundar Raj Ponpandi
David Zhu 🔥David Zhu 🔥
If only for code covering purpose, you can add a test method for getProductsToDisplay method.