package org.motechproject.scheduler.builder;

import org.joda.time.Duration;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.motechproject.model.Time;
import org.motechproject.util.DateUtil;
import org.quartz.SimpleTrigger;

import static org.junit.Assert.assertEquals;

public class CronJobSimpleExpressionBuilderTest {
    @Test
    public void testBuild() {
        CronJobSimpleExpressionBuilder builder = new CronJobSimpleExpressionBuilder(new Time(10, 25));
        assertEquals("0 25 10 * * ?", builder.build());
    }

    @Test
    public void shouldBuildValidCronExpressionWithDayRepeat() {
        String expression = new CronJobSimpleExpressionBuilder(new Time(10, 25)).withRepeatIntervalInDays(7).build();
        assertEquals("0 25 10 */7 * ?", expression);
    }

    @Test
    public void shouldScheduleJobEverySevenDays() {
        SimpleTrigger trigger = new SimpleTrigger("triggerName", "groupName", DateUtil.today().toDate(), null, SimpleTrigger.REPEAT_INDEFINITELY, Duration.standardDays(7).getMillis());
        LocalDate today = DateUtil.today();
        LocalDate nextDate = today.plusDays(7);
        LocalDate nextToNextDate = nextDate.plusDays(7);

        assertEquals(nextDate.toDate(), trigger.getFireTimeAfter(today.toDate()));
        assertEquals(nextToNextDate.toDate(), trigger.getFireTimeAfter(nextDate.toDate()));
    }

}
@@ -232,24 +232,48 @@
 leTrigger;%0A%0A
+import java.util.Date;%0A%0A
 import stati
@@ -1117,26 +1117,22 @@
     
-LocalDate nextDate
+Date yesterday
  = t
@@ -1149,20 +1149,31 @@
 ays(
-7
+-1).toDate(
 );%0A
+%0A
         
 Loca
@@ -1172,139 +1172,319 @@
     
-LocalDate nextToNextDate = nextDate.plusDays(7);%0A%0A        assertEquals(nextDate.toDate(), trigger.getFireTimeAfter(today.toDat
+Date firstFireTime = trigger.getFireTimeAfter(yesterday);%0A        Date secondFireTime = trigger.getFireTimeAfter(firstFireTime);%0A        Date thirdFireTime = trigger.getFireTimeAfter(secondFireTime);%0A%0A        assertEquals(Duration.standardDays(7).getMillis(), secondFireTime.getTime() - firstFireTime.getTim
 e())
-)
 ;%0A  
@@ -1506,77 +1506,97 @@
 als(
-nextToNextDate.toDate(), trigger.getFireTimeAfter(nextDate.toDat
+Duration.standardDays(7).getMillis(), thirdFireTime.getTime() - secondFireTime.getTim
 e())
-)
 ;%0A  

Modify the test so it doesn't fail when daylight savings falls in the interval.