Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
114 changes: 114 additions & 0 deletions src/test/java/org/rutebanken/netex/model/MarshalUnmarshalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;

Check warning on line 38 in src/test/java/org/rutebanken/netex/model/MarshalUnmarshalTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'java.time.Duration'.

See more on https://sonarcloud.io/project/issues?id=entur_netex-java-model&issues=AZzCyRhFHyeRHqrArIWA&open=AZzCyRhFHyeRHqrArIWA&pullRequest=278
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
Expand Down Expand Up @@ -295,6 +296,119 @@
assertThat(departureTimeOffset).isNotNull().hasToString("08:40");
}

@Test
void networkWithAuthorityRefRoundTrip() throws JAXBException {
Marshaller marshaller = jaxbContext.createMarshaller();

Network network = factory.createNetwork()
.withVersion("1").withId("TST:Network:1")
.withName(factory.createMultilingualString().withValue("Test Network"))
.withTransportOrganisationRef(factory.createAuthorityRef(
new AuthorityRef().withRef("TST:Authority:1")));

PublicationDeliveryStructure publicationDelivery = new PublicationDeliveryStructure()
.withPublicationTimestamp(LocalDateTime.now().withNano(0))
.withParticipantRef("test")
.withDataObjects(new PublicationDeliveryStructure.DataObjects()
.withCompositeFrameOrCommonFrame(factory.createCompositeFrame(
factory.createCompositeFrame()
.withVersion("1").withId("TST:CompositeFrame:1")
.withFrames(new Frames_RelStructure()
.withCommonFrame(factory.createServiceFrame(
factory.createServiceFrame()
.withVersion("1").withId("TST:ServiceFrame:1")
.withNetwork(network)))))));

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
marshaller.marshal(factory.createPublicationDelivery(publicationDelivery), byteArrayOutputStream);

String xml = byteArrayOutputStream.toString();

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

@SuppressWarnings("unchecked")
JAXBElement<PublicationDeliveryStructure> jaxbElement = (JAXBElement<PublicationDeliveryStructure>) unmarshaller
.unmarshal(new ByteArrayInputStream(xml.getBytes()));

PublicationDeliveryStructure actual = jaxbElement.getValue();
CompositeFrame compositeFrame = (CompositeFrame) actual.getDataObjects().getCompositeFrameOrCommonFrame().get(0).getValue();
ServiceFrame serviceFrame = (ServiceFrame) compositeFrame.getFrames().getCommonFrame().get(0).getValue();
Network actualNetwork = serviceFrame.getNetwork();

assertThat(actualNetwork.getName().getValue()).isEqualTo("Test Network");
assertThat(actualNetwork.getTransportOrganisationRef().getValue().getRef()).isEqualTo("TST:Authority:1");
}

@Test
void flexibleLineRoundTrip() throws JAXBException {
Marshaller marshaller = jaxbContext.createMarshaller();

FlexibleLine flexibleLine = new FlexibleLine()
.withVersion("1").withId("TST:FlexibleLine:1")
.withName(factory.createMultilingualString().withValue("Flex Route"))
.withTransportMode(AllVehicleModesOfTransportEnumeration.BUS)
.withFlexibleLineType(FlexibleLineTypeEnumeration.FLEXIBLE_AREAS_ONLY)
.withBookingAccess(BookingAccessEnumeration.PUBLIC)
.withBookWhen(PurchaseWhenEnumeration.DAY_OF_TRAVEL_ONLY)
.withLatestBookingTime(LocalTime.of(14, 0))
.withBookingContact(new ContactStructure().withPhone("+47 11223344").withUrl("https://flex.example.com"));

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
marshaller.marshal(factory.createFlexibleLine(flexibleLine), byteArrayOutputStream);

String xml = byteArrayOutputStream.toString();

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

@SuppressWarnings("unchecked")
JAXBElement<FlexibleLine> jaxbElement = (JAXBElement<FlexibleLine>) unmarshaller
.unmarshal(new ByteArrayInputStream(xml.getBytes()));

FlexibleLine actual = jaxbElement.getValue();
assertThat(actual.getName().getValue()).isEqualTo("Flex Route");
assertThat(actual.getTransportMode()).isEqualTo(AllVehicleModesOfTransportEnumeration.BUS);
assertThat(actual.getFlexibleLineType()).isEqualTo(FlexibleLineTypeEnumeration.FLEXIBLE_AREAS_ONLY);
assertThat(actual.getBookingAccess()).isEqualTo(BookingAccessEnumeration.PUBLIC);
assertThat(actual.getBookWhen()).isEqualTo(PurchaseWhenEnumeration.DAY_OF_TRAVEL_ONLY);
assertThat(actual.getLatestBookingTime()).isEqualTo(LocalTime.of(14, 0));
assertThat(actual.getBookingContact().getPhone()).isEqualTo("+47 11223344");
assertThat(actual.getBookingContact().getUrl()).isEqualTo("https://flex.example.com");
}

