@Test
  public void shouldScheduleSecondMilestoneAlertsUsingLastFulfilTimeAsReferenceWhichIsAfterNow()
      throws IOException, URISyntaxException, SchedulerException {
    addSchedule("/schedulingIT/schedule.json");

    try {
      fakeNow(newDateTime(2050, 5, 18, 10, 0, 0));
      String enrollmentId =
          scheduleTrackingService.enroll(
              new EnrollmentRequest()
                  .setExternalId("abcde")
                  .setScheduleName("schedule")
                  .setPreferredAlertTime(null)
                  .setReferenceDate(newDate(2050, 5, 10))
                  .setReferenceTime(new Time(9, 0))
                  .setEnrollmentDate(newDate(2050, 5, 10))
                  .setEnrollmentTime(new Time(9, 0))
                  .setStartingMilestoneName("milestone1")
                  .setMetadata(null));
      scheduleTrackingService.fulfillCurrentMilestone(
          "abcde", "schedule", newDate(2050, 5, 17), new Time(11, 0));

      List<DateTime> fireTimes =
          getFireTimes(
              format(
                  "org.motechproject.scheduletracking.milestone.alert-%s.1-repeat", enrollmentId));
      assertEquals(
          asList(newDateTime(2050, 5, 18, 11, 0, 0), newDateTime(2050, 5, 19, 11, 0, 0)),
          fireTimes);
    } finally {
      stopFakingTime();
    }
  }
  @Test
  public void shouldScheduleSecondMilestoneAlertsAtLastMilestoneFulfilmentTime()
      throws IOException, URISyntaxException, SchedulerException {
    addSchedule("schedulingIT", "schedule_with_floating_alerts.json");

    try {
      AlertListener alertListener = new AlertListener();
      eventListenerRegistry.registerListener(alertListener, EventSubjects.MILESTONE_ALERT);
      TestScheduleUtil.fakeNow(newDateTime(2050, 5, 22, 11, 0, 0));

      Long enrollmentId =
          scheduleTrackingService.enroll(
              new EnrollmentRequest()
                  .setExternalId("abcde")
                  .setScheduleName("schedule_with_floating_alerts")
                  .setPreferredAlertTime(null)
                  .setReferenceDate(newDate(2050, 5, 10))
                  .setReferenceTime(new Time(9, 0))
                  .setEnrollmentDate(newDate(2050, 5, 10))
                  .setEnrollmentTime(new Time(9, 0))
                  .setStartingMilestoneName("milestone1")
                  .setMetadata(null));
      scheduleTrackingService.fulfillCurrentMilestone(
          "abcde", "schedule_with_floating_alerts", newDate(2050, 5, 21), new Time(10, 0));

      assertEquals(newDateTime(2050, 5, 22, 11, 0, 0), alertListener.getTriggerTime());

      List<DateTime> fireTimes =
          getFireTimes(
              format(
                  "org.motechproject.scheduletracking.milestone.alert-%s.1-repeat", enrollmentId));
      assertEquals(
          asList(newDateTime(2050, 5, 23, 10, 0, 0), newDateTime(2050, 5, 24, 10, 0, 0)),
          fireTimes);
    } finally {
      TestScheduleUtil.stopFakingTime();
      eventListenerRegistry.clearListenersForBean("alertsTestListener");
    }
  }