Skip to content

Commit

Permalink
tests: Add user_speed_limits test
Browse files Browse the repository at this point in the history
adds parameter and result tests for custom speeds

Co-authored-by: koebi <[email protected]>
  • Loading branch information
TheGreatRefrigerator and koebi committed Nov 8, 2022
1 parent 357fe82 commit 747475f
Show file tree
Hide file tree
Showing 2 changed files with 361 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1828,4 +1828,215 @@ public void expectNoErrorOnSingleRadiusForMultipleCoordinates() {
.body("any { it.key == 'routes' }", is(true))
.statusCode(200);
}
// when given a user speed limit on a surface property,
// a route should be found and the user speed limit should be present in at least the returned query.
@Test
public void expectPassOnSurfaceSpeed() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject surfaceSpeedLimits = new JSONObject();
surfaceSpeedLimits.put("gravel", 80);
userSpeedLimits.put("roadSpeeds", surfaceSpeedLimits);

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("metadata.query.containsKey('user_speed_limits')", is(true))
.statusCode(200);
}

// when given a user speed limit on a road property,
// a route should be found and the user speed limit should be present in at least the returned query.
@Test
public void expectPassOnRoadSpeed() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject roadSpeedLimits = new JSONObject();
roadSpeedLimits.put("motorway", 80);
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("metadata.query.containsKey('user_speed_limits')", is(true))
.statusCode(200);
}

// when given a user speed limit and a custom unit,
// a route should be found and the user speed limit should be present in at least the returned query.
@Test
public void expectPassOnUserUnit() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject roadSpeedLimits = new JSONObject();
roadSpeedLimits.put("motorway", 55);
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
userSpeedLimits.put("unit", "mph");

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("metadata.query.containsKey('user_speed_limits')", is(true))
.body("metadata.query.user_speed_limits.containsKey('unit')", is(true))
.body("metadata.query.user_speed_limits.unit", is("mph"))
.statusCode(200);
}

@Test
public void expect2012OnUnknownKey() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject roadSpeedLimits = new JSONObject();
roadSpeedLimits.put("primary", 80);
userSpeedLimits.put("unknownKey", roadSpeedLimits);

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(false))
.body("error.code", is(2012))
.statusCode(400);
}


// when given a non-supported road type, an error should appear
@Test
public void expect2012onUnknownRoadType() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject roadSpeedLimits = new JSONObject();
roadSpeedLimits.put("unknownProperty", 80);
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(false))
.body("error.code", is(2012))
.statusCode(400);
}

// when given a non-supported surface type, an error should appear
@Test
public void expect2012onUnknownSurfaceType() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject surfaceSpeedLimits = new JSONObject();
surfaceSpeedLimits.put("unknownProperty", 80);
userSpeedLimits.put("surfaceSpeeds", surfaceSpeedLimits);

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(false))
.body("error.code", is(2012))
.statusCode(400);
}

// when given an invalid speed, an error should appear
@Test
public void expect2003onInvalidSpeed() {
JSONObject userSpeedLimits = new JSONObject();
JSONObject surfaceSpeedLimits = new JSONObject();
surfaceSpeedLimits.put("gravel", -80);
userSpeedLimits.put("surfaceSpeeds", surfaceSpeedLimits);

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(false))
.body("error.code", is(2003))
.statusCode(400);
}

