package org.mifos.schedule;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.joda.time.DateTime;
import org.junit.Test;
import org.mifos.calendar.CalendarUtils;
import org.mifos.calendar.DayOfWeek;
import org.mifos.domain.builders.ScheduledEventBuilder;

public class MonthlyOnWeekAndWeekDayScheduledEventTest {

    private ScheduledEvent scheduledEvent;

    @Test
    public void canRollForwardDateToMatchWeekOfMonthAndDayOfWeekWhenStartingInSameWeekButOneDayBehind() {

        scheduledEvent = new ScheduledEventBuilder().every(1).months().onWeekOfMonth(1).on(DayOfWeek.tuesday()).build();

        DateTime firstOfNextMonth = new DateTime().plusMonths(1).withDayOfMonth(1).toDateMidnight().toDateTime();
        DateTime firstMondayOfNextMonth = CalendarUtils.nearestDayOfWeekTo(DayOfWeek.monday(), firstOfNextMonth);

        DateTime result = scheduledEvent.nearestMatchingDateBeginningAt(firstMondayOfNextMonth);

        assertThat(result, is(firstMondayOfNextMonth.plusDays(1)));
    }

    @Test
    public void canRollForwardDateToMatchWeekOfMonthAndDayOfWeekWhenStartingOneWeekAndDayBehind() {

        scheduledEvent = new ScheduledEventBuilder().every(1).months().onWeekOfMonth(2).on(DayOfWeek.tuesday()).build();

        DateTime firstOfNextMonth = new DateTime().plusMonths(1).withDayOfMonth(1).toDateMidnight().toDateTime();
        DateTime firstMondayOfNextMonth = CalendarUtils.nearestDayOfWeekTo(DayOfWeek.monday(), firstOfNextMonth);

        DateTime result = scheduledEvent.nearestMatchingDateBeginningAt(firstMondayOfNextMonth);

        assertThat(result, is(firstMondayOfNextMonth.plusDays(1).plusWeeks(1)));
    }

    @Test
    public void canRollForwardDateToMatchWeekOfMonthAndDayOfWeekWhenStartingOneWeekAndDayAhead() {

        scheduledEvent = new ScheduledEventBuilder().every(1).months().onWeekOfMonth(1).on(DayOfWeek.tuesday()).build();

        DateTime wednesday7thOfApril = new DateTime().withDayOfMonth(7).withMonthOfYear(4).withYear(2010).withDayOfWeek(DayOfWeek.wednesday())
                .toDateMidnight().toDateTime();

        DateTime result = scheduledEvent.nearestMatchingDateBeginningAt(wednesday7thOfApril);

        assertThat(result, is(tuesdayOnFirstWeekOneMonthFrom(wednesday7thOfApril)));
    }

    private DateTime tuesdayOnFirstWeekOneMonthFrom(final DateTime from) {
        return from.plusMonths(1).withDayOfMonth(7).withDayOfWeek(DayOfWeek.tuesday());
    }
}
@@ -175,50 +175,8 @@
 t;%0D%0A
-import org.mifos.calendar.CalendarUtils;%0D%0A
 impo
@@ -636,35 +636,39 @@
 teTime first
-OfNextMonth
+MondayOfMay2010
  = new DateT
@@ -669,36 +669,56 @@
  DateTime().
-plusMonths(1
+withYear(2010).withMonthOfYear(5
 ).withDayOfM
@@ -714,33 +714,33 @@
 .withDayOfMonth(
-1
+3
 ).toDateMidnight
@@ -760,123 +760,8 @@
 ();%0D
-%0A        DateTime firstMondayOfNextMonth = CalendarUtils.nearestDayOfWeekTo(DayOfWeek.monday(), firstOfNextMonth);%0D
 %0A%0D%0A 
@@ -840,33 +840,31 @@
 irstMondayOf
-NextMonth
+May2010
 );%0D%0A%0D%0A      
@@ -896,33 +896,31 @@
 irstMondayOf
-NextMonth
+May2010
 .plusDays(1)
@@ -1193,27 +1193,31 @@
 me first
-OfNextMonth
+MondayOfMay2010
  = new D
@@ -1226,28 +1226,48 @@
 eTime().
-plusMonths(1
+withYear(2010).withMonthOfYear(5
 ).withDa
@@ -1275,17 +1275,17 @@
 OfMonth(
-1
+3
 ).toDate
@@ -1313,123 +1313,8 @@
 ();%0D
-%0A        DateTime firstMondayOfNextMonth = CalendarUtils.nearestDayOfWeekTo(DayOfWeek.monday(), firstOfNextMonth);%0D
 %0A%0D%0A 
@@ -1397,25 +1397,23 @@
 MondayOf
-NextMonth
+May2010
 );%0D%0A%0D%0A  
@@ -1457,17 +1457,15 @@
 ayOf
-NextMonth
+May2010
 .plu

fix failing tests due to date problems