Skip to content

Commit

Permalink
Add test for EntityResolver
Browse files Browse the repository at this point in the history
Add test for EntityResolver
  • Loading branch information
leonardehrenfried committed Jan 17, 2025
1 parent 6edb414 commit 254566f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class OtpTransitServiceImpl implements OtpTransitService {
this.tripPatterns = immutableList(builder.getTripPatterns().values());
this.trips = immutableList(builder.getTripsById().values());
this.flexTrips = immutableList(builder.getFlexTripsById().values());
this.stopsByScheduledStopPoint = Collections.unmodifiableMap(builder.stopsByScheduledStopPoints());
this.stopsByScheduledStopPoint =
Collections.unmodifiableMap(builder.stopsByScheduledStopPoints());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ private void parseStopAssignments(@Nullable StopAssignmentsInFrame_RelStructure

for (JAXBElement<?> stopAssignment : stopAssignments.getStopAssignment()) {
if (stopAssignment.getValue() instanceof PassengerStopAssignment assignment) {

if (assignment.getQuayRef() == null) {
PASSENGER_STOP_ASSIGNMENT_LOGGER.info(
"PassengerStopAssignment with empty quay ref is dropped. Assigment: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ public void mapNetexToOtp(NetexEntityIndexReadOnlyView netexIndex) {
addEntriesToGroupMapperForPostProcessingLater();
}



/* PRIVATE METHODS */

private void setupGroupMapping() {
Expand Down Expand Up @@ -532,13 +530,15 @@ private void addEntriesToGroupMapperForPostProcessingLater() {
}

private void mapScheduledStopPointsToQuays() {
currentNetexIndex.getQuayIdByStopPointRef().localKeys().forEach(id -> {
var sspid = idFactory.createId(id);
var stopId = idFactory.createId(currentNetexIndex.getQuayIdByStopPointRef().lookup(id));
var stop = Objects.requireNonNull(transitBuilder.getStops().get(stopId));
System.out.printf("%s -> %s%n", sspid, stopId);
transitBuilder.stopsByScheduledStopPoints().put(sspid, stop);
});
currentNetexIndex
.getQuayIdByStopPointRef()
.localKeys()
.forEach(id -> {
var sspid = idFactory.createId(id);
var stopId = idFactory.createId(currentNetexIndex.getQuayIdByStopPointRef().lookup(id));
var stop = Objects.requireNonNull(transitBuilder.getStops().get(stopId));
transitBuilder.stopsByScheduledStopPoints().put(sspid, stop);
});
}

private void mapVehicleParkings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ public Trip resolveTrip(String serviceJourneyId) {
*/
public RegularStop resolveQuay(String stopPointRef) {
var id = resolveId(stopPointRef);
return transitService.findStopByScheduledStopPoint(id).orElseGet(() -> transitService.getRegularStop(id));
return transitService
.findStopByScheduledStopPoint(id)
.orElseGet(() -> transitService.getRegularStop(id));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.opentripplanner.updater.siri;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.opentripplanner.transit.model._data.TimetableRepositoryForTest;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.SiteRepository;
import org.opentripplanner.transit.service.TimetableRepository;

class EntityResolverTest {

private static final TimetableRepositoryForTest TEST_MODEL = TimetableRepositoryForTest.of();
private static final RegularStop STOP_1 = TEST_MODEL.stop("stop-1").build();
private static final RegularStop STOP_2 = TEST_MODEL.stop("stop-2").build();
private static final SiteRepository SITE_REPOSITORY = TEST_MODEL
.siteRepositoryBuilder()
.withRegularStops(List.of(STOP_1, STOP_2))
.build();
private static final String FEED_ID = STOP_1.getId().getFeedId();
private static final FeedScopedId SSP_ID = new FeedScopedId(FEED_ID, "ssp-1");

@Test
void resolveScheduledStopPointId() {
var timetableRepository = new TimetableRepository();
timetableRepository.addScheduledStopPointMapping(Map.of(SSP_ID, STOP_1));
var transitService = new DefaultTransitService(timetableRepository);
var resolver = new EntityResolver(transitService, FEED_ID);
var stop = resolver.resolveQuay(SSP_ID.getId());
assertEquals(STOP_1, stop);
}

@Test
void resolveQuayId() {
var timetableRepository = new TimetableRepository(SITE_REPOSITORY, new Deduplicator());
var transitService = new DefaultTransitService(timetableRepository);
var resolver = new EntityResolver(transitService, FEED_ID);
var stop = resolver.resolveQuay(STOP_1.getId().getId());
assertEquals(STOP_1, stop);
}

@Test
void scheduledStopPointTakesPrecedence() {
var timetableRepository = new TimetableRepository(SITE_REPOSITORY, new Deduplicator());
var transitService = new DefaultTransitService(timetableRepository);
timetableRepository.addScheduledStopPointMapping(Map.of(SSP_ID, STOP_2));
var resolver = new EntityResolver(transitService, FEED_ID);
assertEquals(STOP_2, resolver.resolveQuay(SSP_ID.getId()));
assertEquals(STOP_1, resolver.resolveQuay(STOP_1.getId().getId()));
}
}

0 comments on commit 254566f

Please sign in to comment.