-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: train characteristics request/reply
- Loading branch information
1 parent
6cdb7d7
commit 29ff808
Showing
9 changed files
with
444 additions
and
4 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ock/src/main/java/ch/sbb/sferamock/messages/model/TrainCharacteristicsIdentification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ch.sbb.sferamock.messages.model; | ||
|
||
import lombok.NonNull; | ||
|
||
public record TrainCharacteristicsIdentification(@NonNull String id, int majorVersion, int minorVersion, @NonNull CompanyCode ruId) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...mock/src/main/java/ch/sbb/sferamock/messages/services/TrainCharacteristicsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ch.sbb.sferamock.messages.services; | ||
|
||
import ch.sbb.sferamock.adapters.sfera.model.v0201.TrainCharacteristics; | ||
import ch.sbb.sferamock.messages.common.XmlHelper; | ||
import ch.sbb.sferamock.messages.model.TrainCharacteristicsIdentification; | ||
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 TrainCharacteristicsRepository implements ApplicationRunner { | ||
|
||
private static final String XML_RESOURCES_CLASSPATH = "classpath:static_sfera_resources/*/SFERA_TC_*.xml"; | ||
private static final String XML_REGEX = "/([a-zA-Z0-9]+)_\\w+/SFERA_TC_(([a-zA-Z0-9]+)_\\w+)\\.xml"; | ||
|
||
private final XmlHelper xmlHelper; | ||
|
||
Map<String, TrainCharacteristics> trainCharacteristics = new HashMap<>(); | ||
|
||
public TrainCharacteristicsRepository(XmlHelper xmlHelper) { | ||
this.xmlHelper = xmlHelper; | ||
} | ||
|
||
public static String extractTcId(String filename) { | ||
Pattern pattern = Pattern.compile(XML_REGEX); | ||
Matcher matcher = pattern.matcher(filename); | ||
if (matcher.find()) { | ||
String directoryOperationalNumber = matcher.group(1); | ||
String fileOperationalNumber = matcher.group(3); | ||
if (directoryOperationalNumber != null && directoryOperationalNumber.equals(fileOperationalNumber)) { | ||
return matcher.group(2); | ||
} | ||
} | ||
throw new RuntimeException("TC id extraction failed for file: " + filename); | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
importTcs(); | ||
} | ||
|
||
public Optional<TrainCharacteristics> getTrainCharacteristics(TrainCharacteristicsIdentification tcId) { | ||
return Optional.ofNullable(trainCharacteristics.get(tcId.id())); | ||
} | ||
|
||
private void importTcs() throws IOException { | ||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); | ||
var resources = resolver.getResources(XML_RESOURCES_CLASSPATH); | ||
for (var resource : resources) { | ||
File file = resource.getFile(); | ||
var tcId = extractTcId(file.getPath()); | ||
try (InputStream in = new FileInputStream(file)) { | ||
String xmlPayload = new String(in.readAllBytes()); | ||
var tc = xmlHelper.xmlToObject(xmlPayload); | ||
this.trainCharacteristics.put(tcId, (TrainCharacteristics) tc); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
sfera-mock/src/main/resources/static_sfera_resources/T5_Breaking_series/SFERA_JP_T5.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0"?> | ||
<JourneyProfile JP_Status="Valid" JP_Version="1" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../SFERA_3.0_custom.xsd"> | ||
<TrainIdentification> | ||
<OTN_ID> | ||
<Company>1085</Company> | ||
<OperationalTrainNumber>T5</OperationalTrainNumber> | ||
<StartDate>2022-01-04</StartDate> | ||
</OTN_ID> | ||
</TrainIdentification> | ||
<SegmentProfileList SP_ID="T5_1" SP_VersionMajor="1" SP_VersionMinor="1" SP_Direction="Nominal"> | ||
<SP_Zone> | ||
<IM_ID>0085</IM_ID> | ||
</SP_Zone> | ||
<TimingPointConstraints TP_StopSkipPass="Stopping_Point" TP_Information="None"> | ||
<TimingPointReference> | ||
<TP_ID_Reference TP_ID="Genève-Aéroport"/> | ||
</TimingPointReference> | ||
</TimingPointConstraints> | ||
<TimingPointConstraints TP_StopSkipPass="Stopping_Point" TP_Information="None"> | ||
<TimingPointReference> | ||
<TP_ID_Reference TP_ID="Genève"/> | ||
</TimingPointReference> | ||
</TimingPointConstraints> | ||
<TimingPointConstraints TP_StopSkipPass="Stopping_Point" TP_Information="None"> | ||
<TimingPointReference> | ||
<TP_ID_Reference TP_ID="Gland"/> | ||
</TimingPointReference> | ||
</TimingPointConstraints> | ||
<TrainCharacteristicsRef TC_ID="T5_1" TC_VersionMajor="1" TC_VersionMinor="1" location="0"> | ||
<TC_RU_ID>1185</TC_RU_ID> | ||
</TrainCharacteristicsRef> | ||
</SegmentProfileList> | ||
</JourneyProfile> |
Oops, something went wrong.