• rajasekhar reddy 161
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi Could anyone improve my test coverage please.

Class:

trigger S2S_Subcontractor_Client on SCC_Client__c (after insert, after update, before update, before insert) {
//CH01.End
    string userName = UserInfo.getName();
    String orgName =  UserInfo.getOrganizationName();
    
    if((Trigger.isDelete || Trigger.isUpdate || Trigger.isInsert) && Trigger.isBefore){
        if(userName !='Connection User'){
        //CH01.Start
            for (SCC_Client__c accreditation: Trigger.new) { 
                if(accreditation.Owning_Region__c != All_ConnectionHelpler.owningRegion && !All_ConnectionHelpler.justCreatedRecord) {
                    if(Trigger.isDelete)
                        accreditation.addError(All_ConnectionHelpler.deletePermissionError(accreditation.Owning_Region__c));
                    if(Trigger.isInsert)
                        accreditation.addError(All_ConnectionHelpler.createPermissionError(accreditation.Owning_Region__c));
                    if(Trigger.isUpdate){
                        if(accreditation.SCC__c != Trigger.oldmap.get(accreditation.id).SCC__c)
                            accreditation.SCC__c.addError('Not Editable');

                        else
                            accreditation.addError(All_ConnectionHelpler.updatePermissionError(accreditation.Owning_Region__c));
                    }
                }
            }
            //CH01.End
        }
    }
      
    if((trigger.isUpdate) && Trigger.isAfter){      
        // Define connection id 
        Id networkIdOne = All_ConnectionHelpler.getConnectionId(All_ConnectionHelpler.connectionOne);
        Id networkIdTwo = All_ConnectionHelpler.getConnectionId(All_ConnectionHelpler.connectionTwo);  
        
        List<PartnerNetworkRecordConnection> Connections =  new  List<PartnerNetworkRecordConnection>();
        //CH01.Start
        for (SCC_Client__c subcontractorClient: Trigger.new) { 
            if(subcontractorClient.ConnectionReceivedId != null)
                continue;           
            PartnerNetworkRecordConnection newConnection = 
              new PartnerNetworkRecordConnection( 
                  ConnectionId = networkIdOne, 
                  LocalRecordId = subcontractorClient.Id, 
                  SendClosedTasks = false, 
                  SendOpenTasks = false, 
                  SendEmails = false);
                              
            Connections.add(newConnection); 
        } 
        //CH01.End
        //CH01.start
        for (SCC_Client__c subcontractorClient: Trigger.new) { 
            if(subcontractorClient.ConnectionReceivedId != null)
                continue;           
            PartnerNetworkRecordConnection newConnection = 
              new PartnerNetworkRecordConnection( 
                  ConnectionId = networkIdTwo, 
                  LocalRecordId = subcontractorClient.Id, 
                  SendClosedTasks = false, 
                  SendOpenTasks = false, 
                  SendEmails = false); 
                  
            Connections.add(newConnection); 
        }
        //CH01.End
        if (Connections.size() > 0 ) { 
           database.insert(Connections); 
        }
      //////////////////////////////////  
    }  
        
    //CH01.Start    
    if(Trigger.isBefore && Trigger.isUpdate){
        for (SCC_Client__c subcontractorClient: Trigger.new) {
            //system.debug(subcontractorClient.CreatedById.name);
            //String createdBy = 
            if(!subcontractorClient.CreatedBy__c.startswith('partnernetwork@'))
                subcontractorClient.name = subcontractorClient.h_contract_id__c;
            else
                subcontractorClient.name = subcontractorClient.h_org_id__c;
        }
    }
    //CH01.End
    if((trigger.isUpdate || trigger.isInsert) && Trigger.isBefore){  
        Set<Id> receivedZTESTIds = new Set<Id>(); 
        //CH01.Start
        List<SCC_Client__c> accreditations = new List<SCC_Client__c>(); 
        //CH01.End
        
        // only share records created in this org, do not add ZTEST_Childs received from another org. 
        //CH01.Start
        for (SCC_Client__c subcontractorClient: TRIGGER.new) { 
            if ((subcontractorClient.ConnectionReceivedId != null || userName=='Connection User') ) {/*&& subcontractorClient.h_source_lookup_id__c!= Trigger.oldMap.get(subcontractorClient.id).h_source_lookup_id__c*/ 
                accreditations.add(subcontractorClient);
                receivedZTESTIds.add(subcontractorClient.h_source_lookup_id__c);
            }         
        }
        //CH01.End 
        
        PartnerNetworkRecordConnection[] sCCharingRecords = [SELECT Status, LocalRecordId, ConnectionId ,PartnerRecordId
                                       FROM PartnerNetworkRecordConnection               
                                       WHERE PartnerRecordId IN:receivedZTESTIds];
        //CH01.Start                                       
        for (SCC_Client__c subcontractorClient: accreditations) {
            
            // for each partner connection record for ZTEST_Child's ZTEST, check if it is active
            if(subcontractorClient.h_source_lookup_id__c!=null && subcontractorClient.h_source_lookup_id__c.startswith(SCC__c.sObjectType.getDescribe().getKeyPrefix()))
            {
                subcontractorClient.SCC__c =  subcontractorClient.h_source_lookup_id__c;
                continue;
            } 
            for (PartnerNetworkRecordConnection sCCSharingRecord:sCCharingRecords){
                system.debug('PartnerRecordId'+sCCSharingRecord.PartnerRecordId+'   ');
                system.debug('h_source_lookup_id__c'+subcontractorClient.h_source_lookup_id__c+'   ');
                if ((sCCSharingRecord.status.equalsignorecase('Sent') || sCCSharingRecord.status.equalsignorecase('Received')) 
                    && (/*sCCSharingRecord.ConnectionId == subcontractorClient.ConnectionReceivedId ||*/ userName=='Connection User') && sCCSharingRecord.PartnerRecordId == (Id)subcontractorClient.h_source_lookup_id__c)  
                {
                    //if(Trigger.isInsert)
                    if(sCCSharingRecord.LocalRecordId!=null){
                        subcontractorClient.SCC__c =  sCCSharingRecord.LocalRecordId;
                        break;

                    }
                } 
            }
        }
        //CH01.End
    }
    if((trigger.isUpdate || trigger.isInsert) && Trigger.isBefore){  
        Set<Id> receivedZTESTIds = new Set<Id>(); 
        //CH01.Start
        List<SCC_Client__c> accreditations = new List<SCC_Client__c>(); 
        //CH01.End
        
        // only share records created in this org, do not add ZTEST_Childs received from another org. 
        //CH01.Start
        for (SCC_Client__c accreditation: TRIGGER.new) { 
            if ((accreditation.ConnectionReceivedId != null || userName=='Connection User') ) {/*&& accreditation.h_source_lookup_id__c!= Trigger.oldMap.get(accreditation.id).h_source_lookup_id__c*/ 
                accreditations.add(accreditation);
                receivedZTESTIds.add(accreditation.h_source_lookup_id1__c);
            }         
        } 
        //CH01.End
        
        PartnerNetworkRecordConnection[] sCCharingRecords = [SELECT Status, LocalRecordId, ConnectionId ,PartnerRecordId
                                       FROM PartnerNetworkRecordConnection               
                                       WHERE PartnerRecordId IN:receivedZTESTIds];
             //CH01.Start                                  
        for (SCC_Client__c accreditation: accreditations) {
            system.debug('test'+accreditation.h_source_lookup_id1__c);
            system.debug('Test'+SCC_Clients__c.sObjectType.getDescribe());
            // for each partner connection record for ZTEST_Child's ZTEST, check if it is active
            if(accreditation.h_source_lookup_id1__c!=null && accreditation.h_source_lookup_id1__c.startswith(SCC_Clients__c.sObjectType.getDescribe().getKeyPrefix()))
            {
                accreditation.Client_Names__c =  accreditation.h_source_lookup_id1__c;
                continue;

            } 
            for (PartnerNetworkRecordConnection sCCSharingRecord:sCCharingRecords){
                system.debug('PartnerRecordId'+sCCSharingRecord.PartnerRecordId+'   ');
                system.debug('h_source_lookup_id1__c'+accreditation.h_source_lookup_id1__c+'   ');
                if ((sCCSharingRecord.status.equalsignorecase('Sent') || sCCSharingRecord.status.equalsignorecase('Received')) 
                    && (/*sCCSharingRecord.ConnectionId == accreditation.ConnectionReceivedId ||*/ userName=='Connection User') && sCCSharingRecord.PartnerRecordId == (Id)accreditation.h_source_lookup_id1__c)  
                {
                    //if(Trigger.isInsert)
                    if(sCCSharingRecord.LocalRecordId!=null){
                        accreditation.Client_Names__c =  sCCSharingRecord.LocalRecordId;
                        break;

                    }
                } 
            }
            //CH01.End
        }
    }
    if(Trigger.isAfter && Trigger.isInsert){
    //CH01.Start
        SCC_Client__c[] scc = new List<SCC_Client__c>();
        for (SCC_Client__c subcontractorClient: Trigger.new) {
            scc.add(new SCC_Client__c(id = subcontractorClient.id));
        }
        update scc;
        //CH01.End
    }
}

