feat(back-end front-end): new UX for
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package com.printcalculator.event.listener;
|
||||
|
||||
import com.printcalculator.entity.Customer;
|
||||
import com.printcalculator.entity.Order;
|
||||
import com.printcalculator.event.OrderCreatedEvent;
|
||||
import com.printcalculator.service.email.EmailNotificationService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class OrderEmailListenerTest {
|
||||
|
||||
@Mock
|
||||
private EmailNotificationService emailNotificationService;
|
||||
|
||||
@InjectMocks
|
||||
private OrderEmailListener orderEmailListener;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Map<String, Object>> templateDataCaptor;
|
||||
|
||||
private Order order;
|
||||
private OrderCreatedEvent event;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
Customer customer = new Customer();
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
customer.setEmail("john.doe@test.com");
|
||||
|
||||
order = new Order();
|
||||
order.setId(UUID.randomUUID());
|
||||
order.setCustomer(customer);
|
||||
order.setCreatedAt(OffsetDateTime.parse("2026-02-21T10:00:00Z"));
|
||||
order.setTotalChf(new BigDecimal("150.50"));
|
||||
|
||||
event = new OrderCreatedEvent(this, order);
|
||||
|
||||
ReflectionTestUtils.setField(orderEmailListener, "adminMailEnabled", true);
|
||||
ReflectionTestUtils.setField(orderEmailListener, "adminMailAddress", "admin@printcalculator.local");
|
||||
ReflectionTestUtils.setField(orderEmailListener, "frontendBaseUrl", "https://tuosito.it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleOrderCreatedEvent_ShouldSendCustomerAndAdminEmails() {
|
||||
// Act
|
||||
orderEmailListener.handleOrderCreatedEvent(event);
|
||||
|
||||
// Assert Customer Email
|
||||
verify(emailNotificationService, times(1)).sendEmail(
|
||||
eq("john.doe@test.com"),
|
||||
eq("Conferma Ordine #" + order.getOrderNumber() + " - 3D-Fab"),
|
||||
eq("order-confirmation"),
|
||||
templateDataCaptor.capture()
|
||||
);
|
||||
|
||||
Map<String, Object> customerData = templateDataCaptor.getAllValues().get(0);
|
||||
assertEquals("John", customerData.get("customerName"));
|
||||
assertEquals(order.getId(), customerData.get("orderId"));
|
||||
assertEquals(order.getOrderNumber(), customerData.get("orderNumber"));
|
||||
assertEquals("https://tuosito.it/ordine/" + order.getId(), customerData.get("orderDetailsUrl"));
|
||||
assertEquals(order.getCreatedAt().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")), customerData.get("orderDate"));
|
||||
assertEquals("150.50", customerData.get("totalCost"));
|
||||
|
||||
// Assert Admin Email
|
||||
verify(emailNotificationService, times(1)).sendEmail(
|
||||
eq("admin@printcalculator.local"),
|
||||
eq("Nuovo Ordine Ricevuto #" + order.getOrderNumber() + " - Doe"),
|
||||
eq("order-confirmation"),
|
||||
templateDataCaptor.capture()
|
||||
);
|
||||
|
||||
Map<String, Object> adminData = templateDataCaptor.getAllValues().get(1);
|
||||
assertEquals("John Doe", adminData.get("customerName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleOrderCreatedEvent_WithAdminDisabled_ShouldOnlySendCustomerEmail() {
|
||||
// Arrange
|
||||
ReflectionTestUtils.setField(orderEmailListener, "adminMailEnabled", false);
|
||||
|
||||
// Act
|
||||
orderEmailListener.handleOrderCreatedEvent(event);
|
||||
|
||||
// Assert
|
||||
verify(emailNotificationService, times(1)).sendEmail(
|
||||
eq("john.doe@test.com"),
|
||||
anyString(),
|
||||
anyString(),
|
||||
any()
|
||||
);
|
||||
|
||||
verify(emailNotificationService, never()).sendEmail(
|
||||
eq("admin@printcalculator.local"),
|
||||
anyString(),
|
||||
anyString(),
|
||||
any()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleOrderCreatedEvent_ExceptionHandling_ShouldNotPropagate() {
|
||||
// Arrange
|
||||
doThrow(new RuntimeException("Simulated Mail Failure"))
|
||||
.when(emailNotificationService).sendEmail(anyString(), anyString(), anyString(), any());
|
||||
|
||||
// Act & Assert
|
||||
// Event listener shouldn't throw exception back, thus passing the test.
|
||||
orderEmailListener.handleOrderCreatedEvent(event);
|
||||
|
||||
verify(emailNotificationService, times(1)).sendEmail(anyString(), anyString(), anyString(), any());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.printcalculator.service.email;
|
||||
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.context.Context;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SmtpEmailNotificationServiceTest {
|
||||
|
||||
@Mock
|
||||
private JavaMailSender emailSender;
|
||||
|
||||
@Mock
|
||||
private TemplateEngine templateEngine;
|
||||
|
||||
@Mock
|
||||
private MimeMessage mimeMessage;
|
||||
|
||||
@InjectMocks
|
||||
private SmtpEmailNotificationService emailNotificationService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ReflectionTestUtils.setField(emailNotificationService, "fromAddress", "noreply@test.com");
|
||||
ReflectionTestUtils.setField(emailNotificationService, "mailEnabled", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_Success() {
|
||||
// Arrange
|
||||
String to = "user@test.com";
|
||||
String subject = "Test Subject";
|
||||
String templateName = "test-template";
|
||||
Map<String, Object> contextData = new HashMap<>();
|
||||
contextData.put("key", "value");
|
||||
|
||||
when(templateEngine.process(eq("email/" + templateName), any(Context.class))).thenReturn("<html>Test</html>");
|
||||
when(emailSender.createMimeMessage()).thenReturn(mimeMessage);
|
||||
|
||||
// Act
|
||||
emailNotificationService.sendEmail(to, subject, templateName, contextData);
|
||||
|
||||
// Assert
|
||||
verify(templateEngine, times(1)).process(eq("email/" + templateName), any(Context.class));
|
||||
verify(emailSender, times(1)).createMimeMessage();
|
||||
verify(emailSender, times(1)).send(mimeMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_Exception_ShouldNotThrow() {
|
||||
// Arrange
|
||||
String to = "user@test.com";
|
||||
String subject = "Test Subject";
|
||||
String templateName = "test-template";
|
||||
Map<String, Object> contextData = new HashMap<>();
|
||||
|
||||
when(templateEngine.process(eq("email/" + templateName), any(Context.class))).thenThrow(new RuntimeException("Template error"));
|
||||
|
||||
// Act & Assert
|
||||
// We expect the exception to be caught and logged, not propagated
|
||||
assertDoesNotThrow(() -> emailNotificationService.sendEmail(to, subject, templateName, contextData));
|
||||
|
||||
verify(emailSender, never()).createMimeMessage();
|
||||
verify(emailSender, never()).send(any(MimeMessage.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user