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
Guru 91Guru 91 

Test Class?

Hi,
Require test class help

public with sharing class CompetitorStructure extends Hierarchy
{   
    /**
    * Return ObjectStructureMap to page
    * @return competitorNodes
    */
    public List<CompetitorHierarchyNode> getObjectStructure()
    {
        if ( currentId == null ) {
            currentId = System.currentPageReference().getParameters().get( 'id' );
        }
        
        System.assertNotEquals( currentId, null, 'sObject ID must be provided' );
        return formatObjectStructure( CurrentId );
    }

    /**
    * Query competitor from top down to build the CompetitorHierarchyNode
    * @param currentId
    * @return competitorNodes
    */
    public List<CompetitorHierarchyNode> formatObjectStructure( String currentId )
    {
        List<CompetitorHierarchyNode> competitorNodes = new List<CompetitorHierarchyNode>();
        return (List<CompetitorHierarchyNode>) generateHierarchy(competitorNodes);
    }

    protected override HierarchyNode getAllNodes()
    {
        idToNode.clear();
        List<Competitor__c> competitors = new List<Competitor__c>();
        List<Id> currentParents         = new List<Id>();
        HierarchyNode topNode           = null;

        Integer level                   = 0;
        Boolean noChildren              = false;
        
        //Find highest level obejct in the structure
        currentParents.add( GetTopElement( currentId ) );

        //Loop though all children
        while ( !noChildren ){

            if( level == 0 ){
                //Change below     
                competitors = [ SELECT Parent_Competitor__c, OwnerId, Name, Industry__c FROM Competitor__c WHERE Id IN :currentParents ORDER BY Name ];
            } 
            else {
                //Change below      
                competitors = [ SELECT Parent_Competitor__c, OwnerId, Name, Industry__c FROM Competitor__c WHERE Parent_Competitor__c IN :currentParents ORDER BY Name ];
            }

            if( competitors.size() == 0 )
            {
                noChildren = true;
            }
            else
            {
                currentParents.clear();
                for (Competitor__c competitor : competitors)
                {
                    //Change below
                    HierarchyNode parentNode = idToNode.get(competitor.Parent_Competitor__c);
                    CompetitorHierarchyNode node = new CompetitorHierarchyNode(false, competitor);
                    idToNode.put(competitor.Id, node);
                    currentParents.add( competitor.id );

                    if (parentNode != null)
                        node.setParent(parentNode);
                    else
                        topNode = node;
                }
                
                maxLevel.add( level );
                level++;
            }
        }

        return topNode;
    }
    
    /**
    * Find the tom most element in Heirarchy  
    * @return objId
    */
    public String GetTopElement( String objId ){
        
        Boolean top = false;
        while ( !top ) {
            //Change below
            Competitor__c competitor = [ Select competitor.Id, competitor.Parent_Competitor__c From Competitor__c competitor where competitor.Id =: objId limit 1 ];
            
            if ( competitor.Parent_Competitor__c != null ) {
                objId = competitor.Parent_Competitor__c;
            }
            else {
                top = true;
            }
        }
        return objId ;
    }

    public with sharing class CompetitorHierarchyNode extends HierarchyNode
    {
        public Competitor__c competitor;
        public Competitor__c getCompetitor() { return competitor; }
        public void setCompetitor( Competitor__c competitor ) { this.competitor = competitor; }

        public CompetitorHierarchyNode( Boolean currentNode, Competitor__c competitor)
        {
            super(currentNode);
            this.competitor = competitor;
        }
    }
}