This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
package SuMoC.backend.carrousel.service;
|
||||
|
||||
import SuMoC.backend.carrousel.config.CarrouselProperties;
|
||||
import SuMoC.backend.carrousel.dto.AdHocAlternativeContext;
|
||||
import SuMoC.backend.carrousel.dto.AlternativeCarrouselDTO;
|
||||
import SuMoC.backend.carrousel.dto.RegularTripAlternativeContext;
|
||||
import SuMoC.backend.carrousel.enums.CarrouselCandidateType;
|
||||
import SuMoC.backend.carrousel.models.CarrouselCandidate;
|
||||
import SuMoC.backend.carrousel.repository.CarrouselCandidateRepository;
|
||||
import SuMoC.backend.regulartrip.dtos.DetectedRegularTripDTO;
|
||||
import SuMoC.backend.regulartrip.dtos.MinimalDetectedRegularTripDTO;
|
||||
import SuMoC.backend.routing.dtos.RouteDTO;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import SuMoC.backend.trip.dtos.RegularTripDetectionDTO;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import SuMoC.backend.user.models.User;
|
||||
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 java.time.DayOfWeek;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class CarrouselServiceTest {
|
||||
|
||||
@Mock
|
||||
private CarrouselCandidateRepository carrouselCandidateRepository;
|
||||
|
||||
@Mock(lenient = true)
|
||||
private CarrouselProperties carrouselProperties;
|
||||
|
||||
@InjectMocks
|
||||
private CarrouselService carrouselService;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<CarrouselCandidate> candidateCaptor;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
when(carrouselProperties.getMaxCarrouselItems()).thenReturn(5);
|
||||
when(carrouselProperties.getRecentSuggestionWindowDays()).thenReturn(7);
|
||||
}
|
||||
|
||||
// --- addAdHocTripCandidates ---
|
||||
|
||||
@Test
|
||||
void addAdHocTripCandidates_EmptyList_NoSaves() {
|
||||
Trip trip = createTrip(10L, 5L);
|
||||
|
||||
carrouselService.addAdHocTripCandidates(trip, List.of());
|
||||
|
||||
verify(carrouselCandidateRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAdHocTripCandidates_MultipleAlternatives_SavesEachWithCorrectFields() {
|
||||
Trip trip = createTrip(10L, 5L);
|
||||
RouteDTO route1 = RouteDTO.getMockRouteDTO();
|
||||
RouteDTO route2 = RouteDTO.getMockRouteDTO();
|
||||
|
||||
carrouselService.addAdHocTripCandidates(trip, List.of(route1, route2));
|
||||
|
||||
verify(carrouselCandidateRepository, times(2)).save(candidateCaptor.capture());
|
||||
List<CarrouselCandidate> captured = candidateCaptor.getAllValues();
|
||||
|
||||
for (CarrouselCandidate candidate : captured) {
|
||||
assertEquals(5L, candidate.getUserId());
|
||||
assertEquals(10L, candidate.getAdHocTripId());
|
||||
assertEquals(CarrouselCandidateType.ADHOC_ALTERNATIVE, candidate.getCandidateType());
|
||||
assertNotNull(candidate.getSuggestedAt());
|
||||
}
|
||||
assertEquals(route1, captured.get(0).getSuggestedAlternative());
|
||||
assertEquals(route2, captured.get(1).getSuggestedAlternative());
|
||||
}
|
||||
|
||||
// --- getRegularTripCandidates ---
|
||||
|
||||
@Test
|
||||
void getRegularTripCandidates_EmptyList_EmptyResult() {
|
||||
DetectedRegularTripDTO detectedTrip = createDetectedRegularTrip();
|
||||
|
||||
List<CarrouselCandidate> result = carrouselService.getRegularTripCandidates(1L, detectedTrip, List.of());
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRegularTripCandidates_SingleAlternative_CorrectFields() {
|
||||
DetectedRegularTripDTO detectedTrip = createDetectedRegularTrip();
|
||||
RouteDTO route = RouteDTO.getMockRouteDTO();
|
||||
|
||||
List<CarrouselCandidate> result = carrouselService.getRegularTripCandidates(1L, detectedTrip, List.of(route));
|
||||
|
||||
assertEquals(1, result.size());
|
||||
CarrouselCandidate candidate = result.get(0);
|
||||
assertEquals(1L, candidate.getUserId());
|
||||
assertEquals(CarrouselCandidateType.REGULAR_TRIP_ALTERNATIVE, candidate.getCandidateType());
|
||||
assertEquals(route, candidate.getSuggestedAlternative());
|
||||
assertNotNull(candidate.getDetectedRegularTrip());
|
||||
assertNotNull(candidate.getSuggestedAt());
|
||||
}
|
||||
|
||||
// --- getCarrouselForUser ---
|
||||
|
||||
@Test
|
||||
void getCarrouselForUser_NoCandidates_EmptyDTO() {
|
||||
when(carrouselCandidateRepository.findByUserIdDesc(eq(1L), any(Instant.class)))
|
||||
.thenReturn(List.of());
|
||||
|
||||
AlternativeCarrouselDTO result = carrouselService.getCarrouselForUser(1L);
|
||||
|
||||
assertTrue(result.getAlternatives().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCarrouselForUser_MixedTypes_MappedCorrectly() {
|
||||
CarrouselCandidate adhocCandidate = createCandidate(CarrouselCandidateType.ADHOC_ALTERNATIVE, 1L);
|
||||
CarrouselCandidate regularCandidate = createCandidate(CarrouselCandidateType.REGULAR_TRIP_ALTERNATIVE, 1L);
|
||||
|
||||
when(carrouselCandidateRepository.findByUserIdDesc(eq(1L), any(Instant.class)))
|
||||
.thenReturn(List.of(adhocCandidate, regularCandidate));
|
||||
|
||||
AlternativeCarrouselDTO result = carrouselService.getCarrouselForUser(1L);
|
||||
|
||||
assertEquals(2, result.getAlternatives().size());
|
||||
assertInstanceOf(AdHocAlternativeContext.class, result.getAlternatives().get(0));
|
||||
assertInstanceOf(RegularTripAlternativeContext.class, result.getAlternatives().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCarrouselForUser_MoreThanMax_LimitedTo5() {
|
||||
List<CarrouselCandidate> candidates = IntStream.range(0, 7)
|
||||
.mapToObj(i -> createCandidate(CarrouselCandidateType.ADHOC_ALTERNATIVE, 1L))
|
||||
.toList();
|
||||
|
||||
when(carrouselCandidateRepository.findByUserIdDesc(eq(1L), any(Instant.class)))
|
||||
.thenReturn(candidates);
|
||||
|
||||
AlternativeCarrouselDTO result = carrouselService.getCarrouselForUser(1L);
|
||||
|
||||
assertEquals(5, result.getAlternatives().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCarrouselForUser_AdhocCandidate_MapsToAdHocAlternativeContext() {
|
||||
CarrouselCandidate candidate = createCandidate(CarrouselCandidateType.ADHOC_ALTERNATIVE, 1L);
|
||||
candidate.setAdHocTripId(42L);
|
||||
|
||||
when(carrouselCandidateRepository.findByUserIdDesc(eq(1L), any(Instant.class)))
|
||||
.thenReturn(List.of(candidate));
|
||||
|
||||
AlternativeCarrouselDTO result = carrouselService.getCarrouselForUser(1L);
|
||||
|
||||
assertInstanceOf(AdHocAlternativeContext.class, result.getAlternatives().get(0));
|
||||
AdHocAlternativeContext context = (AdHocAlternativeContext) result.getAlternatives().get(0);
|
||||
assertEquals(42L, context.getTripId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCarrouselForUser_RegularCandidate_MapsToRegularTripAlternativeContext() {
|
||||
CarrouselCandidate candidate = createCandidate(CarrouselCandidateType.REGULAR_TRIP_ALTERNATIVE, 1L);
|
||||
|
||||
when(carrouselCandidateRepository.findByUserIdDesc(eq(1L), any(Instant.class)))
|
||||
.thenReturn(List.of(candidate));
|
||||
|
||||
AlternativeCarrouselDTO result = carrouselService.getCarrouselForUser(1L);
|
||||
|
||||
assertInstanceOf(RegularTripAlternativeContext.class, result.getAlternatives().get(0));
|
||||
RegularTripAlternativeContext context = (RegularTripAlternativeContext) result.getAlternatives().get(0);
|
||||
assertNotNull(context.getDetectedRegularTrip());
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private Trip createTrip(Long tripId, Long userId) {
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
Trip trip = new Trip();
|
||||
trip.setId(tripId);
|
||||
trip.setUser(user);
|
||||
return trip;
|
||||
}
|
||||
|
||||
private DetectedRegularTripDTO createDetectedRegularTrip() {
|
||||
Instant start = Instant.parse("2026-03-12T08:00:00Z");
|
||||
Instant end = Instant.parse("2026-03-12T08:40:00Z");
|
||||
|
||||
return DetectedRegularTripDTO.builder()
|
||||
.userId(1L)
|
||||
.modality(Modality.CAR)
|
||||
.dayOfWeek(DayOfWeek.THURSDAY)
|
||||
.departureWindowStart(LocalTime.of(8, 0))
|
||||
.departureWindowEnd(LocalTime.of(8, 15))
|
||||
.arrivalWindowStart(LocalTime.of(8, 30))
|
||||
.arrivalWindowEnd(LocalTime.of(8, 45))
|
||||
.startH3Center("8a1fa535568ffff")
|
||||
.endH3Center("8a1fa535020ffff")
|
||||
.supportingTrips(List.of(
|
||||
RegularTripDetectionDTO.builder()
|
||||
.tripId(100L)
|
||||
.userId(1L)
|
||||
.startH3Cell("8a1fa535568ffff")
|
||||
.endH3Cell("8a1fa535020ffff")
|
||||
.startTime(start)
|
||||
.endTime(end)
|
||||
.mainModality(Modality.CAR)
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
}
|
||||
|
||||
private CarrouselCandidate createCandidate(CarrouselCandidateType type, Long userId) {
|
||||
CarrouselCandidate.CarrouselCandidateBuilder builder = CarrouselCandidate.builder()
|
||||
.userId(userId)
|
||||
.suggestedAlternative(RouteDTO.getMockRouteDTO())
|
||||
.suggestedAt(Instant.now())
|
||||
.candidateType(type);
|
||||
|
||||
if (type == CarrouselCandidateType.ADHOC_ALTERNATIVE) {
|
||||
builder.adHocTripId(1L);
|
||||
} else {
|
||||
DetectedRegularTripDTO detectedTrip = createDetectedRegularTrip();
|
||||
builder.detectedRegularTrip(MinimalDetectedRegularTripDTO.fromDetectedRegularTripDTO(detectedTrip));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package SuMoC.backend.h3;
|
||||
|
||||
import com.uber.h3core.util.LatLng;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class H3ServiceTest {
|
||||
|
||||
private H3Service h3Service;
|
||||
private H3Configuration config;
|
||||
|
||||
// Essen, Germany
|
||||
private static final double ESSEN_LAT = 51.4556;
|
||||
private static final double ESSEN_LON = 7.0116;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
config = new H3Configuration();
|
||||
h3Service = new H3Service(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void latLngToCell_KnownCoordinates_ReturnsValidH3Cell() {
|
||||
String cell = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
assertNotNull(cell);
|
||||
assertFalse(cell.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void latLngToCellForRoute_UsesConfiguredResolution() {
|
||||
String cellViaRoute = h3Service.latLngToCellForRoute(ESSEN_LAT, ESSEN_LON);
|
||||
String cellExplicit = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
assertEquals(cellExplicit, cellViaRoute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void latLngToCellForRoute_DifferentResolution_UsesThatResolution() {
|
||||
config.setRoutePatternResolution(7);
|
||||
h3Service = new H3Service(config);
|
||||
|
||||
String cellViaRoute = h3Service.latLngToCellForRoute(ESSEN_LAT, ESSEN_LON);
|
||||
String cellExplicit = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 7);
|
||||
|
||||
assertEquals(cellExplicit, cellViaRoute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cellToLatLng_KnownCell_CoordinatesInExpectedRange() {
|
||||
String cell = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
LatLng result = h3Service.cellToLatLng(cell);
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.lat > 51.0 && result.lat < 52.0, "Latitude should be near Essen");
|
||||
assertTrue(result.lng > 6.5 && result.lng < 7.5, "Longitude should be near Essen");
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTrip_LatLngToCellThenBack_ApproximatelySameCoordinates() {
|
||||
String cell = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
LatLng result = h3Service.cellToLatLng(cell);
|
||||
|
||||
assertEquals(ESSEN_LAT, result.lat, 0.01);
|
||||
assertEquals(ESSEN_LON, result.lng, 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getKRingNeighbors_K1_Returns6Cells() {
|
||||
String cell = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
List<String> ring = h3Service.getKRingNeighbors(cell, 1);
|
||||
|
||||
assertEquals(6, ring.size());
|
||||
assertFalse(ring.contains(cell), "Ring at k=1 should not contain the origin cell");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getKRingNeighbors_K0_ReturnsOnlyOriginCell() {
|
||||
String cell = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
List<String> ring = h3Service.getKRingNeighbors(cell, 0);
|
||||
|
||||
assertEquals(1, ring.size());
|
||||
assertEquals(cell, ring.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRoutePatternNeighbors_UsesConfiguredRings() {
|
||||
String cell = h3Service.latLngToCell(ESSEN_LAT, ESSEN_LON, 10);
|
||||
|
||||
List<String> neighbors = h3Service.getRoutePatternNeighbors(cell);
|
||||
List<String> directRing = h3Service.getKRingNeighbors(cell, 1);
|
||||
|
||||
assertEquals(directRing, neighbors);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package SuMoC.backend.routing.services;
|
||||
|
||||
import SuMoC.backend.carta.DirectionsService;
|
||||
import SuMoC.backend.routing.config.AdHocRoutingProperties;
|
||||
import SuMoC.backend.routing.config.OnCallRoutingProperties;
|
||||
import SuMoC.backend.routing.dtos.*;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AlternativeRoutingServiceTest {
|
||||
|
||||
@Mock
|
||||
private AlternativeFilterService alternativeFilterService;
|
||||
|
||||
@Mock(lenient = true)
|
||||
private OnCallRoutingProperties onCallRoutingProperties;
|
||||
|
||||
@Mock(lenient = true)
|
||||
private AdHocRoutingProperties adHocRoutingProperties;
|
||||
|
||||
@Mock
|
||||
private DirectionsService directionsService;
|
||||
|
||||
@InjectMocks
|
||||
private AlternativeRoutingService service;
|
||||
|
||||
private RoutingAlternativeRequestDTO request;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
request = createRequest(true, false);
|
||||
}
|
||||
|
||||
// --- determineAlternativeRoutesOnUserRequest ---
|
||||
|
||||
@Test
|
||||
void determineOnUserRequest_AllFiltersAppliedWithOnCallProperties() {
|
||||
List<RouteDTO> routes = List.of(RouteDTO.getMockRouteDTO(), RouteDTO.getMockRouteDTO());
|
||||
when(directionsService.getPossibleAlternativeRoutes(request)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(request, routes, onCallRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(routes)).thenReturn(routes);
|
||||
|
||||
List<RouteDTO> result = service.determineAlternativeRoutesOnUserRequest(request);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
verify(directionsService).getPossibleAlternativeRoutes(request);
|
||||
verify(alternativeFilterService).filterForAdditionalTravelTime(request, routes, onCallRoutingProperties);
|
||||
verify(alternativeFilterService).filterForInefficientTransitRoutes(routes);
|
||||
verify(alternativeFilterService, never()).filterForBadWeather(any(), any(), any());
|
||||
}
|
||||
|
||||
// --- determineAlternativeRoutesAdHoc ---
|
||||
|
||||
@Test
|
||||
void determineAdHoc_WeatherEnabled_WeatherFilterApplied() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(true, false);
|
||||
List<RouteDTO> routes = List.of(RouteDTO.getMockRouteDTO());
|
||||
when(directionsService.getPossibleAlternativeRoutes(req)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForBadWeather(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(routes)).thenReturn(routes);
|
||||
|
||||
List<RouteDTO> result = service.determineAlternativeRoutesAdHoc(req);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
verify(alternativeFilterService).filterForBadWeather(req, routes, adHocRoutingProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineAdHoc_WeatherDisabled_WeatherFilterSkipped() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(false, false);
|
||||
List<RouteDTO> routes = List.of(RouteDTO.getMockRouteDTO());
|
||||
when(directionsService.getPossibleAlternativeRoutes(req)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(routes)).thenReturn(routes);
|
||||
|
||||
service.determineAlternativeRoutesAdHoc(req);
|
||||
|
||||
verify(alternativeFilterService, never()).filterForBadWeather(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineAdHoc_EmptyAfterFiltering_EmptyList() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(false, false);
|
||||
List<RouteDTO> routes = List.of(RouteDTO.getMockRouteDTO());
|
||||
when(directionsService.getPossibleAlternativeRoutes(req)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(req, routes, adHocRoutingProperties)).thenReturn(List.of());
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(List.of())).thenReturn(List.of());
|
||||
|
||||
List<RouteDTO> result = service.determineAlternativeRoutesAdHoc(req);
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
// --- determineBestAlternativeRouteForRegularTrip ---
|
||||
|
||||
@Test
|
||||
void determineBestForRegularTrip_PreferPhysical_PhysicalExists_ReturnsFastestPhysical() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(true, true);
|
||||
RouteDTO physical1200 = createPhysicalRoute(1200);
|
||||
RouteDTO physical1800 = createPhysicalRoute(1800);
|
||||
RouteDTO nonPhysical900 = createNonPhysicalRoute(900);
|
||||
List<RouteDTO> routes = List.of(physical1200, physical1800, nonPhysical900);
|
||||
|
||||
stubFiltersPassThrough(req, routes);
|
||||
|
||||
RouteDTO result = service.determineBestAlternativeRouteForRegularTrip(req);
|
||||
|
||||
assertEquals(physical1200, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineBestForRegularTrip_PreferPhysical_NoPhysical_ReturnsFastestOverall() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(true, true);
|
||||
RouteDTO nonPhysical1000 = createNonPhysicalRoute(1000);
|
||||
RouteDTO nonPhysical2000 = createNonPhysicalRoute(2000);
|
||||
List<RouteDTO> routes = List.of(nonPhysical1000, nonPhysical2000);
|
||||
|
||||
stubFiltersPassThrough(req, routes);
|
||||
|
||||
RouteDTO result = service.determineBestAlternativeRouteForRegularTrip(req);
|
||||
|
||||
assertEquals(nonPhysical1000, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineBestForRegularTrip_NoPhysicalPref_ReturnsFastestOverall() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(true, false);
|
||||
RouteDTO physical800 = createPhysicalRoute(800);
|
||||
RouteDTO nonPhysical1200 = createNonPhysicalRoute(1200);
|
||||
List<RouteDTO> routes = List.of(physical800, nonPhysical1200);
|
||||
|
||||
stubFiltersPassThrough(req, routes);
|
||||
|
||||
RouteDTO result = service.determineBestAlternativeRouteForRegularTrip(req);
|
||||
|
||||
assertEquals(physical800, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineBestForRegularTrip_NoRoutes_ReturnsNull() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(true, false);
|
||||
when(directionsService.getPossibleAlternativeRoutes(req)).thenReturn(List.of());
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(req, List.of(), adHocRoutingProperties)).thenReturn(List.of());
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(List.of())).thenReturn(List.of());
|
||||
|
||||
RouteDTO result = service.determineBestAlternativeRouteForRegularTrip(req);
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineBestForRegularTrip_WeatherEnabled_WeatherFilterApplied() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(true, false);
|
||||
List<RouteDTO> routes = List.of(createNonPhysicalRoute(1000));
|
||||
stubFiltersPassThrough(req, routes);
|
||||
when(alternativeFilterService.filterForBadWeather(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
|
||||
service.determineBestAlternativeRouteForRegularTrip(req);
|
||||
|
||||
verify(alternativeFilterService).filterForBadWeather(req, routes, adHocRoutingProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void determineBestForRegularTrip_WeatherDisabled_WeatherFilterSkipped() {
|
||||
RoutingAlternativeRequestDTO req = createRequest(false, false);
|
||||
List<RouteDTO> routes = List.of(createNonPhysicalRoute(1000));
|
||||
when(directionsService.getPossibleAlternativeRoutes(req)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(routes)).thenReturn(routes);
|
||||
|
||||
service.determineBestAlternativeRouteForRegularTrip(req);
|
||||
|
||||
verify(alternativeFilterService, never()).filterForBadWeather(any(), any(), any());
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private RoutingAlternativeRequestDTO createRequest(boolean considerWeather, boolean preferPhysical) {
|
||||
RoutingPreferencesDTO preferences = new RoutingPreferencesDTO();
|
||||
preferences.setConsiderWeatherConditions(considerWeather);
|
||||
preferences.setPreferPhysicalActivity(preferPhysical);
|
||||
|
||||
return RoutingAlternativeRequestDTO.builder()
|
||||
.originLat(51.44)
|
||||
.originLon(6.98)
|
||||
.destinationLat(51.46)
|
||||
.destinationLon(7.01)
|
||||
.departureTime(Instant.parse("2026-03-12T08:00:00Z"))
|
||||
.arrivalTime(Instant.parse("2026-03-12T09:00:00Z"))
|
||||
.routingPreferencesDTO(preferences)
|
||||
.build();
|
||||
}
|
||||
|
||||
private RouteDTO createPhysicalRoute(long durationSeconds) {
|
||||
RouteDTO route = RouteDTO.getMockRouteDTO();
|
||||
route.setStartDateTime(Instant.parse("2026-03-12T08:00:00Z"));
|
||||
route.setEndDateTime(Instant.parse("2026-03-12T08:00:00Z").plusSeconds(durationSeconds));
|
||||
route.setSteps(new ArrayList<>(List.of(new StepDTO(Modality.BICYCLE, 5000f))));
|
||||
return route;
|
||||
}
|
||||
|
||||
private RouteDTO createNonPhysicalRoute(long durationSeconds) {
|
||||
RouteDTO route = RouteDTO.getMockRouteDTO();
|
||||
route.setStartDateTime(Instant.parse("2026-03-12T08:00:00Z"));
|
||||
route.setEndDateTime(Instant.parse("2026-03-12T08:00:00Z").plusSeconds(durationSeconds));
|
||||
route.setSteps(new ArrayList<>(List.of(
|
||||
new StepDTO(Modality.WALKING, 300f),
|
||||
new TransitStepDTO(Modality.BUS, 3000f, "101", "Line", "Start", "End"),
|
||||
new StepDTO(Modality.WALKING, 200f)
|
||||
)));
|
||||
return route;
|
||||
}
|
||||
|
||||
private void stubFiltersPassThrough(RoutingAlternativeRequestDTO req, List<RouteDTO> routes) {
|
||||
when(directionsService.getPossibleAlternativeRoutes(req)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForAdditionalTravelTime(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForBadWeather(req, routes, adHocRoutingProperties)).thenReturn(routes);
|
||||
when(alternativeFilterService.filterForInefficientTransitRoutes(routes)).thenReturn(routes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package SuMoC.backend.suggestion.services;
|
||||
|
||||
import SuMoC.backend.carrousel.service.CarrouselService;
|
||||
import SuMoC.backend.notification.enums.NotificationType;
|
||||
import SuMoC.backend.notification.services.NotificationSchedulerService;
|
||||
import SuMoC.backend.routing.dtos.RouteDTO;
|
||||
import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
|
||||
import SuMoC.backend.routing.dtos.StepDTO;
|
||||
import SuMoC.backend.routing.dtos.TransitStepDTO;
|
||||
import SuMoC.backend.routing.services.AlternativeRoutingService;
|
||||
import SuMoC.backend.routing.services.RoutingPreferencesService;
|
||||
import SuMoC.backend.suggestion.config.AdHocSuggestionProperties;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import SuMoC.backend.user.models.User;
|
||||
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 java.time.Instant;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class AdHocSuggestionServiceTest {
|
||||
|
||||
@Mock
|
||||
private AlternativeRoutingService alternativeRoutingService;
|
||||
|
||||
@Mock
|
||||
private RoutingPreferencesService routingPreferencesService;
|
||||
|
||||
@Mock(lenient = true)
|
||||
private AdHocSuggestionProperties adHocSuggestionProperties;
|
||||
|
||||
@Mock
|
||||
private NotificationSchedulerService notificationSchedulerService;
|
||||
|
||||
@Mock
|
||||
private CarrouselService carrouselService;
|
||||
|
||||
@InjectMocks
|
||||
private AdHocSuggestionService service;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<RouteDTO>> routeListCaptor;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
when(adHocSuggestionProperties.getRecentTripThresholdSeconds()).thenReturn(6 * 60 * 60); // 6 hours
|
||||
}
|
||||
|
||||
// --- early exits ---
|
||||
|
||||
@Test
|
||||
void checkNewTrip_OldTrip_ReturnsImmediately() {
|
||||
Trip trip = createTrip(Modality.CAR, Instant.now().minusSeconds(7 * 60 * 60));
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verifyNoInteractions(alternativeRoutingService, notificationSchedulerService, carrouselService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkNewTrip_BicycleTrip_ReturnsImmediately() {
|
||||
Trip trip = createTrip(Modality.BICYCLE, Instant.now().minusSeconds(60));
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verifyNoInteractions(alternativeRoutingService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkNewTrip_WalkingTrip_ReturnsImmediately() {
|
||||
Trip trip = createTrip(Modality.WALKING, Instant.now().minusSeconds(60));
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verifyNoInteractions(alternativeRoutingService);
|
||||
}
|
||||
|
||||
// --- no alternatives ---
|
||||
|
||||
@Test
|
||||
void checkNewTrip_CarTrip_NoAlternatives_NoNotification() {
|
||||
Trip trip = createRecentCarTrip();
|
||||
stubPreferences(false);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of());
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verifyNoInteractions(notificationSchedulerService, carrouselService);
|
||||
}
|
||||
|
||||
// --- pickBestAlternatives ---
|
||||
|
||||
@Test
|
||||
void checkNewTrip_CarTrip_NoPhysicalPref_PicksFastestOnly() {
|
||||
Trip trip = createRecentCarTrip();
|
||||
RoutingPreferencesDTO prefs = stubPreferences(false);
|
||||
RouteDTO fast = createNonPhysicalRoute(1800);
|
||||
RouteDTO slow = createNonPhysicalRoute(2400);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of(fast, slow));
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(carrouselService).addAdHocTripCandidates(eq(trip), routeListCaptor.capture());
|
||||
List<RouteDTO> bestAlternatives = routeListCaptor.getValue();
|
||||
assertEquals(1, bestAlternatives.size());
|
||||
assertEquals(fast, bestAlternatives.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkNewTrip_CarTrip_PhysicalPref_HasPhysicalRoutes_PicksTwo() {
|
||||
Trip trip = createRecentCarTrip();
|
||||
RoutingPreferencesDTO prefs = stubPreferences(true);
|
||||
RouteDTO physical = createPhysicalRoute(2000);
|
||||
RouteDTO nonPhysical = createNonPhysicalRoute(1500);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of(physical, nonPhysical));
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(carrouselService).addAdHocTripCandidates(eq(trip), routeListCaptor.capture());
|
||||
List<RouteDTO> bestAlternatives = routeListCaptor.getValue();
|
||||
assertEquals(2, bestAlternatives.size());
|
||||
assertTrue(bestAlternatives.contains(physical));
|
||||
assertTrue(bestAlternatives.contains(nonPhysical));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkNewTrip_CarTrip_PhysicalPref_FastestPhysicalIsFastest_PicksOne() {
|
||||
Trip trip = createRecentCarTrip();
|
||||
RoutingPreferencesDTO prefs = stubPreferences(true);
|
||||
RouteDTO physical = createPhysicalRoute(1200);
|
||||
RouteDTO nonPhysical = createNonPhysicalRoute(1800);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of(physical, nonPhysical));
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(carrouselService).addAdHocTripCandidates(eq(trip), routeListCaptor.capture());
|
||||
List<RouteDTO> bestAlternatives = routeListCaptor.getValue();
|
||||
assertEquals(1, bestAlternatives.size());
|
||||
assertEquals(physical, bestAlternatives.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkNewTrip_CarTrip_PhysicalPref_NoPhysicalRoutes_PicksFastestOnly() {
|
||||
Trip trip = createRecentCarTrip();
|
||||
RoutingPreferencesDTO prefs = stubPreferences(true);
|
||||
RouteDTO fast = createNonPhysicalRoute(1500);
|
||||
RouteDTO slow = createNonPhysicalRoute(2000);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of(fast, slow));
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(carrouselService).addAdHocTripCandidates(eq(trip), routeListCaptor.capture());
|
||||
List<RouteDTO> bestAlternatives = routeListCaptor.getValue();
|
||||
assertEquals(1, bestAlternatives.size());
|
||||
assertEquals(fast, bestAlternatives.get(0));
|
||||
}
|
||||
|
||||
// --- modality triggers ---
|
||||
|
||||
@Test
|
||||
void checkNewTrip_MotorcycleTrip_TriggersSuggestion() {
|
||||
Trip trip = createTrip(Modality.MOTORCYCLE, Instant.now().minusSeconds(60));
|
||||
stubPreferences(false);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of());
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(alternativeRoutingService).determineAlternativeRoutesAdHoc(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkNewTrip_TaxiTrip_TriggersSuggestion() {
|
||||
Trip trip = createTrip(Modality.TAXI, Instant.now().minusSeconds(60));
|
||||
stubPreferences(false);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of());
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(alternativeRoutingService).determineAlternativeRoutesAdHoc(any());
|
||||
}
|
||||
|
||||
// --- verify notification scheduling ---
|
||||
|
||||
@Test
|
||||
void checkNewTrip_CarTrip_VerifiesNotificationCalledWithCorrectArgs() {
|
||||
Trip trip = createRecentCarTrip();
|
||||
RoutingPreferencesDTO prefs = stubPreferences(false);
|
||||
RouteDTO route = createNonPhysicalRoute(1800);
|
||||
when(alternativeRoutingService.determineAlternativeRoutesAdHoc(any())).thenReturn(List.of(route));
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
|
||||
service.checkNewTripForNotifiedAlternativeSuggestion(trip);
|
||||
|
||||
verify(notificationSchedulerService).schedule(
|
||||
eq(1L),
|
||||
routeListCaptor.capture(),
|
||||
eq(NotificationType.ADHOC_ALTERNATIVE),
|
||||
any(LocalTime.class)
|
||||
);
|
||||
assertEquals(1, routeListCaptor.getValue().size());
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private Trip createTrip(Modality modality, Instant endTime) {
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
Trip trip = new Trip();
|
||||
trip.setId(1L);
|
||||
trip.setUser(user);
|
||||
trip.setMainModality(modality);
|
||||
trip.setStartTime(endTime.minusSeconds(1800));
|
||||
trip.setEndTime(endTime);
|
||||
trip.setStartLatitude(51.44);
|
||||
trip.setStartLongitude(6.98);
|
||||
trip.setEndLatitude(51.46);
|
||||
trip.setEndLongitude(7.01);
|
||||
return trip;
|
||||
}
|
||||
|
||||
private Trip createRecentCarTrip() {
|
||||
return createTrip(Modality.CAR, Instant.now().minusSeconds(60));
|
||||
}
|
||||
|
||||
private RoutingPreferencesDTO stubPreferences(boolean preferPhysical) {
|
||||
RoutingPreferencesDTO prefs = new RoutingPreferencesDTO();
|
||||
prefs.setPreferPhysicalActivity(preferPhysical);
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
return prefs;
|
||||
}
|
||||
|
||||
private RouteDTO createPhysicalRoute(long durationSeconds) {
|
||||
RouteDTO route = RouteDTO.getMockRouteDTO();
|
||||
route.setStartDateTime(Instant.parse("2026-03-12T08:00:00Z"));
|
||||
route.setEndDateTime(Instant.parse("2026-03-12T08:00:00Z").plusSeconds(durationSeconds));
|
||||
route.setSteps(new ArrayList<>(List.of(new StepDTO(Modality.BICYCLE, 5000f))));
|
||||
return route;
|
||||
}
|
||||
|
||||
private RouteDTO createNonPhysicalRoute(long durationSeconds) {
|
||||
RouteDTO route = RouteDTO.getMockRouteDTO();
|
||||
route.setStartDateTime(Instant.parse("2026-03-12T08:00:00Z"));
|
||||
route.setEndDateTime(Instant.parse("2026-03-12T08:00:00Z").plusSeconds(durationSeconds));
|
||||
route.setSteps(new ArrayList<>(List.of(
|
||||
new StepDTO(Modality.WALKING, 300f),
|
||||
new TransitStepDTO(Modality.BUS, 3000f, "101", "Line", "Start", "End"),
|
||||
new StepDTO(Modality.WALKING, 200f)
|
||||
)));
|
||||
return route;
|
||||
}
|
||||
}
|
||||
+267
@@ -0,0 +1,267 @@
|
||||
package SuMoC.backend.suggestion.services;
|
||||
|
||||
import SuMoC.backend.carrousel.models.CarrouselCandidate;
|
||||
import SuMoC.backend.carrousel.repository.CarrouselCandidateRepository;
|
||||
import SuMoC.backend.carrousel.service.CarrouselService;
|
||||
import SuMoC.backend.h3.H3Service;
|
||||
import SuMoC.backend.notification.enums.NotificationType;
|
||||
import SuMoC.backend.notification.interfaces.NotifieableWithTime;
|
||||
import SuMoC.backend.notification.services.NotificationSchedulerService;
|
||||
import SuMoC.backend.regulartrip.dtos.DetectedRegularTripDTO;
|
||||
import SuMoC.backend.regulartrip.services.RegularTripDetectionService;
|
||||
import SuMoC.backend.routing.dtos.RouteDTO;
|
||||
import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
|
||||
import SuMoC.backend.routing.services.AlternativeRoutingService;
|
||||
import SuMoC.backend.routing.services.RoutingPreferencesService;
|
||||
import SuMoC.backend.suggestion.config.RegularTripSuggestionProperties;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import SuMoC.backend.trip.dtos.RegularTripDetectionDTO;
|
||||
import com.uber.h3core.util.LatLng;
|
||||
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.DayOfWeek;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class RegularTripSuggestionServiceTest {
|
||||
|
||||
@Mock
|
||||
private AlternativeRoutingService alternativeRoutingService;
|
||||
|
||||
@Mock
|
||||
private RoutingPreferencesService routingPreferencesService;
|
||||
|
||||
@Mock(lenient = true)
|
||||
private RegularTripSuggestionProperties properties;
|
||||
|
||||
@Mock
|
||||
private NotificationSchedulerService notificationSchedulerService;
|
||||
|
||||
@Mock
|
||||
private RegularTripDetectionService regularTripDetectionService;
|
||||
|
||||
@Mock(lenient = true)
|
||||
private H3Service h3Service;
|
||||
|
||||
@Mock
|
||||
private CarrouselService carrouselService;
|
||||
|
||||
@Mock
|
||||
private CarrouselCandidateRepository carrouselCandidateRepository;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Collection<CarrouselCandidate>> candidateCaptor;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<List<NotifieableWithTime>> notificationCaptor;
|
||||
|
||||
private RegularTripSuggestionService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Synchronous executor for deterministic testing
|
||||
service = new RegularTripSuggestionService(
|
||||
alternativeRoutingService,
|
||||
routingPreferencesService,
|
||||
properties,
|
||||
notificationSchedulerService,
|
||||
regularTripDetectionService,
|
||||
h3Service,
|
||||
carrouselService,
|
||||
carrouselCandidateRepository,
|
||||
Runnable::run // synchronous executor
|
||||
);
|
||||
|
||||
when(properties.getNotifyBeforeMinutes()).thenReturn(15);
|
||||
when(h3Service.cellToLatLng(anyString())).thenReturn(new LatLng(51.44, 6.98));
|
||||
}
|
||||
|
||||
// --- checkAlternativesAndNotify ---
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_EmptyMap_NoSavesNoNotifications() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> emptyMap = new HashMap<>();
|
||||
|
||||
service.checkAlternativesAndNotify(emptyMap);
|
||||
|
||||
verify(carrouselCandidateRepository).saveAll(candidateCaptor.capture());
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
assertTrue(candidateCaptor.getValue().isEmpty());
|
||||
assertTrue(notificationCaptor.getValue().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_SingleUserSingleTrip_AlternativeFound_SavesAndSchedules() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> map = new HashMap<>();
|
||||
DetectedRegularTripDTO trip = createDetectedTrip();
|
||||
map.put(1L, List.of(trip));
|
||||
|
||||
RoutingPreferencesDTO prefs = new RoutingPreferencesDTO();
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(prefs);
|
||||
|
||||
RouteDTO bestRoute = RouteDTO.getMockRouteDTO();
|
||||
when(alternativeRoutingService.determineBestAlternativeRouteForRegularTrip(any())).thenReturn(bestRoute);
|
||||
|
||||
CarrouselCandidate candidate = CarrouselCandidate.builder().userId(1L).build();
|
||||
when(carrouselService.getRegularTripCandidates(eq(1L), eq(trip), any())).thenReturn(List.of(candidate));
|
||||
|
||||
service.checkAlternativesAndNotify(map);
|
||||
|
||||
verify(carrouselCandidateRepository).saveAll(candidateCaptor.capture());
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
assertEquals(1, candidateCaptor.getValue().size());
|
||||
assertEquals(1, notificationCaptor.getValue().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_SingleUserSingleTrip_NoAlternative_EmptyResults() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> map = new HashMap<>();
|
||||
map.put(1L, List.of(createDetectedTrip()));
|
||||
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(new RoutingPreferencesDTO());
|
||||
when(alternativeRoutingService.determineBestAlternativeRouteForRegularTrip(any())).thenReturn(null);
|
||||
|
||||
service.checkAlternativesAndNotify(map);
|
||||
|
||||
verify(carrouselCandidateRepository).saveAll(candidateCaptor.capture());
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
assertTrue(candidateCaptor.getValue().isEmpty());
|
||||
assertTrue(notificationCaptor.getValue().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_MultipleUsers_MixedResults_CorrectAggregation() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> map = new HashMap<>();
|
||||
map.put(1L, List.of(createDetectedTrip()));
|
||||
map.put(2L, List.of(createDetectedTrip()));
|
||||
|
||||
when(routingPreferencesService.getUserRoutingPreferences(anyLong())).thenReturn(new RoutingPreferencesDTO());
|
||||
|
||||
// User 1 gets an alternative, user 2 does not
|
||||
RouteDTO bestRoute = RouteDTO.getMockRouteDTO();
|
||||
when(alternativeRoutingService.determineBestAlternativeRouteForRegularTrip(any()))
|
||||
.thenReturn(bestRoute)
|
||||
.thenReturn(null);
|
||||
|
||||
CarrouselCandidate candidate = CarrouselCandidate.builder().userId(1L).build();
|
||||
when(carrouselService.getRegularTripCandidates(anyLong(), any(), any())).thenReturn(List.of(candidate));
|
||||
|
||||
service.checkAlternativesAndNotify(map);
|
||||
|
||||
verify(carrouselCandidateRepository).saveAll(candidateCaptor.capture());
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
assertEquals(1, candidateCaptor.getValue().size());
|
||||
assertEquals(1, notificationCaptor.getValue().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_ExceptionInOneUser_OtherUsersStillProcessed() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> map = new HashMap<>();
|
||||
map.put(1L, List.of(createDetectedTrip()));
|
||||
map.put(2L, List.of(createDetectedTrip()));
|
||||
|
||||
// User 1 throws, user 2 succeeds
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenThrow(new RuntimeException("DB error"));
|
||||
when(routingPreferencesService.getUserRoutingPreferences(2L)).thenReturn(new RoutingPreferencesDTO());
|
||||
|
||||
RouteDTO bestRoute = RouteDTO.getMockRouteDTO();
|
||||
when(alternativeRoutingService.determineBestAlternativeRouteForRegularTrip(any())).thenReturn(bestRoute);
|
||||
|
||||
CarrouselCandidate candidate = CarrouselCandidate.builder().userId(2L).build();
|
||||
when(carrouselService.getRegularTripCandidates(eq(2L), any(), any())).thenReturn(List.of(candidate));
|
||||
|
||||
service.checkAlternativesAndNotify(map);
|
||||
|
||||
// Should still save user 2's results despite user 1's exception
|
||||
verify(carrouselCandidateRepository).saveAll(candidateCaptor.capture());
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
assertEquals(1, candidateCaptor.getValue().size());
|
||||
assertEquals(1, notificationCaptor.getValue().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_NotificationTime_CorrectCalculation() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> map = new HashMap<>();
|
||||
DetectedRegularTripDTO trip = createDetectedTrip(); // departureWindowStart = 08:00
|
||||
map.put(1L, List.of(trip));
|
||||
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(new RoutingPreferencesDTO());
|
||||
RouteDTO bestRoute = RouteDTO.getMockRouteDTO();
|
||||
when(alternativeRoutingService.determineBestAlternativeRouteForRegularTrip(any())).thenReturn(bestRoute);
|
||||
when(carrouselService.getRegularTripCandidates(eq(1L), eq(trip), any())).thenReturn(List.of());
|
||||
|
||||
service.checkAlternativesAndNotify(map);
|
||||
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
NotifieableWithTime notification = notificationCaptor.getValue().get(0);
|
||||
// departureWindowStart(08:00) - notifyBeforeMinutes(15) = 07:45
|
||||
assertEquals(LocalTime.of(7, 45), notification.getEarliestTimeOfDayToSend());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkAlternativesAndNotify_SingleUserMultipleTrips_EachProcessed() {
|
||||
HashMap<Long, List<DetectedRegularTripDTO>> map = new HashMap<>();
|
||||
map.put(1L, List.of(createDetectedTrip(), createDetectedTrip()));
|
||||
|
||||
when(routingPreferencesService.getUserRoutingPreferences(1L)).thenReturn(new RoutingPreferencesDTO());
|
||||
RouteDTO bestRoute = RouteDTO.getMockRouteDTO();
|
||||
when(alternativeRoutingService.determineBestAlternativeRouteForRegularTrip(any())).thenReturn(bestRoute);
|
||||
|
||||
CarrouselCandidate candidate = CarrouselCandidate.builder().userId(1L).build();
|
||||
when(carrouselService.getRegularTripCandidates(eq(1L), any(), any())).thenReturn(List.of(candidate));
|
||||
|
||||
service.checkAlternativesAndNotify(map);
|
||||
|
||||
verify(alternativeRoutingService, times(2)).determineBestAlternativeRouteForRegularTrip(any());
|
||||
verify(carrouselCandidateRepository).saveAll(candidateCaptor.capture());
|
||||
verify(notificationSchedulerService).scheduleAll(notificationCaptor.capture(), eq(NotificationType.REGULAR_TRIP_ALTERNATIVE));
|
||||
assertEquals(2, candidateCaptor.getValue().size());
|
||||
assertEquals(2, notificationCaptor.getValue().size());
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private DetectedRegularTripDTO createDetectedTrip() {
|
||||
Instant start = Instant.parse("2026-03-12T08:00:00Z");
|
||||
Instant end = Instant.parse("2026-03-12T08:40:00Z");
|
||||
|
||||
return DetectedRegularTripDTO.builder()
|
||||
.userId(1L)
|
||||
.modality(Modality.CAR)
|
||||
.dayOfWeek(DayOfWeek.THURSDAY)
|
||||
.departureWindowStart(LocalTime.of(8, 0))
|
||||
.departureWindowEnd(LocalTime.of(8, 15))
|
||||
.arrivalWindowStart(LocalTime.of(8, 30))
|
||||
.arrivalWindowEnd(LocalTime.of(8, 45))
|
||||
.startH3Center("8a1fa535568ffff")
|
||||
.endH3Center("8a1fa535020ffff")
|
||||
.supportingTrips(List.of(
|
||||
RegularTripDetectionDTO.builder()
|
||||
.tripId(100L)
|
||||
.userId(1L)
|
||||
.startH3Cell("8a1fa535568ffff")
|
||||
.endH3Cell("8a1fa535020ffff")
|
||||
.startTime(start)
|
||||
.endTime(end)
|
||||
.mainModality(Modality.CAR)
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user