2
2
3
3
import static org .junit .jupiter .api .Assertions .*;
4
4
5
+ import java .nio .charset .StandardCharsets ;
5
6
import java .util .List ;
6
7
import java .util .Set ;
8
+ import java .util .concurrent .atomic .AtomicBoolean ;
7
9
import java .util .stream .Collectors ;
8
10
import org .entur .netex .validation .validator .id .VersionOnLocalNetexIdValidator ;
11
+ import org .entur .netex .validation .validator .schema .NetexSchemaValidationContext ;
12
+ import org .entur .netex .validation .validator .schema .NetexSchemaValidator ;
13
+ import org .entur .netex .validation .validator .xpath .XPathValidationContext ;
9
14
import org .entur .netex .validation .xml .NetexXMLParser ;
10
15
import org .junit .jupiter .api .Test ;
11
16
12
17
class NetexValidatorsRunnerTest {
13
18
19
+ private static final String TEST_CODESPACE = "ENT" ;
20
+ private static final String TEST_FILENAME = "netex.xml" ;
21
+ private static final String TEST_VALIDATION_REPORT_ID =
22
+ "validation report id" ;
23
+
24
+ private static final String NETEX_FRAGMENT =
25
+ """
26
+ <PublicationDelivery xmlns="http://www.netex.org.uk/netex" xmlns:ns2="http://www.opengis.net/gml/3.2" xmlns:ns3="http://www.siri.org.uk/siri" version="1.15:NO-NeTEx-networktimetable:1.5">
27
+ <dataObjects>
28
+ <ServiceFrame id="ENT:ServiceFrame:1" version="2223">
29
+ <lines>
30
+ <Line id="ENT:Line:2_1" version="2223">
31
+ </Line>
32
+ </lines>
33
+ </ServiceFrame>
34
+ </dataObjects>
35
+ </PublicationDelivery>
36
+ """ ;
37
+
38
+ @ Test
39
+ void testNoValidator () {
40
+ NetexValidatorsRunner runner = NetexValidatorsRunner .of ().build ();
41
+ ValidationReport report = validationReport (runner );
42
+ assertTrue (report .getValidationReportEntries ().isEmpty ());
43
+ assertEquals (TEST_CODESPACE , report .getCodespace ());
44
+ assertEquals (TEST_VALIDATION_REPORT_ID , report .getValidationReportId ());
45
+ }
46
+
47
+ @ Test
48
+ void testReportSchemaValidationError () {
49
+ NetexValidatorsRunner runner = NetexValidatorsRunner
50
+ .of ()
51
+ .withNetexSchemaValidator (
52
+ new TestNetexSchemaValidator (
53
+ List .of (
54
+ new ValidationIssue (
55
+ NetexSchemaValidator .RULE_ERROR ,
56
+ DataLocation .EMPTY_LOCATION ,
57
+ "an error"
58
+ )
59
+ )
60
+ )
61
+ )
62
+ .build ();
63
+ ValidationReport report = validationReport (runner );
64
+
65
+ assertEquals (1 , report .getValidationReportEntries ().size ());
66
+ }
67
+
68
+ @ Test
69
+ void testSkipXPathValidationOnSchemaError () {
70
+ NetexValidatorsRunner runner = NetexValidatorsRunner
71
+ .of ()
72
+ .withNetexSchemaValidator (
73
+ new TestNetexSchemaValidator (
74
+ List .of (
75
+ new ValidationIssue (
76
+ NetexSchemaValidator .RULE_ERROR ,
77
+ DataLocation .EMPTY_LOCATION ,
78
+ "an error"
79
+ )
80
+ )
81
+ )
82
+ )
83
+ .withNetexXMLParser (new NetexXMLParser ())
84
+ .withXPathValidators (
85
+ List .of (
86
+ new XPathValidator () {
87
+ @ Override
88
+ public List <ValidationIssue > validate (
89
+ XPathValidationContext validationContext
90
+ ) {
91
+ fail (
92
+ "XPath validator should be skipped when Netex schema validation fails"
93
+ );
94
+ return List .of ();
95
+ }
96
+
97
+ @ Override
98
+ public Set <ValidationRule > getRules () {
99
+ return Set .of ();
100
+ }
101
+ }
102
+ )
103
+ )
104
+ .build ();
105
+ ValidationReport report = validationReport (runner );
106
+
107
+ assertEquals (1 , report .getValidationReportEntries ().size ());
108
+ }
109
+
110
+ @ Test
111
+ void testRunXPathValidationIfNoSchemaError () {
112
+ AtomicBoolean xpathValidation = new AtomicBoolean ();
113
+ NetexValidatorsRunner runner = NetexValidatorsRunner
114
+ .of ()
115
+ .withNetexSchemaValidator (
116
+ new NetexSchemaValidator (0 ) {
117
+ @ Override
118
+ public List <ValidationIssue > validate (
119
+ NetexSchemaValidationContext validationContext
120
+ ) {
121
+ return List .of ();
122
+ }
123
+ }
124
+ )
125
+ .withNetexXMLParser (new NetexXMLParser ())
126
+ .withXPathValidators (
127
+ List .of (
128
+ new XPathValidator () {
129
+ @ Override
130
+ public List <ValidationIssue > validate (
131
+ XPathValidationContext validationContext
132
+ ) {
133
+ xpathValidation .set (true );
134
+ return List .of ();
135
+ }
136
+
137
+ @ Override
138
+ public Set <ValidationRule > getRules () {
139
+ return Set .of ();
140
+ }
141
+ }
142
+ )
143
+ )
144
+ .build ();
145
+ ValidationReport report = validationReport (runner );
146
+
147
+ assertEquals (0 , report .getValidationReportEntries ().size ());
148
+ assertTrue (xpathValidation .get ());
149
+ }
150
+
14
151
@ Test
15
152
void testDescriptions () {
16
153
XPathValidator xPathValidator = new VersionOnLocalNetexIdValidator ();
@@ -28,4 +165,32 @@ void testDescriptions() {
28
165
.collect (Collectors .toUnmodifiableSet ());
29
166
assertEquals (xpathRuleDescriptions , ruleDescriptions );
30
167
}
168
+
169
+ private static ValidationReport validationReport (
170
+ NetexValidatorsRunner runner
171
+ ) {
172
+ return runner .validate (
173
+ TEST_CODESPACE ,
174
+ TEST_VALIDATION_REPORT_ID ,
175
+ TEST_FILENAME ,
176
+ NETEX_FRAGMENT .getBytes (StandardCharsets .UTF_8 )
177
+ );
178
+ }
179
+
180
+ private static class TestNetexSchemaValidator extends NetexSchemaValidator {
181
+
182
+ private final List <ValidationIssue > issues ;
183
+
184
+ public TestNetexSchemaValidator (List <ValidationIssue > issues ) {
185
+ super (0 );
186
+ this .issues = issues ;
187
+ }
188
+
189
+ @ Override
190
+ public List <ValidationIssue > validate (
191
+ NetexSchemaValidationContext validationContext
192
+ ) {
193
+ return issues ;
194
+ }
195
+ }
31
196
}
0 commit comments