From 6cdb7d71045cd839dda57c5eae54f5748c0bba77 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 12 Dec 2024 14:16:58 +0100 Subject: [PATCH] chore: new mock data structure (#461) --- sfera-mock/README.md | 13 ++++------- .../sferamock/messages/TrainsActuator.java | 23 +++++++++++++++++++ .../services/JourneyProfileRepository.java | 21 ++++++++++++----- .../services/SegmentProfileRepository.java | 14 +++++++---- .../src/main/resources/application.yaml | 6 +++++ .../SFERA_JP_4816.xml | 0 .../SFERA_SP_4816_1.xml | 0 .../SFERA_SP_4816_2.xml | 0 .../SFERA_JP_500.xml | 0 .../SFERA_SP_500_1.xml | 0 .../SFERA_SP_500_2.xml | 0 .../SFERA_SP_500_3.xml | 0 .../SFERA_JP_513.xml | 0 .../SFERA_SP_513_1.xml | 0 .../SFERA_JP_7839.xml | 0 .../SFERA_SP_7839_1.xml | 0 .../SFERA_JP_9999.xml | 0 .../SFERA_SP_9999_1.xml | 2 +- .../SFERA_SP_9999_2.xml | 0 .../SFERA_SP_9999_3.xml | 0 .../SFERA_SP_9999_4.xml | 0 .../SFERA_SP_9999_5.xml | 0 .../SFERA_JP_T1.xml | 0 .../SFERA_SP_T1_1.xml | 0 .../SFERA_SP_T1_2.xml | 0 .../SFERA_SP_T1_3.xml | 0 .../SFERA_SP_T1_4.xml | 0 .../SFERA_SP_T1_5.xml | 0 28 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 sfera-mock/src/main/java/ch/sbb/sferamock/messages/TrainsActuator.java rename sfera-mock/src/main/resources/static_sfera_resources/{jp => 4816_TAF_TAP_locations}/SFERA_JP_4816.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 4816_TAF_TAP_locations}/SFERA_SP_4816_1.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 4816_TAF_TAP_locations}/SFERA_SP_4816_2.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{jp => 500_ASR_Additional_speed_restrictions}/SFERA_JP_500.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 500_ASR_Additional_speed_restrictions}/SFERA_SP_500_1.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 500_ASR_Additional_speed_restrictions}/SFERA_SP_500_2.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 500_ASR_Additional_speed_restrictions}/SFERA_SP_500_3.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{jp => 513_Protection_sections}/SFERA_JP_513.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 513_Protection_sections}/SFERA_SP_513_1.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{jp => 7839_Stop_on_demand}/SFERA_JP_7839.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 7839_Stop_on_demand}/SFERA_SP_7839_1.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{jp => 9999_Mixed_journey}/SFERA_JP_9999.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 9999_Mixed_journey}/SFERA_SP_9999_1.xml (99%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 9999_Mixed_journey}/SFERA_SP_9999_2.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 9999_Mixed_journey}/SFERA_SP_9999_3.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 9999_Mixed_journey}/SFERA_SP_9999_4.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => 9999_Mixed_journey}/SFERA_SP_9999_5.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{jp => T1_Track_equipment}/SFERA_JP_T1.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => T1_Track_equipment}/SFERA_SP_T1_1.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => T1_Track_equipment}/SFERA_SP_T1_2.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => T1_Track_equipment}/SFERA_SP_T1_3.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => T1_Track_equipment}/SFERA_SP_T1_4.xml (100%) rename sfera-mock/src/main/resources/static_sfera_resources/{sp => T1_Track_equipment}/SFERA_SP_T1_5.xml (100%) diff --git a/sfera-mock/README.md b/sfera-mock/README.md index 36f9c7f4..862f33b2 100644 --- a/sfera-mock/README.md +++ b/sfera-mock/README.md @@ -14,16 +14,11 @@ - architecture: BoardAdviceCalculation - connectivity: Connected -| Train number | Result | -|--------------|--------------------------| -| 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 +see journeys [src/main/resources/static_sfera_resources](src/main/resources/static_sfera_resources) ## 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` +1. add a new directory named `_` in `static_sfera_resources` +2. add a journey profile named `SFERA_JP_` to the directory +2. add corresponding segment profiles named `SFERA_SP__` to the directory diff --git a/sfera-mock/src/main/java/ch/sbb/sferamock/messages/TrainsActuator.java b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/TrainsActuator.java new file mode 100644 index 00000000..0b99ce2f --- /dev/null +++ b/sfera-mock/src/main/java/ch/sbb/sferamock/messages/TrainsActuator.java @@ -0,0 +1,23 @@ +package ch.sbb.sferamock.messages; + +import ch.sbb.sferamock.messages.services.JourneyProfileRepository; +import java.util.Set; +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.stereotype.Component; + +@Component +@Endpoint(id = "trains") +public class TrainsActuator { + + private final JourneyProfileRepository journeyProfileRepository; + + public TrainsActuator(JourneyProfileRepository journeyProfileRepository) { + this.journeyProfileRepository = journeyProfileRepository; + } + + @ReadOperation + public Set getJps() { + return journeyProfileRepository.getAvailableJourneyProfiles(); + } +} 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 0914b285..38b095c1 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 @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.springframework.boot.ApplicationArguments; @@ -20,8 +21,8 @@ @Service public class JourneyProfileRepository implements ApplicationRunner { - private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/jp/*.xml"; - private static final String XML_REGEX = "SFERA_JP_(.+)\\.xml"; + private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/*/SFERA_JP_*.xml"; + private static final String XML_REGEX = "/([a-zA-Z0-9]+)_\\w+/SFERA_JP_([a-zA-Z0-9]+)\\.xml"; private final XmlHelper xmlHelper; Map journeyProfiles = new HashMap<>(); @@ -39,12 +40,16 @@ public Optional getJourneyProfile(TrainIdentification trainIdent return Optional.ofNullable(journeyProfiles.get(trainIdentification.operationalNumber())); } + public Set getAvailableJourneyProfiles() { + return journeyProfiles.keySet(); + } + 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()); + var operationalNumber = extractOperationalNumber(file.getPath()); try (InputStream in = new FileInputStream(file)) { String xmlPayload = new String(in.readAllBytes()); var journeyProfile = xmlHelper.xmlToObject(xmlPayload); @@ -53,12 +58,16 @@ private void importJps() throws IOException { } } - public static String extractOperationalNumber(String filename) { + private static String extractOperationalNumber(String filename) { Pattern pattern = Pattern.compile(XML_REGEX); Matcher matcher = pattern.matcher(filename); if (matcher.find()) { - return matcher.group(1); + String directoryOperationalNumber = matcher.group(1); + String fileOperationalNumber = matcher.group(2); + if (directoryOperationalNumber != null && directoryOperationalNumber.equals(fileOperationalNumber)) { + return directoryOperationalNumber; + } } - return null; + throw new RuntimeException("Operational number extraction in JP repository failed for file: " + filename); } } 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 720c1623..32f2fc0b 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 @@ -20,8 +20,8 @@ @Service public class SegmentProfileRepository implements ApplicationRunner { - private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/sp/*.xml"; - private static final String XML_REGEX = "SFERA_SP_(.+_\\d+)\\.xml"; + private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/*/SFERA_SP_*.xml"; + private static final String XML_REGEX = "/([a-zA-Z0-9]+)_\\w+/SFERA_SP_(([a-zA-Z0-9]+)_\\w+)\\.xml"; private final XmlHelper xmlHelper; @@ -45,7 +45,7 @@ private void importSps() throws IOException { var resources = resolver.getResources(XML_RESOURCES_CLASSPATH); for (var resource : resources) { File file = resource.getFile(); - var segmentId = extractSpId(file.getName()); + var segmentId = extractSpId(file.getPath()); try (InputStream in = new FileInputStream(file)) { String xmlPayload = new String(in.readAllBytes()); var segmentProfile = xmlHelper.xmlToObject(xmlPayload); @@ -58,8 +58,12 @@ public static String extractSpId(String filename) { Pattern pattern = Pattern.compile(XML_REGEX); Matcher matcher = pattern.matcher(filename); if (matcher.find()) { - return matcher.group(1); + String directoryOperationalNumber = matcher.group(1); + String fileOperationalNumber = matcher.group(3); + if (directoryOperationalNumber != null && directoryOperationalNumber.equals(fileOperationalNumber)) { + return matcher.group(2); + } } - return null; + throw new RuntimeException("SP id extraction failed for file: " + filename); } } diff --git a/sfera-mock/src/main/resources/application.yaml b/sfera-mock/src/main/resources/application.yaml index 11fa8edc..3dacc389 100644 --- a/sfera-mock/src/main/resources/application.yaml +++ b/sfera-mock/src/main/resources/application.yaml @@ -11,6 +11,12 @@ management: health: probes: enabled: true + endpoints: + web: + exposure: + include: health,trains + + spring: profiles: diff --git a/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_4816.xml b/sfera-mock/src/main/resources/static_sfera_resources/4816_TAF_TAP_locations/SFERA_JP_4816.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_4816.xml rename to sfera-mock/src/main/resources/static_sfera_resources/4816_TAF_TAP_locations/SFERA_JP_4816.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/4816_TAF_TAP_locations/SFERA_SP_4816_1.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/4816_TAF_TAP_locations/SFERA_SP_4816_1.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_2.xml b/sfera-mock/src/main/resources/static_sfera_resources/4816_TAF_TAP_locations/SFERA_SP_4816_2.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_4816_2.xml rename to sfera-mock/src/main/resources/static_sfera_resources/4816_TAF_TAP_locations/SFERA_SP_4816_2.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_500.xml b/sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_JP_500.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_500.xml rename to sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_JP_500.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_500_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_SP_500_1.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_500_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_SP_500_1.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_500_2.xml b/sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_SP_500_2.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_500_2.xml rename to sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_SP_500_2.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_500_3.xml b/sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_SP_500_3.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_500_3.xml rename to sfera-mock/src/main/resources/static_sfera_resources/500_ASR_Additional_speed_restrictions/SFERA_SP_500_3.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_513.xml b/sfera-mock/src/main/resources/static_sfera_resources/513_Protection_sections/SFERA_JP_513.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_513.xml rename to sfera-mock/src/main/resources/static_sfera_resources/513_Protection_sections/SFERA_JP_513.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_513_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/513_Protection_sections/SFERA_SP_513_1.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_513_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/513_Protection_sections/SFERA_SP_513_1.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_7839.xml b/sfera-mock/src/main/resources/static_sfera_resources/7839_Stop_on_demand/SFERA_JP_7839.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_7839.xml rename to sfera-mock/src/main/resources/static_sfera_resources/7839_Stop_on_demand/SFERA_JP_7839.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_7839_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/7839_Stop_on_demand/SFERA_SP_7839_1.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_7839_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/7839_Stop_on_demand/SFERA_SP_7839_1.xml 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/9999_Mixed_journey/SFERA_JP_9999.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_9999.xml rename to sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_JP_9999.xml 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/9999_Mixed_journey/SFERA_SP_9999_1.xml similarity index 99% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_SP_9999_1.xml index 023b429b..e3a0f659 100644 --- a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_1.xml +++ b/sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_SP_9999_1.xml @@ -102,4 +102,4 @@ - \ No newline at end of file + 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/9999_Mixed_journey/SFERA_SP_9999_2.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_2.xml rename to sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_SP_9999_2.xml 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/9999_Mixed_journey/SFERA_SP_9999_3.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_3.xml rename to sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_SP_9999_3.xml 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/9999_Mixed_journey/SFERA_SP_9999_4.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_4.xml rename to sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_SP_9999_4.xml 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/9999_Mixed_journey/SFERA_SP_9999_5.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_9999_5.xml rename to sfera-mock/src/main/resources/static_sfera_resources/9999_Mixed_journey/SFERA_SP_9999_5.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_T1.xml b/sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_JP_T1.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/jp/SFERA_JP_T1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_JP_T1.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_1.xml b/sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_1.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_1.xml rename to sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_1.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_2.xml b/sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_2.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_2.xml rename to sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_2.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_3.xml b/sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_3.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_3.xml rename to sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_3.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_4.xml b/sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_4.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_4.xml rename to sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_4.xml diff --git a/sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_5.xml b/sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_5.xml similarity index 100% rename from sfera-mock/src/main/resources/static_sfera_resources/sp/SFERA_SP_T1_5.xml rename to sfera-mock/src/main/resources/static_sfera_resources/T1_Track_equipment/SFERA_SP_T1_5.xml