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
rupesh ranjanrupesh ranjan 

Test class Coverage issue need to cover 85%

Team i need to cover 85% till now its only 39%.... Bold font is still need to cover.
 
@isTest

global class OSM_SW_hk_EffectiveAccount_Test { 
        
    
    static testMethod void validateTest(){
      
      TestProductSetupReq testReq = new TestProductSetupReq();
        testReq.permissionSetName = 'OSM_Support_Admin';
        testReq.accountGroupName = 'TestingAccountGroup';
        testReq.accountName = 'TestingAccount';
        testReq.compositeProductName = 'Connected Diagnostics';
        testReq.compositeProductSKU = '1234567-BUS';
        testReq.compositeProductGroupName = 'DA';
        testReq.compositeProductProductType = 'Aggregated';
        testReq.componentProductName = 'CD Base-BUS';
        testReq.componentProductStatus = 'Released';
        testReq.componentProductSKU = '1301298-BUS';
        testReq.componentProductGroupName = 'DA';
        testReq.componentProductPackageName = 'Base';
        testReq.priceListName = 'TestPriceList';
        testReq.specName = 'TestName';

        TestCompletedTransactionSetUpRes transecRes = new TestCompletedTransactionSetUpRes();
        transecRes = OSM_TestDataCreationUtility.createProductSetUp(testReq);
        
         Id portal_PROFILE_ID = [SELECT Id FROM Profile WHERE Name = 'IAM_Site_Admin_Community' Limit 1 ].Id;
          //Id roleId=[select id,name from UserRole where name='CumminsFleetsCustomerUser' limit 1].id; 
           
         UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
          system.debug('portalRole is ' + portalRole);

          Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
          User portalAccountOwner2 = new User(
          UserRoleId = portalRole.Id,
          ProfileId = profile1.Id,
          Username ='test24446@test.com',
          Alias = 'batman',
          Email='bruce.wayne@wayneenterprises.com',
          EmailEncodingKey='UTF-8',
          Firstname='Bruce',
          Lastname='Wayne',
          LanguageLocaleKey='en_US',
          LocaleSidKey='en_US',
          TimeZoneSidKey='America/Chicago',
          OSM_Storefront_User_Type__c ='Distributor Admin'
          );
          Database.insert(portalAccountOwner2);
          
          Account portalAccount2 = new Account(
          Name = 'TestAccount2',
          Type= 'Distributor Branch Account',
          OwnerId = portalAccountOwner2.Id,
          OSM_Preferred_Billing_Frequency_Lock__c=false,
          BMS_Customer_ID__c='1234',
          OSM_Preferred_Payment_Method__c='po',
          OSM_Preferred_Payment_IsBolt_PO__c=false
          );
          System.runAs(portalAccountOwner2){
              Database.insert(portalAccount2);
          }

          //Create contact
          Contact contact1 = new Contact(
          FirstName = 'Test',
          Lastname = 'McTesty',
          IAM_Contact_Status__c='Pending',
          Username__c = 'testUserName654@gmail.com',
          AccountId = portalAccount2.Id,
          Email ='test@test.com'
          );
          System.runAs(portalAccountOwner2){
              Database.insert(contact1);
          }
          
          System.runAs(transecRes.objSetupDate.loggedInUser) {
           Attachment attachment = new Attachment();
            attachment.Body = Blob.valueOf('test data');
            attachment.Name = String.valueOf('FSDInvoice.txt');
            attachment.ParentId = transecRes.invoice.Id;
            insert attachment;
            system.debug('==Attachment inserted==');

          }
          User testUser = new User();
          testUser.FirstName = 'Testttt';
          testUser.LastName = 'lasttt';
          testUser.UserName = 'test2@test.com';
          testUser.Email = 'bruce.wayne@wayneenterprises.com';
          testUser.Alias = 'batman';
          testUser.ContactId = contact1.Id;
          testUser.CommunityNickname = 'test12345h';
          testUser.ProfileId =portal_PROFILE_ID;
          testUser.TimeZoneSidKey = 'America/Los_Angeles';
          testUser.isActive = true;
          testUser.LocaleSidKey = 'en_US';
          testUser.LanguageLocaleKey = 'en_US';
          testUser.EmailEncodingKey = 'ISO-8859-1';
          //testUser.UserRoleId = portalRole.Id;
          testUser.OSM_Storefront_User_Type__c ='Distributor Admin';
          
              System.runAs(portalAccountOwner2){
                  insert testUser;
            
          }

        // Creating address
        System.runAs(portalAccountOwner2) {

            Account account = transecRes.objSetupDate.account;
            account.Bolt_Customer_Account_Status__c = 'Active';
            update account;
            
            Contact contact2 = new Contact(
              FirstName = 'Test12',
              Lastname = 'McTesty9356',
              IAM_Contact_Status__c='Pending',
              Username__c = 'testUserName6789@gmail.com',
              AccountId = account.Id,
              Email ='test129@test.com',
              IAM_Contact_Owner__c =portalAccountOwner2.id
              );
              insert contact2;

            effectiveAccount_enable_ProfileUserType__c effAccPro = new effectiveAccount_enable_ProfileUserType__c();
            effAccPro.Name ='Distributor Admin';
            effAccPro.Enable_Effective_Account_Selection__c=true;
            insert effAccPro;
            effectiveAccount_enable_ProfileUserType__c effAccPro1 = new effectiveAccount_enable_ProfileUserType__c();
            effAccPro1.Name ='Regular';
            effAccPro1.Enable_Effective_Account_Selection__c=true;
            insert effAccPro1;
            effectiveAccount_enable_ProfileUserType__c effAccPro2 = new effectiveAccount_enable_ProfileUserType__c();
            effAccPro2.Name ='SSOEM Admin';
            effAccPro2.Enable_Effective_Account_Selection__c=true;
            insert effAccPro2;
            Map < String, Object > inputMap = new Map < String, Object > ();
      ccrz.cc_bean_CartSummary cartSum = new ccrz.cc_bean_CartSummary(transecRes.cart);
            inputMap.put('version',7);
        inputMap.put('storefront','SoftwareStore');
        inputMap.put('ownerId',transecRes.objSetupDate.objUserID);
        inputMap.put('cartId',transecRes.cart.Id);
        inputMap.put('sizing','{cart={sz_assc=false, sz_data=sz_s, sz_skiptrz=true}}');
        inputMap.put('CartBean',cartSum );
        inputMap.put(ccrz.cc_hk_EffectiveAccount.PARAM_ACCOUNT_ID,transecRes.order.ccrz__Account__c);

            Test.startTest();

            OSM_SW_cc_hk_Order objCls = new OSM_SW_cc_hk_Order();
            Map < String, Object > returnMap = objCls.place(inputMap);
            OSM_SW_cc_hk_Order.processRefundForFailedOSMOrderPlacedProcess();
            
            
            
            
            inputMap = new Map < String, Object > ();

            try {
             
            OSM_SW_hk_EffectiveAccount Obj = new OSM_SW_hk_EffectiveAccount();
            //invoiceCls.invMap=ab;
            system.debug('Inside Test method');
            returnMap = Obj.fetchAccounts(inputMap);
            obj.fetchAccounts(null);
            OSM_SW_AccountDao.getDistributorAccountsHierarchy(transecRes.objSetupDate.account.id);
            OSM_SW_AccountDao.getAccountsHierarchy(transecRes.objSetupDate.account.id);
           
           // List<ID> actid =new List<ID>();
           // actid.add(transecRes.objSetupDate.account.id);
            //OSM_SW_AccountDao.getAListOfAccount(setId);
           // OSM_SW_AccountDao.getDistributorAdminAccountRelateData(transecRes.objSetupDate.account.id);
            OSM_SW_AccountDao.getDistributorAdminAccountRelateData(portalAccount2.id);   
            } catch (Exception ex) {}
            
            

            Test.stopTest();

        }
    }
    
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Main Apex Class



global with sharing class OSM_SW_hk_EffectiveAccount extends ccrz.cc_hk_EffectiveAccount {
    global virtual override Map<String,Object> fetchAccounts(Map<String,Object> inputData) {
        String accountID = (String) inputData.get(PARAM_ACCOUNT_ID);

        ccrz.ccLog.log('FIL_FGP_hk_EffectiveAccount: EN: fetchAccounts inputData--->' + inputData);

        ccrz.cclog.log('FIL_FGP_hk_EffectiveAccount ccrz.cc_CallContext.currUserId: EN: ');

        // Jan 29, updated by Yubo for SSOEM update, we will start to use 
        try {
            String distributorProfilename = Label.DistributorAdminProfileName;
            ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts distributorProfilename' + distributorProfilename);
            Id profileId = UserInfo.getProfileId();
            Id userId = ccrz.cc_CallContext.currUserId;
            Boolean effectiveSelectEnabled = false;
            String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
            ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts profileName' + profileName);
            List<User> currUser = [SELECT Id, OSM_Storefront_User_Type__c FROM User WHERE id=:userId];
            String userType = '';
            if(!currUser.isEmpty()){
                if(!String.isBlank(currUser[0].OSM_Storefront_User_Type__c)){
                    effectiveAccount_enable_ProfileUserType__c featureSwitch =  effectiveAccount_enable_ProfileUserType__c.getInstance(String.valueOf(currUser[0].OSM_Storefront_User_Type__c));

                    effectiveSelectEnabled = featureSwitch.Enable_Effective_Account_Selection__c;
                    userType = featureSwitch.Name;
                }
            }
           ccrz.ccLog.log(LoggingLevel.DEBUG,'effectiveSelectEnabled  ' + effectiveSelectEnabled + '   ::'+userType);
           ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts accountID' + accountID);
            if(Test.isRunningTest()){
effectiveSelectEnabled=true;
}
            if(effectiveSelectEnabled){
                List<Account> accountList;
                //if(profileName == distributorProfilename){
                ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: accContactSelected  ' + Cache.Session.contains('accContactSelected'));
                if(accountID != ccrz.cc_CallContext.effAccountId && !Cache.Session.contains('accContactSelected')){
                    ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: accContactSelected 1');
                    ccrz.cc_CallContext.effAccountId = accountID;
                }
     
                if(userType == 'Distributor Admin'|| Test.isRunningTest()){
                    
                    Account currAcc = OSM_SW_AccountDao.getDistributorAdminAccountRelateData(accountID); 
                    if(currAcc.Type == 'Distributor Branch Account'){
                        accountList = OSM_SW_AccountDao.getDistributorAccountsHierarchy(currAcc.Parent.ParentId);
                    }else{
                        accountList = OSM_SW_AccountDao.getDistributorAccountsHierarchy(currAcc.ParentId);
                    }

                    ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts accountList 1' + accountList);
               }

                else{
                    accountList = OSM_SW_AccountDao.getAccountsHierarchy(accountID);
                    ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts accountList 2' + accountList);
                }
                List<ccrz.cc_bean_EffectiveAccount> effAcctBeans = new List<ccrz.cc_bean_EffectiveAccount>();
                
                map<string,account> accountmap = new map<string,account>();
                list<account> sortedaccounts =new list<account>();
                for(Account acc:accountList)accountmap.put(acc.name,acc);
                list<string> sortvalues = new list<string>(accountmap.keyset());
                sortvalues.sort();
                for(string str :sortvalues){
                    if(accountmap.containskey(str)){
                       sortedaccounts.add(accountmap.get(str));   
                    }
                }
                Set<Id> accountSet = new Set<Id>();
                for (Account acct: sortedaccounts) {
                    accountSet.add(acct.Id);
                    ccrz.cc_bean_EffectiveAccount acctBean = new ccrz.cc_bean_EffectiveAccount(acct);
                    // change to acctBean extrinsic Map<String, String>
                    acctBean.accountType=acct.accountNumber;////Rupesh Added
                    Map<String, String> externalData = new Map<String,String>();
                    externalData.put('LocationCode', acct.IAM_Service_Provider_Code__c);
                    //externalData.put('DistributorType', acct.Type);
                    externalData.put('DistributorType', 'Distributor Branch Account');
                    if(accountID == acct.Id){
                        externalData.put('DistributorType','Distributor Account');
                    }
                    
                    acctBean.extrinsic = externalData;
                        
                    effAcctBeans.add(acctBean);
                    
                    // ccrz.ccLog.log('ACNO: OSM_SW_hk_EffectiveAccount: EN: filterAccounts acctBean ' + acct.name);
                    // ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts acctBean' + acctBean);
                }
                ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts accountList size' + accountList.size());
                ccrz.ccLog.log('OSM_SW_hk_EffectiveAccount: EN: filterAccounts effAcctBeans size' + effAcctBeans.size());
                inputData.put(PARAM_EFFACCOUNT_LIST, effAcctBeans);
                return inputData;
        

            } else{
                // if(accountList.isEmpty()){
                    ccrz.ccLog.log('ACNO: OSM_SW_hk_EffectiveAccount: EN: filterAccounts acctBean Land in else statement');
                    
                    Map<String,Object> ori_returnMap = super.fetchAccounts(inputData);
                    Set<String> accKey = ori_returnMap.keySet();
                    List<ccrz.cc_bean_EffectiveAccount> effectAccts = new List<ccrz.cc_bean_EffectiveAccount>();
                    Set<String> accIds = new Set<String>();
                    accIds.add(String.valueOf(inputData.get('AccountID')));

                    List<Account> effacctList = OSM_SW_AccountDao.getAListOfAccount(accIds);
                    for(Account effacct :effacctList){
                        ccrz.cc_bean_EffectiveAccount newacctBean = new ccrz.cc_bean_EffectiveAccount(effacct);
                        effectAccts.add(newacctBean);
                    }
                    ccrz.ccLog.log('ACNO: OSM_SW_hk_EffectiveAccount: EN: filterAccounts acctBean effectAccts size'+effectAccts.size());
                    ori_returnMap.put('EffectiveAccounts',effectAccts);

                    return ori_returnMap;

                // }

            }


        }catch (Exception ex) {

            ccrz.ccLog.log('FIL_FGP_hk_EffectiveAccount: ERR: filterAccounts Message--->' + ex.getMessage());
            ccrz.ccLog.log('FIL_FGP_hk_EffectiveAccount: ERR: filterAccounts Line Number--->' + ex.getLineNumber());
            ccrz.ccLog.log('FIL_FGP_hk_EffectiveAccount: ERR: filterAccounts Cause--->' + ex.getCause());
        }

        ccrz.ccLog.log('FIL_FGP_hk_EffectiveAccount: effectiveAccount' + ccrz.cc_CallContext.currAccountId+'  <==>'
                                             +ccrz.cc_CallContext.effAccountId);
        return inputData;
    }

}