@Test
void blockRoundTrip() throws JAXBException {
Marshaller marshaller = jaxbContext.createMarshaller();

Block block = new Block()
.withVersion("1").withId("TST:Block:1")
.withName(factory.createMultilingualString().withValue("Test Block"))
.withDescription(factory.createMultilingualString().withValue("Block description"))
.withPrivateCode(new PrivateCodeStructure().withValue("BLK001"))
.withStartTime(LocalTime.of(6, 0))
.withEndTime(LocalTime.of(14, 30));

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
marshaller.marshal(factory.createBlock(block), byteArrayOutputStream);

String xml = byteArrayOutputStream.toString();

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

@SuppressWarnings("unchecked")
JAXBElement<Block> jaxbElement = (JAXBElement<Block>) unmarshaller
.unmarshal(new ByteArrayInputStream(xml.getBytes()));

Block actual = jaxbElement.getValue();
assertThat(actual.getName().getValue()).isEqualTo("Test Block");
assertThat(actual.getDescription().getValue()).isEqualTo("Block description");
assertThat(actual.getPrivateCode().getValue()).isEqualTo("BLK001");
assertThat(actual.getStartTime()).isEqualTo(LocalTime.of(6, 0));
assertThat(actual.getEndTime()).isEqualTo(LocalTime.of(14, 30));
}

