Skip to content

Commit 747475f

Browse files
tests: Add user_speed_limits test
adds parameter and result tests for custom speeds Co-authored-by: koebi <[email protected]>
1 parent 357fe82 commit 747475f

File tree

2 files changed

+361
-0
lines changed

2 files changed

+361
-0
lines changed

openrouteservice-api-tests/src/test/java/org/heigit/ors/v2/services/routing/ParamsTest.java

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,4 +1828,215 @@ public void expectNoErrorOnSingleRadiusForMultipleCoordinates() {
18281828
.body("any { it.key == 'routes' }", is(true))
18291829
.statusCode(200);
18301830
}
1831+
// when given a user speed limit on a surface property,
1832+
// a route should be found and the user speed limit should be present in at least the returned query.
1833+
@Test
1834+
public void expectPassOnSurfaceSpeed() {
1835+
JSONObject userSpeedLimits = new JSONObject();
1836+
JSONObject surfaceSpeedLimits = new JSONObject();
1837+
surfaceSpeedLimits.put("gravel", 80);
1838+
userSpeedLimits.put("roadSpeeds", surfaceSpeedLimits);
1839+
1840+
JSONObject body = new JSONObject();
1841+
body.put("coordinates", getParameter("coordinatesShort"));
1842+
body.put("user_speed_limits", userSpeedLimits);
1843+
1844+
given()
1845+
.header("Accept", "application/json")
1846+
.header("Content-Type", "application/json")
1847+
.pathParam("profile", getParameter("profile"))
1848+
.body(body.toString())
1849+
.when()
1850+
.post(getEndPointPath() + "/{profile}")
1851+
.then()
1852+
.assertThat()
1853+
.body("any { it.key == 'routes' }", is(true))
1854+
.body("metadata.query.containsKey('user_speed_limits')", is(true))
1855+
.statusCode(200);
1856+
}
1857+
1858+
// when given a user speed limit on a road property,
1859+
// a route should be found and the user speed limit should be present in at least the returned query.
1860+
@Test
1861+
public void expectPassOnRoadSpeed() {
1862+
JSONObject userSpeedLimits = new JSONObject();
1863+
JSONObject roadSpeedLimits = new JSONObject();
1864+
roadSpeedLimits.put("motorway", 80);
1865+
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
1866+
1867+
JSONObject body = new JSONObject();
1868+
body.put("coordinates", getParameter("coordinatesShort"));
1869+
body.put("user_speed_limits", userSpeedLimits);
1870+
1871+
given()
1872+
.header("Accept", "application/json")
1873+
.header("Content-Type", "application/json")
1874+
.pathParam("profile", getParameter("profile"))
1875+
.body(body.toString())
1876+
.when()
1877+
.post(getEndPointPath() + "/{profile}")
1878+
.then()
1879+
.assertThat()
1880+
.body("any { it.key == 'routes' }", is(true))
1881+
.body("metadata.query.containsKey('user_speed_limits')", is(true))
1882+
.statusCode(200);
1883+
}
1884+
1885+
// when given a user speed limit and a custom unit,
1886+
// a route should be found and the user speed limit should be present in at least the returned query.
1887+
@Test
1888+
public void expectPassOnUserUnit() {
1889+
JSONObject userSpeedLimits = new JSONObject();
1890+
JSONObject roadSpeedLimits = new JSONObject();
1891+
roadSpeedLimits.put("motorway", 55);
1892+
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
1893+
userSpeedLimits.put("unit", "mph");
1894+
1895+
JSONObject body = new JSONObject();
1896+
body.put("coordinates", getParameter("coordinatesShort"));
1897+
body.put("user_speed_limits", userSpeedLimits);
1898+
1899+
given()
1900+
.header("Accept", "application/json")
1901+
.header("Content-Type", "application/json")
1902+
.pathParam("profile", getParameter("profile"))
1903+
.body(body.toString())
1904+
.when()
1905+
.post(getEndPointPath() + "/{profile}")
1906+
.then()
1907+
.assertThat()
1908+
.body("any { it.key == 'routes' }", is(true))
1909+
.body("metadata.query.containsKey('user_speed_limits')", is(true))
1910+
.body("metadata.query.user_speed_limits.containsKey('unit')", is(true))
1911+
.body("metadata.query.user_speed_limits.unit", is("mph"))
1912+
.statusCode(200);
1913+
}
1914+
1915+
@Test
1916+
public void expect2012OnUnknownKey() {
1917+
JSONObject userSpeedLimits = new JSONObject();
1918+
JSONObject roadSpeedLimits = new JSONObject();
1919+
roadSpeedLimits.put("primary", 80);
1920+
userSpeedLimits.put("unknownKey", roadSpeedLimits);
1921+
1922+
JSONObject body = new JSONObject();
1923+
body.put("coordinates", getParameter("coordinatesShort"));
1924+
body.put("user_speed_limits", userSpeedLimits);
1925+
1926+
given()
1927+
.header("Accept", "application/json")
1928+
.header("Content-Type", "application/json")
1929+
.pathParam("profile", getParameter("profile"))
1930+
.body(body.toString())
1931+
.when()
1932+
.post(getEndPointPath() + "/{profile}")
1933+
.then()
1934+
.assertThat()
1935+
.body("any { it.key == 'routes' }", is(false))
1936+
.body("error.code", is(2012))
1937+
.statusCode(400);
1938+
}
1939+
1940+
1941+
// when given a non-supported road type, an error should appear
1942+
@Test
1943+
public void expect2012onUnknownRoadType() {
1944+
JSONObject userSpeedLimits = new JSONObject();
1945+
JSONObject roadSpeedLimits = new JSONObject();
1946+
roadSpeedLimits.put("unknownProperty", 80);
1947+
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
1948+
1949+
JSONObject body = new JSONObject();
1950+
body.put("coordinates", getParameter("coordinatesShort"));
1951+
body.put("user_speed_limits", userSpeedLimits);
1952+
1953+
given()
1954+
.header("Accept", "application/json")
1955+
.header("Content-Type", "application/json")
1956+
.pathParam("profile", getParameter("profile"))
1957+
.body(body.toString())
1958+
.when()
1959+
.post(getEndPointPath() + "/{profile}")
1960+
.then()
1961+
.assertThat()
1962+
.body("any { it.key == 'routes' }", is(false))
1963+
.body("error.code", is(2012))
1964+
.statusCode(400);
1965+
}
1966+
1967+
// when given a non-supported surface type, an error should appear
1968+
@Test
1969+
public void expect2012onUnknownSurfaceType() {
1970+
JSONObject userSpeedLimits = new JSONObject();
1971+
JSONObject surfaceSpeedLimits = new JSONObject();
1972+
surfaceSpeedLimits.put("unknownProperty", 80);
1973+
userSpeedLimits.put("surfaceSpeeds", surfaceSpeedLimits);
1974+
1975+
JSONObject body = new JSONObject();
1976+
body.put("coordinates", getParameter("coordinatesShort"));
1977+
body.put("user_speed_limits", userSpeedLimits);
1978+
1979+
given()
1980+
.header("Accept", "application/json")
1981+
.header("Content-Type", "application/json")
1982+
.pathParam("profile", getParameter("profile"))
1983+
.body(body.toString())
1984+
.when()
1985+
.post(getEndPointPath() + "/{profile}")
1986+
.then()
1987+
.assertThat()
1988+
.body("any { it.key == 'routes' }", is(false))
1989+
.body("error.code", is(2012))
1990+
.statusCode(400);
1991+
}
1992+
1993+
// when given an invalid speed, an error should appear
1994+
@Test
1995+
public void expect2003onInvalidSpeed() {
1996+
JSONObject userSpeedLimits = new JSONObject();
1997+
JSONObject surfaceSpeedLimits = new JSONObject();
1998+
surfaceSpeedLimits.put("gravel", -80);
1999+
userSpeedLimits.put("surfaceSpeeds", surfaceSpeedLimits);
2000+
2001+
JSONObject body = new JSONObject();
2002+
body.put("coordinates", getParameter("coordinatesShort"));
2003+
body.put("user_speed_limits", userSpeedLimits);
2004+
2005+
given()
2006+
.header("Accept", "application/json")
2007+
.header("Content-Type", "application/json")
2008+
.pathParam("profile", getParameter("profile"))
2009+
.body(body.toString())
2010+
.when()
2011+
.post(getEndPointPath() + "/{profile}")
2012+
.then()
2013+
.assertThat()
2014+
.body("any { it.key == 'routes' }", is(false))
2015+
.body("error.code", is(2003))
2016+
.statusCode(400);
2017+
}
2018+
2019+
// when given an unknown unit, an error should appear
2020+
@Test
2021+
public void expect2003onUnknownUnit(){
2022+
JSONObject userSpeedLimits = new JSONObject();
2023+
userSpeedLimits.put("unit", "unknownUnit");
2024+
2025+
JSONObject body = new JSONObject();
2026+
body.put("coordinates", getParameter("coordinatesShort"));
2027+
body.put("user_speed_limits", userSpeedLimits);
2028+
2029+
given()
2030+
.header("Accept", "application/json")
2031+
.header("Content-Type", "application/json")
2032+
.pathParam("profile", getParameter("profile"))
2033+
.body(body.toString())
2034+
.when()
2035+
.post(getEndPointPath() + "/{profile}")
2036+
.then()
2037+
.assertThat()
2038+
.body("any { it.key == 'routes' }", is(false))
2039+
.body("error.code", is(2003))
2040+
.statusCode(400);
2041+
}
18312042
}

