use of uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO in project isaac-api by isaacphysics.
the class EventBookingManagerTest method requestReservations_reserveSpacesForMoreThanAllowed_throwsEventGroupReservationLimitException.
@Test
public void requestReservations_reserveSpacesForMoreThanAllowed_throwsEventGroupReservationLimitException() throws Exception {
EventBookingManager eventBookingManager = buildEventBookingManager();
ReservationTestDefaults testCase = new ReservationTestDefaults();
// 2 reservation limit, 1 previously reserved, try to reserve another 2
// big enough not to matter
testCase.event.setNumberOfPlaces(999);
testCase.event.setGroupReservationLimit(2);
List<RegisteredUserDTO> studentsToReserve = ImmutableList.of(testCase.student1, testCase.student2);
RegisteredUserDTO previouslyReservedStudent = testCase.student3;
Map<BookingStatus, Map<Role, Long>> previousBookingCounts = generatePlacesAvailableMap();
previousBookingCounts.put(BookingStatus.CONFIRMED, ImmutableMap.of(Role.STUDENT, 1L));
DetailedEventBookingDTO existingEventBooking = new DetailedEventBookingDTO() {
{
setEventId(testCase.event.getId());
setBookingStatus(BookingStatus.CONFIRMED);
setReservedById(testCase.teacher.getId());
setUserBooked(new UserSummaryDTO() {
{
setId(previouslyReservedStudent.getId());
}
});
}
};
// Define expected external calls
expect(dummyTransactionManager.getTransaction()).andReturn(dummyTransaction).once();
dummyEventBookingPersistenceManager.lockEventUntilTransactionComplete(dummyTransaction, testCase.event.getId());
expectLastCall().atLeastOnce();
// No commit expected; exception thrown!
dummyTransaction.close();
expectLastCall().once();
expect(dummyEventBookingPersistenceManager.getEventBookingStatusCounts(testCase.event.getId(), false)).andReturn(previousBookingCounts).once();
expect(dummyEventBookingPersistenceManager.getBookingsByEventId(testCase.event.getId())).andReturn(ImmutableList.of(existingEventBooking));
replay(mockedObjects);
try {
eventBookingManager.requestReservations(testCase.event, studentsToReserve, testCase.teacher);
fail("Expected to fail from trying to reserve 2 students onto an event with only one space.");
} catch (EventGroupReservationLimitException e) {
// success
}
verify(mockedObjects);
}
use of uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO in project isaac-api by isaacphysics.
the class EventBookingManagerTest method requestBooking_cancelledSpaceAndSomeWaitingList_Success.
@Test
public void requestBooking_cancelledSpaceAndSomeWaitingList_Success() throws Exception {
EventBookingManager ebm = this.buildEventBookingManager();
IsaacEventPageDTO testEvent = new IsaacEventPageDTO();
testEvent.setId("someEventId");
testEvent.setNumberOfPlaces(2);
testEvent.setTags(ImmutableSet.of("teacher", "physics"));
testEvent.setEmailEventDetails("Some Details");
testEvent.setDate(someFutureDate);
RegisteredUserDTO firstUserFull = new RegisteredUserDTO();
firstUserFull.setId(6L);
firstUserFull.setEmailVerificationStatus(EmailVerificationStatus.VERIFIED);
firstUserFull.setRole(Role.TEACHER);
DetailedEventBookingDTO firstBooking = new DetailedEventBookingDTO();
UserSummaryDTO firstUser = new UserSummaryDTO();
firstUser.setId(firstUserFull.getId());
firstUser.setRole(Role.TEACHER);
firstBooking.setUserBooked(firstUser);
firstBooking.setBookingStatus(BookingStatus.WAITING_LIST);
EventBookingDTO secondBooking = new EventBookingDTO();
UserSummaryDTO secondUser = new UserSummaryDTO();
secondUser.setRole(Role.TEACHER);
secondUser.setId(7L);
secondBooking.setUserBooked(firstUser);
secondBooking.setBookingStatus(BookingStatus.CANCELLED);
List<EventBookingDTO> currentBookings = Arrays.asList(firstBooking, secondBooking);
Map<BookingStatus, Map<Role, Long>> placesAvailableMap = generatePlacesAvailableMap();
placesAvailableMap.get(BookingStatus.CANCELLED).put(Role.TEACHER, 1L);
placesAvailableMap.get(BookingStatus.WAITING_LIST).put(Role.TEACHER, 1L);
expect(dummyEventBookingPersistenceManager.getEventBookingStatusCounts(testEvent.getId(), false)).andReturn(placesAvailableMap).atLeastOnce();
expect(dummyEventBookingPersistenceManager.getBookingByEventIdAndUserId(testEvent.getId(), firstUserFull.getId())).andReturn(firstBooking).once();
dummyEventBookingPersistenceManager.lockEventUntilTransactionComplete(dummyTransaction, testEvent.getId());
expectLastCall().once();
expect(dummyTransactionManager.getTransaction()).andReturn(dummyTransaction).once();
dummyTransaction.commit();
expectLastCall().once();
dummyTransaction.close();
expectLastCall().once();
expect(dummyEventBookingPersistenceManager.createBooking(dummyTransaction, testEvent.getId(), firstUserFull.getId(), BookingStatus.CONFIRMED, someAdditionalInformation)).andReturn(secondBooking).atLeastOnce();
dummyEmailManager.sendTemplatedEmailToUser(anyObject(), anyObject(), anyObject(), anyObject(), anyObject());
expectLastCall().atLeastOnce();
expect(dummyEmailManager.getEmailTemplateDTO("email-event-booking-confirmed")).andReturn(new EmailTemplateDTO()).atLeastOnce();
replay(mockedObjects);
try {
ebm.requestBooking(testEvent, firstUserFull, someAdditionalInformation);
// success
} catch (EventIsFullException e) {
fail("Expected successful booking as no waiting list bookings.");
}
verify(mockedObjects);
}
use of uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO in project isaac-api by isaacphysics.
the class EventBookingManagerTest method requestBooking_userIsAbleToPromoteBookingReservation_Success.
@Test
public void requestBooking_userIsAbleToPromoteBookingReservation_Success() throws Exception {
EventBookingManager eventBookingManager = this.buildEventBookingManager();
ReservationTestDefaults testCase = new ReservationTestDefaults();
testCase.event.setNumberOfPlaces(1);
RegisteredUserDTO reservedStudent = testCase.student1;
DetailedEventBookingDTO reservedStudentBooking = new DetailedEventBookingDTO() {
{
setEventId(testCase.event.getId());
setBookingStatus(BookingStatus.RESERVED);
setUserBooked(new UserSummaryDTO() {
{
setId(reservedStudent.getId());
}
});
}
};
DetailedEventBookingDTO reservedStudentBookingAfterConfirmation = new DetailedEventBookingDTO() {
{
setEventId(testCase.event.getId());
setBookingStatus(BookingStatus.CONFIRMED);
setUserBooked(new UserSummaryDTO() {
{
setId(reservedStudent.getId());
}
});
}
};
// Expected external calls
dummyEventBookingPersistenceManager.lockEventUntilTransactionComplete(dummyTransaction, testCase.event.getId());
expectLastCall().once();
expect(dummyTransactionManager.getTransaction()).andReturn(dummyTransaction).once();
dummyTransaction.commit();
expectLastCall().once();
dummyTransaction.close();
expectLastCall().once();
expect(dummyEventBookingPersistenceManager.getBookingByEventIdAndUserId(testCase.event.getId(), reservedStudent.getId())).andReturn(reservedStudentBooking).once();
// As a reserved booking exists, expect an update to the booking
expect(dummyEventBookingPersistenceManager.updateBookingStatus(eq(dummyTransaction), eq(testCase.event.getId()), eq(reservedStudent.getId()), eq(BookingStatus.CONFIRMED), anyObject())).andReturn(reservedStudentBookingAfterConfirmation).once();
// Send emails
EmailTemplateDTO emailTemplate = new EmailTemplateDTO();
expect(dummyEmailManager.getEmailTemplateDTO("email-event-booking-confirmed")).andReturn(emailTemplate).once();
dummyEmailManager.sendTemplatedEmailToUser(eq(reservedStudent), eq(emailTemplate), anyObject(), eq(EmailType.SYSTEM), anyObject());
expectLastCall().once();
replay(mockedObjects);
eventBookingManager.requestBooking(testCase.event, reservedStudent, someAdditionalInformation);
verify(mockedObjects);
}
use of uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO in project isaac-api by isaacphysics.
the class EventBookingManagerTest method requestReservations_reserveSpacesWhenThereAreAvailableSpaces_success.
@Test
public void requestReservations_reserveSpacesWhenThereAreAvailableSpaces_success() throws Exception {
EventBookingManager eventBookingManager = buildEventBookingManager();
ReservationTestDefaults testCase = new ReservationTestDefaults();
List<RegisteredUserDTO> students = ImmutableList.of(testCase.student1, testCase.student2);
// Make student two have a cancelled booking
DetailedEventBookingDTO student2sCancelledBooking = new DetailedEventBookingDTO() {
{
setBookingStatus(BookingStatus.CANCELLED);
setEventId(testCase.event.getId());
setUserBooked(new UserSummaryDTO() {
{
setId(testCase.student2.getId());
}
});
}
};
// Define expected external calls
dummyEventBookingPersistenceManager.lockEventUntilTransactionComplete(dummyTransaction, testCase.event.getId());
expectLastCall().once();
// Check existing bookings
expect(dummyEventBookingPersistenceManager.getBookingsByEventId(testCase.event.getId())).andReturn(ImmutableList.of(student2sCancelledBooking)).once();
// Make Reservations
expect(dummyTransactionManager.getTransaction()).andReturn(dummyTransaction).once();
expect(dummyEventBookingPersistenceManager.getBookingByEventIdAndUserId(testCase.event.getId(), testCase.student1.getId())).andReturn(null).once();
expect(dummyEventBookingPersistenceManager.createBooking(eq(dummyTransaction), eq(testCase.event.getId()), eq(testCase.student1.getId()), eq(testCase.teacher.getId()), eq(BookingStatus.RESERVED), anyObject())).andReturn(testCase.student1Booking).once();
expect(dummyEventBookingPersistenceManager.getBookingByEventIdAndUserId(testCase.event.getId(), testCase.student2.getId())).andReturn(student2sCancelledBooking).once();
expect(dummyEventBookingPersistenceManager.updateBookingStatus(eq(dummyTransaction), eq(testCase.event.getId()), eq(testCase.student2.getId()), eq(testCase.teacher.getId()), eq(BookingStatus.RESERVED), anyObject())).andReturn(testCase.student2Booking).once();
dummyTransaction.commit();
expectLastCall().once();
dummyTransaction.close();
expectLastCall().once();
// Send Emails
expect(dummyEmailManager.getEmailTemplateDTO(("email-event-reservation-requested"))).andReturn(testCase.reservationEmail).atLeastOnce();
expect(dummyEmailManager.getEmailTemplateDTO(("email-event-reservation-recap"))).andReturn(testCase.reservationEmail).atLeastOnce();
expect(dummyUserAccountManager.getUserDTOById(testCase.student1.getId())).andReturn(testCase.student1).times(2);
dummyEmailManager.sendTemplatedEmailToUser(eq(testCase.student1), eq(testCase.reservationEmail), anyObject(), eq(EmailType.SYSTEM));
expectLastCall().once();
expect(dummyUserAccountManager.getUserDTOById(testCase.student2.getId())).andReturn(testCase.student2).times(2);
dummyEmailManager.sendTemplatedEmailToUser(eq(testCase.student2), eq(testCase.reservationEmail), anyObject(), eq(EmailType.SYSTEM));
expectLastCall().once();
dummyEmailManager.sendTemplatedEmailToUser(eq(testCase.teacher), eq(testCase.reservationEmail), anyObject(), eq(EmailType.SYSTEM));
expectLastCall().once();
// Run the test for a student event
replay(mockedObjects);
List<EventBookingDTO> actualResults = eventBookingManager.requestReservations(testCase.event, students, testCase.teacher);
List<EventBookingDTO> expectedResults = ImmutableList.of(testCase.student1Booking, testCase.student2Booking);
assertEquals("N results should be returned unaltered", expectedResults, actualResults);
verify(mockedObjects);
}
use of uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO in project isaac-api by isaacphysics.
the class EventBookingManagerTest method requestReservations_cancelledReservationsDoNotCountTowardsReservationLimit_success.
@Test
public void requestReservations_cancelledReservationsDoNotCountTowardsReservationLimit_success() throws Exception {
EventBookingManager eventBookingManager = buildEventBookingManager();
ReservationTestDefaults testCase = new ReservationTestDefaults();
// 1 reservation limit, 1 previously reserved but cancelled, try to reserve another 1
// big enough not to matter
testCase.event.setNumberOfPlaces(999);
testCase.event.setGroupReservationLimit(1);
List<RegisteredUserDTO> students = ImmutableList.of(testCase.student1);
// Make student two have a cancelled booking
DetailedEventBookingDTO student2sCancelledReservation = new DetailedEventBookingDTO() {
{
setBookingStatus(BookingStatus.CANCELLED);
setEventId(testCase.event.getId());
setUserBooked(new UserSummaryDTO() {
{
setId(testCase.student2.getId());
}
});
setReservedById(testCase.teacher.getId());
}
};
Map<BookingStatus, Map<Role, Long>> previousBookingCounts = generatePlacesAvailableMap();
previousBookingCounts.put(BookingStatus.CANCELLED, ImmutableMap.of(Role.STUDENT, 1L));
// Define expected external calls
dummyEventBookingPersistenceManager.lockEventUntilTransactionComplete(dummyTransaction, testCase.event.getId());
expectLastCall().once();
// Check existing bookings
expect(dummyEventBookingPersistenceManager.getBookingsByEventId(testCase.event.getId())).andReturn(ImmutableList.of(student2sCancelledReservation)).once();
expect(dummyEventBookingPersistenceManager.getEventBookingStatusCounts(testCase.event.getId(), false)).andReturn(previousBookingCounts).once();
// Make Reservations
expect(dummyTransactionManager.getTransaction()).andReturn(dummyTransaction).once();
expect(dummyEventBookingPersistenceManager.getBookingByEventIdAndUserId(testCase.event.getId(), testCase.student1.getId())).andReturn(null).once();
expect(dummyEventBookingPersistenceManager.createBooking(eq(dummyTransaction), eq(testCase.event.getId()), eq(testCase.student1.getId()), eq(testCase.teacher.getId()), eq(BookingStatus.RESERVED), anyObject())).andReturn(testCase.student1Booking).once();
dummyTransaction.commit();
expectLastCall().once();
dummyTransaction.close();
expectLastCall().once();
// Send Emails
expect(dummyEmailManager.getEmailTemplateDTO(("email-event-reservation-requested"))).andReturn(testCase.reservationEmail).atLeastOnce();
expect(dummyEmailManager.getEmailTemplateDTO(("email-event-reservation-recap"))).andReturn(testCase.reservationEmail).atLeastOnce();
expect(dummyUserAccountManager.getUserDTOById(testCase.student1.getId())).andReturn(testCase.student1).atLeastOnce();
dummyEmailManager.sendTemplatedEmailToUser(eq(testCase.student1), eq(testCase.reservationEmail), anyObject(), eq(EmailType.SYSTEM));
expectLastCall().once();
dummyEmailManager.sendTemplatedEmailToUser(eq(testCase.teacher), eq(testCase.reservationEmail), anyObject(), eq(EmailType.SYSTEM));
expectLastCall().atLeastOnce();
// Run the test for a student event
replay(mockedObjects);
List<EventBookingDTO> actualResults = eventBookingManager.requestReservations(testCase.event, students, testCase.teacher);
List<EventBookingDTO> expectedResults = ImmutableList.of(testCase.student1Booking);
assertEquals("Student 1 should get reserved despite the existing cancelled reservation", expectedResults, actualResults);
verify(mockedObjects);
}
Aggregations