forked from inspire-eu-validation/ets-repository
-
Notifications
You must be signed in to change notification settings - Fork 4
/
inspire-noggeo-bsxets.xq
380 lines (350 loc) · 19.7 KB
/
inspire-noggeo-bsxets.xq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
declare namespace base32='http://inspire.ec.europa.eu/schemas/base/3.2';
declare namespace base='http://inspire.ec.europa.eu/schemas/base/3.3';
declare namespace gml='http://www.opengis.net/gml/3.2';
declare namespace wfs='http://www.opengis.net/wfs/2.0';
declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
declare namespace xlink='http://www.w3.org/1999/xlink';
declare namespace etf='http://www.interactive-instruments.de/etf/2.0';
declare namespace uuid='java.util.UUID';
declare function local:test($db as document-node()*, $features as element()*, $ets as element()*, $testQuery as xs:string) as element()
{
let $query := $testQuery ||
(let $test-module-results :=
for $module in $ets//*[local-name()='TestModule']
let $moduleresultid := 'EID' || uuid:randomUUID()
let $test-case-results :=
for $case in $module//*[local-name()='TestCase']
let $dep := if ($case/etf:dependencies)
then "let $dependencyResult := local:passed('" || $case/etf:dependencies/etf:testCase/@ref || "')"
else "let $dependencyResult := true()"
let $caseresultid := 'EID' || uuid:randomUUID()
let $test-step-results :=
for $step in $case//*[local-name()='TestStep']
let $stepresultid := 'EID' || uuid:randomUUID()
let $assertion-results :=
for $assertion in $step//*[local-name()='TestAssertion']
let $type := $assertion/etf:testItemType
let $disabled := if (not(matches($assertion/etf:label,$tests_to_execute)) or $type/@ref='EID92f22a19-2ec2-43f0-8971-c2da3eaafcd2' (:disabled :)) then 'NOT_APPLICABLE' else if ($type/@ref='EIDb48eeaa3-6a74-414a-879c-1dc708017e11' (: manual :)) then 'PASSED_MANUAL' else ()
return
if ($disabled) then "
let $startmessage := prof:void(local:start('" || $assertion/@id || "'))
let $endmessage := prof:void(local:end('" || $assertion/@id || "','" || $disabled || "'))
let $logentry := local:log('Test Assertion ''" || $assertion/etf:label || "'': " || $disabled || "')
return
<TestAssertionResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='EID{uuid:randomUUID()}'>
<parent ref='" || $stepresultid || "'/>
<resultedFrom ref='" || $assertion/@id || "'/>
<startTimestamp>" || fn:current-dateTime() || "</startTimestamp>
<duration>0</duration>
<status>" || $disabled || "</status>
</TestAssertionResult>"
else "
if ($dependencyResult) then
let $startmessage := prof:void(local:start('" || $assertion/@id || "'))
let $start := prof:current-ms()
let $timestampAssertion := fn:current-dateTime()
let $result :=
try { " || $assertion/etf:expression || " } catch * {
let $text := '[' || $err:code || '] ' || $err:description || ' 
' || $err:module || ' (' || $err:line-number || '/' || $err:column-number || ')'
return ('FAILED', local:addMessage('TR.systemError', map { 'text': $text }))
}
let $status_returned := $result[1]
let $messages := $result[position()>1]
let $status := if ($status_returned = ('PASSED','FAILED','WARNING','PASSED_MANUAL','INFO','SKIPPED')) then $status_returned else 'UNDEFINED'
let $duration := xs:integer(prof:current-ms()-$start)
let $endmessage := prof:void(local:end('" || $assertion/@id || "',$status))
let $logentry := local:log('Test Assertion ''" || $assertion/etf:label || "'': ' || $status || ' - ' ||$duration || ' ms')
return
<TestAssertionResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='EID{uuid:randomUUID()}'>
{ if (empty($messages)) then () else <messages>{$messages}</messages> }
<parent ref='" || $stepresultid || "'/>
<resultedFrom ref='" || $assertion/@id || "'/>
<startTimestamp>{$timestampAssertion}</startTimestamp>
<duration>{$duration}</duration>
<status>{$status}</status>
</TestAssertionResult>
else
let $startmessage := prof:void(local:start('" || $assertion/@id || "'))
let $timestampAssertion := fn:current-dateTime()
let $status := 'SKIPPED'
let $endmessage := prof:void(local:end('" || $assertion/@id || "',$status))
let $logentry := local:log('Test Assertion ''" || $assertion/etf:label || "'': ' || $status)
return
<TestAssertionResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='EID{uuid:randomUUID()}'>
<parent ref='" || $stepresultid || "'/>
<resultedFrom ref='" || $assertion/@id || "'/>
<startTimestamp>{$timestampAssertion}</startTimestamp>
<duration>0</duration>
<status>{$status}</status>
</TestAssertionResult>"
return "
let $timestampStep := fn:current-dateTime()
let $startmessage := prof:void(local:start('" || $step/@id || "'))
let $assertionresults := (" || string-join( $assertion-results, ',' ) || ")
let $status := if ($dependencyResult) then local:status($assertionresults/etf:status) else 'SKIPPED'
let $endmessage := prof:void(local:end('" || $step/@id || "',$status))
return
<TestStepResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='" || $stepresultid || "'>
<testAssertionResults>{$assertionresults}</testAssertionResults>
<parent ref='" || $caseresultid || "'/>
<resultedFrom ref='" || $step/@id || "'/>
<startTimestamp>{$timestampStep}</startTimestamp>
<duration>{xs:integer(sum($assertionresults/duration))}</duration>
<status>{$status}</status>
</TestStepResult>"
return "
let $timestampCase := fn:current-dateTime()
let $startmessage := prof:void(local:start('" || $case/@id || "'))
let $logentry := local:log('Test Case ''" || $case/etf:label || "'' started')
" || $dep || "
let $teststepresults := (" || string-join( $test-step-results, ',' ) || ")
let $status := if ($dependencyResult) then local:status($teststepresults/etf:status) else 'SKIPPED'
let $endmessage := prof:void(local:end('" || $case/@id || "',$status))
let $logentry := local:log('Test Case ''" || $case/etf:label || "'' finished: ' || $status)
return
<TestCaseResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='" || $caseresultid || "'>
<testStepResults>{$teststepresults}</testStepResults>
<parent ref='" || $moduleresultid || "'/>
<resultedFrom ref='" || $case/@id || "'/>
<startTimestamp>{$timestampCase}</startTimestamp>
<duration>{xs:integer(sum($teststepresults/duration))}</duration>
<status>{$status}</status>
</TestCaseResult>"
return "
let $timestampModule := fn:current-dateTime()
let $startmessage := prof:void(local:start('" || $module/@id || "'))
let $testcaseresults := (" || string-join( $test-case-results, ',' ) || ")
let $status := local:status($testcaseresults/etf:status)
let $endmessage := prof:void(local:end('" || $module/@id || "',$status))
return
<TestModuleResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='" || $moduleresultid || "'>
<testCaseResults>{$testcaseresults}</testCaseResults>
<parent ref='" || $testTaskResultId || "'/>
<resultedFrom ref='" || $module/@id || "'/>
<startTimestamp>{$timestampModule}</startTimestamp>
<duration>{xs:integer(sum($testcaseresults/duration))}</duration>
<status>{$status}</status>
</TestModuleResult>"
return "
let $timestampSuite := fn:current-dateTime()
let $startmessage := prof:void(local:start('" || $ets/@id || "'))
let $logentry := local:log('Test Suite ''" || $ets/etf:label || "'' started')
let $testmoduleresults := (" || string-join( $test-module-results, ',' ) || ")
let $status := local:status($testmoduleresults/etf:status)
let $endmessage := prof:void(local:end('" || $ets/@id || "',$status))
let $logentry := local:log('Test Suite ''" || $ets/etf:label || "'' finished: ' || $status)
return
<TestTaskResult xmlns='http://www.interactive-instruments.de/etf/2.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.interactive-instruments.de/etf/2.0 http://services.interactive-instruments.de/etf/schema/model/resultSet.xsd' id='" || $testTaskResultId || "'>
<testObject ref='{$testObjectId}'/>
<testModuleResults>{$testmoduleresults}</testModuleResults>
<attachments>
{ if (file:exists('" || $logFile || "')) then
<Attachment xmlns='http://www.interactive-instruments.de/etf/2.0' type='LogFile' id='EID{uuid:randomUUID()}'>
<label>Log file</label>
<encoding>UTF-8</encoding>
<mimeType>text/plain</mimeType>
<referencedData href='" || file:path-to-uri($logFile) || "'/>
</Attachment>
else ()}
{ if (file:exists('" || $statFile || "')) then
<Attachment xmlns='http://www.interactive-instruments.de/etf/2.0' type='StatisticalReport' id='EID{uuid:randomUUID()}'>
<label>Feature statistics</label>
<encoding>UTF-8</encoding>
<mimeType>application/xml</mimeType>
<referencedData href='" || file:path-to-uri($statFile) || "'/>
</Attachment>
else ()}
{ if (file:exists('" || $queryFile || "')) then
<Attachment xmlns='http://www.interactive-instruments.de/etf/2.0' type='Query' id='EID{uuid:randomUUID()}'>
<label>XQuery executed against the dataset</label>
<encoding>UTF-8</encoding>
<mimeType>text/plain</mimeType>
<referencedData href='" || file:path-to-uri($queryFile) || "'/>
</Attachment>
else ()}
</attachments>
<resultedFrom ref='" || $ets/@id || "'/>
<startTimestamp>{$timestampSuite}</startTimestamp>
<duration>{xs:integer(sum($testmoduleresults/duration))}</duration>
<status>{$status}</status>
</TestTaskResult>")
let $writeQuery := file:write($queryFile, $query, map { "method": "text", "media-type": "text/plain" })
return try {
xquery:eval($query, map {'features': $features, 'idMap': map:merge($features ! map:entry(fn:string(@gml:id), .)), 'validationErrors': $validationErrors, 'db': $db, 'files_to_test': $files_to_test, 'tests_to_execute': $tests_to_execute, 'limitErrors': $limitErrors, 'testObjectId': $testObjectId, 'logFile': $logFile, 'statFile': $statFile })
} catch * {
let $text := '[' || $err:code || '] ' || $err:description || '
' || $err:module || ' (' || $err:line-number || '/' || $err:column-number || ')'
let $message :=
<message xmlns='http://www.interactive-instruments.de/etf/2.0' ref='TR.systemError'>
<translationArguments>
<argument token='text'>{$text}</argument>
</translationArguments>
</message>
let $test-module-results :=
for $module in $ets//*[local-name()='TestModule']
let $moduleresultid := 'EID' || uuid:randomUUID()
let $test-case-results :=
for $case in $module//*[local-name()='TestCase']
let $caseresultid := 'EID' || uuid:randomUUID()
let $test-step-results :=
for $step in $case//*[local-name()='TestStep']
let $stepresultid := 'EID' || uuid:randomUUID()
let $assertion-results :=
for $assertion in $step//*[local-name()='TestAssertion']
return
<TestAssertionResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='EID{uuid:randomUUID()}'>
<messages>{$message}</messages>
<parent ref='{$stepresultid}'/>
<resultedFrom ref='{$assertion/@id}'/>
<startTimestamp>{fn:current-dateTime()}</startTimestamp>
<duration>0</duration>
<status>FAILED</status>
</TestAssertionResult>
return
<TestStepResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='{$stepresultid}'>
<testAssertionResults>{$assertion-results}</testAssertionResults>
<parent ref='{$caseresultid}'/>
<resultedFrom ref='{$step/@id}'/>
<startTimestamp>{fn:current-dateTime()}</startTimestamp>
<duration>0</duration>
<status>FAILED</status>
</TestStepResult>
return
<TestCaseResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='{$caseresultid}'>
<testStepResults>{$test-step-results}</testStepResults>
<parent ref='{$moduleresultid}'/>
<resultedFrom ref='{$case/@id}'/>
<startTimestamp>{fn:current-dateTime()}</startTimestamp>
<duration>0</duration>
<status>FAILED</status>
</TestCaseResult>
return
<TestModuleResult xmlns='http://www.interactive-instruments.de/etf/2.0' id='{$moduleresultid}'>
<testCaseResults>{$test-case-results}</testCaseResults>
<parent ref='{$testTaskResultId}'/>
<resultedFrom ref='{$module/@id}'/>
<startTimestamp>{fn:current-dateTime()}</startTimestamp>
<duration>0</duration>
<status>FAILED</status>
</TestModuleResult>
let $logentry := file:append($logFile, 'System error in the Executable Test Suite. Please contact a system administrator. Error information:' || file:line-separator() || $text || file:line-separator(), map { 'method': 'text', 'media-type': 'text/plain' })
let $logout := prof:dump($text)
return
<TestTaskResult xmlns="http://www.interactive-instruments.de/etf/2.0" id='{$testTaskResultId}'>
<testObject ref='{$testObjectId}'/>
<testModuleResults>{$test-module-results}</testModuleResults>
<attachments>
<Attachment xmlns="http://www.interactive-instruments.de/etf/2.0" type="LogFile" id="EID{uuid:randomUUID()}">
<label>Log file</label>
<encoding>UTF-8</encoding>
<mimeType>text/plain</mimeType>
<referencedData href='{file:path-to-uri($logFile)}'/>
</Attachment>
{ if (file:exists($statFile)) then
<Attachment xmlns="http://www.interactive-instruments.de/etf/2.0" type="StatisticalReport" id="EID{uuid:randomUUID()}">
<label>Feature statistics</label>
<encoding>UTF-8</encoding>
<mimeType>application/xml</mimeType>
<referencedData href='{file:path-to-uri($statFile)}'/>
</Attachment>
else ()}
{ if (file:exists($queryFile)) then
<Attachment xmlns="http://www.interactive-instruments.de/etf/2.0" type="Query" id="EID{uuid:randomUUID()}">
<label>XQuery executed against the dataset</label>
<encoding>UTF-8</encoding>
<mimeType>text/plain</mimeType>
<referencedData href='{file:path-to-uri($queryFile)}'/>
</Attachment>
else ()}
</attachments>
<resultedFrom ref='{$ets/@id}'/>
<startTimestamp>{fn:current-dateTime()}</startTimestamp>
<duration>0</duration>
<status>FAILED</status>
</TestTaskResult> }
};
(: Parameters as strings :)
declare variable $files_to_test external := ".*";
declare variable $tests_to_execute external := ".*";
declare variable $schema_file external;
(: ETF test driver parameters :)
(: For local testing set $projDir, $dbBaseName in local DB without "-0" suffix and $etsFile :)
declare variable $validationErrors external := "";
declare variable $testRunId external;
declare variable $testObjectId external := 'EID' || uuid:randomUUID();
declare variable $testObjectTypeIds external;
declare variable $executableTestSuiteId external;
declare variable $testTaskId external := 'EID' || uuid:randomUUID();
declare variable $testTaskResultId external := 'EID' || uuid:randomUUID();
declare variable $translationTemplateBundleId external := "EID70a263c0-0ad7-42f2-9d4d-0d8a4ca71b52" ;
declare variable $projDir external := "/Users/portele/Documents/Dropbox/EC JRC/Are3na Reference Platform 2/WP4/D4.3.3/github/ets-repository";
declare variable $tmpDir external := $projDir || file:dir-separator() || "tmp";
declare variable $outputFile external := $tmpDir || file:dir-separator() || $testTaskResultId || "-result.xml";
declare variable $logFile external := $tmpDir || file:dir-separator() || $testTaskResultId || "-log.txt";
declare variable $statFile external := $tmpDir || file:dir-separator() || $testTaskResultId || "-stat.xml";
declare variable $queryFile external := $tmpDir || file:dir-separator() || $testTaskResultId || "-query.xq";
declare variable $statisticalReportTableType external := $projDir || file:dir-separator() || "include-metadata" || file:dir-separator() || "StatisticalReportTableType-EID8bb8f162-1082-434f-bd06-23d6507634b8.xml";
declare variable $translationTemplateBundle external := $projDir || file:dir-separator() || "include-metadata" || file:dir-separator() || "TranslationTemplateBundle-EID70a263c0-0ad7-42f2-9d4d-0d8a4ca71b52.xml";
declare variable $dbDir external;
declare variable $dbBaseName external := "e-tn-ra";
declare variable $dbCount external := 1;
declare variable $etsFile external := $projDir || file:dir-separator() || "data-tn" || file:dir-separator() || "tn-ra-as" || file:dir-separator() || "ets-tn-ra-as-bsxets.xml";
(: Project internals :)
declare variable $testQueryFile := "testquery-noggeo.xq";
declare variable $limitErrors := 1000;
declare variable $paramerror := xs:QName("etf:ParameterError");
(: Parameter checks :)
declare variable $count :=
try { xs:int($dbCount - 1)
} catch * {
error($paramerror,concat("System error: Internal parameter $dbCount must be an integer value. The value is '",data($dbCount),"'. Please contact an administrator.
"))
};
if ($count ge 0) then () else error($paramerror,concat("System error: Internal parameter $dbCount must be a positive integer value. The value is '",data($dbCount),"'. Please contact an administrator.
")),
try { let $x := matches('filename.gml',$files_to_test)
return ()
} catch * {
error($paramerror,concat("Parameter $files_to_test must be a valid regular expression. You have set the value to '",data($files_to_test),"', which results in the following error during execution:
 '",data($err:description),"'
"))
},
try { let $x := matches('module.case.assertion',$tests_to_execute)
return ()
} catch * {
error($paramerror,concat("Parameter $tests_to_execute must be a valid regular expression. You have set the value to '",data($tests_to_execute),"', which results in the following error during execution:
 '",data($err:description),"'
"))
},
if (file:exists($projDir)) then if (file:is-dir($projDir)) then () else error($paramerror,concat("System error: Internal parameter $projDir must be the path of an existing directory in the file system. The current value is '",data($projDir),"', which is a file, not a directory. Please contact an administrator.
")) else error($paramerror,concat("Internal parameter $projDir must be the path of an existing directory in the file system. The current value is '",data($projDir),"'. Please contact an administrator.
")),
if (file:exists($outputFile)) then if (file:is-file($outputFile)) then () else error($paramerror,concat("System error: Internal parameter $outputFile is an existing directory, not a file. The current value is '",data($outputFile),"'. Please contact an administrator.
")) else (),
for $i in 0 to $count return if (db:exists($dbBaseName || '-' || $i)) then () else error($paramerror,concat("System error: XML database '",concat($dbBaseName,"-",$i),"' was specified, but not found. Please contact an administrator.
")),
let $startlog := "let $startlog := local:log('Executing Test Suite: " || $etsFile || "')
"
let $ets :=
try{
doc($etsFile)/element()
} catch * {
error($paramerror,concat("System error: The Executable Test Suite file '",data($etsFile),"' was not found or is invalid. Please contact an administrator.
"))
}
let $testQueryFile := concat($projDir, file:dir-separator(),$testQueryFile)
let $testQuery :=
try{
file:read-text($testQueryFile, "UTF-8")
} catch * {
error($paramerror,concat("System error: The system file '",data($testQueryFile),"' could not be opened. Please contact an administrator.
"))
}
let $db := for $i in 0 to $count return db:open($dbBaseName || '-' || $i)[matches(db:path(.),$files_to_test)]
let $features := $db/wfs:FeatureCollection/wfs:member/* | $db/gml:FeatureCollection/gml:featureMember/* | $db/gml:FeatureCollection/gml:featureMembers/* | $db/base:SpatialDataSet/base:member/* | $db/base32:SpatialDataSet/base32:member/*
let $stattmpl := if (not($statisticalReportTableType) or not(fn:doc-available($statisticalReportTableType))) then () else doc($statisticalReportTableType)
let $stat := if (not($stattmpl)) then "let $logentry := local:log('Statistics table: " || string($statisticalReportTableType) || "')" else "
let $start := prof:current-ms()
let $entries := (" || string-join($stattmpl//etf:StatisticalReportTableType[1]/etf:rowExpressions/etf:expression,', ') || ")
let $statTable :=
<StatisticalReportTable xmlns='http://www.interactive-instruments.de/etf/2.0'>
<type ref='" || $stattmpl//etf:StatisticalReportTableType[1]/@id || "'/>
<entries>
{ for $entry in $entries return <entry xmlns='http://www.interactive-instruments.de/etf/2.0'>{$entry}</entry> }
</entries>
</StatisticalReportTable>
let $statWrite := file:write($statFile, $statTable, map { 'method': 'xml', 'media-type': 'application/xml' })
let $duration := xs:integer(prof:current-ms()-$start)
let $logentry := local:log('Statistics table: ' || $duration || ' ms')
"
let $res := local:test($db, $features, $ets, $testQuery || $startlog || $stat)
let $dummy := file:write($outputFile,$res)
return ($res)