Skip to content

Commit

Permalink
Improve XPath test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Dec 15, 2024
1 parent 76895ab commit 01a7108
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 202 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<outputDirectory>target/tests</outputDirectory>
<includes>
<include>org/entur/netex/validation/test/**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.entur.netex.validation.validator.jaxb.support;
package org.entur.netex.validation.test.jaxb.support;

import jakarta.xml.bind.JAXBElement;
import javax.annotation.Nonnull;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.entur.netex.validation.test.xpath.support;

import static org.entur.netex.validation.test.xpath.support.XPathTestSupport.NETEX_XML_PARSER;

import net.sf.saxon.s9api.XdmNode;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;

public class TestValidationContextBuilder {

private final XdmNode document;
private String codespace = XPathTestSupport.TEST_CODESPACE;
private String filename = XPathTestSupport.TEST_FILENAME;

public TestValidationContextBuilder(XdmNode xdmNode) {
this.document = xdmNode;
}

public static TestValidationContextBuilder ofDocument(XdmNode document) {
return new TestValidationContextBuilder(document);
}

public static TestValidationContextBuilder ofNetexFragment(
String netexFragment
) {
return ofDocument(XPathTestSupport.parseDocument(netexFragment));
}

public TestValidationContextBuilder withCodespace(String codespace) {
this.codespace = codespace;
return this;
}

public TestValidationContextBuilder withFilename(String filename) {
this.filename = filename;
return this;
}

public XPathRuleValidationContext build() {
return new XPathRuleValidationContext(
document,
NETEX_XML_PARSER,
codespace,
filename
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.entur.netex.validation.test.xpath.support;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import net.sf.saxon.s9api.XdmNode;
import org.entur.netex.validation.exception.NetexValidationException;
import org.entur.netex.validation.xml.NetexXMLParser;

/**
* Utility methods for testing XPath validators.
*/
public class XPathTestSupport {

public static final String TEST_CODESPACE = "ENT";
public static final String TEST_FILENAME = "netex.xml";

static final NetexXMLParser NETEX_XML_PARSER = new NetexXMLParser();

/**
* Parse a NeTEx fragment and return an XML document.
* The XML fragment must be namespaced with the NeTEx schema namespace http://www.netex.org.uk/netex
*/
public static XdmNode parseDocument(String netexFragment) {
return parseDocument(new ByteArrayInputStream(netexFragment.getBytes()));
}

/**
* Parse a NeTEx fragment and return an XML document.
* The XML fragment must be namespaced with the NeTEx schema namespace http://www.netex.org.uk/netex
*/
public static XdmNode parseDocument(InputStream inputStream) {
XdmNode parsedDocument;
try {
parsedDocument =
NETEX_XML_PARSER.parseByteArrayToXdmNode(inputStream.readAllBytes());
} catch (IOException e) {
throw new NetexValidationException(e);
}
XdmNode rootElement = parsedDocument.children().iterator().next();
String namespaceURI = rootElement.getNodeName().getNamespaceURI();
if (!NetexXMLParser.NETEX_NAMESPACE.equals(namespaceURI)) {
throw new IllegalStateException(
"The XML fragment is not namespaced under the NeTEx namespace. Provided namespace: '" +
namespaceURI +
"'"
);
}
return rootElement;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.entur.netex.validation.validator.jaxb;

import static org.entur.netex.validation.validator.jaxb.support.JAXBUtils.createJaxbElement;
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

import java.util.List;
import java.util.Set;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.test.xpath.support.XPathTestSupport;
import org.entur.netex.validation.validator.Severity;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.xpath.rules.ValidateNotExist;
import org.entur.netex.validation.validator.xpath.support.XPathTestSupport;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -39,7 +40,8 @@ void setUp() {
tree = new ValidationTree("Service Frame", "ServiceFrame");
tree.addValidationRule(RULE_NO_LINE);

xpathValidationContext = XPathTestSupport.validationContext(NETEX_FRAGMENT);
xpathValidationContext =
TestValidationContextBuilder.ofNetexFragment(NETEX_FRAGMENT).build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.entur.netex.validation.validator.xpath.rules;

import static org.entur.netex.validation.validator.xpath.support.XPathTestSupport.validationContext;

import java.util.List;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -51,9 +50,10 @@ void testInvalidBookingAccess() {
BookingAccessEnumeration.OTHER.value()
);

XPathRuleValidationContext xpathRuleValidationContext = validationContext(
flexibleLineWithInvalidBookingAccess
);
XPathRuleValidationContext xpathRuleValidationContext =
TestValidationContextBuilder
.ofNetexFragment(flexibleLineWithInvalidBookingAccess)
.build();
List<ValidationIssue> validationIssues =
validateAllowedBookingAccessProperty.validate(xpathRuleValidationContext);
Assertions.assertNotNull(validationIssues);
Expand All @@ -68,9 +68,10 @@ void testValidBookingAccess() {
"${BOOKING_ACCESS}",
BookingAccessEnumeration.PUBLIC.value()
);
XPathRuleValidationContext xpathRuleValidationContext = validationContext(
flexibleLineWithValidBookingAccess
);
XPathRuleValidationContext xpathRuleValidationContext =
TestValidationContextBuilder
.ofNetexFragment(flexibleLineWithValidBookingAccess)
.build();

List<ValidationIssue> validationIssues =
validateAllowedBookingAccessProperty.validate(xpathRuleValidationContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;
import org.entur.netex.validation.validator.xpath.support.XPathTestSupport;
import org.junit.jupiter.api.Test;

class ValidateAllowedTransportModeAndSubmodeOnLineTest {
Expand Down Expand Up @@ -138,11 +138,13 @@ private XPathRuleValidationContext createXPathValidationContext(
String transportMode,
String transportSubmode
) {
return XPathTestSupport.validationContext(
NETEX_FRAGMENT
.replace("${TRANSPORT_MODE}", transportMode)
.replace("${TRANSPORT_SUBMODE}", transportSubmode)
.replace("${LINE}", line)
);
return TestValidationContextBuilder
.ofNetexFragment(
NETEX_FRAGMENT
.replace("${TRANSPORT_MODE}", transportMode)
.replace("${TRANSPORT_SUBMODE}", transportSubmode)
.replace("${LINE}", line)
)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;
import org.entur.netex.validation.validator.xpath.support.XPathTestSupport;
import org.junit.jupiter.api.Test;

class ValidateAllowedTransportModeAndSubmodeOnServiceJourneyTest {
Expand Down Expand Up @@ -94,10 +94,12 @@ private XPathRuleValidationContext createXPathValidationContext(
String transportMode,
String transportSubmode
) {
return XPathTestSupport.validationContext(
NETEX_FRAGMENT
.replace("${TRANSPORT_MODE}", transportMode)
.replace("${TRANSPORT_SUBMODE}", transportSubmode)
);
return TestValidationContextBuilder
.ofNetexFragment(
NETEX_FRAGMENT
.replace("${TRANSPORT_MODE}", transportMode)
.replace("${TRANSPORT_SUBMODE}", transportSubmode)
)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.entur.netex.validation.validator.xpath.rules;

import static org.entur.netex.validation.validator.xpath.support.XPathTestSupport.validationContext;

import java.util.List;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -108,9 +107,10 @@ void testTimetabledPassingTimesIds(
.replace("${ID_3}", ids.get(2))
.replace("${ID_4}", ids.get(3));

XPathRuleValidationContext xpathRuleValidationContext = validationContext(
vehicleJourneysFragment
);
XPathRuleValidationContext xpathRuleValidationContext =
TestValidationContextBuilder
.ofNetexFragment(vehicleJourneysFragment)
.build();
List<ValidationIssue> validationIssues =
validateDuplicatedTimetabledPassingTimeId.validate(
xpathRuleValidationContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.validator.Severity;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.ValidationRule;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;
import org.entur.netex.validation.validator.xpath.support.XPathTestSupport;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -35,7 +35,7 @@ class ValidateExactlyOneTest {
@BeforeEach
void setUp() {
xpathRuleValidationContext =
XPathTestSupport.validationContext(NETEX_FRAGMENT);
TestValidationContextBuilder.ofNetexFragment(NETEX_FRAGMENT).build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.entur.netex.validation.validator.xpath.rules;

import static org.entur.netex.validation.validator.xpath.support.XPathTestSupport.parseDocument;
import static org.entur.netex.validation.validator.xpath.support.XPathTestSupport.validationContext;
import static org.entur.netex.validation.test.xpath.support.XPathTestSupport.parseDocument;
import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import net.sf.saxon.s9api.XdmNode;
import org.entur.netex.validation.test.xpath.support.TestValidationContextBuilder;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.xpath.XPathRuleValidationContext;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -82,9 +82,8 @@ void testMissingTimetablePassingTimesMatch() {
.children("TimetableFrame")
.iterator()
.next();
XPathRuleValidationContext xpathValidationContext = validationContext(
timetableFrame
);
XPathRuleValidationContext xpathValidationContext =
TestValidationContextBuilder.ofDocument(timetableFrame).build();
List<ValidationIssue> validationIssues = validator.validate(
xpathValidationContext
);
Expand All @@ -106,9 +105,8 @@ void testMissingTimetablePassingTimesNoMatch() {
.children("TimetableFrame")
.iterator()
.next();
XPathRuleValidationContext xpathValidationContext = validationContext(
timetableFrame
);
XPathRuleValidationContext xpathValidationContext =
TestValidationContextBuilder.ofDocument(timetableFrame).build();
List<ValidationIssue> validationIssues = validator.validate(
xpathValidationContext
);
Expand Down
Loading

0 comments on commit 01a7108

Please sign in to comment.