Skip to content

Commit 4c57cc4

Browse files
Add test for serialization
1 parent e4f42f7 commit 4c57cc4

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.opentripplanner.ext.ojp.mapping;
2+
3+
import static org.opentripplanner.framework.io.FileUtils.assertFileEquals;
4+
import static org.opentripplanner.framework.io.FileUtils.writeFile;
5+
6+
import de.vdv.ojp20.OJP;
7+
import jakarta.xml.bind.JAXBContext;
8+
import jakarta.xml.bind.JAXBException;
9+
import jakarta.xml.bind.Marshaller;
10+
import java.io.File;
11+
import java.io.StringWriter;
12+
import java.time.Instant;
13+
import java.time.ZonedDateTime;
14+
import java.util.List;
15+
import java.util.Set;
16+
import org.junit.jupiter.api.Test;
17+
import org.opentripplanner.api.model.transit.DefaultFeedIdMapper;
18+
import org.opentripplanner.model.plan.Itinerary;
19+
import org.opentripplanner.model.plan.PlanTestConstants;
20+
import org.opentripplanner.model.plan.TestItineraryBuilder;
21+
import org.opentripplanner.model.plan.TripPlan;
22+
import org.opentripplanner.routing.api.response.RoutingResponse;
23+
import org.opentripplanner.test.support.ResourceLoader;
24+
25+
class TripResponseMapperTest implements PlanTestConstants {
26+
27+
private static final ResourceLoader LOADER = ResourceLoader.of(TripResponseMapperTest.class);
28+
private static final File XML_FILE = LOADER.extTestResourceFile("trip-response.xml");
29+
30+
@Test
31+
void map() throws JAXBException {
32+
Itinerary itinerary = TestItineraryBuilder.newItinerary(A, T11_00)
33+
.walk(D3m, B)
34+
.bus(1, T11_05, T11_15, C)
35+
.walk(D2m, D)
36+
.build();
37+
38+
// Create routing response with the itinerary
39+
var tripPlan = new TripPlan(A, B, Instant.EPOCH, List.of(itinerary));
40+
var routingResponse = new RoutingResponse(tripPlan, null, null, null, null, null);
41+
42+
// Map to OJP
43+
var idMapper = new DefaultFeedIdMapper();
44+
var mapper = new TripResponseMapper(idMapper, Set.of());
45+
ZonedDateTime timestamp = ZonedDateTime.parse("2026-01-12T10:00:00Z");
46+
OJP ojp = mapper.mapTripPlan(routingResponse, timestamp);
47+
48+
// Serialize to XML using JAXB
49+
JAXBContext jaxbContext = JAXBContext.newInstance(OJP.class);
50+
var marshaller = jaxbContext.createMarshaller();
51+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
52+
53+
StringWriter writer = new StringWriter();
54+
marshaller.marshal(ojp, writer);
55+
String xml = writer.toString();
56+
57+
writeFile(XML_FILE, xml);
58+
assertFileEquals(xml, XML_FILE);
59+
}
60+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<OJP version="2.0" xmlns="http://www.vdv.de/ojp" xmlns:ifopt="http://www.ifopt.org.uk/ifopt" xmlns:opengis="http://www.opengis.net/gml/3.2" xmlns:ns8="http://datex2.eu/schema/2_0RC1/2_0" xmlns:siri="http://www.siri.org.uk/siri" xmlns:acsb="http://www.ifopt.org.uk/acsb" xmlns:netex="http://www.netex.org.uk/netex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<OJPResponse>
4+
<siri:ServiceDelivery>
5+
<siri:ResponseTimestamp>2026-01-12T10:00:00Z</siri:ResponseTimestamp>
6+
<siri:ProducerRef>OpenTripPlanner</siri:ProducerRef>
7+
<OJPTripDelivery>
8+
<siri:ResponseTimestamp>2026-01-12T10:00:00Z</siri:ResponseTimestamp>
9+
<TripResponseContext>
10+
<Places>
11+
<Place>
12+
<StopPoint>
13+
<siri:StopPointRef>F:A</siri:StopPointRef>
14+
<StopPointName>
15+
<Text xsi:type="siri:DefaultedTextStructure">A</Text>
16+
</StopPointName>
17+
</StopPoint>
18+
<Name>
19+
<Text xsi:type="siri:DefaultedTextStructure">A</Text>
20+
</Name>
21+
<GeoPosition>
22+
<siri:Longitude>8.0</siri:Longitude>
23+
<siri:Latitude>5.0</siri:Latitude>
24+
</GeoPosition>
25+
</Place>
26+
<Place>
27+
<StopPoint>
28+
<siri:StopPointRef>F:B</siri:StopPointRef>
29+
<StopPointName>
30+
<Text xsi:type="siri:DefaultedTextStructure">B</Text>
31+
</StopPointName>
32+
</StopPoint>
33+
<Name>
34+
<Text xsi:type="siri:DefaultedTextStructure">B</Text>
35+
</Name>
36+
<GeoPosition>
37+
<siri:Longitude>8.5</siri:Longitude>
38+
<siri:Latitude>6.0</siri:Latitude>
39+
</GeoPosition>
40+
</Place>
41+
<Place>
42+
<StopPoint>
43+
<siri:StopPointRef>F:C</siri:StopPointRef>
44+
<StopPointName>
45+
<Text xsi:type="siri:DefaultedTextStructure">C</Text>
46+
</StopPointName>
47+
</StopPoint>
48+
<Name>
49+
<Text xsi:type="siri:DefaultedTextStructure">C</Text>
50+
</Name>
51+
<GeoPosition>
52+
<siri:Longitude>9.0</siri:Longitude>
53+
<siri:Latitude>7.0</siri:Latitude>
54+
</GeoPosition>
55+
</Place>
56+
<Place>
57+
<StopPoint>
58+
<siri:StopPointRef>F:D</siri:StopPointRef>
59+
<StopPointName>
60+
<Text xsi:type="siri:DefaultedTextStructure">D</Text>
61+
</StopPointName>
62+
</StopPoint>
63+
<Name>
64+
<Text xsi:type="siri:DefaultedTextStructure">D</Text>
65+
</Name>
66+
<GeoPosition>
67+
<siri:Longitude>9.5</siri:Longitude>
68+
<siri:Latitude>8.0</siri:Latitude>
69+
</GeoPosition>
70+
</Place>
71+
</Places>
72+
</TripResponseContext>
73+
<TripResult>
74+
<Id>2d18c6f6-693e-3882-89da-77c0bcfc5f38</Id>
75+
<Trip>
76+
<Id>2d18c6f6-693e-3882-89da-77c0bcfc5f38</Id>
77+
<Duration>PT17M</Duration>
78+
<StartTime>2020-02-02T11:00:00Z</StartTime>
79+
<EndTime>2020-02-02T11:17:00Z</EndTime>
80+
<Transfers>2</Transfers>
81+
<Leg>
82+
<Id>0</Id>
83+
<Duration>PT3M</Duration>
84+
<ContinuousLeg>
85+
<LegStart>
86+
<siri:StopPointRef>F:A</siri:StopPointRef>
87+
<Name>
88+
<Text xsi:type="siri:DefaultedTextStructure">A</Text>
89+
</Name>
90+
</LegStart>
91+
<LegEnd>
92+
<siri:StopPointRef>F:B</siri:StopPointRef>
93+
<Name>
94+
<Text xsi:type="siri:DefaultedTextStructure">B</Text>
95+
</Name>
96+
</LegEnd>
97+
<Service>
98+
<PersonalModeOfOperation>own</PersonalModeOfOperation>
99+
<PersonalMode>foot</PersonalMode>
100+
</Service>
101+
<TimeWindowStart>2020-02-02T11:00:00Z</TimeWindowStart>
102+
<TimeWindowEnd>2020-02-02T11:03:00Z</TimeWindowEnd>
103+
<Duration>PT3M</Duration>
104+
<Length>252</Length>
105+
</ContinuousLeg>
106+
</Leg>
107+
<Leg>
108+
<Id>1</Id>
109+
<Duration>PT10M</Duration>
110+
<TimedLeg>
111+
<LegBoard>
112+
<siri:StopPointRef>F:B</siri:StopPointRef>
113+
<StopPointName>
114+
<Text xsi:type="siri:DefaultedTextStructure">B</Text>
115+
</StopPointName>
116+
<ServiceDeparture>
117+
<TimetabledTime>2020-02-02T11:05:00Z</TimetabledTime>
118+
</ServiceDeparture>
119+
</LegBoard>
120+
<LegAlight>
121+
<siri:StopPointRef>F:C</siri:StopPointRef>
122+
<StopPointName>
123+
<Text xsi:type="siri:DefaultedTextStructure">C</Text>
124+
</StopPointName>
125+
<ServiceArrival>
126+
<TimetabledTime>2020-02-02T11:15:00Z</TimetabledTime>
127+
</ServiceArrival>
128+
</LegAlight>
129+
<Service>
130+
<OperatingDayRef>2020-02-02</OperatingDayRef>
131+
<JourneyRef>F:1</JourneyRef>
132+
<PublicCode>1</PublicCode>
133+
<siri:LineRef>F:1</siri:LineRef>
134+
<siri:DirectionRef>UNKNOWN</siri:DirectionRef>
135+
<Mode>
136+
<PtMode>bus</PtMode>
137+
</Mode>
138+
<PublishedServiceName>
139+
<Text xsi:type="siri:DefaultedTextStructure">1</Text>
140+
</PublishedServiceName>
141+
<OriginStopPointRef>F:B</OriginStopPointRef>
142+
<OriginText>
143+
<Text xsi:type="siri:DefaultedTextStructure">B</Text>
144+
</OriginText>
145+
<siri:OperatorRef>F:A1</siri:OperatorRef>
146+
<DestinationStopPointRef>F:C</DestinationStopPointRef>
147+
<DestinationText>
148+
<Text xsi:type="siri:DefaultedTextStructure">Trip headsign 1</Text>
149+
</DestinationText>
150+
</Service>
151+
</TimedLeg>
152+
</Leg>
153+
<Leg>
154+
<Id>2</Id>
155+
<Duration>PT2M</Duration>
156+
<ContinuousLeg>
157+
<LegStart>
158+
<siri:StopPointRef>F:C</siri:StopPointRef>
159+
<Name>
160+
<Text xsi:type="siri:DefaultedTextStructure">C</Text>
161+
</Name>
162+
</LegStart>
163+
<LegEnd>
164+
<siri:StopPointRef>F:D</siri:StopPointRef>
165+
<Name>
166+
<Text xsi:type="siri:DefaultedTextStructure">D</Text>
167+
</Name>
168+
</LegEnd>
169+
<Service>
170+
<PersonalModeOfOperation>own</PersonalModeOfOperation>
171+
<PersonalMode>foot</PersonalMode>
172+
</Service>
173+
<TimeWindowStart>2020-02-02T11:15:00Z</TimeWindowStart>
174+
<TimeWindowEnd>2020-02-02T11:17:00Z</TimeWindowEnd>
175+
<Duration>PT2M</Duration>
176+
<Length>168</Length>
177+
</ContinuousLeg>
178+
</Leg>
179+
</Trip>
180+
</TripResult>
181+
</OJPTripDelivery>
182+
</siri:ServiceDelivery>
183+
</OJPResponse>
184+
</OJP>

0 commit comments

Comments
 (0)