@Test
void fragmentShouldNotContainNetexNamespace() throws Exception {
JAXBContext netexJaxBContext = JAXBContext.newInstance("net.opengis.gml._3:org.rutebanken.netex.model:uk.org.siri.siri");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/

package org.rutebanken.netex.model;

import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;

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

class UnmarshalResourceFrameExtendedTest extends AbstractUnmarshalFrameTest {

@Test
void unmarshalAuthorityWithContactDetails() throws JAXBException {

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<PublicationDelivery xmlns=\"http://www.netex.org.uk/netex\" version=\"1.13:NO-NeTEx-networktimetable:1.3\">\n" +
" <PublicationTimestamp>2023-08-03T00:20:33.505</PublicationTimestamp>\n" +
" <ParticipantRef>RB</ParticipantRef>\n" +
" <dataObjects>\n" +
" <CompositeFrame version=\"1\" id=\"TST:CompositeFrame:1\">\n" +
" <frames>\n" +
" <ResourceFrame version=\"1\" id=\"TST:ResourceFrame:1\">\n" +
" <organisations>\n" +
" <Authority version=\"1\" id=\"TST:Authority:1\">\n" +
" <Name>Test Authority</Name>\n" +
" <ContactDetails>\n" +
" <Phone>+47 99887766</Phone>\n" +
" <Url>https://www.testauthority.no</Url>\n" +
" </ContactDetails>\n" +
" </Authority>\n" +
" </organisations>\n" +
" </ResourceFrame>\n" +
" </frames>\n" +
" </CompositeFrame>\n" +
" </dataObjects>\n" +
"</PublicationDelivery>";

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

@SuppressWarnings("unchecked")
JAXBElement<PublicationDeliveryStructure> jaxbElement = (JAXBElement<PublicationDeliveryStructure>) unmarshaller
.unmarshal(new ByteArrayInputStream(xml.getBytes()));

PublicationDeliveryStructure publicationDeliveryStructure = jaxbElement.getValue();
CompositeFrame compositeFrame = (CompositeFrame) publicationDeliveryStructure.getDataObjects().getCompositeFrameOrCommonFrame().get(0).getValue();

ResourceFrame resourceFrame = (ResourceFrame) compositeFrame.getFrames().getCommonFrame().get(0).getValue();

Authority authority = (Authority) resourceFrame.getOrganisations().getOrganisation_().get(0).getValue();
assertEquals("Test Authority", authority.getName().getValue());
assertEquals("+47 99887766", authority.getContactDetails().getPhone());
assertEquals("https://www.testauthority.no", authority.getContactDetails().getUrl());
}

@Test
void unmarshalOperatorWithBrandingRef() throws JAXBException {

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<PublicationDelivery xmlns=\"http://www.netex.org.uk/netex\" version=\"1.13:NO-NeTEx-networktimetable:1.3\">\n" +
" <PublicationTimestamp>2023-08-03T00:20:33.505</PublicationTimestamp>\n" +
" <ParticipantRef>RB</ParticipantRef>\n" +
" <dataObjects>\n" +
" <CompositeFrame version=\"1\" id=\"TST:CompositeFrame:1\">\n" +
" <frames>\n" +
" <ResourceFrame version=\"1\" id=\"TST:ResourceFrame:1\">\n" +
" <typesOfValue>\n" +
" <Branding version=\"1\" id=\"TST:Branding:1\">\n" +
" <Name>Test Brand</Name>\n" +
" <Url>https://www.testbrand.no</Url>\n" +
" <Image>https://www.testbrand.no/logo.png</Image>\n" +
" </Branding>\n" +
" </typesOfValue>\n" +
" <organisations>\n" +
" <Operator version=\"1\" id=\"TST:Operator:1\">\n" +
" <BrandingRef ref=\"TST:Branding:1\"/>\n" +
" <Name>Test Operator</Name>\n" +
" </Operator>\n" +
" </organisations>\n" +
" </ResourceFrame>\n" +
" </frames>\n" +
" </CompositeFrame>\n" +
" </dataObjects>\n" +
"</PublicationDelivery>";

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

@SuppressWarnings("unchecked")
JAXBElement<PublicationDeliveryStructure> jaxbElement = (JAXBElement<PublicationDeliveryStructure>) unmarshaller
.unmarshal(new ByteArrayInputStream(xml.getBytes()));

PublicationDeliveryStructure publicationDeliveryStructure = jaxbElement.getValue();
CompositeFrame compositeFrame = (CompositeFrame) publicationDeliveryStructure.getDataObjects().getCompositeFrameOrCommonFrame().get(0).getValue();

ResourceFrame resourceFrame = (ResourceFrame) compositeFrame.getFrames().getCommonFrame().get(0).getValue();

Branding branding = (Branding) resourceFrame.getTypesOfValue().getValueSetOrTypeOfValue().get(0).getValue();
assertEquals("Test Brand", branding.getName().getValue());
assertEquals("https://www.testbrand.no", branding.getUrl());
assertEquals("https://www.testbrand.no/logo.png", branding.getImage());

Operator operator = (Operator) resourceFrame.getOrganisations().getOrganisation_().get(0).getValue();
assertEquals("Test Operator", operator.getName().getValue());
assertNotNull(operator.getBrandingRef());
assertEquals("TST:Branding:1", operator.getBrandingRef().getRef());
}
}
Loading