Skip to content

Commit 794f5e7

Browse files
committed
CSCEXAM-000 Make calendar tests more robust
- Used to fail when run close to midnight
1 parent e056438 commit 794f5e7

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

test/controllers/iop/ExternalCalendarInterfaceTest.java

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,16 @@ public void testLoginAsTemporalStudentVisitor() throws Exception {
537537
String eppn = "[email protected]";
538538
assertThat(user).isNull();
539539

540+
// Create reservation within the same day to avoid midnight boundary issues
541+
DateTime[] times = createSafeTimes();
542+
DateTime startTime = times[0];
543+
DateTime endTime = times[1];
544+
540545
Reservation reservation = new Reservation();
541546
reservation.setExternalUserRef(eppn);
542547
reservation.setExternalRef(RESERVATION_REF);
543-
reservation.setStartAt(DateTime.now().plusHours(2));
544-
reservation.setEndAt(DateTime.now().plusHours(3));
548+
reservation.setStartAt(startTime);
549+
reservation.setEndAt(endTime);
545550
reservation.setMachine(room.getExamMachines().get(0));
546551
reservation.save();
547552

@@ -587,11 +592,17 @@ public void testLoginAsTemporalStudentVisitorWrongMachine() throws Exception {
587592
ExamMachine machine = room.getExamMachines().get(0);
588593
machine.setIpAddress("128.2.2.2");
589594
machine.update();
595+
596+
// Create reservation within the same day to avoid midnight boundary issues
597+
DateTime[] times = createSafeTimes();
598+
DateTime startTime = times[0];
599+
DateTime endTime = times[1];
600+
590601
Reservation reservation = new Reservation();
591602
reservation.setExternalUserRef(eppn);
592603
reservation.setExternalRef(RESERVATION_REF);
593-
reservation.setStartAt(DateTime.now().plusHours(2));
594-
reservation.setEndAt(DateTime.now().plusHours(3));
604+
reservation.setStartAt(startTime);
605+
reservation.setEndAt(endTime);
595606
reservation.setMachine(room.getExamMachines().get(0));
596607
reservation.save();
597608

@@ -791,11 +802,16 @@ public void testRequestReservationRemovalAfterRemoteLogin() throws Exception {
791802
String eppn = "[email protected]";
792803
assertThat(user).isNull();
793804

805+
// Create reservation within the same day to avoid midnight boundary issues
806+
DateTime[] times = createSafeTimes();
807+
DateTime startTime = times[0];
808+
DateTime endTime = times[1];
809+
794810
Reservation reservation = new Reservation();
795811
reservation.setExternalUserRef(eppn);
796812
reservation.setExternalRef(RESERVATION_REF);
797-
reservation.setStartAt(DateTime.now().plusHours(2));
798-
reservation.setEndAt(DateTime.now().plusHours(3));
813+
reservation.setStartAt(startTime);
814+
reservation.setEndAt(endTime);
799815
reservation.setMachine(room.getExamMachines().get(0));
800816
reservation.save();
801817

@@ -813,4 +829,47 @@ public void testRequestReservationRemovalAfterRemoteLogin() throws Exception {
813829
assertThat(DB.find(ExamEnrolment.class, enrolment.getId())).isNull();
814830
assertThat(DB.find(ExternalExam.class, enrolment.getExternalExam().getId())).isNull();
815831
}
832+
833+
/**
834+
* Creates safe start and end times that avoid the midnight boundary issue.
835+
* The lookup logic in getUpcomingExternalReservation looks ahead until midnight,
836+
* so we ensure reservations are scheduled before then and always in the future.
837+
*/
838+
private DateTime[] createSafeTimes() {
839+
DateTime now = DateTime.now();
840+
DateTime startTime = now.plusMinutes(30); // Start in 30 minutes
841+
DateTime endTime = now.plusMinutes(90); // End in 90 minutes
842+
843+
// If this would cross midnight, move it to a safe time today
844+
DateTime midnight = now.plusDays(1).withMillisOfDay(0);
845+
if (startTime.isAfter(midnight) || endTime.isAfter(midnight)) {
846+
// If we're close to midnight, schedule for a safe time earlier today
847+
// But ensure it's still in the future
848+
DateTime safeStart = now.withHourOfDay(10).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
849+
DateTime safeEnd = safeStart.plusHours(1);
850+
851+
// If 10 AM is in the past, use the original future times but cap at 23:30
852+
if (safeEnd.isBefore(now)) {
853+
// Use original times but ensure they don't cross midnight
854+
startTime = now.plusMinutes(30);
855+
endTime = now.plusMinutes(90);
856+
857+
// If still crossing midnight, cap at 23:30
858+
DateTime latestStart = now
859+
.withHourOfDay(23)
860+
.withMinuteOfHour(30)
861+
.withSecondOfMinute(0)
862+
.withMillisOfSecond(0);
863+
if (startTime.isAfter(latestStart)) {
864+
startTime = latestStart;
865+
endTime = startTime.plusMinutes(30); // Short reservation to stay before midnight
866+
}
867+
} else {
868+
startTime = safeStart;
869+
endTime = safeEnd;
870+
}
871+
}
872+
873+
return new DateTime[] { startTime, endTime };
874+
}
816875
}

0 commit comments

Comments
 (0)