Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@
<version>3.1.0</version> <!-- or latest -->
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ public String setAccommodationUrl(Accommodation accommodation) {
public String getDashboardUrl() {
return DASHBOARD_URL;
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/SleepUp/SU/utils/email/EmailServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.SleepUp.SU.utils.email;

import com.SleepUp.SU.accommodation.entity.Accommodation;
import com.SleepUp.SU.reservation.entity.Reservation;
import com.SleepUp.SU.user.entity.User;
import jakarta.mail.MessagingException;
Expand All @@ -8,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;
Expand All @@ -26,6 +28,7 @@ public class EmailServiceImpl implements EmailService {
private final EmailServiceHelper emailHelper;

@Override
@Async
public void sendWelcomeEmail(User user) {
if (!emailHelper.canSendEmails(user)) return;

Expand All @@ -40,6 +43,7 @@ public void sendWelcomeEmail(User user) {
}

@Override
@Async
public void sendOwnerReservedNotification(Reservation reservation) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -58,6 +62,7 @@ public void sendOwnerReservedNotification(Reservation reservation) {
}

@Override
@Async
public void sendGuestReservationConfirmationEmail(Reservation reservation, BigDecimal discountAmount) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -73,6 +78,7 @@ public void sendGuestReservationConfirmationEmail(Reservation reservation, BigDe
}

@Override
@Async
public void sendGuestReservationReminderEmail(Reservation reservation) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -88,6 +94,7 @@ public void sendGuestReservationReminderEmail(Reservation reservation) {
}

@Override
@Async
public void sendOwnerReservationReminderEmail(Reservation reservation) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -103,6 +110,7 @@ public void sendOwnerReservationReminderEmail(Reservation reservation) {
}

@Override
@Async
public void sendCancellationConfirmationEmail(Reservation reservation) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -118,6 +126,7 @@ public void sendCancellationConfirmationEmail(Reservation reservation) {
}

@Override
@Async
public void sendCancellationByOwnerNotificationEmail(Reservation reservation) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -133,6 +142,7 @@ public void sendCancellationByOwnerNotificationEmail(Reservation reservation) {
}

@Override
@Async
public void sendCancellationNotificationToOwnerEmail(Reservation reservation) {
if (!emailHelper.canSendReservationEmails(reservation)) return;

Expand All @@ -152,6 +162,7 @@ private void sendEmail(String toEmail, String subject, String templateName, Cont
MimeMessageHelper helper = new MimeMessageHelper(message, true, UTF8_ENCODING);

String htmlContent = templateEngine.process(templateName, context);

helper.setTo(toEmail);
helper.setSubject(subject);
helper.setText(htmlContent, true);
Expand Down
21 changes: 14 additions & 7 deletions src/test/java/com/SleepUp/SU/auth/AuthControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.SleepUp.SU.user.entity.CustomUserDetails;
import com.SleepUp.SU.user.entity.User;
import com.SleepUp.SU.user.repository.UserRepository;
import com.SleepUp.SU.utils.email.EmailService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import jakarta.mail.internet.MimeMessage;
Expand All @@ -22,7 +23,8 @@
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

import static org.hamcrest.Matchers.hasLength;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -62,6 +64,12 @@ public class AuthControllerTest {
@MockitoBean
private JavaMailSender mailSender;

@MockitoBean
private SpringTemplateEngine templateEngine;

@MockitoBean
private EmailService emailService;

private CustomUserDetails principal;

@BeforeEach
Expand All @@ -76,11 +84,11 @@ class RegisterTests {

@Test
void register_validRequest_shouldReturnCreatedUser() throws Exception {
JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
MimeMessage mimeMessage = new JavaMailSenderImpl().createMimeMessage();

when(mailSender.createMimeMessage()).thenReturn(mimeMessage);
doNothing().when(mailSender).send(any(MimeMessage.class));
doNothing().when(emailService).sendWelcomeEmail(any(User.class));
when(templateEngine.process(anyString(), any(Context.class))).thenReturn("html");

UserRequest request = new UserRequest("newUser", "New Name", "new@email.com", "password123");

Expand All @@ -92,10 +100,9 @@ void register_validRequest_shouldReturnCreatedUser() throws Exception {
.andExpect(jsonPath("$.email").value("new@email.com"))
.andExpect(jsonPath("$.name").value("New Name"));

verify(mailSender, times(1)).send(any(MimeMessage.class));
verify(emailService, times(1)).sendWelcomeEmail(any(User.class));
}


@Test
void register_invalidRequest_shouldReturnBadRequest() throws Exception {
String invalidJson = """
Expand Down Expand Up @@ -196,4 +203,4 @@ void logout_withAuthentication_shouldReturnMessage() throws Exception {
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@
import com.SleepUp.SU.accommodation.entity.Accommodation;
import com.SleepUp.SU.reservation.entity.Reservation;
import com.SleepUp.SU.user.entity.User;
import com.SleepUp.SU.utils.email.EmailServiceImpl;
import com.SleepUp.SU.utils.email.EmailService;
import jakarta.mail.MessagingException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.context.ActiveProfiles;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;

@SpringBootTest
@ActiveProfiles("test")
@EnableAsync
public class EmailServiceMailHogIntegrationTest {

@Autowired
private EmailServiceImpl emailService;
private EmailService emailService;

@Autowired
private JavaMailSender mailSender;

private User owner;
private User guest;
Expand Down Expand Up @@ -53,48 +62,48 @@ void setUp() {
@Test
public void integration_sendWelcomeEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendWelcomeEmail(guest);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendOwnerReservedNotification_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendOwnerReservedNotification(reservation);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendGuestReservationConfirmationEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendGuestReservationConfirmationEmail(reservation, new BigDecimal("100.00"));
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendGuestReservationReminderEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendGuestReservationReminderEmail(reservation);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendOwnerReservationReminderEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendOwnerReservationReminderEmail(reservation);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendCancellationConfirmationEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendCancellationConfirmationEmail(reservation);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendCancellationByOwnerNotificationEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendCancellationByOwnerNotificationEmail(reservation);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}

@Test
public void integration_sendCancellationNotificationToOwnerEmail_shouldNotThrowException() throws MessagingException, InterruptedException {
emailService.sendCancellationNotificationToOwnerEmail(reservation);
Thread.sleep(1000);
await().atMost(5, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> true);
}
}
}
Loading