openrouteservice-api-tests/src/test/java/org/heigit/ors/v2/services/routing/ResultTest.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,6 +3710,156 @@ public void expectMaxpeedHgvForward() {
37103710
.statusCode(200);
37113711
}
37123712

3713+
@Test
3714+
public void testUserRoadSpeed() {
3715+
JSONObject body = new JSONObject();
3716+
JSONArray coords = new JSONArray();
3717+
JSONArray neuenheim = new JSONArray();
3718+
neuenheim.put(8.685036);
3719+
neuenheim.put(49.4201314);
3720+
JSONArray dossenheim = new JSONArray();
3721+
dossenheim.put(8.671297);
3722+
dossenheim.put(49.4436);
3723+
coords.put(neuenheim);
3724+
coords.put(dossenheim);
3725+
body.put("coordinates", coords);
3726+
3727+
// since we're testing on the same profile, "shortest" would not be dependent on speed settings
3728+
// and "recommended" will make too many assumptions on what route could be preferred.
3729+
body.put("preference", "fastest");
3730+
3731+
// request route without specifying user Speed
3732+
given()
3733+
.header("Accept", "application/json")
3734+
.header("Content-Type", "application/json")
3735+
.pathParam("profile", getParameter("carProfile"))
3736+
.body(body.toString())
3737+
.when().log().ifValidationFails()
3738+
.post(getEndPointPath() + "/{profile}")
3739+
.then().log().ifValidationFails()
3740+
.assertThat()
3741+
.body("any { it.key == 'routes' }", is(true))
3742+
.body("routes[0].summary.distance", is(3822.6f))
3743+
.body("routes[0].summary.duration", is(550.0f))
3744+
.statusCode(200);
3745+
3746+
JSONObject userSpeedLimits = new JSONObject();
3747+
JSONObject roadSpeedLimits = new JSONObject();
3748+
roadSpeedLimits.put("primary", 30);
3749+
roadSpeedLimits.put("secondary", 30);
3750+
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
3751+
body.put("user_speed_limits", userSpeedLimits);
3752+
3753+
// request route limiting speed on primary and secondary roads to 30
3754+
given()
3755+
.header("Accept", "application/json")
3756+
.header("Content-Type", "application/json")
3757+
.pathParam("profile", getParameter("carProfile"))
3758+
.body(body.toString())
3759+
.when().log().ifValidationFails()
3760+
.post(getEndPointPath() + "/{profile}")
3761+
.then().log().ifValidationFails()
3762+
.assertThat()
3763+
.body("any { it.key == 'routes' }", is(true))
3764+
.body("routes[0].summary.distance", is(3958.0f))
3765+
.body("routes[0].summary.duration", is(625.7f))
3766+
.statusCode(200);
3767+
}
3768+
3769+
@Test
3770+
public void testUserSurfaceSpeed() {
3771+
JSONObject body = new JSONObject();
3772+
JSONArray coords = new JSONArray();
3773+
JSONArray south = new JSONArray();
3774+
south.put(8.707808);
3775+
south.put(49.398337);
3776+
JSONArray north = new JSONArray();
3777+
north.put(8.710012);
3778+
north.put(49.405015);
3779+
coords.put(south);
3780+
coords.put(north);
3781+
body.put("coordinates", coords);
3782+
3783+
// use "shortest" as "recommended" will make too many assumptions on what route could be preferred.
3784+
body.put("preference", "shortest");
3785+
3786+
// request route without specifying user Speed
3787+
given()
3788+
.header("Accept", "application/json")
3789+
.header("Content-Type", "application/json")
3790+
.pathParam("profile", "cycling-mountain")
3791+
.body(body.toString())
3792+
.when().log().ifValidationFails()
3793+
.post(getEndPointPath() + "/{profile}")
3794+
.then().log().ifValidationFails()
3795+
.assertThat()
3796+
.body("any { it.key == 'routes' }", is(true))
3797+
.body("routes[0].summary.distance", is(1529.7f))
3798+
.body("routes[0].summary.duration", is(349.2f))
3799+
.statusCode(200);
3800+
3801+
JSONObject userSpeedLimits = new JSONObject();
3802+
JSONObject surfaceSpeedLimits = new JSONObject();
3803+
surfaceSpeedLimits.put("gravel", 2);
3804+
surfaceSpeedLimits.put("ground", 2);
3805+
surfaceSpeedLimits.put("compacted", 2);
3806+
userSpeedLimits.put("surfaceSpeeds", surfaceSpeedLimits);
3807+
body.put("user_speed_limits", userSpeedLimits);
3808+
3809+
// with modified speeds travel time should increase while the distance is expected to stay the same
3810+
given()
3811+
.header("Accept", "application/json")
3812+
.header("Content-Type", "application/json")
3813+
.pathParam("profile", "cycling-mountain")
3814+
.body(body.toString())
3815+
.when().log().ifValidationFails()
3816+
.post(getEndPointPath() + "/{profile}")
3817+
.then().log().ifValidationFails()
3818+
.assertThat()
3819+
.body("any { it.key == 'routes' }", is(true))
3820+
.body("routes[0].summary.distance", is(1529.7f))
3821+
.body("routes[0].summary.duration", is(greaterThan(349.2f)))
3822+
.statusCode(200);
3823+
}
3824+
3825+
@Test
3826+
public void testUserSpeedUnit() {
3827+
JSONObject body = new JSONObject();
3828+
JSONArray coords = new JSONArray();
3829+
JSONArray neuenheim = new JSONArray();
3830+
neuenheim.put(8.685036);
3831+
neuenheim.put(49.4201314);
3832+
JSONArray dossenheim = new JSONArray();
3833+
dossenheim.put(8.671297);
3834+
dossenheim.put(49.4436);
3835+
coords.put(neuenheim);
3836+
coords.put(dossenheim);
3837+
body.put("coordinates", coords);
3838+
3839+
// this is the same query as testUserRoadSpeed uses, but has speeds in mph
3840+
JSONObject userSpeedLimits = new JSONObject();
3841+
JSONObject roadSpeedLimits = new JSONObject();
3842+
roadSpeedLimits.put("primary", 18.6412f);
3843+
roadSpeedLimits.put("secondary", 18.6412f);
3844+
userSpeedLimits.put("roadSpeeds", roadSpeedLimits);
3845+
userSpeedLimits.put("unit", "mph");
3846+
body.put("user_speed_limits", userSpeedLimits);
3847+
3848+
given()
3849+
.header("Accept", "application/json")
3850+
.header("Content-Type", "application/json")
3851+
.pathParam("profile", getParameter("carProfile"))
3852+
.body(body.toString())
3853+
.when().log().ifValidationFails()
3854+
.post(getEndPointPath() + "/{profile}")
3855+
.then().log().ifValidationFails()
3856+
.assertThat()
3857+
.body("any { it.key == 'routes' }", is(true))
3858+
.body("routes[0].summary.distance", is(3958.0f))
3859+
.body("routes[0].summary.duration", is(625.7f))
3860+
.statusCode(200);
3861+
}
3862+
37133863
private JSONArray constructBearings(String coordString) {
37143864
JSONArray coordinates = new JSONArray();
37153865
String[] coordPairs = coordString.split("\\|");

0 commit comments

Comments
 (0)