Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions src/test/java/com/SleepUp/SU/utils/email/EmailServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,28 @@ class EmailServiceImplTest {

private MimeMessage mimeMessage;

private User guest;

private Reservation reservation;

private Context context;

@BeforeEach
void setUp() {
mimeMessage = mock(MimeMessage.class);
lenient().when(mailSender.createMimeMessage()).thenReturn(mimeMessage);

guest = new User();
guest.setEmail("guest@example.com");

reservation = new Reservation();
reservation.setUser(guest);

context = new Context();
}

@Test
void testSendWelcomeEmail_Success() throws Exception {
void sendWelcomeEmail_userCanReceiveEmails_shouldSendEmail() throws Exception {
User user = new User();
user.setEmail("test@example.com");
when(emailHelper.canSendEmails(user)).thenReturn(true);
Expand All @@ -56,7 +70,7 @@ void testSendWelcomeEmail_Success() throws Exception {
}

@Test
void testSendWelcomeEmail_FailureOnCanSend() {
void sendWelcomeEmail_userCannotReceiveEmails_shouldNotSendEmail() {
User user = new User();
user.setEmail("fail@example.com");
when(emailHelper.canSendEmails(user)).thenReturn(false);
Expand All @@ -65,7 +79,7 @@ void testSendWelcomeEmail_FailureOnCanSend() {
}

@Test
void testSendOwnerReservedNotification_Success() throws Exception {
void sendOwnerReservedNotification_reservationValid_shouldSendNotification() throws Exception {
User owner = new User();
owner.setEmail("owner@example.com");
User guest = new User();
Expand All @@ -85,7 +99,7 @@ void testSendOwnerReservedNotification_Success() throws Exception {
}

@Test
void testSendGuestReservationConfirmationEmail_Success() throws Exception {
void sendGuestReservationConfirmationEmail_reservationValid_shouldSendConfirmation() throws Exception {
User guest = new User();
guest.setEmail("guest@example.com");
Reservation reservation = new Reservation();
Expand All @@ -99,7 +113,7 @@ void testSendGuestReservationConfirmationEmail_Success() throws Exception {
}

@Test
void testSendGuestReservationReminderEmail_Success() throws Exception {
void sendCancellationByGuestNotificationEmail_reservationValid_shouldSendNotification() throws Exception {
User guest = new User();
guest.setEmail("guest@example.com");
Reservation reservation = new Reservation();
Expand All @@ -113,7 +127,7 @@ void testSendGuestReservationReminderEmail_Success() throws Exception {
}

@Test
void testSendOwnerReservationReminderEmail_Success() throws Exception {
void sendOwnerReservationReminderEmail_reservationValid_shouldSendNotification() throws Exception {
User owner = new User();
owner.setEmail("owner@example.com");
User guest = new User();
Expand All @@ -132,7 +146,7 @@ void testSendOwnerReservationReminderEmail_Success() throws Exception {
}

@Test
void testSendCancellationConfirmationEmail_Success() throws Exception {
void sendCancellationConfirmationEmail_reservationValid_shouldSendEmail() throws Exception {
User user = new User();
user.setEmail("user@example.com");
Reservation reservation = new Reservation();
Expand All @@ -146,7 +160,7 @@ void testSendCancellationConfirmationEmail_Success() throws Exception {
}

@Test
void testSendCancellationByOwnerNotificationEmail_Success() throws Exception {
void sendCancellationByOwnerNotificationEmail_reservationValid_shouldSendNotification() throws Exception {
User guest = new User();
guest.setEmail("guest@example.com");
Reservation reservation = new Reservation();
Expand All @@ -165,7 +179,7 @@ void testSendCancellationByOwnerNotificationEmail_Success() throws Exception {
}

@Test
void testSendCancellationNotificationToOwnerEmail_Success() throws Exception {
void sendCancellationNotificationToOwnerEmail_reservationValid_shouldSendNotification() throws Exception {
User owner = new User();
owner.setEmail("owner@example.com");
Reservation reservation = new Reservation();
Expand All @@ -182,7 +196,51 @@ void testSendCancellationNotificationToOwnerEmail_Success() throws Exception {
}

@Test
void testSendWelcomeEmail_LogsMailSendException() throws Exception {
void sendCancellationByOwnerNotificationEmail_whenMessagingException_shouldLogError() throws Exception {
when(emailHelper.canSendReservationEmails(reservation)).thenReturn(true);
when(emailHelper.createFullContext(reservation, guest, null)).thenReturn(context);
when(templateEngine.process(eq("guest_cancellationByOwner"), any(Context.class))).thenReturn("<html>Email content</html>");

doThrow(new MailSendException("SMTP Connection failed"))
.when(mailSender).send(any(MimeMessage.class));

emailService.sendCancellationByOwnerNotificationEmail(reservation);

verify(emailHelper).canSendReservationEmails(reservation);
verify(emailHelper).createFullContext(reservation, guest, null);
verify(mailSender).send(any(MimeMessage.class));
}

@Test
void sendCancellationByOwnerNotificationEmail_whenIllegalArgumentException_shouldCatchAndLog() throws Exception {
when(emailHelper.canSendReservationEmails(reservation)).thenReturn(true);
when(emailHelper.createFullContext(reservation, guest, null)).thenReturn(context);

doThrow(new IllegalArgumentException("Invalid argument"))
.when(mailSender).send(any(MimeMessage.class));

when(templateEngine.process(eq("guest_cancellationByOwner"), any(Context.class))).thenReturn("<html>Email content</html>");

emailService.sendCancellationByOwnerNotificationEmail(reservation);

verify(mailSender).send(any(MimeMessage.class));
}

@Test
void sendCancellationByOwnerNotificationEmail_whenTemplateEngineThrowsException_shouldCatchAndLog() throws Exception {
when(emailHelper.canSendReservationEmails(reservation)).thenReturn(true);
when(emailHelper.createFullContext(reservation, guest, null)).thenReturn(context);
when(templateEngine.process(anyString(), any(Context.class)))
.thenThrow(new RuntimeException("Template not found"));

emailService.sendCancellationByOwnerNotificationEmail(reservation);

verify(templateEngine).process(anyString(), any(Context.class));
verify(mailSender, never()).send(any(MimeMessage.class));
}

@Test
void sendWelcomeEmail_mailSendingFails_shouldNotThrowException() throws Exception {
User user = new User();
user.setEmail("test@example.com");
when(emailHelper.canSendEmails(user)).thenReturn(true);
Expand All @@ -196,5 +254,4 @@ void testSendWelcomeEmail_LogsMailSendException() throws Exception {
verify(mailSender).send(any(MimeMessage.class));
}


}
}
Loading