+ Start a Discussion
Sudha#aSudha#a 

any one help me write test class

global without sharing class EbestCreateUserAndRoleBatch implements Schedulable,Database.Batchable<sObject>
{
    global final String querySQL;
    global List<UserRole> lst_roleMap = [Select id, name from UserRole];
    global List<ebMobile__PickListMaster__c> lst_plm=[SELECT ebMobile__PicklistCode__c,ebMobile__PicklistValue__c FROM ebMobile__PickListMaster__c WHERE ebMobile__ObjectName__c = 'UserMaster' AND ebMobile__FieldName__c = 'BusinessUnit__c' AND ebMobile__IsActive__c = True];
    global List<ebMobile__PickListMaster__c> lst_region=[SELECT ebMobile__PicklistCode__c,ebMobile__PicklistValue__c FROM ebMobile__PickListMaster__c WHERE ebMobile__ObjectName__c = 'UserMaster' AND ebMobile__FieldName__c = 'Region__c' AND ebMobile__IsActive__c = True];
    global List<UserSettings__c> sfaSetting = UserSettings__c.getall().values();
    global Map<String,Id> roleNameMap = new Map<String,Id>();
    global Map<String, String> businessunit = new Map<String, String>();
  global Map<String, String> region = new Map<String, String>();
    
    global EbestCreateUserAndRoleBatch()
    {
        querySQL = 'SELECT Id,ebMobile__IsActive__c,BusinessUnit__c,Region__c,ebMobile__ParentUserCode__c, ebMobile__IsDirect__c, ebMobile__IsUser__c,ebMobile__ParentRoleName__c,  ebMobile__LastName__c,ebMobile__RoleApiName__c, ebMobile__Unique__c, CountryCode__c, ebMobile__UserCode__c, Name,ebMobile__PJPLocking__c FROM ebMobile__UserMapping__c where LastModifiedDate >= Last_N_DAYS:1 ' ;//
        for(UserRole userole : lst_roleMap)
        {
            roleNameMap.put((userole.Name).touppercase(),userole.Id);
        }
        for(ebMobile__PickListMaster__c pickmast : lst_plm)
        {
            businessunit.put(pickmast.ebMobile__PicklistCode__c,(pickmast.ebMobile__PicklistValue__c).touppercase());
        }
    for(ebMobile__PickListMaster__c pick : lst_region)
        {
            region.put(pick.ebMobile__PicklistCode__c,(pick.ebMobile__PicklistValue__c).touppercase());
        }
    }
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator(querySQL);
    }

    global void execute(Database.BatchableContext BC, List<ebMobile__UserMapping__c> userMappingList)
    {
        try
        {
            if(userMappingList.size()>0)
            {
                dealUser(userMappingList);
            }
        }
        catch(Exception ex){}
    }
    
    global void dealUser(List<ebMobile__UserMapping__c> userMappingList)
    {
        List<User> upsertUserList = new List<User>();
        List<String> userCodeList = new List<String>();
        for(ebMobile__UserMapping__c um : userMappingList)
        {
            if(um.ebMobile__IsUser__c)
            {
                system.debug('usercode12:'+um.ebMobile__UserCode__c);
                userCodeList.add(um.ebMobile__UserCode__c);
                User u = new User();
                u.IsActive=um.ebMobile__IsActive__c;
                u.FirstName=um.ebMobile__FirstName__c.toLowerCase();
                //u.CommunityNickname=um.ebMobile__UserCode__c.toLowerCase();
                u.Alias = um.ebMobile__UserCode__c;
                //u.Email= sfaSetting[0].UserEmail__c;
                u.LastName = um.ebMobile__LastName__c;
                u.EmailEncodingKey = sfaSetting[0].EmailEncodingKey__c;
                u.LanguageLocaleKey = sfaSetting[0].LanguageLocaleKey__c;
                //u.ProfileId =  sfaSetting[0].ProfileId__c;
                if(um.CountryCode__c=='AE')
                {
                    u.TimeZoneSidKey = 'Asia/Dubai';
                    u.LocaleSidKey = 'ar_AE';
                    u.UserName = (um.ebMobile__UserCode__c + '@coca-cola.co.ae.full').toLowerCase();
                    u.Email= (um.ebMobile__UserCode__c + '@coca-cola.co.ae.full').toLowerCase();
                }
                else if (um.CountryCode__c=='OM')
                {
                    u.TimeZoneSidKey = 'Asia/Dubai';
                    u.LocaleSidKey = 'ar_OM';
                    u.UserName = (um.ebMobile__UserCode__c + '@coca-cola.co.om.full').toLowerCase();
                    u.Email= (um.ebMobile__UserCode__c + '@coca-cola.co.om.full').toLowerCase();
                }
                else if (um.CountryCode__c=='BH')
                {
                    u.TimeZoneSidKey = 'Asia/Riyadh';
                    u.LocaleSidKey = 'ar_BH';
                    u.UserName = (um.ebMobile__UserCode__c + '@coca-cola.co.bh.full').toLowerCase();
                    u.Email= (um.ebMobile__UserCode__c + '@coca-cola.co.bh.full').toLowerCase();
                }
                else if (um.CountryCode__c=='QA')
                {
                    u.TimeZoneSidKey = 'Asia/Riyadh';
                    u.LocaleSidKey = 'ar_QA';
                    u.UserName = (um.ebMobile__UserCode__c + '@coca-cola.co.qa.full').toLowerCase();
                    u.Email= (um.ebMobile__UserCode__c + '@coca-cola.co.qa.full').toLowerCase();
                }
                
                else
                {
                    u.TimeZoneSidKey = 'Asia/Dubai';
                    u.LocaleSidKey = 'en_US';
                    u.UserName = (um.ebMobile__UserCode__c + '@coca-cola.co.ae.full').toLowerCase();
                    u.Email= (um.ebMobile__UserCode__c + '@coca-cola.co.ae.full').toLowerCase();
                }
                u.ebMobile__UserCode__c = um.ebMobile__UserCode__c ;
                u.ebMobile__IsDirect__c = um.ebMobile__IsDirect__c;
                u.ebMobile__PjpLocking__c = um.ebMobile__PJPLocking__c;
                u.CommunityNickname =  um.ebMobile__UserCode__c +  sfaSetting[0].UserEmail__c ;
                u.ParentUC__c = um.ebMobile__ParentUserCode__c;
                u.ebMobile__IsSupervisor__c = false;
                u.ebMobile__CountryCode__c=um.countrycode__c  ;
                u.ebMobile__Region__c=region.get(um.Region__c);
                u.BusinessUnit__c=businessunit.get(um.BusinessUnit__c);
                if(sfaSetting[0].LicenseHaveRole__c)
                {
                    /*Boolean isHave = false;
                    String roleId = '';
                    for(String roleName : roleNameMap.keySet())
                    {
                        if(um.ebMobile__RoleApiName__c.contains(roleName))
                        {
                            isHave = true;
                            roleId  = roleNameMap.get(roleName);
                        }
                    }
                    if(isHave == true)
                    {
                        u.UserRoleId = roleId;
                        upsertUserList.add(u);
                    }
                    else
                    {
                        u.UserRoleId = sfaSetting[0].DefaultParentRoleId__c ;
                        upsertUserList.add(u);
                    }*/
         
                    if(um.ebMobile__RoleApiName__c.contains('REPRESENTATIVE') && um.Region__c!='')
                    {
                        String bu=region.get(um.Region__c);
                        String role='REPRESENTATIVE_'+bu;
                        String rid=roleNameMap.get(role);
                        u.UserRoleId=rid;
                        u.ProfileId =  sfaSetting[0].ProfileId__c;
                        upsertUserList.add(u);
                    }
                    else if(um.ebMobile__RoleApiName__c.contains('SUPERVISOR') && um.Region__c!='')
                    {
                        String bu=region.get(um.Region__c);
                        String role='SUPERVISOR_'+bu;
                        String rid=roleNameMap.get(role);
                        u.UserRoleId=rid;
                        u.ProfileId =  sfaSetting[0].ProfileId__c;
                        upsertUserList.add(u);
                    }
                    else
                    {
                        String bu=region.get(um.Region__c);
                        String role='MANAGER_'+bu;
                        String rid=roleNameMap.get(role);
                        u.UserRoleId=rid;
                        u.ProfileId =  '00e5D000000QJodQAG';
                        upsertUserList.add(u);
                    }
                }
                else
                {
                    upsertUserList.add(u);
                }
            }
        }
      
       system.debug('upsertUserList******'+upsertUserList);
       List<Database.UpsertResult> result = Database.upsert(upsertUserList, User.ebMobile__UserCode__c  , false);
    }

     //Schedulable exceute
    global void execute(SchedulableContext ctx)
    {
        EbestCreateUserAndRoleBatch batch = new EbestCreateUserAndRoleBatch();
        Database.executeBatch(batch, 10);
    }

    global void finish(Database.BatchableContext BC)
    {
        /*if(!Test.isRunningTest())
        {
            EbestUpdateUserManagerBatch batch = new EbestUpdateUserManagerBatch();
            Database.executeBatch(batch, 1);  
        }*/
    }
}