// when given an unknown unit, an error should appear
@Test
public void expect2003onUnknownUnit(){
JSONObject userSpeedLimits = new JSONObject();
userSpeedLimits.put("unit", "unknownUnit");

JSONObject body = new JSONObject();
body.put("coordinates", getParameter("coordinatesShort"));
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("profile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(false))
.body("error.code", is(2003))
.statusCode(400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3710,6 +3710,156 @@ public void expectMaxpeedHgvForward() {
.statusCode(200);
}

@Test
public void testUserRoadSpeed() {
JSONObject body = new JSONObject();
JSONArray coords = new JSONArray();
JSONArray neuenheim = new JSONArray();
neuenheim.put(8.685036);
neuenheim.put(49.4201314);
JSONArray dossenheim = new JSONArray();
dossenheim.put(8.671297);
dossenheim.put(49.4436);
coords.put(neuenheim);
coords.put(dossenheim);
body.put("coordinates", coords);

// since we're testing on the same profile, "shortest" would not be dependent on speed settings
// and "recommended" will make too many assumptions on what route could be preferred.
body.put("preference", "fastest");

// request route without specifying user Speed
given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("carProfile"))
.body(body.toString())
.when().log().ifValidationFails()
.post(getEndPointPath() + "/{profile}")
.then().log().ifValidationFails()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].summary.distance", is(3822.6f))
.body("routes[0].summary.duration", is(550.0f))
.statusCode(200);

JSONObject userSpeedLimits = new JSONObject();
JSONObject roadSpeedLimits = new JSONObject();
roadSpeedLimits.put("primary", 30);
roadSpeedLimits.put("secondary", 30);
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
body.put("user_speed_limits", userSpeedLimits);

// request route limiting speed on primary and secondary roads to 30
given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("carProfile"))
.body(body.toString())
.when().log().ifValidationFails()
.post(getEndPointPath() + "/{profile}")
.then().log().ifValidationFails()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].summary.distance", is(3958.0f))
.body("routes[0].summary.duration", is(625.7f))
.statusCode(200);
}

@Test
public void testUserSurfaceSpeed() {
JSONObject body = new JSONObject();
JSONArray coords = new JSONArray();
JSONArray south = new JSONArray();
south.put(8.707808);
south.put(49.398337);
JSONArray north = new JSONArray();
north.put(8.710012);
north.put(49.405015);
coords.put(south);
coords.put(north);
body.put("coordinates", coords);

// use "shortest" as "recommended" will make too many assumptions on what route could be preferred.
body.put("preference", "shortest");

// request route without specifying user Speed
given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", "cycling-mountain")
.body(body.toString())
.when().log().ifValidationFails()
.post(getEndPointPath() + "/{profile}")
.then().log().ifValidationFails()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].summary.distance", is(1529.7f))
.body("routes[0].summary.duration", is(349.2f))
.statusCode(200);

JSONObject userSpeedLimits = new JSONObject();
JSONObject surfaceSpeedLimits = new JSONObject();
surfaceSpeedLimits.put("gravel", 2);
surfaceSpeedLimits.put("ground", 2);
surfaceSpeedLimits.put("compacted", 2);
userSpeedLimits.put("surfaceSpeeds", surfaceSpeedLimits);
body.put("user_speed_limits", userSpeedLimits);

// with modified speeds travel time should increase while the distance is expected to stay the same
given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", "cycling-mountain")
.body(body.toString())
.when().log().ifValidationFails()
.post(getEndPointPath() + "/{profile}")
.then().log().ifValidationFails()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].summary.distance", is(1529.7f))
.body("routes[0].summary.duration", is(greaterThan(349.2f)))
.statusCode(200);
}

@Test
public void testUserSpeedUnit() {
JSONObject body = new JSONObject();
JSONArray coords = new JSONArray();
JSONArray neuenheim = new JSONArray();
neuenheim.put(8.685036);
neuenheim.put(49.4201314);
JSONArray dossenheim = new JSONArray();
dossenheim.put(8.671297);
dossenheim.put(49.4436);
coords.put(neuenheim);
coords.put(dossenheim);
body.put("coordinates", coords);

// this is the same query as testUserRoadSpeed uses, but has speeds in mph
JSONObject userSpeedLimits = new JSONObject();
JSONObject roadSpeedLimits = new JSONObject();
roadSpeedLimits.put("primary", 18.6412f);
roadSpeedLimits.put("secondary", 18.6412f);
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
userSpeedLimits.put("unit", "mph");
body.put("user_speed_limits", userSpeedLimits);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("carProfile"))
.body(body.toString())
.when().log().ifValidationFails()
.post(getEndPointPath() + "/{profile}")
.then().log().ifValidationFails()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].summary.distance", is(3958.0f))
.body("routes[0].summary.duration", is(625.7f))
.statusCode(200);
}

private JSONArray constructBearings(String coordString) {
JSONArray coordinates = new JSONArray();
String[] coordPairs = coordString.split("\\|");
Expand Down

0 comments on commit 747475f

Please sign in to comment.