RouteListener implements ApplicationListener<RouteChangedEvent> { @Override public void onApplicationEvent(RouteChangedEvent event) { Location depot = event.depotId().flatMap(locationRepository::find).orElse(null); try { Map<Long, Vehicle> vehicleMap = event.vehicleIds().stream() .collect(toMap(vehicleId -> vehicleId, this::findVehicleById)); Map<Long, Location> visitMap = event.visitIds().stream() .collect(toMap(visitId -> visitId, this::findLocationById)); List<RouteWithTrack> routes = event.routes().stream() .map(shallowRoute -> new Route( vehicleMap.get(shallowRoute.vehicleId), findLocationById(shallowRoute.depotId), shallowRoute.visitIds.stream() .map(visitMap::get) .collect(toList()))) .map(route -> new RouteWithTrack(route, track(route.depot(), route.visits()))) .collect(toList()); bestRoutingPlan = new RoutingPlan( event.distance(), new ArrayList<>(vehicleMap.values()), depot, new ArrayList<>(visitMap.values()), routes); routingPlanConsumer.consumePlan(bestRoutingPlan); } catch (IllegalStateException e) { logger.warn("Discarding an outdated routing plan: {}", e.toString()); } } @Autowired  RouteListener(
            Router router,
            RoutingPlanConsumer routingPlanConsumer,
            VehicleRepository vehicleRepository,
            LocationRepository locationRepository); @Override void onApplicationEvent(RouteChangedEvent event); RoutingPlan getBestRoutingPlan();  }
@Test void event_with_no_routes_should_be_consumed_as_an_empty_routing_plan() { final long vehicleId = 12; final Vehicle vehicle = VehicleFactory.testVehicle(vehicleId); when(vehicleRepository.find(vehicleId)).thenReturn(Optional.of(vehicle)); RouteChangedEvent event = new RouteChangedEvent( this, Distance.ZERO, singletonList(vehicleId), null, emptyList(), emptyList()); routeListener.onApplicationEvent(event); verifyNoInteractions(router); RoutingPlan routingPlan = verifyAndCaptureConsumedPlan(); assertThat(routingPlan.vehicles()).containsExactly(vehicle); assertThat(routingPlan.depot()).isEmpty(); assertThat(routingPlan.visits()).isEmpty(); assertThat(routingPlan.routes()).isEmpty(); }