Skip to content

Commit

Permalink
Merge pull request #55 from naviqore/test/NAV-77-Integration-Test-for…
Browse files Browse the repository at this point in the history
…-Rest-Controller
  • Loading branch information
munterfi authored Jun 10, 2024
2 parents e4f9e83 + 50fe527 commit 92c332f
Show file tree
Hide file tree
Showing 9 changed files with 613 additions and 123 deletions.
73 changes: 56 additions & 17 deletions src/main/java/ch/naviqore/app/controller/RoutingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,64 @@ public RoutingController(PublicTransitService service) {
this.service = service;
}

private static GeoCoordinate validateCoordinate(double latitude, double longitude) {
try {
return new GeoCoordinate(latitude, longitude);
} catch (IllegalArgumentException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Coordinates must be valid, Latitude between -90 and 90 and Longitude between -180 and 180.");
}
}

private static void validateQueryParams(int maxWalkingDuration, int maxTransferNumber, int maxTravelTime,
int minTransferTime) {
if (maxWalkingDuration < 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Max walking duration must be greater than or equal to 0.");
}
if (maxTransferNumber < 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Max transfer number must be greater than or equal to 0.");
}
if (maxTravelTime <= 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Max travel time must be greater than 0.");
}
if (minTransferTime < 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Min transfer time must be greater than or equal to 0.");
}
}

@GetMapping("/connections")
public List<Connection> getConnections(@RequestParam(required = false) String sourceStopId,
@RequestParam(required = false, defaultValue = "-1.0") double sourceLatitude,
@RequestParam(required = false, defaultValue = "-1.0") double sourceLongitude,
@RequestParam(required = false, defaultValue = "-91.0") double sourceLatitude,
@RequestParam(required = false, defaultValue = "-181.0") double sourceLongitude,
@RequestParam(required = false) String targetStopId,
@RequestParam(required = false, defaultValue = "-1.0") double targetLatitude,
@RequestParam(required = false, defaultValue = "-1.0") double targetLongitude,
@RequestParam(required = false, defaultValue = "-91.0") double targetLatitude,
@RequestParam(required = false, defaultValue = "-181.0") double targetLongitude,
@RequestParam(required = false) LocalDateTime departureDateTime,
@RequestParam(required = false, defaultValue = "2147483647") int maxWalkingDuration,
@RequestParam(required = false, defaultValue = "2147483647") int maxTransferNumber,
@RequestParam(required = false, defaultValue = "2147483647") int maxTravelTime,
@RequestParam(required = false, defaultValue = "0") int minTransferTime) {

GeoCoordinate sourceCoordinate = null;
GeoCoordinate targetCoordinate = null;

if (sourceStopId == null) {
if (sourceLatitude < 0 || sourceLongitude < 0) {
if (sourceLatitude == -91.0 || sourceLongitude == -181.0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Either sourceStopId or sourceLatitude and sourceLongitude must be provided.");
}
sourceCoordinate = validateCoordinate(sourceLatitude, sourceLongitude);
}

if (targetStopId == null) {
if (targetLatitude < 0 || targetLongitude < 0) {
if (targetLatitude == -91.0 || targetLongitude == -181.0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Either targetStopId or targetLatitude and targetLongitude must be provided.");
}
targetCoordinate = validateCoordinate(targetLatitude, targetLongitude);
}

if (departureDateTime == null) {
Expand All @@ -68,9 +102,7 @@ public List<Connection> getConnections(@RequestParam(required = false) String so
Stop sourceStop = sourceStopId != null ? getStop(sourceStopId) : null;
Stop targetStop = targetStopId != null ? getStop(targetStopId) : null;

GeoCoordinate sourceCoordinate = sourceStop == null ? new GeoCoordinate(sourceLatitude, sourceLongitude) : null;
GeoCoordinate targetCoordinate = targetStop == null ? new GeoCoordinate(targetLatitude, targetLongitude) : null;

validateQueryParams(maxWalkingDuration, maxTransferNumber, maxTravelTime, minTransferTime);
ConnectionQueryConfig config = new ConnectionQueryConfig(maxWalkingDuration, minTransferTime, maxTransferNumber,
maxTravelTime);

Expand All @@ -94,28 +126,35 @@ public List<Connection> getConnections(@RequestParam(required = false) String so

@GetMapping("/isolines")
public List<EarliestArrival> getIsolines(@RequestParam(required = false) String sourceStopId,
@RequestParam(required = false, defaultValue = "-1.0") double sourceLatitude,
@RequestParam(required = false, defaultValue = "-1.0") double sourceLongitude,
@RequestParam(required = false, defaultValue = "-91") double sourceLatitude,
@RequestParam(required = false, defaultValue = "-181") double sourceLongitude,
@RequestParam(required = false) LocalDateTime departureDateTime,
@RequestParam(required = false, defaultValue = "2147483647") int maxWalkingDuration,
@RequestParam(required = false, defaultValue = "2147483647") int maxTransferNumber,
@RequestParam(required = false, defaultValue = "2147483647") int maxTravelTime,
@RequestParam(required = false, defaultValue = "0") int minTransferTime) {

GeoCoordinate sourceCoordinate = null;
if (sourceStopId == null) {
if (sourceLatitude < 0 || sourceLongitude < 0) {
if (sourceLatitude == -91.0 || sourceLongitude == -181.0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Either sourceStopId or sourceLatitude and sourceLongitude must be provided.");
}
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED,
"Location based routing is not implemented yet.");
sourceCoordinate = validateCoordinate(sourceLatitude, sourceLongitude);
}

Stop sourceStop = getStop(sourceStopId);
Stop sourceStop = sourceStopId != null ? getStop(sourceStopId) : null;

validateQueryParams(maxWalkingDuration, maxTransferNumber, maxTravelTime, minTransferTime);
ConnectionQueryConfig config = new ConnectionQueryConfig(maxWalkingDuration, minTransferTime, maxTransferNumber,
maxTravelTime);

Map<Stop, ch.naviqore.service.Connection> connections = service.getIsolines(sourceStop, departureDateTime,
config);
Map<Stop, ch.naviqore.service.Connection> connections;
if (sourceStop != null) {
connections = service.getIsolines(sourceStop, departureDateTime, config);
} else {
connections = service.getIsolines(sourceCoordinate, departureDateTime, config);
}

List<EarliestArrival> arrivals = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public List<DistanceToStop> getNearestStops(@RequestParam double latitude, @Requ
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Max distance can not be negative");
}

return service.getNearestStops(new GeoCoordinate(latitude, longitude), maxDistance, limit)
GeoCoordinate location;
try {
location = new GeoCoordinate(latitude, longitude);
} catch (IllegalArgumentException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage(), e);
}

return service.getNearestStops(location, maxDistance, limit)
.stream()
.map(stop -> map(stop, latitude, longitude))
.toList();
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/ch/naviqore/example/Container.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/ch/naviqore/example/Dummy.java

This file was deleted.

Loading

0 comments on commit 92c332f

Please sign in to comment.