diff --git a/.gitignore b/.gitignore index ae2b4d4f..6603d4ee 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ sfera-mock/src/main/java/generated/** sfera-mock/src/main/java/META-INF/JAXB/** sfera-mock/src/main/java/org/w3/_2001/xmlschema/** *.p12 +*.env ### STS ### .apt_generated diff --git a/sfera-mock/README.md b/sfera-mock/README.md index 0136b6de..36f9c7f4 100644 --- a/sfera-mock/README.md +++ b/sfera-mock/README.md @@ -16,8 +16,14 @@ | Train number | Result | |--------------|--------------------------| -| 4816 | Zürich HB - Aarau
| +| 4816 | Zürich HB - Aarau | | 7839 | Solothurn - Oberdorf SO | +| 9999 | Fictional Bahnhof A - D1 | TODO: 29137 Lenzburg - Luzern https://miro.com/app/board/uXjVKK4zJFk=/?moveToWidget=3458764596975113381&cot=14 +## Add new Scenario +To create a new scenario some resources need to be added +1. add journey profile named `SFERA_JP_` to `static_sfera_resources/jp` +2. add corresponding segment profiles named `SFERA_SP__` to `static_sfera_resources/sp` + diff --git a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/common/XmlHelper.java b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/common/XmlHelper.java index deee9efb..aeb6ec86 100644 --- a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/common/XmlHelper.java +++ b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/common/XmlHelper.java @@ -1,9 +1,5 @@ package ch.sbb.sferamock.messages.common; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import javax.xml.transform.stream.StreamResult; @@ -12,7 +8,6 @@ import org.slf4j.LoggerFactory; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.stereotype.Component; -import org.springframework.util.ResourceUtils; @Component public class XmlHelper { @@ -35,10 +30,7 @@ public String toString(Object object) { } } - public Object xmlToObject(String filePath) throws IOException { - File file = ResourceUtils.getFile(filePath); - InputStream in = new FileInputStream(file); - String xmlPayload = new String(in.readAllBytes()); - return jaxb2Marshaller.unmarshal(new StreamSource(new StringReader(xmlPayload))); + public Object xmlToObject(String xml) { + return jaxb2Marshaller.unmarshal(new StreamSource(new StringReader(xml))); } } diff --git a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/JourneyProfileRepository.java b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/JourneyProfileRepository.java index 1bee3394..2e5e1627 100644 --- a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/JourneyProfileRepository.java +++ b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/JourneyProfileRepository.java @@ -3,18 +3,25 @@ import ch.sbb.sferamock.adapters.sfera.model.v0201.JourneyProfile; import ch.sbb.sferamock.messages.common.XmlHelper; import ch.sbb.sferamock.messages.model.TrainIdentification; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.stereotype.Service; @Service public class JourneyProfileRepository implements ApplicationRunner { - private static final String XML_RESOURCES_CLASSPATH = "classpath:sfera_example_messages/"; + private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/jp/*.xml"; + private static final String XML_REGEX = "SFERA_JP_(\\d+)\\.xml"; private final XmlHelper xmlHelper; Map journeyProfiles = new HashMap<>(); @@ -25,16 +32,33 @@ public JourneyProfileRepository(XmlHelper xmlHelper) { @Override public void run(ApplicationArguments args) throws Exception { - importJp("4816", "SFERA_JP_4816.xml"); - importJp("7839", "SFERA_JP_7839.xml"); + importJps(); } public Optional getJourneyProfile(TrainIdentification trainIdentification) { return Optional.ofNullable(journeyProfiles.get(trainIdentification.operationalNumber())); } - private void importJp(String operationalNumber, String path) throws IOException { - var journeyProfile = xmlHelper.xmlToObject(XML_RESOURCES_CLASSPATH + path); - journeyProfiles.put(operationalNumber, (JourneyProfile) journeyProfile); + private void importJps() throws IOException { + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + var resources = resolver.getResources(XML_RESOURCES_CLASSPATH); + for (var resource : resources) { + File file = resource.getFile(); + var operationalNumber = extractOperationalNumber(file.getName()); + try (InputStream in = new FileInputStream(file)) { + String xmlPayload = new String(in.readAllBytes()); + var journeyProfile = xmlHelper.xmlToObject(xmlPayload); + journeyProfiles.put(operationalNumber, (JourneyProfile) journeyProfile); + } + } + } + + public static String extractOperationalNumber(String filename) { + Pattern pattern = Pattern.compile(XML_REGEX); + Matcher matcher = pattern.matcher(filename); + if (matcher.find()) { + return matcher.group(1); + } + return null; } } diff --git a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/SegmentProfileRepository.java b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/SegmentProfileRepository.java index 585178a8..ed1b2cf1 100644 --- a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/SegmentProfileRepository.java +++ b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/services/SegmentProfileRepository.java @@ -3,18 +3,26 @@ import ch.sbb.sferamock.adapters.sfera.model.v0201.SegmentProfile; import ch.sbb.sferamock.messages.common.XmlHelper; import ch.sbb.sferamock.messages.model.SegmentIdentification; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.stereotype.Service; @Service public class SegmentProfileRepository implements ApplicationRunner { - private static final String XML_RESOURCES_CLASSPATH = "classpath:sfera_example_messages/"; + private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/sp/*.xml"; + private static final String XML_REGEX = "SFERA_SP_(\\d+_\\d+)\\.xml"; + private final XmlHelper xmlHelper; Map segmentProfiles = new HashMap<>(); @@ -25,17 +33,33 @@ public SegmentProfileRepository(XmlHelper xmlHelper) { @Override public void run(ApplicationArguments args) throws Exception { - importSp("1", "SFERA_SP_1.xml"); - importSp("2", "SFERA_SP_2.xml"); - importSp("3", "SFERA_SP_3.xml"); + importSps(); } public Optional getSegmentProfile(SegmentIdentification spId) { return Optional.ofNullable(segmentProfiles.get(spId.id())); } - private void importSp(String operationalNumber, String path) throws IOException { - var segmentProfile = xmlHelper.xmlToObject(XML_RESOURCES_CLASSPATH + path); - segmentProfiles.put(operationalNumber, (SegmentProfile) segmentProfile); + private void importSps() throws IOException { + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + var resources = resolver.getResources(XML_RESOURCES_CLASSPATH); + for (var resource : resources) { + File file = resource.getFile(); + var segmentId = extractSpId(file.getName()); + try (InputStream in = new FileInputStream(file)) { + String xmlPayload = new String(in.readAllBytes()); + var segmentProfile = xmlHelper.xmlToObject(xmlPayload); + segmentProfiles.put(segmentId, (SegmentProfile) segmentProfile); + } + } + } + + public static String extractSpId(String filename) { + Pattern pattern = Pattern.compile(XML_REGEX); + Matcher matcher = pattern.matcher(filename); + if (matcher.find()) { + return matcher.group(1); + } + return null; } } diff --git a/sfera-mock/src/main/resources/SFERA_3.0_custom.xsd b/sfera-mock/src/main/resources/SFERA_3.0_custom.xsd index 66d00d00..97506a30 100644 --- a/sfera-mock/src/main/resources/SFERA_3.0_custom.xsd +++ b/sfera-mock/src/main/resources/SFERA_3.0_custom.xsd @@ -9,6 +9,8 @@ SFERA v3 custom - TAF_TAP_location_type - TimingPoint - TAF_TAP_LocationReference +- SignalPhysicalCharacteristics + - visualIdentifier --> @@ -2083,6 +2085,7 @@ SF12 SFERA_G2B_ReplyMessage + @@ -6023,6 +6026,11 @@ Resolution: 15 min Vertical offset (in m) from the top of the track to the middle of the object. + + + Visual identifier of the signal. Typically a sign placed over or under the signal itself facilitates the visual identification by the driver. + + Voltage value (V) diff --git a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_JP_4816.xml b/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_4816.xml similarity index 92% rename from sfera-mock/src/main/resources/sfera_example_messages/SFERA_JP_4816.xml rename to sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_4816.xml index 43fde7a9..68782328 100644 --- a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_JP_4816.xml +++ b/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_4816.xml @@ -1,7 +1,7 @@ + xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd"> 1085 @@ -9,7 +9,7 @@ 2022-01-04 - + 0085 @@ -24,7 +24,7 @@ - + 0085 diff --git a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_JP_7839.xml b/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_7839.xml similarity index 91% rename from sfera-mock/src/main/resources/sfera_example_messages/SFERA_JP_7839.xml rename to sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_7839.xml index e7300aab..a8409b29 100644 --- a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_JP_7839.xml +++ b/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_7839.xml @@ -1,7 +1,7 @@ + xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd"> 1085 @@ -9,7 +9,7 @@ 2022-01-04 - + 0085 diff --git a/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_9999.xml b/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_9999.xml new file mode 100644 index 00000000..1c9fcefa --- /dev/null +++ b/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_9999.xml @@ -0,0 +1,65 @@ + + + + + 1085 + 9999 + 2022-01-04 + + + + + 0085 + + + + + + + + + + 0085 + + + + + + + + + + 0085 + + + + + + + + + + + + + 0085 + + + + + + + + + + 0085 + + + + + + + + diff --git a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_1.xml similarity index 92% rename from sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_1.xml index 3bf2d7a3..8681cb82 100644 --- a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_1.xml +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_1.xml @@ -1,7 +1,7 @@ + xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd" + SP_ID="4816_1" SP_VersionMajor="1" SP_VersionMinor="0" SP_Length="41500" SP_Status="Valid"> 0085 diff --git a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_2.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_2.xml similarity index 98% rename from sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_2.xml rename to sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_2.xml index ec912f3a..1cdabb08 100644 --- a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_2.xml +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_2.xml @@ -1,7 +1,7 @@ + xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd" + SP_ID="4816_2" SP_VersionMajor="1" SP_VersionMinor="0" SP_Length="41500" SP_Status="Valid"> 0085 diff --git a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_3.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_7839_1.xml similarity index 97% rename from sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_3.xml rename to sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_7839_1.xml index d0e2b6c4..11a184b2 100644 --- a/sfera-mock/src/main/resources/sfera_example_messages/SFERA_SP_3.xml +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_7839_1.xml @@ -1,7 +1,7 @@ + xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd" + SP_ID="7839_1" SP_VersionMajor="1" SP_VersionMinor="0" SP_Length="41500" SP_Status="Valid"> 0085 diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_1.xml new file mode 100644 index 00000000..2aedc0c0 --- /dev/null +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_1.xml @@ -0,0 +1,92 @@ + + + + 0085 + + + + + + + CH + 9991 + + + + + + block + + + + + laneChange + + + + + + + + + + + + + + + + + + + + CH + 9991 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_2.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_2.xml new file mode 100644 index 00000000..418f201d --- /dev/null +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_2.xml @@ -0,0 +1,68 @@ + + + + 0085 + + + + + + CH + 9992 + + + + + + + entry + + + + + exit + + + + + + + + + + + + + + + + CH + 9992 + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_3.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_3.xml new file mode 100644 index 00000000..c70c3ec1 --- /dev/null +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_3.xml @@ -0,0 +1,62 @@ + + + + 0085 + + + + + + CH + 9993 + + + + + + intermediate + + + + + + + + + + + + + + + + CH + 9993 + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_4.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_4.xml new file mode 100644 index 00000000..ca6f1c25 --- /dev/null +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_4.xml @@ -0,0 +1,52 @@ + + + + 0085 + + + + + + CH + 9994 + + + + + + protection + + + + + + + + + CH + 9994 + + + + + + + + + + + + + + + + + + + + + + diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_5.xml b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_5.xml new file mode 100644 index 00000000..23d32f55 --- /dev/null +++ b/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_5.xml @@ -0,0 +1,61 @@ + + + + 0085 + + + + + + + CH + 9995 + + + + + block + intermediate + + + + + + + + + + + + + + + CH + 9995 + + + + + + + + + + + + + + + + + + + + + + + +