Skip to content

Commit

Permalink
Merge pull request #360 from GIScience/bugfix-#291
Browse files Browse the repository at this point in the history
Bugfix #291
  • Loading branch information
Adam Rousell authored Nov 22, 2018
2 parents 973daa3 + eec4fb6 commit 1fb9018
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Added Unit Tests for RouteSearchParameters.class() (while fixing Issue #291)
### Fixed
- Added a new ParameterValueException in RouteSearchParameters if the profile is driving-car and profile_params are set in the options (Issue #291)
- Fixed API Test to consider the new ParameterValueException (while fixing Issue #291)
### Changed
### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public void testGeoJsonExport(){
}

@Test
public void expectCarToRejectBikeParams() {
public void expectCarToRejectProfileParams() {

// options for cycling profiles
JSONObject options = new JSONObject();
Expand All @@ -428,7 +428,7 @@ public void expectCarToRejectBikeParams() {
.get(getEndPointName())
.then()
.assertThat()
.statusCode(200);
.statusCode(400);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@
*/
package heigit.ors.routing;

import java.text.ParseException;
import java.util.Iterator;

import heigit.ors.routing.pathprocessors.BordersExtractor;
import org.json.JSONArray;
import org.json.JSONObject;

import com.graphhopper.util.Helper;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;

import heigit.ors.exceptions.ParameterValueException;
import heigit.ors.exceptions.UnknownParameterValueException;
import heigit.ors.geojson.GeometryJSON;
import heigit.ors.routing.graphhopper.extensions.HeavyVehicleAttributes;
import heigit.ors.routing.graphhopper.extensions.VehicleLoadCharacteristicsFlags;
import heigit.ors.routing.graphhopper.extensions.WheelchairTypesEncoder;
import heigit.ors.routing.parameters.*;
import heigit.ors.routing.parameters.CyclingParameters;
import heigit.ors.routing.parameters.ProfileParameters;
import heigit.ors.routing.parameters.VehicleParameters;
import heigit.ors.routing.parameters.WalkingParameters;
import heigit.ors.routing.parameters.WheelchairParameters;
import heigit.ors.routing.pathprocessors.BordersExtractor;
import heigit.ors.util.StringUtility;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.Iterator;

/**
* This class is used to store the search/calculation Parameters to calculate the desired Route/Isochrones etc…
Expand Down Expand Up @@ -255,29 +256,17 @@ public void setOptions(String options) throws Exception {
}
}

if (json.has("profile_params")) {
if (json.has("profile_params") && _profileType == RoutingProfileType.DRIVING_CAR) {
throw new ParameterValueException(RoutingErrorCodes.INVALID_PARAMETER_VALUE, "profile_params");
} else if (json.has("profile_params")) {
JSONObject jProfileParams = json.getJSONObject("profile_params");
JSONObject jRestrictions = null;

if (jProfileParams.has("restrictions"))
jRestrictions = jProfileParams.getJSONObject("restrictions");

if (RoutingProfileType.isCycling(_profileType)) {
CyclingParameters cyclingParams = new CyclingParameters();

// To make the new API compatible with a new one, we create 'weightings' element.
/*if (!jProfileParams.has("weightings") && (jProfileParams.has("difficulty_level") || jProfileParams.has("maximum_gradient")))
{
JSONObject jWeightings = new JSONObject();
if (jProfileParams.has("difficulty_level"))
jWeightings.put("difficulty_level", jProfileParams.get("difficulty_level"));
else if (jProfileParams.has("maximum_gradient"))
jWeightings.put("maximum_gradient", jProfileParams.get("maximum_gradient"));
jProfileParams.put("weightings", jWeightings);
}*/

if (jRestrictions != null) {
if (jRestrictions.has("gradient"))
cyclingParams.setMaximumGradient(jRestrictions.getInt("gradient"));
Expand Down Expand Up @@ -312,7 +301,7 @@ else if (jProfileParams.has("maximum_gradient"))
}

_profileParams = walkingParams;
} else if (RoutingProfileType.isHeavyVehicle(_profileType) == true) {
} else if (RoutingProfileType.isHeavyVehicle(_profileType)) {
VehicleParameters vehicleParams = new VehicleParameters();


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package heigit.ors.routing;

import com.vividsolutions.jts.geom.Polygon;
import heigit.ors.exceptions.ParameterValueException;
import heigit.ors.routing.graphhopper.extensions.HeavyVehicleAttributes;
import heigit.ors.routing.parameters.VehicleParameters;
import heigit.ors.routing.pathprocessors.BordersExtractor;
import org.junit.Assert;
import org.junit.Test;

public class RouteSearchParametersTest {

@Test(expected = ParameterValueException.class)
public void expectFailingProfileParamsWithVehicleProfile() throws Exception {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setProfileType(1);
routeSearchParameters.setOptions("{\"profile_params\":{\"weightings\":{\"green\":{\"factor\":0.8}}}}");
}

@Test
public void getProfileType() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertEquals(0, routeSearchParameters.getProfileType());
}

@Test
public void setProfileType() throws Exception {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setProfileType(2);
Assert.assertEquals(2, routeSearchParameters.getProfileType());
}

@Test
public void getMaximumSpeed() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertEquals(-1.0, routeSearchParameters.getMaximumSpeed(), 0.0);
}

@Test
public void setMaximumSpeed() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setMaximumSpeed(2.0);
Assert.assertEquals(2.0, routeSearchParameters.getMaximumSpeed(), 0.0);
}

@Test
public void getWeightingMethod() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertEquals(WeightingMethod.FASTEST, routeSearchParameters.getWeightingMethod(), 0.0);
}

@Test
public void setWeightingMethod() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setWeightingMethod(WeightingMethod.RECOMMENDED);
Assert.assertEquals(WeightingMethod.RECOMMENDED, routeSearchParameters.getWeightingMethod(), 0.0);
}

@Test
public void getConsiderTraffic() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.getConsiderTraffic());
}

