Skip to content

Commit

Permalink
test: Add more tests for ProfilePropertiesTest
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelsJP committed Jul 30, 2024
1 parent ccdff61 commit a64f8f6
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfileProperties extends ProfileProperties {
public DefaultProfileProperties() {
super();
this.setEncoderName(EncoderNameEnum.UNKNOWN);
setEnabled(false);
setElevation(true);
setElevationSmoothing(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesBikeElectric extends ProfileProperties {
public class DefaultProfilePropertiesBikeElectric extends DefaultProfileProperties {
public DefaultProfilePropertiesBikeElectric() {
super();
this.setEncoderName(EncoderNameEnum.CYCLING_ELECTRIC);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesBikeMountain extends ProfileProperties {
public class DefaultProfilePropertiesBikeMountain extends DefaultProfileProperties {
public DefaultProfilePropertiesBikeMountain() {
super();
this.setEncoderName(EncoderNameEnum.CYCLING_MOUNTAIN);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesBikeRegular extends ProfileProperties {
public class DefaultProfilePropertiesBikeRegular extends DefaultProfileProperties {
public DefaultProfilePropertiesBikeRegular() {
super();
this.setEncoderName(EncoderNameEnum.CYCLING_REGULAR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesBikeRoad extends ProfileProperties {
public class DefaultProfilePropertiesBikeRoad extends DefaultProfileProperties {
public DefaultProfilePropertiesBikeRoad() {
super();
this.setEncoderName(EncoderNameEnum.CYCLING_ROAD);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesCar extends ProfileProperties {
public class DefaultProfilePropertiesCar extends DefaultProfileProperties {

public DefaultProfilePropertiesCar() {
super();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesHgv extends ProfileProperties {
public class DefaultProfilePropertiesHgv extends DefaultProfileProperties {
public DefaultProfilePropertiesHgv() {
super();
this.setEncoderName(EncoderNameEnum.DRIVING_HGV);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesHiking extends ProfileProperties {
public class DefaultProfilePropertiesHiking extends DefaultProfileProperties {
public DefaultProfilePropertiesHiking() {
super();
this.setEncoderName(EncoderNameEnum.FOOT_HIKING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

import java.nio.file.Path;

@JsonIgnoreProperties({"ext_storages"})
public class DefaultProfilePropertiesPublicTransport extends ProfileProperties {
public class DefaultProfilePropertiesPublicTransport extends DefaultProfileProperties {
public DefaultProfilePropertiesPublicTransport() {
super();
this.setEncoderName(EncoderNameEnum.PUBLIC_TRANSPORT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesWalking extends ProfileProperties {
public class DefaultProfilePropertiesWalking extends DefaultProfileProperties {
public DefaultProfilePropertiesWalking() {
super();
this.setEncoderName(EncoderNameEnum.FOOT_WALKING);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.heigit.ors.config.profile.defaults;

import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;

public class DefaultProfilePropertiesWheelchair extends ProfileProperties {
public class DefaultProfilePropertiesWheelchair extends DefaultProfileProperties {
public DefaultProfilePropertiesWheelchair() {
super();
this.setEncoderName(EncoderNameEnum.WHEELCHAIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static EncoderOptionsProperties createEncoderOptionsWithBlockFords() {
}

private static PreparationProperties.MethodsProperties createPreparationMethodsWithChEnabled() {
PreparationProperties.MethodsProperties o1 = new PreparationProperties.MethodsProperties();
PreparationProperties.MethodsProperties o1 = new PreparationProperties.MethodsProperties(true);
o1.getCh().setEnabled(true);
return o1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.heigit.ors.config.profile.defaults.DefaultProfilePropertiesCar;
import org.heigit.ors.config.profile.storages.*;
import org.heigit.ors.config.profile.storages.ExtendedStorageGreenIndex;
import org.heigit.ors.config.profile.storages.ExtendedStorageHeavyVehicle;
import org.heigit.ors.config.profile.storages.ExtendedStorageWayCategory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -21,7 +23,9 @@ static void setUp() {
}

@Test
void testDeserializeExtendedStorages() throws JsonProcessingException {
void testDeserializeExtendedStoragesWithNonDefaultStorages() throws JsonProcessingException {
// This will initialize custom storages to make sure only them are deserialized without adding any other default storages.
// Example JSON:
// car:
// encoder_name: driving-car
// ext_storages:
Expand All @@ -37,7 +41,7 @@ void testDeserializeExtendedStorages() throws JsonProcessingException {
ProfileProperties foo = mapper.readValue(json, ProfileProperties.class);
assertEquals("driving-car", foo.getEncoderName().getName());
assertInstanceOf(DefaultProfilePropertiesCar.class, foo);
assertEquals(6, foo.getExtStorages().size());
assertEquals(3, foo.getExtStorages().size());
assertTrue(foo.getExtStorages().containsKey("WayCategory"));
assertTrue(foo.getExtStorages().containsKey("HeavyVehicle"));
assertTrue(foo.getExtStorages().containsKey("GreenIndex"));
Expand All @@ -57,18 +61,6 @@ void testDeserializeExtendedStorages() throws JsonProcessingException {
assertTrue(value.getEnabled());
assertEquals(Path.of("/path/to/file.csv"), ((ExtendedStorageGreenIndex) value).getFilepath());
}
case "Tollways" -> {
assertInstanceOf(ExtendedStorageTollways.class, value);
assertTrue(value.getEnabled());
}
case "WaySurfaceType" -> {
assertInstanceOf(ExtendedStorageWaySurfaceType.class, value);
assertTrue(value.getEnabled());
}
case "RoadAccessRestrictions" -> {
assertInstanceOf(ExtendedStorageRoadAccessRestrictions.class, value);
assertTrue(value.getEnabled());
}
default -> fail("Unexpected key: " + key);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.heigit.ors.config.profile.defaults;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.heigit.ors.common.EncoderNameEnum;
import org.heigit.ors.config.profile.ProfileProperties;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

class ProfilePropertiesTest {

public static Stream<Arguments> encoderAndExpectedKeys() {
return Stream.of(
Arguments.of(new DefaultProfileProperties(), EncoderNameEnum.UNKNOWN),
Arguments.of(new DefaultProfilePropertiesBikeRegular(), EncoderNameEnum.CYCLING_REGULAR),
Arguments.of(new DefaultProfilePropertiesBikeElectric(), EncoderNameEnum.CYCLING_ELECTRIC),
Arguments.of(new DefaultProfilePropertiesBikeMountain(), EncoderNameEnum.CYCLING_MOUNTAIN),
Arguments.of(new DefaultProfilePropertiesBikeRoad(), EncoderNameEnum.CYCLING_ROAD),
Arguments.of(new DefaultProfilePropertiesCar(), EncoderNameEnum.DRIVING_CAR),
Arguments.of(new DefaultProfilePropertiesWalking(), EncoderNameEnum.FOOT_WALKING),
Arguments.of(new DefaultProfilePropertiesHiking(), EncoderNameEnum.FOOT_HIKING),
Arguments.of(new DefaultProfilePropertiesHgv(), EncoderNameEnum.DRIVING_HGV),
Arguments.of(new DefaultProfilePropertiesWheelchair(), EncoderNameEnum.WHEELCHAIR),
Arguments.of(new DefaultProfilePropertiesPublicTransport(), EncoderNameEnum.PUBLIC_TRANSPORT)

);
}

public static Stream<Arguments> propertyJsonAndExpectedKeys() {
return Stream.of(
Arguments.of("{}", EncoderNameEnum.UNKNOWN, new DefaultProfileProperties()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.UNKNOWN.getName() + "\"}", EncoderNameEnum.UNKNOWN, new DefaultProfileProperties()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.CYCLING_REGULAR.getName() + "\"}", EncoderNameEnum.CYCLING_REGULAR, new DefaultProfilePropertiesBikeRegular()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.CYCLING_ELECTRIC.getName() + "\"}", EncoderNameEnum.CYCLING_ELECTRIC, new DefaultProfilePropertiesBikeElectric()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.CYCLING_MOUNTAIN.getName() + "\"}", EncoderNameEnum.CYCLING_MOUNTAIN, new DefaultProfilePropertiesBikeMountain()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.CYCLING_ROAD.getName() + "\"}", EncoderNameEnum.CYCLING_ROAD, new DefaultProfilePropertiesBikeRoad()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.DRIVING_CAR.getName() + "\"}", EncoderNameEnum.DRIVING_CAR, new DefaultProfilePropertiesCar()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.FOOT_WALKING.getName() + "\"}", EncoderNameEnum.FOOT_WALKING, new DefaultProfilePropertiesWalking()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.FOOT_HIKING.getName() + "\"}", EncoderNameEnum.FOOT_HIKING, new DefaultProfilePropertiesHiking()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.DRIVING_HGV.getName() + "\"}", EncoderNameEnum.DRIVING_HGV, new DefaultProfilePropertiesHgv()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.WHEELCHAIR.getName() + "\"}", EncoderNameEnum.WHEELCHAIR, new DefaultProfilePropertiesWheelchair()),
Arguments.of("{\"encoder_name\":\"" + EncoderNameEnum.PUBLIC_TRANSPORT.getName() + "\"}", EncoderNameEnum.PUBLIC_TRANSPORT, new DefaultProfilePropertiesPublicTransport())

);
}

@ParameterizedTest
@MethodSource("encoderAndExpectedKeys")
void defaultPropertiesAreSetCorrectly(ProfileProperties properties, EncoderNameEnum expectedEncoderName) {
assertEquals(expectedEncoderName, properties.getEncoderName());
assertFalse(properties.getEnabled());
assertTrue(properties.getElevation());
assertTrue(properties.getElevationSmoothing());
assertEquals(8, properties.getEncoderFlagsSize());
assertTrue(properties.getInstructions());
assertFalse(properties.getOptimize());
assertFalse(properties.getTraffic());
assertTrue(properties.getInterpolateBridgesAndTunnels());
assertFalse(properties.getForceTurnCosts());
assertEquals(500, properties.getLocationIndexResolution());
assertEquals(4, properties.getLocationIndexSearchIterations());
assertEquals(100000d, properties.getMaximumDistance());
assertEquals(100000d, properties.getMaximumDistanceDynamicWeights());
assertEquals(100000d, properties.getMaximumDistanceAvoidAreas());
assertEquals(100000d, properties.getMaximumDistanceAlternativeRoutes());
assertEquals(100000d, properties.getMaximumDistanceRoundTripRoutes());
assertEquals(80d, properties.getMaximumSpeedLowerBound());
assertEquals(50, properties.getMaximumWayPoints());

if (expectedEncoderName == EncoderNameEnum.WHEELCHAIR)
assertEquals(50, properties.getMaximumSnappingRadius());
else
assertEquals(400, properties.getMaximumSnappingRadius());

assertEquals(1000000, properties.getMaximumVisitedNodes());
}

@ParameterizedTest
@MethodSource("encoderAndExpectedKeys")
void defaultConstructorDefaultPropertiesBike(ProfileProperties properties, EncoderNameEnum expectedEncoderName) {
assertEquals(expectedEncoderName, properties.getEncoderName());
assertInstanceOf(ProfileProperties.class, properties);
assertInstanceOf(DefaultProfileProperties.class, properties);
assertFalse(properties.getEnabled());
}

@ParameterizedTest
@MethodSource("propertyJsonAndExpectedKeys")
void deSerializeProfileProperties(String profileJson, EncoderNameEnum expectedEncoderName, ProfileProperties profilePropertiesClass) throws JsonProcessingException {
// Step 2: Serialize the object to JSON
ObjectMapper mapper = new ObjectMapper();
// Step 3: Deserialize the JSON to an object
ProfileProperties profileProperties = mapper.readValue(profileJson, ProfileProperties.class);
assertEquals(expectedEncoderName, profileProperties.getEncoderName());
assertFalse(profileProperties.getEnabled());
assertInstanceOf(ProfileProperties.class, profileProperties);
assertInstanceOf(DefaultProfileProperties.class, profileProperties);
}

@ParameterizedTest
@MethodSource("encoderAndExpectedKeys")
void serializeProfileProperties(ProfileProperties profileProperties, EncoderNameEnum expectedEncoderName) throws JsonProcessingException {
// Step 2: Serialize the object to JSON
ObjectMapper mapper = new ObjectMapper();
String serializedJson = mapper.writeValueAsString(profileProperties);
// Step 3: Deserialize the JSON to an object
ProfileProperties deserializedProfileProperties = mapper.readValue(serializedJson, ProfileProperties.class);

assertEquals(expectedEncoderName, deserializedProfileProperties.getEncoderName());
assertInstanceOf(ProfileProperties.class, deserializedProfileProperties);
}

}

0 comments on commit a64f8f6

Please sign in to comment.