diff --git a/src/main/java/no/entur/mummu/resources/RestResource.java b/src/main/java/no/entur/mummu/resources/RestResource.java index cf75306..940e737 100644 --- a/src/main/java/no/entur/mummu/resources/RestResource.java +++ b/src/main/java/no/entur/mummu/resources/RestResource.java @@ -14,6 +14,8 @@ import org.rutebanken.netex.model.Parking; import org.rutebanken.netex.model.ParkingsInFrame_RelStructure; import org.rutebanken.netex.model.Quay; +import org.rutebanken.netex.model.ScheduledStopPoint; +import org.rutebanken.netex.model.ScheduledStopPointsInFrame_RelStructure; import org.rutebanken.netex.model.StopPlace; import org.rutebanken.netex.model.StopPlacesInFrame_RelStructure; import org.rutebanken.netex.model.StopTypeEnumeration; @@ -108,6 +110,17 @@ public JAXBElement getJAXBElementStopPlaceById(@PathVariable String i return netexObjectFactory.createStopPlace(stopPlace); } + @GetMapping(value = "/stop-places/{id}/scheduled-stop-points", produces = "application/json") + public Collection getScheduledStopPointsForStopPlace(@PathVariable String id) { + return netexEntitiesService.getScheduledStopPointsForStopPlaceWithId(id); + } + + @GetMapping(value = "/stop-places/{id}/scheduled-stop-points", produces = "application/xml") + public JAXBElement getJAXBElementScheduledStopPointsForStopPlace(@PathVariable String id) { + var scheduledStopPoints = netexEntitiesService.getScheduledStopPointsForStopPlaceWithId(id); + return netexObjectFactory.createScheduledStopPoints(scheduledStopPoints); + } + @GetMapping(value = "/stop-places/{id}/versions", produces = "application/json") public Collection getStopPlaceVersions(@PathVariable String id) { return netexEntitiesService.getStopPlaceVersions(id); @@ -441,4 +454,31 @@ public JAXBElement getJAXBElementFareZoneVersion(@PathVariable String var fareZone = netexEntitiesService.getFareZoneVersion(id, version); return netexObjectFactory.createFareZone(fareZone); } + + @GetMapping(value = "/scheduled-stop-points", produces = "application/json") + public List getScheduledStopPoints( + @RequestParam(defaultValue = "10") Integer count, + @RequestParam(defaultValue = "0") Integer skip + ) { + return netexEntitiesService.getScheduledStopPoints(count, skip); + } + + @GetMapping(value = "/scheduled-stop-points", produces = "application/xml") + public JAXBElement getJAXBElementScheduledStopPoints( + @RequestParam(defaultValue = "10") Integer count, + @RequestParam(defaultValue = "0") Integer skip + ) { + var scheduledStopPoints = netexEntitiesService.getScheduledStopPoints(count, skip); + return netexObjectFactory.createScheduledStopPoints(scheduledStopPoints); + } + + @GetMapping(value = "/scheduled-stop-points/{id}", produces = "application/json") + public ScheduledStopPoint getScheduledStopPointById(@PathVariable String id) { + return netexEntitiesService.getScheduledStopPoint(id); + } + + @GetMapping(value = "/scheduled-stop-points/{id}", produces = "application/xml") + public JAXBElement getJAXBElementScheduledStopPointById(@PathVariable String id) { + return netexObjectFactory.createScheduledStopPoint(netexEntitiesService.getScheduledStopPoint(id)); + } } diff --git a/src/main/java/no/entur/mummu/services/NetexEntitiesService.java b/src/main/java/no/entur/mummu/services/NetexEntitiesService.java index 1776542..c4a8527 100644 --- a/src/main/java/no/entur/mummu/services/NetexEntitiesService.java +++ b/src/main/java/no/entur/mummu/services/NetexEntitiesService.java @@ -18,6 +18,7 @@ import org.rutebanken.netex.model.GroupOfTariffZones; import org.rutebanken.netex.model.Parking; import org.rutebanken.netex.model.Quay; +import org.rutebanken.netex.model.ScheduledStopPoint; import org.rutebanken.netex.model.StopPlace; import org.rutebanken.netex.model.StopTypeEnumeration; import org.rutebanken.netex.model.TariffZone; @@ -316,4 +317,29 @@ public FareZone getFareZoneVersion(String id, String version) { netexEntitiesIndex.getFareZoneIndex().getVersion(id, version) ).orElseThrow(NotFoundException::new); } + + public List getScheduledStopPoints( + Integer count, + Integer skip + ) { + return netexEntitiesIndex.getScheduledStopPointIndex().getLatestVersions().stream() + .sorted(new NetexIdComparator()) + .skip(skip) + .limit(count) + .collect(Collectors.toList()); + } + + public Collection getScheduledStopPointsForStopPlaceWithId(String id) { + return netexEntitiesIndex.getPassengerStopAssignmentsByStopPointRefIndex().entries().stream().filter(entry -> { + var passengerStopAssignment = entry.getValue(); + return passengerStopAssignment.getStopPlaceRef() != null && passengerStopAssignment.getStopPlaceRef().getRef().equals(id); + }).map(entry -> { + var stopPointRef = entry.getKey(); + return netexEntitiesIndex.getScheduledStopPointIndex().getLatestVersion(stopPointRef); + }).collect(Collectors.toSet()); + } + + public ScheduledStopPoint getScheduledStopPoint(String id) { + return netexEntitiesIndex.getScheduledStopPointIndex().getLatestVersion(id); + } } diff --git a/src/main/java/no/entur/mummu/services/NetexObjectFactory.java b/src/main/java/no/entur/mummu/services/NetexObjectFactory.java index 26e5d9c..ae21d96 100644 --- a/src/main/java/no/entur/mummu/services/NetexObjectFactory.java +++ b/src/main/java/no/entur/mummu/services/NetexObjectFactory.java @@ -10,6 +10,8 @@ import org.rutebanken.netex.model.ObjectFactory; import org.rutebanken.netex.model.Parking; import org.rutebanken.netex.model.ParkingsInFrame_RelStructure; +import org.rutebanken.netex.model.ScheduledStopPoint; +import org.rutebanken.netex.model.ScheduledStopPointsInFrame_RelStructure; import org.rutebanken.netex.model.StopPlace; import org.rutebanken.netex.model.StopPlacesInFrame_RelStructure; import org.rutebanken.netex.model.TariffZone; @@ -33,6 +35,7 @@ public class NetexObjectFactory extends ObjectFactory { private static final QName _groupsOfTariffZones_QNAME = new QName(NAMESPACE_URI, "groupsOfTariffZones"); private static final QName _parkings_QNAME = new QName(NAMESPACE_URI, "parkings"); private static final QName _topographicPlaces_QNAME = new QName(NAMESPACE_URI, "topographicPlaces"); + private static final QName _scheduledStopPoints_QNAME = new QName(NAMESPACE_URI, "scheduledStopPoints"); public JAXBElement createGroupsOfStopPlaces(List groupsOfStopPlaces) { var groupsOfStopPlacesInFrame = createGroupsOfStopPlacesInFrame_RelStructure().withGroupOfStopPlaces(groupsOfStopPlaces); @@ -72,4 +75,9 @@ public JAXBElement createTopographicPlace var topographicPlacesInFrame = createTopographicPlacesInFrame_RelStructure().withTopographicPlace(topographicPlaces); return new JAXBElement<>(_topographicPlaces_QNAME, TopographicPlacesInFrame_RelStructure.class, topographicPlacesInFrame); } + + public JAXBElement createScheduledStopPoints(Collection scheduledStopPoints) { + var scheduledStopPointsInFrame = createScheduledStopPointsInFrame_RelStructure().withScheduledStopPoint(scheduledStopPoints); + return new JAXBElement<>(_scheduledStopPoints_QNAME, ScheduledStopPointsInFrame_RelStructure.class, scheduledStopPointsInFrame); + } } diff --git a/src/test/java/no/entur/mummu/RestResourceIntegrationTest.java b/src/test/java/no/entur/mummu/RestResourceIntegrationTest.java index 0e7d294..0497d37 100644 --- a/src/test/java/no/entur/mummu/RestResourceIntegrationTest.java +++ b/src/test/java/no/entur/mummu/RestResourceIntegrationTest.java @@ -3,6 +3,7 @@ import jakarta.xml.bind.JAXBElement; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.rutebanken.netex.model.ScheduledStopPoint; import org.rutebanken.netex.model.StopPlace; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -283,4 +284,45 @@ void testXMLOutputCanBeMarshalled() throws Exception { JAXBElement stopPlace = (JAXBElement) unmarshaller.unmarshal(new ByteArrayInputStream(contentAsString.getBytes())); Assertions.assertEquals("NSR:StopPlace:4004", stopPlace.getValue().getId()); } + + @Test + void testGetScheduledStopPointForStopPlace() throws Exception { + mvc.perform(get("/stop-places/NSR:StopPlace:4004/scheduled-stop-points")) + .andExpect(status().isOk()) + .andExpect(content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$[0].name.value").value("Jernbanetorget")); + } + + @Test + void testGetScheduledStopPoints() throws Exception { + mvc.perform(get("/scheduled-stop-points")) + .andExpect(status().isOk()) + .andExpect(content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$[0].name.value").value("Jernbanetorget")); + } + + @Test + void testGetScheduledStopPointById() throws Exception { + mvc.perform(get("/scheduled-stop-points/NSR:ScheduledStopPoint:S4004")) + .andExpect(status().isOk()) + .andExpect(content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.name.value").value("Jernbanetorget")); + } + + @Test + void testGetScheduledStopPointByIdAsXML() throws Exception { + ResultActions resultActions = mvc.perform(get("/scheduled-stop-points/NSR:ScheduledStopPoint:S4004") + .accept(MediaType.APPLICATION_XML)) + .andExpect(status().isOk()); + MvcResult mvcResult = resultActions.andReturn(); + String contentAsString = mvcResult.getResponse().getContentAsString(); + Unmarshaller unmarshaller = JAXBContext + .newInstance(ScheduledStopPoint.class) + .createUnmarshaller(); + JAXBElement scheduledStopPoint = (JAXBElement) unmarshaller.unmarshal(new ByteArrayInputStream(contentAsString.getBytes())); + Assertions.assertEquals("NSR:ScheduledStopPoint:S4004", scheduledStopPoint.getValue().getId()); + } } diff --git a/src/test/resources/IntegrationTestFixture.xml b/src/test/resources/IntegrationTestFixture.xml index 7a58ef3..a0c16c0 100644 --- a/src/test/resources/IntegrationTestFixture.xml +++ b/src/test/resources/IntegrationTestFixture.xml @@ -9,6 +9,50 @@ Europe/Oslo + + + + 2017-06-19T19:12:29.887 + 2021-10-05T12:11:14.999 + + Jernbanetorget + + + + Jernbanetorget + + + + 2017-06-19T19:33:17.697 + + Norefjell skiheis + + + + + + 2017-06-19T19:12:29.887 + 2021-10-05T12:11:14.999 + + + + + + + 2017-06-19T19:12:30.689 + 2018-01-18T12:00:54.999 + + + + + + + 2017-06-19T19:33:17.697 + + + + + Site frame ExportParams{topographicPlaceExportMode=RELEVANT, municipalityReferences=[], countyReferences=[], countryReferences=[], stopPlaceSearch=StopPlaceSearch{stopPlaceType=[], netexIdList=[], allVersions=false, versionValidity=CURRENT, withoutLocationOnly=false, withoutQuaysOnly=false, withDuplicatedQuayImportedIds=false, withTags=[], tags=[], page=0, size=20}, tariffZoneExportMode=RELEVANT, serviceFrameExportMode=ALL} @@ -1041,4 +1085,4 @@ - \ No newline at end of file + diff --git a/src/test/resources/IntegrationTestFixture.xml.zip b/src/test/resources/IntegrationTestFixture.xml.zip index 5bbfb7f..84a2cab 100644 Binary files a/src/test/resources/IntegrationTestFixture.xml.zip and b/src/test/resources/IntegrationTestFixture.xml.zip differ