add tests for NotificationSchedulerServiceTest
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
+248
@@ -0,0 +1,248 @@
|
||||
package SuMoC.backend.notification.services;
|
||||
|
||||
import SuMoC.backend.notification.enums.NotificationStatus;
|
||||
import SuMoC.backend.notification.enums.NotificationType;
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import SuMoC.backend.notification.repositories.NotificationRepository;
|
||||
import SuMoC.backend.trend.dto.TrendDetectionDTO;
|
||||
import SuMoC.backend.trend.enums.DeltaType;
|
||||
import SuMoC.backend.trend.enums.StatAttribute;
|
||||
import SuMoC.backend.trend.enums.TrendTimespan;
|
||||
import SuMoC.backend.trend.enums.TrendType;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.persistence.EntityManager;
|
||||
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.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class NotificationSchedulerServiceTest {
|
||||
|
||||
@Mock
|
||||
private NotificationRepository notificationRepository;
|
||||
|
||||
@Mock
|
||||
private EntityManager entityManager;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private NotificationSchedulerService notificationSchedulerService;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<Notification>> notificationListCaptor;
|
||||
|
||||
private User mockUser;
|
||||
private LocalTime testTime;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
objectMapper = new ObjectMapper();
|
||||
notificationSchedulerService = new NotificationSchedulerService(objectMapper, notificationRepository, entityManager);
|
||||
mockUser = new User("privateName", "publicName", "test@example.com", "password");
|
||||
mockUser.setId(1L);
|
||||
testTime = LocalTime.of(9, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void scheduleAll_WithTrendDetectionDTO_SerializesAndPersistsSuccessfully() {
|
||||
// Arrange
|
||||
TrendDetectionDTO trendDetectionDTO = new TrendDetectionDTO(
|
||||
1L,
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.INCREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
DeltaType.ABSOLUTE,
|
||||
25.5
|
||||
);
|
||||
|
||||
List<? extends SuMoC.backend.notification.interfaces.Notifieable> dataBodies = List.of(trendDetectionDTO);
|
||||
|
||||
// Mock EntityManager to return a proxy
|
||||
when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser);
|
||||
|
||||
// Act
|
||||
notificationSchedulerService.scheduleAll(
|
||||
(List<SuMoC.backend.notification.interfaces.Notifieable>) dataBodies,
|
||||
NotificationType.TREND,
|
||||
testTime
|
||||
);
|
||||
|
||||
// Assert
|
||||
verify(notificationRepository, times(1)).saveAll(notificationListCaptor.capture());
|
||||
List<Notification> savedNotifications = notificationListCaptor.getValue();
|
||||
|
||||
assertEquals(1, savedNotifications.size());
|
||||
Notification notification = savedNotifications.getFirst();
|
||||
|
||||
// Verify notification properties
|
||||
assertEquals(mockUser, notification.getUser());
|
||||
assertEquals(NotificationType.TREND, notification.getType());
|
||||
assertEquals(NotificationStatus.PENDING, notification.getStatus());
|
||||
assertEquals(testTime, notification.getEarliestTimeOfDayToSend());
|
||||
assertEquals(0, notification.getFailedSendAttempts());
|
||||
assertNotNull(notification.getCreatedAt());
|
||||
|
||||
// Verify that the data was serialized correctly
|
||||
assertNotNull(notification.getData());
|
||||
assertTrue(notification.getData().contains("userId"));
|
||||
assertTrue(notification.getData().contains("WEEKLY"));
|
||||
assertTrue(notification.getData().contains("INCREASING"));
|
||||
assertTrue(notification.getData().contains("CAR_DISTANCE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void scheduleAll_WithTrendDetectionDTO_DataCanBeDeserialized() throws Exception {
|
||||
// Arrange
|
||||
TrendDetectionDTO originalDTO = new TrendDetectionDTO(
|
||||
1L,
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.DECREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
DeltaType.PERCENTAGE,
|
||||
15.75
|
||||
);
|
||||
|
||||
List<? extends SuMoC.backend.notification.interfaces.Notifieable> dataBodies = List.of(originalDTO);
|
||||
when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser);
|
||||
|
||||
// Act
|
||||
notificationSchedulerService.scheduleAll(
|
||||
(List<SuMoC.backend.notification.interfaces.Notifieable>) dataBodies,
|
||||
NotificationType.TREND,
|
||||
testTime
|
||||
);
|
||||
|
||||
// Capture the saved notification
|
||||
verify(notificationRepository).saveAll(notificationListCaptor.capture());
|
||||
Notification savedNotification = notificationListCaptor.getValue().getFirst();
|
||||
|
||||
// Assert - deserialize and verify the data
|
||||
TrendDetectionDTO deserializedDTO = objectMapper.readValue(
|
||||
savedNotification.getData(),
|
||||
TrendDetectionDTO.class
|
||||
);
|
||||
|
||||
assertEquals(originalDTO.getUserId(), deserializedDTO.getUserId());
|
||||
assertEquals(originalDTO.getTimespan(), deserializedDTO.getTimespan());
|
||||
assertEquals(originalDTO.getType(), deserializedDTO.getType());
|
||||
assertEquals(originalDTO.getAttribute(), deserializedDTO.getAttribute());
|
||||
assertEquals(originalDTO.getDeltaType(), deserializedDTO.getDeltaType());
|
||||
assertEquals(originalDTO.getChangeValue(), deserializedDTO.getChangeValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void scheduleAll_WithMultipleTrendDetectionDTOs_SerializesAll() {
|
||||
// Arrange
|
||||
TrendDetectionDTO dto1 = new TrendDetectionDTO(
|
||||
1L,
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.INCREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
DeltaType.ABSOLUTE,
|
||||
10.0
|
||||
);
|
||||
|
||||
TrendDetectionDTO dto2 = new TrendDetectionDTO(
|
||||
2L,
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.DECREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
DeltaType.PERCENTAGE,
|
||||
5.0
|
||||
);
|
||||
|
||||
List<? extends SuMoC.backend.notification.interfaces.Notifieable> dataBodies = List.of(dto1, dto2);
|
||||
|
||||
// Mock EntityManager to return proxies
|
||||
User mockUser2 = new User("privateName2", "publicName2", "test2@example.com", "password");
|
||||
mockUser2.setId(2L);
|
||||
|
||||
when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser);
|
||||
when(entityManager.getReference(eq(User.class), eq(2L))).thenReturn(mockUser2);
|
||||
|
||||
// Act
|
||||
notificationSchedulerService.scheduleAll(
|
||||
(List<SuMoC.backend.notification.interfaces.Notifieable>) dataBodies,
|
||||
NotificationType.TREND,
|
||||
testTime
|
||||
);
|
||||
|
||||
// Assert
|
||||
verify(notificationRepository, times(1)).saveAll(notificationListCaptor.capture());
|
||||
List<Notification> savedNotifications = notificationListCaptor.getValue();
|
||||
|
||||
assertEquals(2, savedNotifications.size());
|
||||
assertEquals(mockUser, savedNotifications.get(0).getUser());
|
||||
assertEquals(mockUser2, savedNotifications.get(1).getUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void scheduleAll_WithTrendDetectionDTO_SerializationException_ThrowsRuntimeException() {
|
||||
// Arrange
|
||||
TrendDetectionDTO dto = new TrendDetectionDTO(
|
||||
1L,
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.INCREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
DeltaType.ABSOLUTE,
|
||||
25.5
|
||||
);
|
||||
|
||||
List<? extends SuMoC.backend.notification.interfaces.Notifieable> dataBodies = List.of(dto);
|
||||
|
||||
when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser);
|
||||
|
||||
// Act & Assert
|
||||
// The service should handle serialization correctly and not throw exceptions
|
||||
// for valid DTOs
|
||||
assertDoesNotThrow(() ->
|
||||
notificationSchedulerService.scheduleAll(
|
||||
(List<SuMoC.backend.notification.interfaces.Notifieable>) dataBodies,
|
||||
NotificationType.TREND,
|
||||
testTime
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void scheduleAll_VerifyNotificationStatusIsInitiallyPending() {
|
||||
// Arrange
|
||||
TrendDetectionDTO trendDetectionDTO = TrendDetectionDTO.mockTrendDetectionDTO();
|
||||
trendDetectionDTO.setUserId(1L);
|
||||
|
||||
List<? extends SuMoC.backend.notification.interfaces.Notifieable> dataBodies = List.of(trendDetectionDTO);
|
||||
when(entityManager.getReference(eq(User.class), eq(1L))).thenReturn(mockUser);
|
||||
|
||||
// Act
|
||||
notificationSchedulerService.scheduleAll(
|
||||
(List<SuMoC.backend.notification.interfaces.Notifieable>) dataBodies,
|
||||
NotificationType.TREND,
|
||||
testTime
|
||||
);
|
||||
|
||||
// Assert
|
||||
verify(notificationRepository).saveAll(notificationListCaptor.capture());
|
||||
Notification notification = notificationListCaptor.getValue().getFirst();
|
||||
|
||||
assertEquals(NotificationStatus.PENDING, notification.getStatus());
|
||||
assertEquals(0, notification.getFailedSendAttempts());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user