Spock Web Console

subscribe to the feed Subscribe
Schedule Date Caluclation (via #spockwebconsole)
tweet this script Tweet

Schedule Date Caluclation

Published 4 months ago by Abdul Vali with tags MVR pull schedule date
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
class DriverMvrScheduleDataObject {
      Long driver_id;
      String schedule_type;
      Integer day_of_month;
      Integer month;
      Date one_time_date;
}

public class DriverMvrSchedule {

    public Calendar getNextPullDay(DriverMvrScheduleDataObject driverMvrSchedule, Calendar inDate)  {

        
        Calendar nextPullDay = Calendar.getInstance();       
 
        nextPullDay.set(Calendar.DAY_OF_MONTH,1);
        nextPullDay.set(Calendar.MONTH,Calendar.JANUARY);
        nextPullDay.set(Calendar.YEAR,1900);   

        
        switch(driverMvrSchedule.getSchedule_type()) {
            case "MONTHLY":
                nextPullDay = inDate;
                if (driverMvrSchedule.getDay_of_month() >= inDate.get(Calendar.DAY_OF_MONTH)) {
                    if (driverMvrSchedule.getDay_of_month().equals(inDate.getActualMaximum(Calendar.DAY_OF_MONTH)))
                        nextPullDay.set(Calendar.DAY_OF_MONTH, inDate.getActualMaximum(Calendar.DAY_OF_MONTH));
                    else
                        nextPullDay.set(Calendar.DAY_OF_MONTH, driverMvrSchedule.getDay_of_month());
                } else {
                    nextPullDay.set(Calendar.DAY_OF_MONTH, driverMvrSchedule.getDay_of_month());
                    nextPullDay.set(Calendar.MONTH, inDate.get(Calendar.MONTH) + 1);
                }
                break;

        }

        return  nextPullDay;
    }
}

class TestMvrScheduleClass extends Specification {

    def "test MvrScheduleClass for Day_of_month=1 on the first day of a month"() {
        given:
        
        def   driverMvrScheduleObject = new DriverMvrScheduleDataObject();
        driverMvrScheduleObject.driver_id= 111111
        driverMvrScheduleObject.schedule_type ="MONTHLY"
        driverMvrScheduleObject.day_of_month =1

        def   setDate = new GregorianCalendar();
        setDate.set(Calendar.DAY_OF_MONTH,17);
        setDate.set(Calendar.MONTH,Calendar.JULY);
        setDate.set(Calendar.YEAR,2017);        
          

        when:
        DriverMvrSchedule driverMvrScheduleClass = new DriverMvrSchedule();
        def   nextPullDate = driverMvrScheduleClass.getNextPullDay(driverMvrScheduleObject,setDate);

        then:
           nextPullDate.time.format("yyyy-MM-dd")==setDate.time.format("yyyy-MM-dd")    
     }

def "test MvrScheduleClass for Invalid or Null schedule type"() {
        given:
        
        def   driverMvrScheduleObject = new DriverMvrScheduleDataObject();
        driverMvrScheduleObject.driver_id= 111111
        driverMvrScheduleObject.schedule_type =null
        driverMvrScheduleObject.day_of_month =1

        def   setDate = new GregorianCalendar();
        setDate.set(Calendar.DAY_OF_MONTH,1);
        setDate.set(Calendar.MONTH,Calendar.JANUARY);
        setDate.set(Calendar.YEAR,2017);
        
        Calendar minCal = Calendar.getInstance();       
 
        minCal.set(Calendar.DAY_OF_MONTH,1);
        minCal.set(Calendar.MONTH,Calendar.JANUARY);
        minCal.set(Calendar.YEAR,1900);   

        when:
        DriverMvrSchedule driverMvrScheduleClass = new DriverMvrSchedule();
        def Calendar nextPullDate = driverMvrScheduleClass.getNextPullDay(driverMvrScheduleObject,setDate);

        then:
           nextPullDate.time.format("yyyy-MM-dd")==minCal.time.format("yyyy-MM-dd")    
     }
}