GraphHopperRouter implements Router, DistanceCalculator, Region { @Override public long travelTimeMillis(Coordinates from, Coordinates to) { GHRequest ghRequest = new GHRequest( from.latitude().doubleValue(), from.longitude().doubleValue(), to.latitude().doubleValue(), to.longitude().doubleValue()); GHResponse ghResponse = graphHopper.route(ghRequest); if (ghResponse.hasErrors()) { throw new DistanceCalculationException("No route from " + from + " to " + to, ghResponse.getErrors().get(0)); } return ghResponse.getBest().getTime(); } @Autowired  GraphHopperRouter(GraphHopperOSM graphHopper); @Override List<Coordinates> getPath(Coordinates from, Coordinates to); @Override long travelTimeMillis(Coordinates from, Coordinates to); @Override BoundingBox getBounds();  }
@Test void travel_time_should_return_graphhopper_time() { whenRouteReturnResponse(); whenBestReturnPath(); long travelTimeMillis = 135 * 60 * 60 * 1000; when(pathWrapper.getTime()).thenReturn(travelTimeMillis); assertThat(new GraphHopperRouter(graphHopper).travelTimeMillis(from, to)).isEqualTo(travelTimeMillis); }
@Test void getDistance_should_throw_exception_when_no_route_exists() { whenRouteReturnResponse(); when(ghResponse.hasErrors()).thenReturn(true); when(ghResponse.getErrors()).thenReturn(Collections.singletonList(new RuntimeException())); GraphHopperRouter graphHopperRouter = new GraphHopperRouter(graphHopper); assertThatThrownBy(() -> graphHopperRouter.travelTimeMillis(from, to)) .isNotInstanceOf(NullPointerException.class) .isInstanceOf(RuntimeException.class) .hasMessageContaining("No route"); }