Skip to content

Commit

Permalink
refactor: replace RouteProfileConfiguration and get tests to work again
Browse files Browse the repository at this point in the history
  • Loading branch information
takb committed Jul 29, 2024
1 parent 83e1617 commit e1964a6
Show file tree
Hide file tree
Showing 46 changed files with 580 additions and 1,361 deletions.
28 changes: 14 additions & 14 deletions ors-api/src/main/java/org/heigit/ors/api/controllers/StatusAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import jakarta.servlet.http.HttpServletRequest;
import org.heigit.ors.api.config.EndpointsProperties;
import org.heigit.ors.api.util.AppInfo;
import org.heigit.ors.config.profile.ProfileProperties;
import org.heigit.ors.localization.LocalizationManager;
import org.heigit.ors.routing.RoutingProfile;
import org.heigit.ors.routing.RoutingProfileManager;
import org.heigit.ors.routing.RoutingProfileManagerStatus;
import org.heigit.ors.routing.configuration.RouteProfileConfiguration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -79,30 +79,30 @@ public ResponseEntity fetchHealth(HttpServletRequest request) throws Exception {
int i = 1;

for (RoutingProfile rp : profileManager.getProfiles().getUniqueProfiles()) {
RouteProfileConfiguration rpc = rp.getConfiguration();
ProfileProperties profile = rp.getProfileConfiguration();
org.json.JSONObject jProfileProps = new org.json.JSONObject(true);

jProfileProps.put("profiles", rpc.getProfiles());
jProfileProps.put("profiles", profile.getEncoderName());
StorableProperties storageProps = rp.getGraphProperties();
jProfileProps.put("creation_date", storageProps.get("osmreader.import.date"));

if (rpc.getExtStorages() != null && rpc.getExtStorages().size() > 0)
jProfileProps.put("storages", rpc.getExtStorages());
if (profile.getExtStorages() != null && !profile.getExtStorages().isEmpty())
jProfileProps.put("storages", profile.getExtStorages());

org.json.JSONObject jProfileLimits = new org.json.JSONObject(true);
if (rpc.getMaximumDistance() > 0)
jProfileLimits.put("maximum_distance", rpc.getMaximumDistance());
if (profile.getMaximumDistance() != null)
jProfileLimits.put("maximum_distance", profile.getMaximumDistance());

if (rpc.getMaximumDistanceDynamicWeights() > 0)
jProfileLimits.put("maximum_distance_dynamic_weights", rpc.getMaximumDistanceDynamicWeights());
if (profile.getMaximumDistanceDynamicWeights() != null)
jProfileLimits.put("maximum_distance_dynamic_weights", profile.getMaximumDistanceDynamicWeights());

if (rpc.getMaximumDistanceAvoidAreas() > 0)
jProfileLimits.put("maximum_distance_avoid_areas", rpc.getMaximumDistanceAvoidAreas());
if (profile.getMaximumDistanceAvoidAreas() != null)
jProfileLimits.put("maximum_distance_avoid_areas", profile.getMaximumDistanceAvoidAreas());

if (rpc.getMaximumWayPoints() > 0)
jProfileLimits.put("maximum_waypoints", rpc.getMaximumWayPoints());
if (profile.getMaximumWayPoints() != null)
jProfileLimits.put("maximum_waypoints", profile.getMaximumWayPoints());

if (jProfileLimits.length() > 0)
if (!jProfileLimits.isEmpty())
jProfileProps.put("limits", jProfileLimits);

jProfiles.put("profile " + i, jProfileProps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.heigit.ors.routing.RoutingProfileManagerStatus;
import org.heigit.ors.util.FormatUtility;
import org.heigit.ors.util.StringUtility;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

import java.io.FileOutputStream;
Expand All @@ -59,6 +60,7 @@ public class ORSInitContextListener implements ServletContextListener {
private final SystemMessageProperties systemMessageProperties;
private final LoggingProperties loggingProperties;
private final ServerProperties serverProperties;
public final static String ORS_API_TESTS_FLAG = "ORS_API_TESTS_FLAG";

public ORSInitContextListener(EndpointsProperties endpointsProperties, CorsProperties corsProperties, SystemMessageProperties systemMessageProperties, LoggingProperties loggingProperties, ServerProperties serverProperties) {
this.endpointsProperties = endpointsProperties;
Expand All @@ -80,6 +82,15 @@ public void contextInitialized(ServletContextEvent contextEvent) {
return;
}
}
if (!StringUtility.isNullOrEmpty(System.getProperty(ORS_API_TESTS_FLAG))) {
try {
configFileString = new ClassPathResource("application-test.yml").getContentAsString(Charset.defaultCharset());
} catch (IOException e) {
LOGGER.error("Failed to read configuration file");
RoutingProfileManagerStatus.setShutdown(true);
return;
}
}
YAMLFactory yf = new CustomYAMLFactory()
.disable(WRITE_DOC_START_MARKER)
.disable(SPLIT_LINES)
Expand All @@ -100,7 +111,7 @@ public void contextInitialized(ServletContextEvent contextEvent) {
RoutingProfileManagerStatus.setShutdown(true);
return;
}
if (engineProperties.getConfigOutputMode()) {
if (Boolean.TRUE.equals(engineProperties.getConfigOutputMode())) {
try (FileOutputStream fos = new FileOutputStream("ors-config-example.yml"); JsonGenerator generator = mapper.createGenerator(fos)) {
LOGGER.info("Output configuration file");
ORSConfigBundle ors = new ORSConfigBundle(corsProperties, systemMessageProperties, endpointsProperties, engineProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.heigit.ors.api.servlet.listeners.ORSInitContextListener.ORS_API_TESTS_FLAG;

@Order(Integer.MIN_VALUE) // Run before even spring context has been built
public class InitializeGraphsOnce implements BeforeAllCallback {

Expand All @@ -24,6 +26,7 @@ public class InitializeGraphsOnce implements BeforeAllCallback {

@Override
public void beforeAll(ExtensionContext extensionContext) {
System.setProperty(ORS_API_TESTS_FLAG, "true");
ExtensionContext.Store store = rootStore(extensionContext);
deleteGraphsFolderOncePerTestRun(store);
SpringExtension.getApplicationContext(extensionContext);
Expand Down
26 changes: 13 additions & 13 deletions ors-api/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ors:
profiles:
car:
enabled: true
profile: driving-car
encoder_name: driving-car
encoder_options:
turn_costs: true
block_fords: false
Expand Down Expand Up @@ -77,15 +77,15 @@ ors:
use_for_warnings: true
HereTraffic:
enabled: true
streets: ./src/test/files/traffic_data/here/Streets.shp
ref_pattern: ./src/test/files/traffic_data/here/ref_pattern.csv
pattern_15min: ./src/test/files/traffic_data/here/pattern_kph_15min.csv
streets: src/test/files/traffic_data/here/Streets.shp
ref_pattern: src/test/files/traffic_data/here/ref_pattern.csv
pattern_15min: src/test/files/traffic_data/here/pattern_kph_15min.csv
radius: 150
output_log: false
log_location: ors/traffic_log
hgv:
enabled: true
profile: driving-hgv
encoder_name: driving-hgv
maximum_speed_lower_bound: 75
encoder_options:
turn_costs: true
Expand Down Expand Up @@ -139,7 +139,7 @@ ors:
log_location: ors/traffic_log
bike-regular:
enabled: true
profile: cycling-regular
encoder_name: cycling-regular
encoder_options:
consider_elevation: false
turn_costs: true
Expand All @@ -163,7 +163,7 @@ ors:
TrailDifficulty:
bike-mountain:
enabled: true
profile: cycling-mountain
encoder_name: cycling-mountain
maximum_snapping_radius: 10
encoder_options:
consider_elevation: false
Expand All @@ -176,7 +176,7 @@ ors:
TrailDifficulty:
bike-road:
enabled: true
profile: cycling-road
encoder_name: cycling-road
encoder_options:
consider_elevation: false
turn_costs: false
Expand All @@ -188,7 +188,7 @@ ors:
TrailDifficulty:
bike-electric:
enabled: true
profile: cycling-electric
encoder_name: cycling-electric
encoder_options:
consider_elevation: false
turn_costs: true
Expand All @@ -200,7 +200,7 @@ ors:
TrailDifficulty:
walking:
enabled: true
profile: foot-walking
encoder_name: foot-walking
interpolate_bridges_and_tunnels: false
encoder_options:
block_fords: false
Expand All @@ -219,7 +219,7 @@ ors:
TrailDifficulty:
hiking:
enabled: true
profile: foot-hiking
encoder_name: foot-hiking
encoder_options:
block_fords: false
ext_storages:
Expand All @@ -235,7 +235,7 @@ ors:
TrailDifficulty:
wheelchair:
enabled: true
profile: wheelchair
encoder_name: wheelchair
maximum_snapping_radius: 50
encoder_options:
block_fords: false
Expand All @@ -247,7 +247,7 @@ ors:
OsmId:
public-transport:
enabled: true
profile: public-transport
encoder_name: public-transport
encoder_options:
block_fords: false
maximum_visited_nodes: 15000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
@Getter
@Setter(AccessLevel.PACKAGE)
public class ElevationProperties {
private boolean preprocessed = false;
private Boolean preprocessed = false;
@JsonProperty("data_access")
private String dataAccess = "MMAP";
@JsonProperty("cache_clear")
private boolean cacheClear = false;
private Boolean cacheClear = false;
@JsonProperty("provider")
private String provider = "multi";
@JsonProperty("cache_path")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
import org.heigit.ors.config.profile.defaults.*;
import org.heigit.ors.config.utils.PathDeserializer;
import org.heigit.ors.config.utils.PathSerializer;
import org.heigit.ors.routing.configuration.RouteProfileConfiguration;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Getter
@Setter(AccessLevel.PACKAGE)
public class EngineProperties {

private static final Map<String, ProfileProperties> DEFAULT_PROFILES = new LinkedHashMap<>();

static {
DEFAULT_PROFILES.put("car", new CarProfileProperties());
DEFAULT_PROFILES.put("hgv", new HgvProfileProperties());
Expand All @@ -51,7 +49,7 @@ public class EngineProperties {
@JsonProperty("graphs_root_path")
@JsonDeserialize(using = PathDeserializer.class)
@JsonSerialize(using = PathSerializer.class)
private Path graphsRootPath = Paths.get("./graphs");
private Path graphsRootPath = Path.of("graphs").toAbsolutePath();
@JsonProperty("graphs_data_access")
private String graphsDataAccess = "RAM_STORE";

Expand All @@ -63,73 +61,14 @@ public class EngineProperties {
private Map<String, ProfileProperties> profiles = DEFAULT_PROFILES;

@JsonIgnore
public RouteProfileConfiguration[] getConvertedProfiles() {
List<RouteProfileConfiguration> convertedProfiles = new ArrayList<>();
// if (profiles != null) {
// for (Map.Entry<String, ProfileProperties> profileEntry : profiles.entrySet()) {
// ProfileProperties profile = profileEntry.getValue();
// boolean enabled = profile.getEnabled() != null ? profile.getEnabled() : profileDefault.getEnabled();
// if (!enabled) {
// continue;
// }
// RouteProfileConfiguration convertedProfile = new RouteProfileConfiguration();
// convertedProfile.setName(profileEntry.getKey());
// convertedProfile.setEnabled(enabled);
// convertedProfile.setProfiles(profile.getEncoderName());
// String graphPath = profile.getGraphPath();
// String rootGraphsPath = getGraphsRootPath();
// if (!Helper.isEmpty(rootGraphsPath)) {
// if (Helper.isEmpty(graphPath))
// graphPath = Paths.get(rootGraphsPath, profileEntry.getKey()).toString();
// }
// convertedProfile.setGraphPath(graphPath);
// convertedProfile.setEncoderOptions(profile.getEncoderOptionsString());
// convertedProfile.setOptimize(profile.getOptimize() != null ? profile.getOptimize() : profileDefault.getOptimize());
// convertedProfile.setEncoderFlagsSize(profile.getEncoderFlagsSize() != null ? profile.getEncoderFlagsSize() : profileDefault.getEncoderFlagsSize());
// convertedProfile.setInstructions(profile.getInstructions() != null ? profile.getInstructions() : profileDefault.getInstructions());
// convertedProfile.setMaximumDistance(profile.getMaximumDistance() != null ? profile.getMaximumDistance() : profileDefault.getMaximumDistance());
// convertedProfile.setMaximumDistanceDynamicWeights(profile.getMaximumDistanceDynamicWeights() != null ? profile.getMaximumDistanceDynamicWeights() : profileDefault.getMaximumDistanceDynamicWeights());
// convertedProfile.setMaximumDistanceAvoidAreas(profile.getMaximumDistanceAvoidAreas() != null ? profile.getMaximumDistanceAvoidAreas() : profileDefault.getMaximumDistanceAvoidAreas());
// convertedProfile.setMaximumDistanceAlternativeRoutes(profile.getMaximumDistanceAlternativeRoutes() != null ? profile.getMaximumDistanceAlternativeRoutes() : profileDefault.getMaximumDistanceAlternativeRoutes());
// convertedProfile.setMaximumDistanceRoundTripRoutes(profile.getMaximumDistanceRoundTripRoutes() != null ? profile.getMaximumDistance() : profileDefault.getMaximumDistanceRoundTripRoutes());
// convertedProfile.setMaximumSpeedLowerBound(profile.getMaximumSpeedLowerBound() != null ? profile.getMaximumSpeedLowerBound() : profileDefault.getMaximumSpeedLowerBound());
// convertedProfile.setMaximumWayPoints(profile.getMaximumWayPoints() != null ? profile.getMaximumWayPoints() : profileDefault.getMaximumWayPoints());
// convertedProfile.setMaximumSnappingRadius(profile.getMaximumSnappingRadius() != null ? profile.getMaximumSnappingRadius() : profileDefault.getMaximumSnappingRadius());
// convertedProfile.setLocationIndexResolution(profile.getLocationIndexResolution() != null ? profile.getLocationIndexResolution() : profileDefault.getLocationIndexResolution());
// convertedProfile.setLocationIndexSearchIterations(profile.getLocationIndexSearchIterations() != null ? profile.getLocationIndexSearchIterations() : profileDefault.getLocationIndexSearchIterations());
// convertedProfile.setEnforceTurnCosts(profile.getForceTurnCosts() != null ? profile.getForceTurnCosts() : profileDefault.getForceTurnCosts());
// convertedProfile.setGtfsFile(profile.getGtfsFile() != null ? profile.getGtfsFile() : "");
// convertedProfile.setMaximumVisitedNodesPT(profile.getMaximumVisitedNodes() != null ? profile.getMaximumVisitedNodes() : profileDefault.getMaximumVisitedNodes());
// if (profile.getElevation() != null && profile.getElevation() || profileDefault.getElevation()) {
// convertedProfile.setElevationProvider(elevation.getProvider());
// convertedProfile.setElevationCachePath(elevation.getCachePath());
// convertedProfile.setElevationDataAccess(elevation.getDataAccess());
// convertedProfile.setElevationCacheClear(elevation.isCacheClear());
// convertedProfile.setElevationSmoothing(profile.getElevationSmoothing() != null ? profile.getElevationSmoothing() : profileDefault.getElevationSmoothing());
// convertedProfile.setInterpolateBridgesAndTunnels(profile.getInterpolateBridgesAndTunnels() != null ? profile.getInterpolateBridgesAndTunnels() : profileDefault.getInterpolateBridgesAndTunnels());
// }
// Map<String, Object> preparation = profile.preparation != null ? profile.preparation : profileDefault.getPreparation();
// if (preparation != null) {
// convertedProfile.setPreparationOpts(ConfigFactory.parseMap(preparation));
// String methodsKey = "methods";
// if (preparation.containsKey(methodsKey) && preparation.get(methodsKey) != null && ((Map<String, Object>) preparation.get(methodsKey)).containsKey("fastisochrones")) {
// convertedProfile.setIsochronePreparationOpts(ConfigFactory.parseMap((Map<String, Object>) ((Map<String, Object>) preparation.get(methodsKey)).get("fastisochrones")));
// }
// }
// Map<String, Object> execution = profile.execution != null ? profile.execution : profileDefault.getExecution();
// if (execution != null) {
// convertedProfile.setExecutionOpts(ConfigFactory.parseMap(execution));
// }
// if (profile.getExtStorages() != null) {
// for (Map<String, String> storageParams : profile.getExtStorages().values()) {
// storageParams.put("gh_profile", ProfileTools.makeProfileName(RoutingProfileType.getEncoderName(RoutingProfileType.getFromString(convertedProfile.getProfiles())), "fastest", RouteProfileConfiguration.hasTurnCosts(convertedProfile.getEncoderOptions())));
// storageParams.remove("");
// }
// convertedProfile.getExtStorages().putAll(profile.getExtStorages());
// }
// convertedProfiles.add(convertedProfile);
// }
// }
return convertedProfiles.toArray(new RouteProfileConfiguration[0]);
public Map<String, ProfileProperties> getActiveProfiles() {
Map<String, ProfileProperties> activeProfiles = new LinkedHashMap<>();
for (Map.Entry<String, ProfileProperties> prof : profiles.entrySet()) {
prof.getValue().mergeDefaultsAndSetGraphPath(profileDefault, graphsRootPath, prof.getKey());
if (Boolean.TRUE.equals(prof.getValue().getEnabled()) && prof.getValue().getEncoderName() != null) {
activeProfiles.put(prof.getKey(), prof.getValue());
}
}
return activeProfiles;
}
}
Loading

0 comments on commit e1964a6

Please sign in to comment.