You need to sign in to do that
Don't have an account?
Apex developer 21
How to approach test class / unitest for following example
Can anyone help me where and how to start with writing a testclass for the following example?
public with sharing class AccountTree{ public String currentId; public List<ObjectStructureMap> asm ; public Map<String, ObjectStructureMap> masm; public List<Integer> maxLevel; public AccountTree() { this.asm = new List<ObjectStructureMap>{}; this.masm = new Map<String, ObjectStructureMap>{}; this.maxLevel = new List<Integer>{}; } public void setcurrentId( String cid ){ currentId = cid; } public List<ObjectStructureMap> getObjectStructure(){ asm.clear(); if ( currentId == null ) { currentId = System.currentPageReference().getParameters().get( 'id' ); } System.assertNotEquals( currentId, null, 'sObject ID must be provided' ); asm = formatObjectStructure( CurrentId ); return asm; } public ObjectStructureMap[] formatObjectStructure( String currentId ){ List<ObjectStructureMap> asm = new List<ObjectStructureMap>{}; masm.clear(); List<Account> al = new List<Account>{}; List<ID> currentParent = new List<ID>{}; Map<ID, String> nodeList = new Map<ID, String>{}; List<String> nodeSortList = new List<String>{}; List<Boolean> levelFlag = new List<Boolean>{}; List<Boolean> closeFlag = new List<Boolean>{}; String nodeId = '0'; String nodeType = 'child'; Integer count = 0; Integer level = 0; Boolean endOfStructure = false; currentParent.add( GetTopElement( currentId ) ); while ( !endOfStructure ){ if( level == 0 ){ //Change below al = [ SELECT a.Type, a.Site, a.ParentId, a.OwnerId, a.Name, a.Industry, a.Id FROM Account a WHERE a.id IN : CurrentParent ORDER BY a.Name ]; } else { //Change below al = [ SELECT a.Type, a.Site, a.ParentId, a.OwnerId, a.Name, a.Industry, a.Id FROM Account a WHERE a.ParentID IN : CurrentParent ORDER BY a.Name ]; } if( al.size() == 0 ){ endOfStructure = true; } else{ currentParent.clear(); for ( Integer i = 0 ; i < al.size(); i++ ){ //Change below Account a = al[i]; nodeId = ( level > 0 ) ? NodeList.get( a.ParentId )+'.'+String.valueOf( i ) : String.valueOf( i ); masm.put( NodeID, new ObjectStructureMap( nodeID, levelFlag, closeFlag, nodeType, false, false, a ) ); currentParent.add( a.id ); nodeList.put( a.id,nodeId ); nodeSortList.add( nodeId ); } maxLevel.add( level ); level++; } } NodeSortList.sort(); for( Integer i = 0; i < NodeSortList.size(); i++ ){ List<String> pnl = new List<String> {}; List<String> cnl = new List<String> {}; List<String> nnl = new List<String> {}; if ( i > 0 ){ String pn = NodeSortList[i-1]; pnl = pn.split( '\\.', -1 ); } String cn = NodeSortList[i]; cnl = cn.split( '\\.', -1 ); if( i < NodeSortList.size()-1 ){ String nn = NodeSortList[i+1]; nnl = nn.split( '\\.', -1 ); } ObjectStructureMap tasm = masm.get( cn ); if ( cnl.size() < nnl.size() ){ //Parent tasm.nodeType = ( isLastNode( cnl ) ) ? 'parent_end' : 'parent'; } else if( cnl.size() > nnl.size() ){ tasm.nodeType = 'child_end'; tasm.closeFlag = setcloseFlag( cnl, nnl, tasm.nodeType ); } else{ tasm.nodeType = 'child'; } tasm.levelFlag = setlevelFlag( cnl, tasm.nodeType ); //Change below if ( tasm.account.id == currentId ) { tasm.currentNode = true; } asm.add( tasm ); } asm[0].nodeType = 'start'; asm[asm.size()-1].nodeType = 'end'; return asm; } public List<Boolean> setlevelFlag( List<String> nodeElements, String nodeType ){ List<Boolean> flagList = new List<Boolean>{}; String searchNode = ''; String workNode = ''; Integer cn = 0; for( Integer i = 0; i < nodeElements.size() - 1; i++ ){ cn = Integer.valueOf( nodeElements[i] ); cn++; searchNode = workNode + String.valueOf( cn ); workNode = workNode + nodeElements[i] + '.'; if ( masm.containsKey( searchNode ) ){ flagList.add( true ); } else { flagList.add( false ); } } return flagList; } public List<Boolean> setcloseFlag( List<String> cnl, List<String> nnl, String nodeType ){ List<Boolean> flagList = new List<Boolean>{}; String searchNode = ''; String workNode = ''; Integer cn = 0; for( Integer i = nnl.size(); i < cnl.size(); i++ ){ flagList.add( true ); } return flagList; } public Boolean isLastNode( List<String> nodeElements ){ String searchNode = ''; Integer cn = 0; for( Integer i = 0; i < nodeElements.size(); i++ ){ if ( i == nodeElements.size()-1 ){ cn = Integer.valueOf( nodeElements[i] ); cn++; searchNode = searchNode + String.valueOf( cn ); } else { searchNode = searchNode + nodeElements[i] + '.'; } } if ( masm.containsKey( searchNode ) ){ return false; } else{ return true; } } public String GetTopElement( String objId ){ Boolean top = false; while ( !top ) { //Change below Account a = [ Select a.Id, a.ParentId From Account a where a.Id =: objId limit 1 ]; if ( a.ParentID != null ) { objId = a.ParentID; } else { top = true; } } return objId ; } public with sharing class ObjectStructureMap{ public String nodeId; public Boolean[] levelFlag = new Boolean[]{}; public Boolean[] closeFlag = new Boolean[]{}; public String nodeType; public Boolean currentNode; public Account account; public String getnodeId() { return nodeId; } public Boolean[] getlevelFlag() { return levelFlag; } public Boolean[] getcloseFlag() { return closeFlag; } public String getnodeType() { return nodeType; } public Boolean getcurrentNode() { return currentNode; } public Account getaccount() { return account; } public void setnodeId( String n ) { this.nodeId = n; } public void setlevelFlag( Boolean l ) { this.levelFlag.add(l); } public void setlcloseFlag( Boolean l ) { this.closeFlag.add(l); } public void setnodeType( String nt ) { this.nodeType = nt; } public void setcurrentNode( Boolean cn ) { this.currentNode = cn; } public void setaccount( Account a ) { this.account = a; } public ObjectStructureMap( String nodeId, Boolean[] levelFlag,Boolean[] closeFlag , String nodeType, Boolean lastNode, Boolean currentNode, Account a ){ this.nodeId = nodeId; this.levelFlag = levelFlag; this.closeFlag = closeFlag; this.nodeType = nodeType; this.currentNode = currentNode; this.account = a; } } }