@Test
public void setConsiderTraffic() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setConsiderTraffic(true);
Assert.assertTrue(routeSearchParameters.getConsiderTraffic());
}

@Test
public void getAvoidAreas() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertArrayEquals(null, routeSearchParameters.getAvoidAreas());
}

@Test
public void setAvoidAreas() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setAvoidAreas(new Polygon[0]);
Assert.assertArrayEquals(new Polygon[0], routeSearchParameters.getAvoidAreas());
}

@Test
public void hasAvoidAreas() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.hasAvoidAreas());
routeSearchParameters.setAvoidAreas(new Polygon[1]);
Assert.assertTrue(routeSearchParameters.hasAvoidAreas());
}

@Test
public void getAvoidFeatureTypes() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertEquals(0, routeSearchParameters.getAvoidFeatureTypes());

}

@Test
public void setAvoidFeatureTypes() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setAvoidFeatureTypes(1);
Assert.assertEquals(1, routeSearchParameters.getAvoidFeatureTypes());
}

@Test
public void hasAvoidFeatures() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.hasAvoidFeatures());
routeSearchParameters.setAvoidFeatureTypes(1);
Assert.assertTrue(routeSearchParameters.hasAvoidFeatures());
}

@Test
public void getAvoidCountries() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertNull(routeSearchParameters.getAvoidCountries());
}

@Test
public void setAvoidCountries() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setAvoidCountries(new int[1]);
Assert.assertArrayEquals(new int[1], routeSearchParameters.getAvoidCountries());
}

@Test
public void hasAvoidCountries() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.hasAvoidCountries());
routeSearchParameters.setAvoidCountries(new int[1]);
Assert.assertTrue(routeSearchParameters.hasAvoidCountries());
}

@Test
public void hasAvoidBorders() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.hasAvoidBorders());
routeSearchParameters.setAvoidBorders(BordersExtractor.Avoid.CONTROLLED);
Assert.assertTrue(routeSearchParameters.hasAvoidBorders());
}

@Test
public void setAvoidBorders() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setAvoidBorders(BordersExtractor.Avoid.CONTROLLED);
Assert.assertEquals(BordersExtractor.Avoid.CONTROLLED, routeSearchParameters.getAvoidBorders());
}

@Test
public void getAvoidBorders() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertEquals(BordersExtractor.Avoid.NONE, routeSearchParameters.getAvoidBorders());
}

@Test
public void getConsiderTurnRestrictions() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.getConsiderTurnRestrictions());
}

@Test
public void setConsiderTurnRestrictions() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setConsiderTurnRestrictions(true);
Assert.assertTrue(routeSearchParameters.getConsiderTurnRestrictions());
}

@Test
public void getVehicleType() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertEquals(HeavyVehicleAttributes.UNKNOWN, routeSearchParameters.getVehicleType());
}

@Test
public void setVehicleType() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setVehicleType(HeavyVehicleAttributes.AGRICULTURE);
Assert.assertEquals(HeavyVehicleAttributes.AGRICULTURE, routeSearchParameters.getVehicleType());

}

@Test
public void getOptions() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertNull(routeSearchParameters.getOptions());
}

@Test
public void setOptions() throws Exception {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setOptions("{\"maximum_speed\": 10}");
Assert.assertEquals("{\"maximum_speed\": 10}", routeSearchParameters.getOptions());
}

@Test
public void hasParameters() throws Exception {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.hasParameters(routeSearchParameters.getClass()));
routeSearchParameters.setProfileType(2);
routeSearchParameters.setOptions("{\"profile_params\":{\"weightings\":{\"green\":{\"factor\":0.8}}}}");
Assert.assertTrue(routeSearchParameters.hasParameters(VehicleParameters.class));
}

@Test
public void getProfileParameters() throws Exception {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertNull(routeSearchParameters.getProfileParameters());
}

@Test
public void getFlexibleMode() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertFalse(routeSearchParameters.getFlexibleMode());
}

@Test
public void setFlexibleMode() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setFlexibleMode(true);
Assert.assertTrue(routeSearchParameters.getFlexibleMode());
}

@Test
public void getMaximumRadiuses() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertNull(routeSearchParameters.getMaximumRadiuses());
}

@Test
public void setMaximumRadiuses() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setMaximumRadiuses(new double[0]);
Assert.assertNotNull(routeSearchParameters.getMaximumRadiuses());
Assert.assertSame(routeSearchParameters.getMaximumRadiuses().getClass(), double[].class);
Assert.assertEquals(0, routeSearchParameters.getMaximumRadiuses().length);
}

@Test
public void getBearings() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
Assert.assertNull(routeSearchParameters.getBearings());
}

@Test
public void setBearings() {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setBearings(new WayPointBearing[]{});
Assert.assertArrayEquals(new WayPointBearing[]{}, routeSearchParameters.getBearings());
}
}

0 comments on commit 1fb9018

Please sign in to comment.