Skip to content

Adds ScheduledStopPointRef->FlexibleStopPointRef map to common data repo #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.entur</groupId>
<artifactId>netex-validator-java</artifactId>
<version>9.2.1-SNAPSHOT</version>
<version>9.3.0-SNAPSHOT</version>

<name>netex-validator-java</name>
<description>Library for validating NeTEx datasets against the Nordic NeTEx Profile.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ FromToScheduledStopPointId fromToScheduledStopPointIdForServiceLink(
ServiceLinkId serviceLinkId,
String validationReportId
);

/**
* Returns flexible stop place ref mapped by provided stopPointRef.
*/
String getFlexibleStopPlaceRefByStopPointRef(
String validationReportId,
String stopPointRef
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public class DefaultCommonDataRepository implements CommonDataRepositoryLoader {

private final Map<String, Map<String, String>> scheduledStopPointAndQuayIdCache;
private final Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache;
private final Map<String, Map<String, String>> scheduledStopPointToFlexibleStopPlaceCache;

/**
* The default constructor initializes synchronized data structures for storing the common data.
*/
public DefaultCommonDataRepository() {
this(
Collections.synchronizedMap(new HashMap<>()),
Collections.synchronizedMap(new HashMap<>()),
Collections.synchronizedMap(new HashMap<>())
);
Expand All @@ -43,11 +45,30 @@ public DefaultCommonDataRepository() {
*/
public DefaultCommonDataRepository(
Map<String, Map<String, String>> scheduledStopPointAndQuayIdCache,
Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache
Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache,
Map<String, Map<String, String>> scheduledStopPointToFlexibleStopPlaceCache
) {
this.scheduledStopPointAndQuayIdCache = scheduledStopPointAndQuayIdCache;
this.serviceLinksAndFromToScheduledStopPointIdCache =
serviceLinksAndFromToScheduledStopPointIdCache;
this.scheduledStopPointToFlexibleStopPlaceCache =
scheduledStopPointToFlexibleStopPlaceCache;
}

@Override
public String getFlexibleStopPlaceRefByStopPointRef(
String validationReportId,
String stopPointRef
) {
Map<String, String> stopPlaceRefToFlexibleStopPlaceMap =
scheduledStopPointToFlexibleStopPlaceCache.get(validationReportId);
if (stopPlaceRefToFlexibleStopPlaceMap == null) {
throw new NetexValidationException(
"Flexible stop place cache not found for validation report with id: " +
validationReportId
);
}
return stopPlaceRefToFlexibleStopPlaceMap.get(stopPointRef);
}

@Override
Expand Down Expand Up @@ -109,6 +130,15 @@ public void collect(
}
);

scheduledStopPointToFlexibleStopPlaceCache.merge(
validationReportId,
netexEntitiesIndex.getFlexibleStopPlaceIdByStopPointRefIndex(),
(existingMap, newMap) -> {
existingMap.putAll(newMap);
return existingMap;
}
);

Map<String, String> scheduledStopPointIdsPerServiceLinkId =
getFromToScheduledStopPointIdPerServiceLinkId(netexEntitiesIndex)
.entrySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,28 @@ public TransportModeAndSubMode transportModeAndSubMode(
flexibleLine.getTransportSubmode()
);
}

/**
* Returns the FlexibleStopPlaceRef connected to ScheduledStopPointRef in a FlexibleStopAssignment.
*/
@Nullable
public String flexibleStopPlaceRefFromScheduledStopPointRef(
String scheduledStopPointRef
) {
String flexibleStopPlaceRefFromCommon =
commonDataRepository.getFlexibleStopPlaceRefByStopPointRef(
validationReportId,
scheduledStopPointRef
);

String flexibleStopPlaceRefFromNetexEntities = netexEntitiesIndex
.getFlexibleStopPlaceIdByStopPointRefIndex()
.get(scheduledStopPointRef);

if (flexibleStopPlaceRefFromNetexEntities == null) {
return flexibleStopPlaceRefFromCommon;
}

return flexibleStopPlaceRefFromNetexEntities;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class DefaultCommonDataRepositoryTest {
public static final String TEST_SCHEDULED_STOP_POINT_ID_1 =
"TST:ScheduledStopPoint:1";

public static final String TEST_SCHEDULED_STOP_POINT_REF_ID =
"TST:ScheduledStopPointRef:1";

public static final String TEST_FLEXIBLE_STOP_POINT_REF_ID =
"TST:FlexibleStopPlaceRef:1";

public static final String TEST_QUAY_ID_1 = "TST:Quay:1";

public static final String TEST_SCHEDULED_STOP_POINT_ID_2 =
Expand All @@ -32,6 +38,37 @@ class DefaultCommonDataRepositoryTest {

public static final String TEST_SERVICE_LINK_ID = "TST:ServiceLink:1";

@Test
void testStopPlaceToFlexibleStopPlaceMapping() {
DefaultCommonDataRepository repository = new DefaultCommonDataRepository();
NetexEntitiesIndex netexEntitiesIndex = new NetexEntitiesIndexImpl();

netexEntitiesIndex
.getFlexibleStopPlaceIdByStopPointRefIndex()
.put(TEST_SCHEDULED_STOP_POINT_REF_ID, TEST_FLEXIBLE_STOP_POINT_REF_ID);
repository.collect(TEST_REPORT_ID, netexEntitiesIndex);
assertEquals(
TEST_FLEXIBLE_STOP_POINT_REF_ID,
repository.getFlexibleStopPlaceRefByStopPointRef(
TEST_REPORT_ID,
TEST_SCHEDULED_STOP_POINT_REF_ID
)
);
}

@Test
void testStopPlaceToFlexibleStopPlaceRefMappingThrowsWhenNoValidationReport() {
DefaultCommonDataRepository repository = new DefaultCommonDataRepository();
assertThrows(
NetexValidationException.class,
() ->
repository.getFlexibleStopPlaceRefByStopPointRef(
TEST_REPORT_ID,
TEST_SCHEDULED_STOP_POINT_REF_ID
)
);
}

@Test
void testSharedScheduledStopPoints() {
DefaultCommonDataRepository repository = new DefaultCommonDataRepository();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import static org.entur.netex.validation.test.jaxb.support.JAXBUtils.createJaxbElement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.*;

import java.util.HashMap;
import java.util.Map;
import org.entur.netex.index.api.NetexEntitiesIndex;
import org.entur.netex.index.impl.NetexEntitiesIndexImpl;
import org.entur.netex.validation.validator.DataLocation;
import org.entur.netex.validation.validator.id.IdVersion;
import org.entur.netex.validation.validator.model.TransportModeAndSubMode;
import org.entur.netex.validation.validator.model.TransportSubMode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.rutebanken.netex.model.AllVehicleModesOfTransportEnumeration;
Expand Down Expand Up @@ -205,4 +209,84 @@ void transportModeAndSubModeDefinedOnServiceJourneyFromServiceJourney() {
transportModeAndSubMode.subMode()
);
}

@Test
void flexibleStopPlaceRefFromScheduledStopPointRefInCommonDataRepositoryTest() {
String scheduledStopPointRef = "scheduledStopPointRef";
String validationReportId = "testValidationReportId";
String flexibleStopPointRef = "flexibleStopPointRef";
Map<String, String> scheduledStopPointRefMap = Map.ofEntries(
Map.entry(scheduledStopPointRef, flexibleStopPointRef)
);

Map<String, Map<String, String>> validationReportMap = Map.ofEntries(
Map.entry(validationReportId, scheduledStopPointRefMap)
);
CommonDataRepository commonDataRepository = new DefaultCommonDataRepository(
new HashMap<>(),
new HashMap<>(),
validationReportMap
);

CommonDataRepository commonDataRepositorySpy = spy(commonDataRepository);

JAXBValidationContext context = new JAXBValidationContext(
validationReportId,
netexEntitiesIndex,
commonDataRepositorySpy,
null,
null,
FILE_NAME,
Map.of()
);
String result = context.flexibleStopPlaceRefFromScheduledStopPointRef(
scheduledStopPointRef
);

Assertions.assertEquals(flexibleStopPointRef, result);
verify(commonDataRepositorySpy, times(1))
.getFlexibleStopPlaceRefByStopPointRef(
validationReportId,
scheduledStopPointRef
);
}

@Test
void flexibleStopPlaceRefFromScheduledStopPointRefInNetexEntitiesIndexTest() {
String scheduledStopPointRef = "scheduledStopPointRef";
String validationReportId = "testValidationReportId";
String flexibleStopPointRef2 = "flexibleStopPointRef2";

Map<String, String> scheduledStopPointRefMap = Map.ofEntries(
Map.entry(scheduledStopPointRef, "flexibleStopPointRef1")
);
Map<String, Map<String, String>> validationReportMap = Map.ofEntries(
Map.entry(validationReportId, scheduledStopPointRefMap)
);

CommonDataRepository commonDataRepository = new DefaultCommonDataRepository(
new HashMap<>(),
new HashMap<>(),
validationReportMap
);
NetexEntitiesIndex netexEntitiesIndex = new NetexEntitiesIndexImpl();
netexEntitiesIndex
.getFlexibleStopPlaceIdByStopPointRefIndex()
.put(scheduledStopPointRef, flexibleStopPointRef2);

JAXBValidationContext context = new JAXBValidationContext(
validationReportId,
netexEntitiesIndex,
commonDataRepository,
null,
null,
FILE_NAME,
Map.of()
);

String result = context.flexibleStopPlaceRefFromScheduledStopPointRef(
scheduledStopPointRef
);
Assertions.assertEquals(flexibleStopPointRef2, result);
}
}