diff --git a/.github/workflows/test-and-build.yml b/.github/workflows/test-and-build.yml index 40781059..af7c7d0b 100644 --- a/.github/workflows/test-and-build.yml +++ b/.github/workflows/test-and-build.yml @@ -7,11 +7,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK 25 uses: actions/setup-java@v3 with: distribution: 'temurin' - java-version: '11' + java-version: '25' cache: 'maven' - name: Run Spotless Apply run: mvn spotless:apply @@ -28,11 +28,11 @@ jobs: if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK 25 uses: actions/setup-java@v3 with: distribution: 'temurin' - java-version: '11' + java-version: '25' cache: 'maven' - name: Publish package run: mvn --batch-mode deploy diff --git a/pom.xml b/pom.xml index f31b37ee..f83b2788 100644 --- a/pom.xml +++ b/pom.xml @@ -2,44 +2,36 @@ 4.0.0 fi.hsl transitdata-common - 2.0.5 + 3.0.0 jar Common utilities for Transitdata projects + UTF-8 - 11 - 11 + 25 3.0.7 1.18.3 2.43.0 - 1.17.0 + 1.23.0 + 2.20.1 - unit-test true - unit-test true false - + integration-test - integration-test false true @@ -61,82 +53,77 @@ protobuf-java 3.21.12 - - com.dslplatform - dsl-json-java8 - 1.10.0 + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-annotations + 2.20 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + ${jackson.version} - org.apache.pulsar pulsar-client ${pulsar.version} - org.apache.pulsar pulsar-client-admin ${pulsar.version} - redis.clients jedis 4.4.3 - jar - compile - com.typesafe config 1.4.2 - ch.qos.logback logback-classic 1.4.7 - ch.qos.logback.contrib logback-json-classic 0.1.5 - ch.qos.logback.contrib logback-jackson 0.1.5 - org.slf4j slf4j-api 2.0.7 - - - com.fasterxml.jackson.core - jackson-databind - 2.15.0 - - org.jetbrains annotations 24.0.1 - junit junit 4.13.2 test - org.testcontainers testcontainers @@ -149,17 +136,16 @@ ${testcontainers.version} test - ${project.artifactId}-${project.version} + src/main/resources true - logback.xml @@ -167,6 +153,15 @@ + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + 25 + + + org.apache.maven.plugins maven-assembly-plugin @@ -188,39 +183,11 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.11.0 - - 11 - 11 - - - - com.coderplus.maven.plugins - copy-rename-maven-plugin - 1.0.1 - - - copy-and-rename-pom-file - package - - copy - - - ${project.basedir}/pom.xml - ${project.build.directory}/${project.build.finalName}.pom - - - - - - org.apache.maven.plugins maven-jar-plugin + 3.4.1 @@ -229,31 +196,12 @@ - - org.apache.maven.plugins - maven-assembly-plugin - - - test-jar-with-dependencies.xml - - - - - - - - single - - - - org.codehaus.mojo build-helper-maven-plugin 3.4.0 - add-integration-test-sources generate-test-sources @@ -261,13 +209,11 @@ add-test-source - src/integration-test/java - add-integration-test-resources generate-test-resources @@ -277,13 +223,12 @@ - true src/integration-test/resources + true - @@ -293,7 +238,6 @@ 3.1.2 ${skip.unit.tests} - **/IT*.java @@ -306,6 +250,7 @@ + org.apache.maven.plugins maven-failsafe-plugin @@ -323,6 +268,7 @@ + com.diffplug.spotless spotless-maven-plugin @@ -349,6 +295,7 @@ + diff --git a/src/main/java/fi/hsl/common/hfp/HfpJson.java b/src/main/java/fi/hsl/common/hfp/HfpJson.java index cbeff088..6b1296cb 100644 --- a/src/main/java/fi/hsl/common/hfp/HfpJson.java +++ b/src/main/java/fi/hsl/common/hfp/HfpJson.java @@ -1,22 +1,32 @@ package fi.hsl.common.hfp; -import com.dslplatform.json.*; +import com.fasterxml.jackson.annotation.JsonAlias; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.io.IOException; -// ignore unknown properties (default for objects). -// to disallow unknown properties in JSON set it to FAIL which will result in exception instead -@CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) +@JsonIgnoreProperties(ignoreUnknown = true) public class HfpJson { //Specification: https://digitransit.fi/en/developers/apis/4-realtime-api/vehicle-positions/ //Example payload: // {"VP":{"desi":"81","dir":"2","oper":22,"veh":792,"tst":"2018-04-05T17:38:36Z","tsi":1522949916,"spd":0.16,"hdg":225,"lat":60.194481,"long":25.03095,"acc":0,"dl":-25,"odo":2819,"drst":0,"oday":"2018-04-05","jrn":636,"line":112,"start":"20:25"}} - @JsonAttribute(nullable = false, name = "VP", alternativeNames = {"DUE", "ARR", "DEP", "ARS", "PDE", "PAS", "WAIT", - "DOO", "DOC", "TLR", "TLA", "DA", "DOUT", "BA", "BOUT", "VJA", "VJOUT"}) + @JsonProperty("VP") + @JsonAlias({"DUE", "ARR", "DEP", "ARS", "PDE", "PAS", "WAIT", "DOO", "DOC", "TLR", "TLA", "DA", "DOUT", "BA", + "BOUT", "VJA", "VJOUT"}) public Payload payload; - @CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) + @JsonIgnoreProperties(ignoreUnknown = true) public static class Payload { public String desi; @@ -27,10 +37,10 @@ public static class Payload { public Integer veh; - @JsonAttribute(nullable = false) + @JsonProperty(required = true) public String tst; - @JsonAttribute(nullable = false) + @JsonProperty(required = true) public long tsi; public Double spd; @@ -39,14 +49,15 @@ public static class Payload { public Double lat; - @JsonAttribute(name = "long") //use alternative name in JSON + @JsonProperty("long") //use alternative name in JSON public Double longitude; public Double acc; public Integer dl; - @JsonAttribute(converter = Odo.class) + @JsonDeserialize(using = OdoDeserializer.class) + @JsonSerialize(using = OdoSerializer.class) public Double odo; public Integer drst; @@ -73,65 +84,69 @@ public static class Payload { public String ttdep; - @JsonAttribute(name = "dr-type") //use alternative name in JSON + @JsonProperty("dr-type") //use alternative name in JSON public Integer dr_type; - @JsonAttribute(name = "tlp-requestid") //use alternative name in JSON + @JsonProperty("tlp-requestid") public Integer tlp_requestid; - @JsonAttribute(name = "tlp-requesttype") //use alternative name in JSON + @JsonProperty("tlp-requesttype") public String tlp_requesttype; - @JsonAttribute(name = "tlp-prioritylevel") //use alternative name in JSON + @JsonProperty("tlp-prioritylevel") public String tlp_prioritylevel; - @JsonAttribute(name = "tlp-reason") //use alternative name in JSON + @JsonProperty("tlp-reason") public String tlp_reason; - @JsonAttribute(name = "tlp-att-seq") //use alternative name in JSON + @JsonProperty("tlp-att-seq") public Integer tlp_att_seq; - @JsonAttribute(name = "tlp-decision") //use alternative name in JSON + @JsonProperty("tlp-decision") public String tlp_decision; public Integer sid; - @JsonAttribute(name = "signal-groupid") //use alternative name in JSON + @JsonProperty("signal-groupid") public Integer signal_groupid; - @JsonAttribute(name = "tlp-signalgroupnbr") //use alternative name in JSON + @JsonProperty("tlp-signalgroupnbr") public Integer tlp_signalgroupnbr; - @JsonAttribute(name = "tlp-line-configid") //use alternative name in JSON + @JsonProperty("tlp-line-configid") public Integer tlp_line_configid; - @JsonAttribute(name = "tlp-point-configid") //use alternative name in JSON + @JsonProperty("tlp-point-configid") public Integer tlp_point_configid; - @JsonAttribute(name = "tlp-frequency") //use alternative name in JSON + @JsonProperty("tlp-frequency") public Integer tlp_frequency; - @JsonAttribute(name = "tlp-protocol") //use alternative name in JSON + @JsonProperty("tlp-protocol") public String tlp_protocol; public String label; } - public static abstract class Odo { - public static final JsonReader.ReadObject JSON_READER = new JsonReader.ReadObject() { - public Double read(JsonReader reader) throws IOException { - return reader.wasNull() ? null : NumberConverter.deserializeDouble(reader); + public static class OdoDeserializer extends JsonDeserializer { + @Override + public Double deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException, JsonProcessingException { + if (p.currentToken().isNumeric()) { + return p.getDoubleValue(); } - }; - - public static final JsonWriter.WriteObject JSON_WRITER = new JsonWriter.WriteObject() { - public void write(JsonWriter writer, Double value) { - if (value == null) - writer.writeNull(); - else - NumberConverter.serializeNullable(value.intValue(), writer); - } - }; + return null; + } } + public static class OdoSerializer extends JsonSerializer { + @Override + public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + if (value == null) { + gen.writeNull(); + } else { + gen.writeNumber(value.intValue()); + } + } + } } diff --git a/src/main/java/fi/hsl/common/hfp/HfpParser.java b/src/main/java/fi/hsl/common/hfp/HfpParser.java index 1a262229..d2a82f05 100644 --- a/src/main/java/fi/hsl/common/hfp/HfpParser.java +++ b/src/main/java/fi/hsl/common/hfp/HfpParser.java @@ -1,15 +1,14 @@ package fi.hsl.common.hfp; -import com.dslplatform.json.DslJson; -import com.dslplatform.json.ParsingException; -import com.dslplatform.json.runtime.Settings; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import fi.hsl.common.hfp.proto.Hfp; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.validation.constraints.Null; import java.io.IOException; import java.sql.Date; import java.sql.Time; @@ -29,13 +28,11 @@ public class HfpParser { private static Set vehiclesWithEmptyTransportMode = new HashSet<>(); - // Let's use dsl-json (https://github.com/ngs-doo/dsl-json) for performance. - // Based on this benchmark: https://github.com/fabienrenaud/java-json-benchmark - - //Example: https://github.com/ngs-doo/dsl-json/blob/master/examples/MavenJava8/src/main/java/com/dslplatform/maven/Example.java - - //Note! Apparently not thread safe, for per thread reuse use ThreadLocal pattern or create separate instances - final DslJson dslJson = new DslJson<>(Settings.withRuntime().allowArrayFormat(true).includeServiceLoader()); + final ObjectMapper objectMapper = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) + .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); private static void foundVehicleWithEmptyTransportMode(String uniqueVehicleId) { vehiclesWithEmptyTransportMode.add(uniqueVehicleId); @@ -54,32 +51,35 @@ public static HfpParser newInstance() { /** * Methods for parsing the Json Payload **/ - @Nullable public HfpJson parseJson(@NotNull byte[] data) throws IOException, InvalidHfpPayloadException { try { - return dslJson.deserialize(HfpJson.class, data, data.length); + HfpJson hfpJson = objectMapper.readValue(data, HfpJson.class); + validateRequiredFields(hfpJson); + return hfpJson; } catch (IOException ioe) { - if (ioe instanceof ParsingException) { - throw new InvalidHfpPayloadException("Failed to parse HFP JSON", (ParsingException) ioe); - } else { - throw ioe; - } + throw new InvalidHfpPayloadException("Failed to parse HFP JSON", ioe); + } + } + + private void validateRequiredFields(HfpJson hfpJson) throws InvalidHfpPayloadException { + if (hfpJson == null || hfpJson.payload == null) { + throw new InvalidHfpPayloadException("Payload is missing or null"); + } + HfpJson.Payload p = hfpJson.payload; + if (p.tst == null) { + throw new InvalidHfpPayloadException("Field 'tst' cannot be null"); } } @NotNull public String serializeToString(@NotNull final HfpJson json) throws IOException { - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - dslJson.serialize(json, os); - return os.toString("UTF-8"); + return objectMapper.writeValueAsString(json); } @NotNull public byte[] serializeToByteArray(@NotNull final HfpJson json) throws IOException { - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - dslJson.serialize(json, os); - return os.toByteArray(); + return objectMapper.writeValueAsBytes(json); } public Optional safeParse(@NotNull byte[] data) { @@ -97,12 +97,11 @@ public static Hfp.Payload parsePayload(@NotNull HfpJson json) { final HfpJson.Payload payload = json.payload; Hfp.Payload.Builder builder = Hfp.Payload.newBuilder(); - // Required attributes + builder.setSchemaVersion(builder.getSchemaVersion()); HfpValidator.validateString(payload.tst).ifPresent(builder::setTst); // TODO add validation for offsetdatetime format builder.setTsi(payload.tsi); - // Optional attributes HfpValidator.validateString(payload.desi).ifPresent(builder::setDesi); HfpValidator.validateString(payload.dir).ifPresent(builder::setDir); if (payload.oper != null) @@ -174,7 +173,6 @@ public static Hfp.Payload parsePayload(@NotNull HfpJson json) { /** * Methods for parsing the data from the topic */ - public static Optional safeParseTopic(@NotNull String topic) { try { return Optional.of(parseTopic(topic)); @@ -441,7 +439,11 @@ private InvalidHfpTopicException(String message) { } public static class InvalidHfpPayloadException extends Exception { - private InvalidHfpPayloadException(String message, ParsingException cause) { + public InvalidHfpPayloadException(String message) { + super(message); + } + + public InvalidHfpPayloadException(String message, Throwable cause) { super(message, cause); } } diff --git a/src/main/java/fi/hsl/common/passengercount/PassengerCountParser.java b/src/main/java/fi/hsl/common/passengercount/PassengerCountParser.java index e4d80045..17605ca8 100644 --- a/src/main/java/fi/hsl/common/passengercount/PassengerCountParser.java +++ b/src/main/java/fi/hsl/common/passengercount/PassengerCountParser.java @@ -1,8 +1,9 @@ package fi.hsl.common.passengercount; -import com.dslplatform.json.DslJson; -import com.dslplatform.json.ParsingException; -import com.dslplatform.json.runtime.Settings; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import fi.hsl.common.passengercount.json.*; import fi.hsl.common.passengercount.proto.PassengerCount; import org.jetbrains.annotations.NotNull; @@ -20,7 +21,10 @@ public class PassengerCountParser { static final Pattern topicVersionRegex = Pattern.compile("(^v\\d+|dev)"); - final DslJson dslJson = new DslJson<>(Settings.withRuntime().allowArrayFormat(true).includeServiceLoader()); + ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) + .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); @NotNull public static PassengerCountParser newInstance() { @@ -259,23 +263,21 @@ public ApcJson toJson(PassengerCount.Payload passengerCountPayload) { } public OutputStream serializeJson(ApcJson apcJson, OutputStream outputStream) throws IOException { - dslJson.serialize(apcJson, outputStream); + objectMapper.writeValue(outputStream, apcJson); return outputStream; } @Nullable public ApcJson parseJson(byte @NotNull [] data) throws IOException, InvalidAPCPayloadException { try { - return dslJson.deserialize(ApcJson.class, data, data.length); + return objectMapper.readValue(data, ApcJson.class); } catch (IOException ioe) { - if (ioe instanceof ParsingException) { - throw new PassengerCountParser.InvalidAPCPayloadException("Failed to parse APC JSON", - (ParsingException) ioe); + if (ioe instanceof com.fasterxml.jackson.core.JsonProcessingException jpe) { + throw new PassengerCountParser.InvalidAPCPayloadException("Failed to parse APC JSON", jpe); } else { throw ioe; } } - } private static OptionalDouble safeParseDouble(String s) { @@ -334,7 +336,7 @@ private InvalidAPCTopicException(String message) { } public static class InvalidAPCPayloadException extends Exception { - private InvalidAPCPayloadException(String message, ParsingException cause) { + private InvalidAPCPayloadException(String message, JsonProcessingException cause) { super(message, cause); } } diff --git a/src/main/java/fi/hsl/common/passengercount/json/Apc.java b/src/main/java/fi/hsl/common/passengercount/json/Apc.java index 33ff2284..d59770c1 100644 --- a/src/main/java/fi/hsl/common/passengercount/json/Apc.java +++ b/src/main/java/fi/hsl/common/passengercount/json/Apc.java @@ -1,11 +1,11 @@ package fi.hsl.common.passengercount.json; -import com.dslplatform.json.CompiledJson; -import com.dslplatform.json.JsonAttribute; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Date; -@CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) +@JsonIgnoreProperties(ignoreUnknown = true) public class Apc { public String desi; public String dir; @@ -14,7 +14,7 @@ public class Apc { public Date tst; public Long tsi; public Double lat; - @JsonAttribute(name = "long") + @JsonProperty("long") public Double lon; public Double odo; public String oday; diff --git a/src/main/java/fi/hsl/common/passengercount/json/ApcJson.java b/src/main/java/fi/hsl/common/passengercount/json/ApcJson.java index fa41c590..2aac3d58 100644 --- a/src/main/java/fi/hsl/common/passengercount/json/ApcJson.java +++ b/src/main/java/fi/hsl/common/passengercount/json/ApcJson.java @@ -1,10 +1,11 @@ package fi.hsl.common.passengercount.json; -import com.dslplatform.json.CompiledJson; -import com.dslplatform.json.JsonAttribute; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; -@CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) +@JsonIgnoreProperties(ignoreUnknown = true) public class ApcJson { - @JsonAttribute(nullable = false, name = "APC") + + @JsonProperty(value = "APC", required = true) public Apc apc; } diff --git a/src/main/java/fi/hsl/common/passengercount/json/Count.java b/src/main/java/fi/hsl/common/passengercount/json/Count.java index 70dcf943..b9c11a20 100644 --- a/src/main/java/fi/hsl/common/passengercount/json/Count.java +++ b/src/main/java/fi/hsl/common/passengercount/json/Count.java @@ -1,12 +1,12 @@ package fi.hsl.common.passengercount.json; -import com.dslplatform.json.CompiledJson; -import com.dslplatform.json.JsonAttribute; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; -@CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) +@JsonIgnoreProperties(ignoreUnknown = true) public class Count { - @JsonAttribute(nullable = false, name = "class") + @JsonProperty(value = "class", required = true) public String clazz; public int in; public int out; diff --git a/src/main/java/fi/hsl/common/passengercount/json/DoorCount.java b/src/main/java/fi/hsl/common/passengercount/json/DoorCount.java index 633b8e33..bf0c39eb 100644 --- a/src/main/java/fi/hsl/common/passengercount/json/DoorCount.java +++ b/src/main/java/fi/hsl/common/passengercount/json/DoorCount.java @@ -1,10 +1,10 @@ package fi.hsl.common.passengercount.json; -import com.dslplatform.json.CompiledJson; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; -@CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) +@JsonIgnoreProperties(ignoreUnknown = true) public class DoorCount { public String door; public List count; diff --git a/src/main/java/fi/hsl/common/passengercount/json/Vehiclecounts.java b/src/main/java/fi/hsl/common/passengercount/json/Vehiclecounts.java index 28aaa8e8..ee783dc2 100644 --- a/src/main/java/fi/hsl/common/passengercount/json/Vehiclecounts.java +++ b/src/main/java/fi/hsl/common/passengercount/json/Vehiclecounts.java @@ -1,10 +1,10 @@ package fi.hsl.common.passengercount.json; -import com.dslplatform.json.CompiledJson; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; -@CompiledJson(onUnknown = CompiledJson.Behavior.IGNORE) +@JsonIgnoreProperties(ignoreUnknown = true) public class Vehiclecounts { public String countquality; public int vehicleload; diff --git a/src/test/java/fi/hsl/common/passengercount/PassengerCountParserTest.java b/src/test/java/fi/hsl/common/passengercount/PassengerCountParserTest.java index d311772e..8e4f3391 100644 --- a/src/test/java/fi/hsl/common/passengercount/PassengerCountParserTest.java +++ b/src/test/java/fi/hsl/common/passengercount/PassengerCountParserTest.java @@ -18,17 +18,16 @@ public class PassengerCountParserTest { private final String TEST_TOPIC = "/hfp/v2/journey/ongoing/apc/bus/0022/01288"; + @Test + public void parseJson_ignoreUnknownProperties_Test() throws Exception { + final var apc = parseJsonFromResources("src/test/resources/passenger-count-unknown-properties-sample.json").apc; + performAssertions(apc); + } + @Test public void parseJsonTest() throws Exception { Apc apc = parseJsonFromResources("src/test/resources/passenger-count-sample.json").apc; - assertEquals("555", apc.desi); - assertEquals(12, (int) apc.oper); - assertEquals("GPS", apc.loc); - assertEquals("regular | defect | other", apc.vehiclecounts.countquality); - assertEquals(15, apc.vehiclecounts.vehicleload); - assertEquals(1, apc.vehiclecounts.doorcounts.size()); - assertEquals("door1", apc.vehiclecounts.doorcounts.get(0).door); - + performAssertions(apc); } private ApcJson parseJsonFromResources(String filename) throws Exception { @@ -38,6 +37,16 @@ private ApcJson parseJsonFromResources(String filename) throws Exception { return apcJson; } + private void performAssertions(Apc apc) { + assertEquals("555", apc.desi); + assertEquals(12, (int) apc.oper); + assertEquals("GPS", apc.loc); + assertEquals("regular | defect | other", apc.vehiclecounts.countquality); + assertEquals(15, apc.vehiclecounts.vehicleload); + assertEquals(1, apc.vehiclecounts.doorcounts.size()); + assertEquals("door1", apc.vehiclecounts.doorcounts.get(0).door); + } + @Test public void convertJsonToProtobufMessageTest() throws Exception { ApcJson apcJson = parseJsonFromResources("src/test/resources/passenger-count-sample.json"); diff --git a/src/test/resources/passenger-count-unknown-properties-sample.json b/src/test/resources/passenger-count-unknown-properties-sample.json new file mode 100644 index 00000000..53e453eb --- /dev/null +++ b/src/test/resources/passenger-count-unknown-properties-sample.json @@ -0,0 +1,39 @@ +{ + "APC": { + "desi": "555", + "dir": "1", + "oper": 12, + "veh": 10, + "tst": "2019-05-23T14:15:16.000Z", + "tsi": 1416308975, + "lat": 24.9435, + "long": 60.1967, + "odo": 45.12, + "oday": "2019-05-23", + "jrn": 1, + "invalid": true, + "line": 264, + "start": "13:40", + "loc": "GPS", + "stop": 1234567, + "route": "2551", + "vehiclecounts": { + "countquality": "regular | defect | other", + "vehicleload": 15, + "vehicleloadratio": 0.2, + "doorcounts": [ + { + "door" : "door1", + "count": [ + { + "class": "adult | child | pram | bike | wheelchair | other", + "in": 1, + "out": 2 + } + ] + } + ], + "extensions": "extension data" + } + } +} \ No newline at end of file