Test class: 

@isTest
public with sharing class Testclassforsubcontractor { 

    public static testMethod void test1(){
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        
        //User ux = [select id from User where  name = 'Connection User']; 
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection',LastName='User', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) {
            system.debug('****************************'+UserInfo.getName());
            //System.assertEquals(u.username, null);
            
            string owningRegion = 'South';
        
            SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
            insert sccc;
        
        SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
        insert sccAcc;
        
        
        SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
        insert scc;
        
        Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
        insert acc; 
        //CH02.Start
        SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
        insert sc;
        //CH02.End
        
        //CH02.Start
        SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
        insert sa;
        //CH02.End

        //CH02.Start
        HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
        insert sccC34;
        //CH02.End
        
        SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
        insert pr;
        
        SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
        insert seh;
            
            Blob picture = Blob.valueOf('');
            
            Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
            Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
            
            insert lstAt;
            insert lstNote;
            test.stoptest();    
            
            Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
            Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
            
            
           try{
                insert lstAt1;
                insert lstNote1;
            }catch(Exception e){}
            
            
            
               
            All_ConnectionHelpler.owningRegion = 'North';
            All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
            All_ConnectionHelpler.createPermissionError('North');
            All_ConnectionHelpler.updatePermissionError('North');
            All_ConnectionHelpler.deletePermissionError('North');
            
            
            
            All_ConnectionHelpler.createPersonel('a#@#a');
            All_ConnectionHelpler.createPersonel('a#@#a');
            
            try{
                update sccc;
            }catch(Exception e){}
            try{
                update sccAcc;
            }catch(Exception e){}
            try{
                update scc;
            }catch(Exception e){}
            try{
                update acc;
            }catch(Exception e){}
            try{
                update sc;
            }catch(Exception e){}
             try{
                 update sccC34;
            }catch(Exception e){}
            try{
                update pr;
            }catch(Exception e){}
            try{
                update seh;
            }catch(Exception e){}
           try{
                 update lstAt;
            }catch(Exception e){}
        }
    }// CH-01 START
    
   }