+ Start a Discussion
SFDC Apex DevSFDC Apex Dev 

anyone please help me to write test class for the below apex class..

public class NewClass {

    public static ServicePower.spLogin fetchLoginInfo() {
        ServicePower.spLogin servicePowerLogin = new ServicePower.spLogin();
        String orgId = UserInfo.getOrganizationId().subString(0,15);
        ServicePower_Credentials__c creds = ServicePower_Credentials__c.getValues(orgId);
        String spUser = creds.SpUser__c;
        String dbUser = creds.DBUser__c;
        String dbPassWord = creds.DBPassword__c;
        
        servicePowerLogin.databaseUser = dbUser;
        servicePowerLogin.password = dbPassWord;
        servicePowerLogin.spUser = spUser;
        return servicePowerLogin;
    }        

    public static ServicePower.TimeRequest mapJobChangeFinishTimeRequest(String woName, String finishDateTime) {
        ServicePower.TimeRequest request= new ServicePower.TimeRequest();
        request.login = fetchLoginInfo();
        request.jobId = woName;
        request.finishtime = finishDateTime;
        return request;
    }
        
    public static void SPowerUpdateEndTime(List<S_Order__c> woList){
         Map<String, String> woNameTimeMap = new Map<String, String>();
         List<String> woNameList = new List<String>();
         Datetime woFinishTime;
         for(S_Order__c wo : woList)
            { 
                if (wo.Update_Duration__c != null && wo.Add_Subtract_Minutes__c != null )
                    {
                        woFinishTime = wo.S__Scheduled_Date_Time__c.addSeconds(Integer.valueOf(wo.S__Serv_Duration__c));
                        System.debug('order Current Finish Time: ' + woFinishTime);
                        DateTime newDateTime;
                        List<String> woString;
                        if (wo.Update_Duration__c == 'Add')
                            newDateTime = wo.Scheduled_End__c.addMinutes((Integer)wo.Add_Subtract_Minutes__c);
                        else
                            newDateTime = wo.Scheduled_End__c.addMinutes((Integer)wo.Add_Subtract_Minutes__c * -1);
                        woNameList.add(wo.Name);
                        woNameTimeMap.put(wo.Name, newDateTime.formatgmt('yyyy-MM-dd\'T\'HH:mm'));
                        wo.S__Opti_Status__c = 'OPT_PENDING';
                     }
            }
            
            if (woNameTimeMap.size() > 0)
                updateEndTime(woNameTimeMap, woNameList); 
                
    }
    
    @future(callout=true)
    public static void updateEndTime(Map<String, String> nameTimeMap, List<String> woNameList){
        try{    
            for (String woName: woNameList){
               
                String newDateTime = nameTimeMap.get(woName);
                ServicePower.TimeRequest request = mapJobChangeFinishTimeRequest(woName, newDateTime);
                System.debug('This is the request');
                System.debug(request);
                S_ServicePower_Service2.sp objSP = new S_ServicePower_Service2.sp();
                ServicePower.StandardResponse response= objSP.ChangeJobFinishTime(request);
                System.debug('This is the response ' + response);
                ServicePower.ReturnCode returnCode = response.result;
                S_Order__c wo = [select S__Opti_Status__c, S__Opti_Error__c, Update_Duration__c,  Add_Subtract_Minutes__c  from S_Order__c where name =: woName];
                
                if(S_SP_ErrorCodesMapper.isSuccess(returnCode.code)){
                    wo.S__Opti_Status__c = 'OPT_COMPLETED';
                }else{
                    wo.S__Opti_Status__c = 'OPT_ERROR';
                }
                
                String errorText = 'Return Code:' + returnCode.code + ' | Return Type:' + returnCode.type_x + ' | Return Note:' + returnCode.errNote + ' | Return Msg:' + S_SP_ErrorCodesMapper.getStatusMsg(returnCode.code);
                SM.WSCH_CommonUtils.setOptimizerErrorText(wo, errorText);
                
                wo.Update_Duration__c = null;
                wo.Add_Subtract_Minutes__c = null;
                update wo;
            }
        }catch(Exception e){
           system.debug('Exception occured in updateEndTime() ' + e.getMessage());
        }
    }
}
Raj VakatiRaj Vakati
Try this 
 
@isTest
public class NewClassTest{  

    public static testmethod void firstTest(){
		
		 Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduasdasdasdser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarasdasdasdduser@testorg.com');
			insert u ;
			
			
			
		ServicePower_Credentials__c creds = new ServicePower_Credentials__c() ;
		 creds.SpUser__c = 'standarasdasdasdduser@testorg.com';
        creds.DBUser__c='standarasdasdasdduser@testorg.com';
        creds.DBPassword__c ='asdasdasd';
		insert creds ;
        
		
		S_Order__c o = new S_Order__c() ;
		o.Name = 'Test';
		o.Update_Duration__c =213123 ; 
		o.Add_Subtract_Minutes__c =12 ;
		o.S__Scheduled_Date_Time__c =System.today() ;
		
		insert o ;
		
       
	     NewClass  cls = new NewClass ();
		 NewClass.fetchLoginInfo();
		 NewClass.mapJobChangeFinishTimeRequest('woName', String.value( Datetime.newInstance(2013, 5, 28, 1, 6, 8)));
		 NewClass.SPowerUpdateEndTime(new List<S_Order__c>{o});
		Map<String,String> tm = new Map<String,String>() ;
		//put value in tm mapJobChangeFinishTimeRequest
		 List<String> lstTs = new List<String>() ; 
		 //add values to lstTs 
	   NewClass.updateEndTime(tm , lstTs);
    }  

}

 
SFDC Apex DevSFDC Apex Dev

Hi Raj,

I tried but it's not working at all.. even getting error  on line 33..can't call string to date... 

it is failing and code coverage is 0%

Thank you