From 690bdceddc9dbd71cdee8ee52df4c5bfa9adada9 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 10:30:31 +0200
Subject: [PATCH 01/22] feat: move manifest related code to separate module

---
 exist-app/modules/tapi-manifest.xqm       | 189 ++++++++++++++++
 exist-app/modules/tapi.xqm                | 185 ----------------
 exist-app/tests-runner.xq                 |   6 +-
 exist-app/tests.xqm                       | 133 -----------
 exist-app/tests/tapi-collection-tests.xqm |  24 +-
 exist-app/tests/tapi-manifest-tests.xqm   | 258 ++++++++++++++++++++++
 exist-app/tests/test-commons.xqm          |  24 ++
 7 files changed, 477 insertions(+), 342 deletions(-)
 create mode 100644 exist-app/modules/tapi-manifest.xqm
 create mode 100644 exist-app/tests/tapi-manifest-tests.xqm
 create mode 100644 exist-app/tests/test-commons.xqm

diff --git a/exist-app/modules/tapi-manifest.xqm b/exist-app/modules/tapi-manifest.xqm
new file mode 100644
index 00000000..27da1faf
--- /dev/null
+++ b/exist-app/modules/tapi-manifest.xqm
@@ -0,0 +1,189 @@
+xquery version "3.1";
+
+(: 
+ : This module handles calls to the API on manifest level, e.g.
+ : 
+ : /textapi/ahikar/3r9ps3rx15/manifest.json
+ :)
+
+module namespace t-man="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest";
+
+declare namespace ore="http://www.openarchives.org/ore/terms/";
+declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
+declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
+
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
+import module namespace requestr="http://exquery.org/ns/request";
+import module namespace rest="http://exquery.org/ns/restxq";
+
+declare variable $t-man:server := 
+    if(requestr:hostname() = "existdb") then
+        $commons:expath-pkg/*/@name => replace("/$", "")
+    else
+        "http://localhost:8094/exist/restxq";
+        
+
+(:~
+ : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#manifest
+ :)
+declare
+    %rest:GET
+    %rest:HEAD
+    %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}/manifest.json")
+    %output:method("json")
+function t-man:endpoint($collection-uri as xs:string,
+    $manifest-uri as xs:string)
+as item()+ {
+    $commons:responseHeader200,
+    t-man:get-json($collection-uri, $manifest-uri, $t-man:server)
+};
+
+
+declare function t-man:get-json($collection-uri as xs:string,
+    $manifest-uri as xs:string,
+    $server as xs:string)
+as element(object) {
+    <object>
+        <textapi>{$commons:version}</textapi>
+        <id>{$server || "/api/textapi/ahikar/" || $collection-uri || "/" || $manifest-uri || "/manifest.json"}</id>
+        <label>{t-man:get-manifest-title($manifest-uri)}</label>
+        { 
+            t-man:make-editors($manifest-uri),
+            t-man:make-creation-date($manifest-uri),
+            t-man:make-origin($manifest-uri),
+            t-man:make-current-location($manifest-uri)
+        }
+        <license>CC0-1.0</license>
+        <annotationCollection>{$server}/api/textapi/ahikar/{$collection-uri}/{$manifest-uri}/annotationCollection.json</annotationCollection>
+        {t-man:make-sequences($collection-uri, $manifest-uri, $server)}
+    </object>
+};
+
+
+declare function t-man:get-aggregation($manifest-uri as xs:string)
+as document-node() {
+    doc($commons:agg || $manifest-uri || ".xml")
+};
+
+declare function t-man:make-sequences($collection-uri as xs:string,
+    $manifest-uri as xs:string,
+    $server as xs:string)
+as element(sequence)+ {
+    let $valid-pages := t-man:get-valid-page-ids($manifest-uri)
+    return
+        for $page in $valid-pages
+        let $uri := "/api/textapi/ahikar/" || $collection-uri || "/" || $manifest-uri || "-" ||  $page || "/latest/item.json"
+        return
+            <sequence>
+                <id>{$server}{$uri}</id>
+                <type>item</type>
+            </sequence>
+};
+
+declare function t-man:get-valid-page-ids($manifest-uri as xs:string)
+as xs:string+ {
+    let $tei-xml :=  t-man:get-tei-xml-for-manifest($manifest-uri)
+    return
+        $tei-xml//tei:pb[@facs]/string(@n)
+};
+
+declare function t-man:get-tei-xml-for-manifest($manifest-uri as xs:string)
+as document-node() {
+    let $xml-uri := t-man:get-xml-uri($manifest-uri)
+    return
+        doc($commons:data || $xml-uri || ".xml")
+};
+
+declare function t-man:get-xml-uri($manifest-uri as xs:string)
+as xs:string {
+    let $aggregation-file := t-man:get-aggregation($manifest-uri)
+    return
+        $aggregation-file//ore:aggregates[1]/@rdf:resource
+        => substring-after(":")
+};
+
+declare function t-man:get-manifest-title($manifest-uri as xs:string)
+as xs:string {
+    let $metadata-file := t-man:get-metadata-file($manifest-uri)
+    return
+        $metadata-file//tgmd:title/string()
+};
+
+declare function t-man:get-metadata-file($manifest-uri as xs:string)
+as document-node() {
+    doc($commons:tg-collection || "/meta/" || $manifest-uri || ".xml")
+};
+
+
+declare function t-man:make-editors($manifest-uri as xs:string)
+as element(x-editor)+ {
+    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $editors := $tei-xml//tei:titleStmt//tei:editor
+    return
+        if (exists($editors)) then
+            for $editor in $editors
+            return
+                <x-editor>
+                    <role>editor</role>
+                    <name>{$editor/string()}</name>
+                </x-editor>
+        else
+            <x-editor>
+                <name>none</name>
+            </x-editor>
+};
+
+
+declare function t-man:make-creation-date($manifest-uri as xs:string)
+as element(x-date) {
+    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $creation-date := $tei-xml//tei:history//tei:date
+    let $string :=
+        if ($creation-date) then
+            $creation-date/string()
+        else
+            "unknown"
+    return
+        <x-date>{$string}</x-date>
+};
+
+
+declare function t-man:make-origin($manifest-uri as xs:string) as 
+element(x-origin) {
+    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $country := $tei-xml//tei:history//tei:country
+    let $place := $tei-xml//tei:history//tei:placeName
+    let $string :=
+        if ($country and $place) then
+            $place/string() || ", " || $country/string()
+        else if ($country) then
+            $country/string()
+        else if($place) then
+            $place/string()
+        else
+            "unknown"
+    return
+        <x-origin>{$string}</x-origin>
+};
+
+
+declare function t-man:make-current-location($manifest-uri as xs:string) as
+element(x-location) {
+    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $institution := $tei-xml//tei:msIdentifier//tei:institution
+    let $country := $tei-xml//tei:msIdentifier//tei:country
+    let $string :=
+        if ($country and $institution) then
+            $institution || ", " || $country
+        else if ($country) then
+            $country/string()
+        else if($institution) then
+            $institution/string()
+        else
+            "unknown"
+    return
+        <x-location>{$string}</x-location>
+
+};
\ No newline at end of file
diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index 9cae4a0c..fb552029 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -81,191 +81,6 @@ declare function tapi:remove-whitespaces($doc as document-node()) as document-no
 };
 
 
-(:~
- : Returns information about a given document as specified at
- : https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#manifest.
- :
- : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#manifest
- : @param $collection The unprefixed TextGrid URI of a collection, e.g. '3r84g'
- : @param $document The unprefixed TextGrid URI of a document, e.g. '3r679'
- : @return An object element containing all necessary information about a manifest object
- :)
-declare
-    %rest:GET
-    %rest:HEAD
-    %rest:path("/textapi/ahikar/{$collection}/{$document}/manifest.json")
-    %output:method("json")
-function tapi:manifest-rest($collection as xs:string, $document as xs:string)
-as item()+ {
-    $commons:responseHeader200,
-    tapi:manifest($collection, $document, $tapi:server)
-};
-
-
-(:~
- : Returns information about an edition object (i.e. an aggregation) which holds
- : an XML document as well as the facsimiles.
- : 
- : In contrast to the generic TextAPI specs on manifest objects we do not provide an
- : ID at this point. One reason is the TextGrid metadata model, the other is FRBR.
- : 
- : @param $collection The URI of the document's parent collection, e.g. '3r9ps'
- : @param $document The URI of an edition object, e.g. '3r177'
- : @param $server A string indicating the server. This parameter has been introduced to make this function testable and defaults to $tapi:server.
- : @return An object element containing all necessary information about a manifest object
- :)
-declare function tapi:manifest($collection as xs:string, $document as xs:string,
-$server as xs:string)
-as element(object) {
-    let $aggNode := doc($commons:agg || $document || ".xml")
-    let $metaNode := doc($commons:tg-collection || "/meta/" || $document || ".xml")
-    let $documentUri := $aggNode//ore:aggregates[1]/@rdf:resource => substring-after(":")
-    let $documentNode := doc($commons:data || $documentUri || ".xml")
-    let $sequence :=
-        for $page in $documentNode//tei:pb[@facs]/string(@n)
-        let $uri := "/api/textapi/ahikar/" || $collection || "/" || $document || "-" ||  $page || "/latest/item.json"
-        return
-            <sequence>
-                <id>{$server}{$uri}</id>
-                <type>item</type>
-            </sequence>
-    let $id := $server || "/api/textapi/ahikar/" || $collection || "/" || $document || "/manifest.json"
-            
-    return
-    <object>
-        <textapi>{$commons:version}</textapi>
-        <id>{$id}</id>
-        <label>{string($metaNode//tgmd:title)}</label>
-        {
-            tapi:make-editors($documentNode),
-            tapi:make-date($documentNode),
-            tapi:make-origin($documentNode),
-            tapi:make-location($documentNode)
-        }
-        <license>CC0-1.0</license>
-        <annotationCollection>{$server}/api/textapi/ahikar/{$collection}/{$document}/annotationCollection.json</annotationCollection>
-        {$sequence}
-    </object>
-};
-
-
-(:~ 
- : Creates the necessary information about editors.
- : 
- : @see https://subugoe.pages.gwdg.de/ahiqar/api-documentation/page/text-api-specs/#actor-object
- : @param $documentNode The opened TEI file of the current manifest
- : @return An x-editor element with all information necessary for an Actor Object.
- :)
-declare function tapi:make-editors($documentNode as document-node()) as element(x-editor)* {
-    let $role := "editor"
-    let $has-editor := exists($documentNode//tei:titleStmt//tei:editor)
-    return
-        if ($has-editor) then
-            for $editor in $documentNode//tei:titleStmt//tei:editor
-            return
-                <x-editor>
-                    <role>{$role}</role>
-                    <name>{$editor/string()}</name>
-                </x-editor>
-        else
-            <x-editor>
-                <name>none</name>
-            </x-editor>
-};
-
-(:~
- : Creates the necessary information about a manuscript's origin from the TEI
- : header.
- : 
- : @see https://subugoe.pages.gwdg.de/ahiqar/api-documentation/page/text-api-specs/#manifest-object
- : @param $documentNode The opened TEI file of the current manifest
- : @return An x-origin element containing a descriptive string
- :)
-declare function tapi:make-origin($documentNode as document-node()) as 
-element(x-origin)? {
-    let $country := $documentNode//tei:history//tei:country
-    let $place := $documentNode//tei:history//tei:placeName
-    let $string :=
-        if ($country and $place) then
-            $place/string() || ", " || $country/string()
-        else if ($country) then
-            $country/string()
-        else if($place) then
-            $place/string()
-        else
-            "unknown"
-    return
-        <x-origin>{$string}</x-origin>
-};
-
-
-(:~
- : Creates the necessary information about a manuscript's creation date from the
- : TEI header.
- : 
- : @see https://subugoe.pages.gwdg.de/ahiqar/api-documentation/page/text-api-specs/#manifest-object
- : @param $documentNode The opened TEI file of the current manifest
- : @return An x-date element containing a descriptive string
- :)
-declare function tapi:make-date($documentNode as document-node()) as
-element(x-date)? {
-    let $date := $documentNode//tei:history//tei:date
-    let $string :=
-        if ($date) then
-            $date/string()
-        else
-            "unknown"
-    return
-        <x-date>{$string}</x-date>
-};
-
-
-(:~
- : Creates the necessary information about a manuscript's current location from
- : the TEI header.
- : 
- : @see https://subugoe.pages.gwdg.de/ahiqar/api-documentation/page/text-api-specs/#manifest-object
- : @param $documentNode The opened TEI file of the current manifest
- : @return An x-location element containing a descriptive string
- :)
-declare function tapi:make-location($documentNode as document-node()) as
-element(x-location) {
-    let $institution := $documentNode//tei:msIdentifier//tei:institution
-    let $country := $documentNode//tei:msIdentifier//tei:country
-    let $string :=
-        if ($country and $institution) then
-            $institution || ", " || $country
-        else if ($country) then
-            $country/string()
-        else if($institution) then
-            $institution/string()
-        else
-            "unknown"
-    return
-        <x-location>{$string}</x-location>
-
-};
-
-
-(:~ 
- : Returns the API endpoints of all pages of the manuscript.
- : Since we also have transliterations, only "original" pages (i.e. the ones
- : with a facsimile) are considered for the endpoints.
- : 
- : @param $documentNode The opened TEI file of the current manifest
- : @retun A sequence of sequence elements
- :)
-declare function tapi:make-sequence($documentNode as document-node()) as
-element(sequence)+ {
-    for $page in $documentNode//tei:pb[@facs]/string(@n)
-        let $uri := "/api/textapi/ahikar/" || $collection || "/" || $document || "-" ||  $page || "/latest/item.json"
-        return
-            <sequence>
-                <id>{$server}{$uri}</id>
-                <type>item</type>
-            </sequence>
-};
-
 (:~
  : Returns information about a given page in a document. This is mainly compliant
  : with the SUB TextAPI, but has the following additions:
diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 6eeb8e79..68e38c12 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -7,11 +7,13 @@ xquery version "3.1";
 import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tests/tapi-collection-tests.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace tests="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests.xqm";
+import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tests/tapi-manifest-tests.xqm";
 import module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests" at "tests/collate-tests.xqm";
 import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
 
-(: test API endpoints :)
+
 test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),
 test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/coll-tests")),
 test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
-test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests"))
\ No newline at end of file
+test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),
+test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests"))
\ No newline at end of file
diff --git a/exist-app/tests.xqm b/exist-app/tests.xqm
index be645f1d..5a38831b 100644
--- a/exist-app/tests.xqm
+++ b/exist-app/tests.xqm
@@ -86,28 +86,6 @@ function tests:api-info()  as item() {
 };
 
 
-declare
-    (: check if all parts are present.
-     : no further tests are needed since the content has been tested by testing
-     : the underlying function. :)
-    %test:assertXPath("map:contains($result, 'textapi')")
-    %test:assertXPath("map:contains($result, 'id')")
-    %test:assertXPath("map:contains($result, 'label')")
-    %test:assertXPath("map:contains($result, 'x-editor')")
-    %test:assertXPath("map:contains($result, 'x-date')")
-    %test:assertXPath("map:contains($result, 'x-origin')")
-    %test:assertXPath("map:contains($result, 'x-location')")
-    %test:assertXPath("map:contains($result, 'license')")
-    %test:assertXPath("map:contains($result, 'sequence')")
-function tests:manifest-rest() as item() {
-    let $url := $tests:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
-    let $req := <http:request href="{$url}" method="get">
-                        <http:header name="Connection" value="close"/>
-                   </http:request>
-    return http:send-request($req)[2] => util:base64-decode() => parse-json()
-};
-
-
 declare
     (: check if all parts are present.
      : no further tests are needed since the content has been tested while testing
@@ -179,17 +157,6 @@ function tests:content-txt() as xs:string {
  :)
 
 
-declare
-    %test:args("ahiqar_collection", "ahiqar_agg", "http://localhost:8080/exist/restxq")
-    (: tests if the object is created at all :)
-    %test:assertXPath("$result//*[local-name(.) = 'label'] = 'Beispieldatei zum Testen' ")
-    (: tests if the sequence construction works properly :)
-    %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://localhost:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json' ")
-function tests:manifest($collection as xs:string, $document as xs:string, 
-$server as xs:string) as element(object) {
-    tapi:manifest($collection, $document, $server)
-};
-
 
 
 declare
@@ -249,106 +216,6 @@ function tests:get-tei($document as xs:string, $type as xs:string) as element()
 };
 
 
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-editor' and name/text() = 'Aly Elrefaei']")
-function tests:make-editors() as element()+ {
-    let $documentNode := doc("/db/apps/sade/textgrid/data/ahiqar_sample.xml")
-    return
-        tapi:make-editors($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-editor' and name/text() = 'none']")
-function tests:make-editors-fail-gracefully() as element()+ {
-    let $documentNode := doc("/db/test-records/sample-tei.xml")
-    return
-        tapi:make-editors($documentNode)
-};
-
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-date'][text() = '18.10.1697']")
-function tests:make-date() as element() {
-    let $documentNode := doc("/db/apps/sade/textgrid/data/ahiqar_sample.xml")
-    return
-        tapi:make-date($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-date'][text() = 'unknown']")
-function tests:make-date-none() as element() {
-    let $documentNode := doc("/db/test-records/header-empty-history-msIdentifier.xml")
-    return
-        tapi:make-date($documentNode)
-};
-
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-origin'][text() = 'Alqosh, Iraq']")
-function tests:make-origin() as element() {
-    let $documentNode := doc("/db/apps/sade/textgrid/data/ahiqar_sample.xml")
-    return
-        tapi:make-origin($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-origin'][text() = 'Iraq']")
-function tests:make-origin-country-only() as element() {
-    let $documentNode := doc("/db/test-records/origin-country-only.xml")
-    return
-        tapi:make-origin($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-origin'][text() = 'Alqosh']")
-function tests:make-origin-place-only() as element() {
-    let $documentNode := doc("/db/test-records/origin-place-only.xml")
-    return
-        tapi:make-origin($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-origin'][text() = 'unknown']")
-function tests:make-origin-none() as element() {
-    let $documentNode := doc("/db/test-records/header-empty-history-msIdentifier.xml")
-    return
-        tapi:make-origin($documentNode)
-};
-
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-location'][text() = 'University of Cambridge - Cambridge University Library, Great Britain']")
-function tests:make-location() as element() {
-    let $documentNode := doc("/db/apps/sade/textgrid/data/ahiqar_sample.xml")
-    return
-        tapi:make-location($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-location'][text() = 'Great Britain']")
-function tests:make-location-country-only() as element() {
-    let $documentNode := doc("/db/test-records/location-country-only.xml")
-    return
-        tapi:make-location($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-location'][text() = 'University of Cambridge - Cambridge University Library']")
-function tests:make-location-institution-only() as element() {
-    let $documentNode := doc("/db/test-records/location-institution-only.xml")
-    return
-        tapi:make-location($documentNode)
-};
-
-declare
-    %test:assertXPath("$result[local-name(.) = 'x-location'][text() = 'unknown']")
-function tests:make-location-none() as element() {
-    let $documentNode := doc("/db/test-records/header-empty-history-msIdentifier.xml")
-    return
-        tapi:make-location($documentNode)
-};
-
-
 declare
     %test:assertXPath("$result/string() = '1234 5678'")
 function tests:remove-whitespaces() as document-node() {
diff --git a/exist-app/tests/tapi-collection-tests.xqm b/exist-app/tests/tapi-collection-tests.xqm
index 00f35f48..b34557fe 100644
--- a/exist-app/tests/tapi-collection-tests.xqm
+++ b/exist-app/tests/tapi-collection-tests.xqm
@@ -7,6 +7,7 @@ declare namespace tei="http://www.tei-c.org/ns/1.0";
 
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "../modules/commons.xqm";
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
+import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace t-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection" at "../modules/tapi-collection.xqm";
 
@@ -107,7 +108,7 @@ declare
 function tct:is-endpoint-available() {
     let $url := $tct:restxq || "/textapi/ahikar/ahiqar_collection/collection.json"
     return
-        local:is-endpoint-http200($url)
+        tc:is-endpoint-http200($url)
 };
 
 declare
@@ -237,24 +238,3 @@ as item() {
                    </http:request>
     return http:send-request($req)[2] => util:base64-decode() => parse-json()
 };
-
-
-
-
-declare function local:is-endpoint-http200($url as xs:string) as xs:boolean {
-    let $http-status := local:get-http-status($url)
-    return
-        $http-status = "200"
-};
-
-declare function local:get-http-status($url as xs:string) as xs:string {
-    let $req := local:make-request($url)
-    return
-        http:send-request($req)[1]/@status
-};
-
-declare function local:make-request($url as xs:string) {
-    <http:request href="{$url}" method="get">
-        <http:header name="Connection" value="close"/>
-   </http:request>
-};
\ No newline at end of file
diff --git a/exist-app/tests/tapi-manifest-tests.xqm b/exist-app/tests/tapi-manifest-tests.xqm
new file mode 100644
index 00000000..fc1cc2f8
--- /dev/null
+++ b/exist-app/tests/tapi-manifest-tests.xqm
@@ -0,0 +1,258 @@
+xquery version "3.1";
+
+module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests";
+
+declare namespace http = "http://expath.org/ns/http-client";
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "../modules/commons.xqm";
+import module namespace map="http://www.w3.org/2005/xpath-functions/map";
+import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
+import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
+import module namespace t-man="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest" at "../modules/tapi-manifest.xqm";
+
+declare variable $tmt:restxq := "http://0.0.0.0:8080/exist/restxq";
+declare variable $tmt:manifest1 := "test-manifest1.xml";
+declare variable $tmt:manifest2 := "test-manifest2.xml";
+declare variable $tmt:manifest3 := "test-manifest3.xml";
+declare variable $tmt:tei1-uri := "test-tei-1.xml";
+declare variable $tmt:tei2-uri := "test-tei-2.xml";
+declare variable $tmt:tei3-uri := "test-tei-3.xml";
+
+
+
+declare
+    %test:setUp
+function tmt:_test-setup(){
+    let $manifest1 :=
+        <rdf:RDF xmlns:ore="http://www.openarchives.org/ore/terms/"
+        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+            <rdf:Description rdf:about="test-aggregation-1">
+                <ore:aggregates rdf:resource="textgrid:test-tei-1"/>
+            </rdf:Description>
+        </rdf:RDF>
+    let $manifest2 :=
+        <rdf:RDF xmlns:ore="http://www.openarchives.org/ore/terms/"
+        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+            <rdf:Description rdf:about="test-aggregation-1">
+                <ore:aggregates rdf:resource="textgrid:test-tei-2"/>
+            </rdf:Description>
+        </rdf:RDF>
+    let $manifest3 :=
+        <rdf:RDF xmlns:ore="http://www.openarchives.org/ore/terms/"
+        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+            <rdf:Description rdf:about="test-aggregation-1">
+                <ore:aggregates rdf:resource="textgrid:test-tei-3"/>
+            </rdf:Description>
+        </rdf:RDF>
+        
+        
+    let $tei1 :=
+        <TEI xmlns="http://www.tei-c.org/ns/1.0">
+            <teiHeader>
+                <fileDesc>
+                    <titleStmt>
+                        <title type="main">A Minimal Dummy TEI</title>
+                    </titleStmt>
+                </fileDesc>
+            </teiHeader>
+        </TEI>
+        
+        let $tei2 :=
+            <TEI xmlns="http://www.tei-c.org/ns/1.0">
+                <teiHeader>
+                    <fileDesc>
+                        <titleStmt>
+                            <title type="main">A Minimal Dummy TEI2</title>
+                        </titleStmt>
+                        <sourceDesc>
+                            <msDesc>
+                                <msIdentifier>
+                                    <institution>University of Cambridge - Cambridge University Library</institution>
+                                </msIdentifier>
+                                <history>
+                                    <origin>
+                                        <country>Iraq</country>
+                                    </origin>
+                                </history>
+                            </msDesc>
+                        </sourceDesc>
+                    </fileDesc>
+                </teiHeader>
+            </TEI>
+        
+        let $tei3 :=
+            <TEI xmlns="http://www.tei-c.org/ns/1.0">
+                <teiHeader>
+                    <fileDesc>
+                        <titleStmt>
+                            <title type="main">A Minimal Dummy TEI3</title>
+                        </titleStmt>
+                        <sourceDesc>
+                            <msDesc>
+                                <msIdentifier>
+                                    <settlement>
+                                        <country>Great Britain</country>
+                                    </settlement>
+                                </msIdentifier>
+                                <history>
+                                    <origin>
+                                        <placeName>Alqosh</placeName>
+                                    </origin>
+                                </history>
+                            </msDesc>
+                        </sourceDesc>
+                    </fileDesc>
+                </teiHeader>
+            </TEI>
+        
+        
+    return
+        (
+            xmldb:store($commons:agg, $tmt:manifest1, $manifest1),
+            xmldb:store($commons:agg, $tmt:manifest2, $manifest2),
+            xmldb:store($commons:agg, $tmt:manifest3, $manifest3),
+            xmldb:store($commons:data, $tmt:tei1-uri, $tei1),
+            xmldb:store($commons:data, $tmt:tei2-uri, $tei2),
+            xmldb:store($commons:data, $tmt:tei3-uri, $tei3)
+
+        )
+};
+
+declare
+    %test:tearDown
+function tmt:_test-teardown() {
+    xmldb:remove($commons:agg, $tmt:manifest1),
+    xmldb:remove($commons:agg, $tmt:manifest2),
+    xmldb:remove($commons:agg, $tmt:manifest3),
+    xmldb:remove($commons:data, $tmt:tei1),
+    xmldb:remove($commons:data, $tmt:tei2),
+    xmldb:remove($commons:data, $tmt:tei3)
+};
+
+declare
+    %test:assertTrue
+function tmt:is-endpoint-200() {
+    let $url := $tmt:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
+    return
+        tc:is-endpoint-http200($url)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result//@* = 'textgrid:ahiqar_sample'")
+function tmt:get-aggregation($manifest-uri) {
+    t-man:get-aggregation($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result//* = 'textgrid:ahiqar_agg.0'")
+function tmt:get-metadata-file($manifest-uri) {
+    t-man:get-metadata-file($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result//*[local-name(.) = 'title'] = 'The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
+               king of Assyria and Nineveh'")
+function tmt:get-tei-xml-for-manifest($manifest-uri) {
+    t-man:get-tei-xml-for-manifest($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
+function tmt:get-xml-uri($manifest-uri as xs:string) {
+    t-man:get-xml-uri($manifest-uri)
+};
+
+
+declare
+    %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json')]")
+function tmt:make-sequences($collection-uri as xs:string,
+    $manifest-uri as xs:string) {
+    t-man:make-sequences($collection-uri, $manifest-uri, $tmt:restxq)
+};
+
+declare
+     %test:args("ahiqar_agg") %test:assertXPath("count($result) = 4")
+function tmt:get-valid-page-ids($manifest-uri as xs:string) {
+    t-man:get-valid-page-ids($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//label = 'Beispieldatei zum Testen'")
+    %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json')]")
+function tmt:get-json($collection-uri as xs:string,
+    $manifest-uri as xs:string) {
+    t-man:get-json($collection-uri, $manifest-uri, $tmt:restxq)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("Beispieldatei zum Testen")
+function tmt:get-manifest-title($manifest-uri as xs:string) {
+    t-man:get-manifest-title($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("count($result) = 2")
+    %test:args("ahiqar_agg") %test:assertXPath("$result//name = 'Simon Birol'")
+    %test:args("test-manifest1") %test:assertXPath("count($result) = 1")
+    %test:args("test-manifest1") %test:assertXPath("$result//name = 'none'")
+function tmt:make-editors($manifest-uri as xs:string) {
+    t-man:make-editors($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result/string() = '18.10.1697'")
+    %test:args("test-manifest1") %test:assertXPath("$result/string() = 'unknown'")
+function tmt:make-creation-date($manifest-uri as xs:string) {
+    t-man:make-creation-date($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result/string() = 'Alqosh, Iraq'")
+    %test:args("test-manifest1") %test:assertXPath("$result/string() = 'unknown'")
+    %test:args("test-manifest2") %test:assertXPath("$result/string() = 'Iraq'")
+    %test:args("test-manifest3") %test:assertXPath("$result/string() = 'Alqosh'")
+function tmt:make-origin($manifest-uri as xs:string) {
+    t-man:make-origin($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result/string() = 'University of Cambridge - Cambridge University Library, Great Britain'")
+    %test:args("test-manifest1") %test:assertXPath("$result/string() = 'unknown'")
+    %test:args("test-manifest2") %test:assertXPath("$result/string() = 'University of Cambridge - Cambridge University Library'")
+    %test:args("test-manifest3") %test:assertXPath("$result/string() = 'Great Britain'")
+function tmt:make-current-location($manifest-uri as xs:string) {
+    t-man:make-current-location($manifest-uri)
+};
+
+declare
+     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertExists
+function tmt:get-json($collection-uri as xs:string,
+    $manifest-uri as xs:string) {
+    t-man:get-json($collection-uri, $manifest-uri, $tmt:restxq)
+};
+
+
+declare
+    (: check if all parts are present.
+     : no further tests are needed since the content has been tested while testing
+     : the underlying function. :)
+    %test:assertXPath("map:contains($result, 'textapi')")
+    %test:assertXPath("map:contains($result, 'id')")
+    %test:assertXPath("map:contains($result, 'label')")
+    %test:assertXPath("map:contains($result, 'x-editor')")
+    %test:assertXPath("map:contains($result, 'x-date')")
+    %test:assertXPath("map:contains($result, 'x-origin')")
+    %test:assertXPath("map:contains($result, 'x-location')")
+    %test:assertXPath("map:contains($result, 'license')")
+    %test:assertXPath("map:contains($result, 'annotationCollection')")
+    %test:assertXPath("map:contains($result, 'sequence')")
+function tmt:endpoint()
+as item() {
+    let $url := $tmt:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
+    let $req := tc:make-request($url)
+    return
+        http:send-request($req)[2]
+        => util:base64-decode()
+        => parse-json()
+};
diff --git a/exist-app/tests/test-commons.xqm b/exist-app/tests/test-commons.xqm
new file mode 100644
index 00000000..2ca75f4b
--- /dev/null
+++ b/exist-app/tests/test-commons.xqm
@@ -0,0 +1,24 @@
+xquery version "3.1";
+
+module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons";
+
+declare namespace http = "http://expath.org/ns/http-client";
+
+
+declare function tc:is-endpoint-http200($url as xs:string) as xs:boolean {
+    let $http-status := tc:get-http-status($url)
+    return
+        $http-status = "200"
+};
+
+declare function tc:get-http-status($url as xs:string) as xs:string {
+    let $req := tc:make-request($url)
+    return
+        http:send-request($req)[1]/@status
+};
+
+declare function tc:make-request($url as xs:string) {
+    <http:request href="{$url}" method="get">
+        <http:header name="Connection" value="close"/>
+   </http:request>
+};
\ No newline at end of file
-- 
GitLab


From a212677c6a2d0c0ea5122ecd5c722d4a8f704ddc Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 12:42:38 +0200
Subject: [PATCH 02/22] tests: make runner self-reporting

---
 exist-app/tests-runner.xq | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 68e38c12..592a01d5 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -7,13 +7,24 @@ xquery version "3.1";
 import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tests/tapi-collection-tests.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace tests="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests.xqm";
+import module namespace tit="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tests/tapi-item-tests.xqm";
 import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tests/tapi-manifest-tests.xqm";
 import module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests" at "tests/collate-tests.xqm";
 import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
 
+let $test-results :=
+    (
+        (:test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),:)
+        (:test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/coll-tests")),:)
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
+        (:test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),:)
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests")),
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests"))
+    )
 
-test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),
-test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/coll-tests")),
-test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
-test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),
-test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests"))
\ No newline at end of file
+for $result in $test-results return
+    if ($result//@failures = 0
+    and $result//@errors = 0) then
+        "Everything okay: " || $result//@package
+    else
+        $result
\ No newline at end of file
-- 
GitLab


From ef96206a16f6307e9e1dda755b4778f31c25651d Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 12:44:03 +0200
Subject: [PATCH 03/22] feat: move item specific functions to separate module

---
 exist-app/modules/commons.xqm           |  28 ++++-
 exist-app/modules/tapi-item.xqm         | 140 ++++++++++++++++++++++++
 exist-app/modules/tapi-manifest.xqm     |  32 +-----
 exist-app/modules/tapi.xqm              |  85 --------------
 exist-app/tests.xqm                     |  49 ---------
 exist-app/tests/commons-tests.xqm       |  21 +++-
 exist-app/tests/tapi-item-tests.xqm     | 109 ++++++++++++++++++
 exist-app/tests/tapi-manifest-tests.xqm |  19 ----
 exist-app/tests/test-commons.xqm        |   1 +
 9 files changed, 301 insertions(+), 183 deletions(-)
 create mode 100644 exist-app/modules/tapi-item.xqm
 create mode 100644 exist-app/tests/tapi-item-tests.xqm

diff --git a/exist-app/modules/commons.xqm b/exist-app/modules/commons.xqm
index ece25019..2162a07e 100644
--- a/exist-app/modules/commons.xqm
+++ b/exist-app/modules/commons.xqm
@@ -2,6 +2,12 @@ xquery version "3.1";
 
 module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons";
 
+declare namespace ore="http://www.openarchives.org/ore/terms/";
+declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
+declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
+
 declare variable $commons:expath-pkg := doc("../expath-pkg.xml");
 declare variable $commons:version := $commons:expath-pkg/*/@version;
 declare variable $commons:tg-collection := "/db/apps/sade/textgrid";
@@ -15,4 +21,24 @@ declare variable $commons:responseHeader200 :=
         <http:response xmlns:http="http://expath.org/ns/http-client" status="200">
             <http:header name="Access-Control-Allow-Origin" value="*"/>
         </http:response>
-    </rest:response>;
\ No newline at end of file
+    </rest:response>;
+
+declare function commons:get-aggregation($manifest-uri as xs:string)
+as document-node() {
+    doc($commons:agg || $manifest-uri || ".xml")
+};
+
+declare function commons:get-xml-uri($manifest-uri as xs:string)
+as xs:string {
+    let $aggregation-file := commons:get-aggregation($manifest-uri)
+    return
+        $aggregation-file//ore:aggregates[1]/@rdf:resource
+        => substring-after(":")
+};
+
+declare function commons:get-tei-xml-for-manifest($manifest-uri as xs:string)
+as document-node() {
+    let $xml-uri := commons:get-xml-uri($manifest-uri)
+    return
+        doc($commons:data || $xml-uri || ".xml")
+};
\ No newline at end of file
diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
new file mode 100644
index 00000000..2ac51754
--- /dev/null
+++ b/exist-app/modules/tapi-item.xqm
@@ -0,0 +1,140 @@
+xquery version "3.1";
+
+(: 
+ : This module handles calls to the API on manifest level, e.g.
+ : 
+ : /textapi/ahikar/3r9ps/3rx15-8a/latest/item.json
+ :)
+
+module namespace t-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item";
+
+declare namespace ore="http://www.openarchives.org/ore/terms/";
+declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
+declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
+
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
+import module namespace requestr="http://exquery.org/ns/request";
+import module namespace rest="http://exquery.org/ns/restxq";
+
+declare variable $t-item:server := 
+    if(requestr:hostname() = "existdb") then
+        $commons:expath-pkg/*/@name => replace("/$", "")
+    else
+        "http://localhost:8094/exist/restxq";
+
+(:~
+ : Returns information about a given page in a document. This is mainly compliant
+ : with the SUB TextAPI, but has the following additions:
+ :  * the division number, 'n', is mandatory
+ :  * 'image' is mandatory since every page has a facsimile
+ : 
+ : The parameter $collection-uri is actually not necessary but introduced to keep
+ : the structure of the API clear.
+ :
+ : Sample call to API: /api/textapi/ahikar/3r17c/3r1pq-147a/latest/item.json
+ :
+ : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#item
+ : @param $collection-uri The unprefixed TextGrid URI of a collection, e.g. '3r17c'
+ : @param $manifest-uri The unprefixed TextGrid URI of a document, e.g. '3r1pq'
+ : @param $page A page number as encoded in a tei:pb/@n, e.g. '147a'
+ : @return Information about a page
+ :)
+declare
+    %rest:GET
+    %rest:HEAD
+    %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}-{$page}/latest/item.json")
+    %output:method("json")
+function t-item:endpoint($collection-uri as xs:string,
+    $manifest-uri as xs:string,
+    $page as xs:string)
+as item()+ {
+    $commons:responseHeader200,
+    t-item:get-json($collection-uri, $manifest-uri, $page, $t-item:server)
+};
+
+
+declare function t-item:get-json($collection-uri as xs:string,
+    $manifest-uri as xs:string,
+    $page as xs:string,
+    $server as xs:string)
+as element(object) {
+    <object>
+        <textapi>{$commons:version}</textapi>
+        <title>{t-item:make-title($manifest-uri)}</title>
+        <type>page</type>
+        <n>{$page}</n>
+        <content>{$server}/api/content/{commons:get-xml-uri($manifest-uri)}-{$page}.html</content>
+        <content-type>application/xhtml+xml</content-type>
+        {
+            t-item:make-iso-language-elements($manifest-uri),
+            t-item:make-alt-language-elements($manifest-uri)
+        }
+        <x-langString>{t-item:get-language-string($manifest-uri)}</x-langString>
+        <image>
+            <id>{t-item:make-facsimile-id($manifest-uri, $page, $server)}</id>
+        </image>
+        <annotationCollection>{$server}/api/textapi/ahikar/{$collection-uri}/{$manifest-uri}-{$page}/annotationCollection.json</annotationCollection>
+    </object>
+};
+
+
+declare function t-item:make-title($manifest-uri)
+as xs:string {
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
+    return
+        $tei-xml//tei:title[@type = "main"]/string()
+};
+
+
+declare function t-item:make-iso-language-elements($manifest-uri as xs:string)
+as element(lang)* {
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
+    let $iso-languages := 
+        $tei-xml//tei:language[@xml:base = "https://iso639-3.sil.org/code/"]/@ident/string()
+    for $lang in $iso-languages return
+        element lang {$lang}
+};
+
+
+declare function t-item:make-alt-language-elements($manifest-uri as xs:string)
+as element(lang)* {
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
+    let $iso-languages := 
+        $tei-xml//tei:language[not(@xml:base = "https://iso639-3.sil.org/code/")]/@ident/string()
+    for $lang in $iso-languages return
+        element langAlt {$lang}
+};
+
+
+declare function t-item:get-language-string($manifest-uri as xs:string)
+as xs:string {
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
+    let $langString :=
+        for $lang in $tei-xml//tei:language/text()
+        order by $lang
+        return $lang
+    return
+        string-join($langString, ", ")
+};
+
+
+declare function t-item:make-facsimile-id($manifest-uri as xs:string,
+    $page as xs:string,
+    $server as xs:string)
+as xs:string {
+    let $image-uri := t-item:get-facsimile-uri-for-page($manifest-uri, $page)
+    return
+        $server || "/api/images/" || $image-uri
+};
+
+
+declare function t-item:get-facsimile-uri-for-page($manifest-uri as xs:string,
+    $page as xs:string)
+as xs:string {
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
+    return
+        $tei-xml//tei:pb[@n = $page]/@facs
+        => substring-after("textgrid:")
+};
diff --git a/exist-app/modules/tapi-manifest.xqm b/exist-app/modules/tapi-manifest.xqm
index 27da1faf..b65e0373 100644
--- a/exist-app/modules/tapi-manifest.xqm
+++ b/exist-app/modules/tapi-manifest.xqm
@@ -3,7 +3,7 @@ xquery version "3.1";
 (: 
  : This module handles calls to the API on manifest level, e.g.
  : 
- : /textapi/ahikar/3r9ps3rx15/manifest.json
+ : /textapi/ahikar/3r9ps/3rx15/manifest.json
  :)
 
 module namespace t-man="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest";
@@ -62,11 +62,6 @@ as element(object) {
 };
 
 
-declare function t-man:get-aggregation($manifest-uri as xs:string)
-as document-node() {
-    doc($commons:agg || $manifest-uri || ".xml")
-};
-
 declare function t-man:make-sequences($collection-uri as xs:string,
     $manifest-uri as xs:string,
     $server as xs:string)
@@ -84,26 +79,11 @@ as element(sequence)+ {
 
 declare function t-man:get-valid-page-ids($manifest-uri as xs:string)
 as xs:string+ {
-    let $tei-xml :=  t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     return
         $tei-xml//tei:pb[@facs]/string(@n)
 };
 
-declare function t-man:get-tei-xml-for-manifest($manifest-uri as xs:string)
-as document-node() {
-    let $xml-uri := t-man:get-xml-uri($manifest-uri)
-    return
-        doc($commons:data || $xml-uri || ".xml")
-};
-
-declare function t-man:get-xml-uri($manifest-uri as xs:string)
-as xs:string {
-    let $aggregation-file := t-man:get-aggregation($manifest-uri)
-    return
-        $aggregation-file//ore:aggregates[1]/@rdf:resource
-        => substring-after(":")
-};
-
 declare function t-man:get-manifest-title($manifest-uri as xs:string)
 as xs:string {
     let $metadata-file := t-man:get-metadata-file($manifest-uri)
@@ -119,7 +99,7 @@ as document-node() {
 
 declare function t-man:make-editors($manifest-uri as xs:string)
 as element(x-editor)+ {
-    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $editors := $tei-xml//tei:titleStmt//tei:editor
     return
         if (exists($editors)) then
@@ -138,7 +118,7 @@ as element(x-editor)+ {
 
 declare function t-man:make-creation-date($manifest-uri as xs:string)
 as element(x-date) {
-    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $creation-date := $tei-xml//tei:history//tei:date
     let $string :=
         if ($creation-date) then
@@ -152,7 +132,7 @@ as element(x-date) {
 
 declare function t-man:make-origin($manifest-uri as xs:string) as 
 element(x-origin) {
-    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $country := $tei-xml//tei:history//tei:country
     let $place := $tei-xml//tei:history//tei:placeName
     let $string :=
@@ -171,7 +151,7 @@ element(x-origin) {
 
 declare function t-man:make-current-location($manifest-uri as xs:string) as
 element(x-location) {
-    let $tei-xml := t-man:get-tei-xml-for-manifest($manifest-uri)
+    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $institution := $tei-xml//tei:msIdentifier//tei:institution
     let $country := $tei-xml//tei:msIdentifier//tei:country
     let $string :=
diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index fb552029..8d640be7 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -81,91 +81,6 @@ declare function tapi:remove-whitespaces($doc as document-node()) as document-no
 };
 
 
-(:~
- : Returns information about a given page in a document. This is mainly compliant
- : with the SUB TextAPI, but has the following additions:
- :  * the division number, 'n', is mandatory
- :  * 'image' is mandatory since every page has a facsimile
- : 
- : The parameter $collection is actually not necessary but introduced to keep
- : the structure of the API clear.
- :
- : Sample call to API: /api/textapi/ahikar/3r17c/3r1pq-147a/latest/item.json
- :
- : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#item
- : @param $collection The unprefixed TextGrid URI of a collection, e.g. '3r17c'
- : @param $document The unprefixed TextGrid URI of a document, e.g. '3r1pq'
- : @param $page A page number as encoded in a tei:pb/@n, e.g. '147a'
- : @return Information about a page
- :)
-declare
-    %rest:GET
-    %rest:HEAD
-    %rest:path("/textapi/ahikar/{$collection}/{$document}-{$page}/latest/item.json")
-    %output:method("json")
-function tapi:item-rest($collection as xs:string, $document as xs:string,
-$page as xs:string) as item()+ {
-    $commons:responseHeader200,
-    tapi:item($collection, $document, $page, $tapi:server)
-};
-
-
-(:~
- : Returns information about a given page.
- :
- : @param $document The unprefixed TextGrid URI of a collection, e.g. '3r9ps'
- : @param $document The unprefixed TextGrid URI of a document, e.g. '3r1pq'
- : @param $page A page number as encoded in a tei:pb/@n, e.g. '147a'
- : @param $server A string indicating the server. This parameter has been introduced to make this function testable and defaults to $tapi:server.
- : @return An object element containing all necessary information about an item
- :)
-declare function tapi:item($collection as xs:string, $document as xs:string,
-$page as xs:string, $server as xs:string) 
-as element(object) {
-    let $aggNode := doc($commons:agg || $document || ".xml")
-    let $teiUri :=
-        if($aggNode)
-            then $aggNode//ore:aggregates[1]/@rdf:resource => substring-after(":")
-        else $document
-    let $image := doc($commons:data || $teiUri || ".xml")//tei:pb[@n = $page]/@facs => substring-after("textgrid:")
-    
-    let $xml := doc($commons:data || $teiUri || ".xml")
-    let $title := $xml//tei:title[@type = "main"]/string()
-    let $iso-languages := 
-        $xml//tei:language[@xml:base = "https://iso639-3.sil.org/code/"]/@ident/string()
-    let $alt-languages :=
-        $xml//tei:language[not(@xml:base = "https://iso639-3.sil.org/code/")]/@ident/string()
-    let $langString :=
-        for $lang in $xml//tei:language/text()
-        order by $lang
-        return $lang
-    let $langString := string-join($langString, ", ")
-    
-    return
-    <object>
-        <textapi>{$commons:version}</textapi>
-        <title>{$title}</title>
-        <type>page</type>
-        <n>{$page}</n>
-        <content>{$server}/api/content/{$teiUri}-{$page}.html</content>
-        <content-type>application/xhtml+xml</content-type>
-        {
-            for $lang in $iso-languages return
-                element lang {$lang}
-        }
-        {
-            for $lang in $alt-languages return
-                element langAlt {$lang}
-        }
-        <x-langString>{$langString}</x-langString>
-        <image>
-            <id>{$server}/api/images/{$image}</id>
-        </image>
-        <annotationCollection>{$server}/api/textapi/ahikar/{$collection}/{$document}-{$page}/annotationCollection.json</annotationCollection>
-    </object>
-};
-
-
 (:~
  : Returns an HTML rendering of a given page.
  : 
diff --git a/exist-app/tests.xqm b/exist-app/tests.xqm
index 5a38831b..c9cab391 100644
--- a/exist-app/tests.xqm
+++ b/exist-app/tests.xqm
@@ -86,27 +86,6 @@ function tests:api-info()  as item() {
 };
 
 
-declare
-    (: check if all parts are present.
-     : no further tests are needed since the content has been tested while testing
-     : the underlying function. :)
-    %test:assertXPath("map:contains($result, 'textapi')")
-    %test:assertXPath("map:contains($result, 'title')")
-    %test:assertXPath("map:contains($result, 'type')")
-    %test:assertXPath("map:contains($result, 'n')")
-    %test:assertXPath("map:contains($result, 'content')")
-    %test:assertXPath("map:contains($result, 'content-type')")
-    %test:assertXPath("map:contains($result, 'lang')")
-    %test:assertXPath("map:contains($result, 'image')")
-function tests:item-rest() as item() {
-    let $url := $tests:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
-    let $req := <http:request href="{$url}" method="get">
-                        <http:header name="Connection" value="close"/>
-                   </http:request>
-    return http:send-request($req)[2] => util:base64-decode() => parse-json()
-};
-
-
 declare
     (: check if tei:div is present.
      : no further tests are needed since the content has been tested while testing
@@ -150,34 +129,6 @@ function tests:content-txt() as xs:string {
 };
 
 
-(: ************************
- : * UNDERLYING FUNCTIONS *
- : ************************
- : all the functions that contribute to but do not define RESTXQ endpoints
- :)
-
-
-
-
-declare
-    %test:args("ahiqar_collection", "ahiqar_agg", "82a", "http://localhost:8080/exist/restxq")
-    (: checks if the correct file has been opened :)
-    %test:assertXPath("$result//*[local-name(.) = 'title'] = 'The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
-               king of Assyria and Nineveh' ")
-    (: checks if language assembling works correctly :)
-    %test:assertXPath("$result//*[local-name(.) = 'lang'] = 'syc' ")
-    %test:assertXPath("$result//*[local-name(.) = 'langAlt'] = 'karshuni' ")
-    %test:assertXPath("$result//*[local-name(.) = 'x-langString'][matches(., 'Classical Syriac')]")
-    (: checks if underlying pages are identified :)
-    %test:assertXPath("$result//*[local-name(.) = 'content'] = 'http://localhost:8080/exist/restxq/api/content/ahiqar_sample-82a.html' ")
-    (: checks if images connected to underlying pages are identified :)
-    %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://localhost:8080/exist/restxq/api/images/3r1nz' ")
-function tests:item($collection as xs:string, $document as xs:string, $page as xs:string, $server as xs:string) 
-as element(object){
-    tapi:item($collection, $document, $page, $server)
-};
-
-
 declare
     %test:args("ahiqar_sample", "82a")
     (: checks if there is text at all in the result :)
diff --git a/exist-app/tests/commons-tests.xqm b/exist-app/tests/commons-tests.xqm
index 78dc0a5c..f33acf42 100644
--- a/exist-app/tests/commons-tests.xqm
+++ b/exist-app/tests/commons-tests.xqm
@@ -5,13 +5,28 @@ module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests";
 declare namespace http = "http://expath.org/ns/http-client";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "../modules/commons.xqm";
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 
 declare variable $ct:restxq := "http://0.0.0.0:8080/exist/restxq/";
 
 declare
-    %test:assertTrue
-function ct:succes() {
-    true()
+    %test:args("ahiqar_agg") %test:assertXPath("$result//@* = 'textgrid:ahiqar_sample'")
+function ct:get-aggregation($manifest-uri as xs:string) {
+    commons:get-aggregation($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
+function ct:get-xml-uri($manifest-uri as xs:string)
+as xs:string {
+    commons:get-xml-uri($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("$result//*[local-name(.) = 'title'] = 'The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
+               king of Assyria and Nineveh'")
+function ct:get-tei-xml-for-manifest($manifest-uri) {
+    commons:get-tei-xml-for-manifest($manifest-uri)
 };
\ No newline at end of file
diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
new file mode 100644
index 00000000..5f14afcc
--- /dev/null
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -0,0 +1,109 @@
+xquery version "3.1";
+
+module namespace tit="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests";
+
+declare namespace http = "http://expath.org/ns/http-client";
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+
+import module namespace map="http://www.w3.org/2005/xpath-functions/map";
+import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
+import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
+import module namespace t-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item" at "../modules/tapi-item.xqm";
+
+
+declare
+    %test:assertTrue
+function tit:is-endpoint-200()
+as xs:boolean {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
+    return
+        tc:is-endpoint-http200($url)
+};
+
+declare
+    %test:args("ahiqar_agg", "82a") %test:assertEquals("3r1nz")
+function tit:get-facsimile-uri-for-page($manifest-uri as xs:string,
+    $page as xs:string)
+as xs:string {
+    t-item:get-facsimile-uri-for-page($manifest-uri, $page)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("Arabic, Classical Syriac, Eastern Syriac, Karshuni, Western Syriac")
+function tit:get-language-string($manifest-uri as xs:string)
+as xs:string {
+    t-item:get-language-string($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg", "82a") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/images/3r1nz")
+function tit:make-facsimile-id($manifest-uri as xs:string,
+    $page as xs:string)
+as xs:string {
+    t-item:make-facsimile-id($manifest-uri, $page, $tc:server)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("count($result) = 2")
+    %test:args("ahiqar_agg") %test:assertXPath("$result = 'ara'")
+function tit:make-iso-language-elements($manifest-uri as xs:string)
+as element(lang)+ {
+    t-item:make-iso-language-elements($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("count($result) = 3")
+    %test:args("ahiqar_agg") %test:assertXPath("$result = 'syc-syrj'")
+function tit:make-alt-language-elements($manifest-uri as xs:string)
+as element(lang)+ {
+    t-item:make-alt-language-elements($manifest-uri)
+};
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
+               king of Assyria and Nineveh")
+function tit:make-title($manifest-uri as xs:string)
+as xs:string {
+    t-item:make-title($manifest-uri)
+};
+
+
+declare
+    (: check if all parts are present.
+     : no further tests are needed since the content has been tested while testing
+     : the underlying function. :)
+    %test:assertXPath("map:contains($result, 'textapi')")
+    %test:assertXPath("map:contains($result, 'title')")
+    %test:assertXPath("map:contains($result, 'type')")
+    %test:assertXPath("map:contains($result, 'n')")
+    %test:assertXPath("map:contains($result, 'content')")
+    %test:assertXPath("map:contains($result, 'content-type')")
+    %test:assertXPath("map:contains($result, 'lang')")
+    %test:assertXPath("map:contains($result, 'image')")
+function tit:is-data-complete() as item() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
+    let $req := tc:make-request($url)
+    return http:send-request($req)[2]
+        => util:base64-decode()
+        => parse-json()
+};
+
+declare
+    %test:args("ahiqar_collection", "ahiqar_agg", "82a")
+    (: checks if the correct file has been opened :)
+    %test:assertXPath("$result//*[local-name(.) = 'title'] = 'The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
+               king of Assyria and Nineveh' ")
+    (: checks if language assembling works correctly :)
+    %test:assertXPath("$result//*[local-name(.) = 'lang'] = 'syc' ")
+    %test:assertXPath("$result//*[local-name(.) = 'langAlt'] = 'karshuni' ")
+    %test:assertXPath("$result//*[local-name(.) = 'x-langString'][matches(., 'Classical Syriac')]")
+    (: checks if underlying pages are identified :)
+    %test:assertXPath("$result//*[local-name(.) = 'content'] = 'http://0.0.0.0:8080/exist/restxq/api/content/ahiqar_sample-82a.html' ")
+    (: checks if images connected to underlying pages are identified :)
+    %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://0.0.0.0:8080/exist/restxq/api/images/3r1nz' ")
+function tit:get-json($collection as xs:string,
+    $document as xs:string,
+    $page as xs:string) 
+as element(object){
+    t-item:get-json($collection, $document, $page, $tc:server)
+};
\ No newline at end of file
diff --git a/exist-app/tests/tapi-manifest-tests.xqm b/exist-app/tests/tapi-manifest-tests.xqm
index fc1cc2f8..75c0d3cf 100644
--- a/exist-app/tests/tapi-manifest-tests.xqm
+++ b/exist-app/tests/tapi-manifest-tests.xqm
@@ -138,31 +138,12 @@ function tmt:is-endpoint-200() {
         tc:is-endpoint-http200($url)
 };
 
-declare
-    %test:args("ahiqar_agg") %test:assertXPath("$result//@* = 'textgrid:ahiqar_sample'")
-function tmt:get-aggregation($manifest-uri) {
-    t-man:get-aggregation($manifest-uri)
-};
-
 declare
     %test:args("ahiqar_agg") %test:assertXPath("$result//* = 'textgrid:ahiqar_agg.0'")
 function tmt:get-metadata-file($manifest-uri) {
     t-man:get-metadata-file($manifest-uri)
 };
 
-declare
-    %test:args("ahiqar_agg") %test:assertXPath("$result//*[local-name(.) = 'title'] = 'The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
-               king of Assyria and Nineveh'")
-function tmt:get-tei-xml-for-manifest($manifest-uri) {
-    t-man:get-tei-xml-for-manifest($manifest-uri)
-};
-
-declare
-    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
-function tmt:get-xml-uri($manifest-uri as xs:string) {
-    t-man:get-xml-uri($manifest-uri)
-};
-
 
 declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json')]")
diff --git a/exist-app/tests/test-commons.xqm b/exist-app/tests/test-commons.xqm
index 2ca75f4b..eaa17779 100644
--- a/exist-app/tests/test-commons.xqm
+++ b/exist-app/tests/test-commons.xqm
@@ -4,6 +4,7 @@ module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons";
 
 declare namespace http = "http://expath.org/ns/http-client";
 
+declare variable $tc:server := "http://0.0.0.0:8080/exist/restxq";
 
 declare function tc:is-endpoint-http200($url as xs:string) as xs:boolean {
     let $http-status := tc:get-http-status($url)
-- 
GitLab


From 7fb34818398a617793b4aafa54aa4ec767b8dcf7 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 15:12:47 +0200
Subject: [PATCH 04/22] refactor: duplicate code

---
 exist-app/modules/tapi-item.xqm     | 33 +++++++++--------------------
 exist-app/tests/tapi-item-tests.xqm | 30 +++++++++++---------------
 2 files changed, 22 insertions(+), 41 deletions(-)

diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index 2ac51754..294916e5 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -29,9 +29,6 @@ declare variable $t-item:server :=
  : with the SUB TextAPI, but has the following additions:
  :  * the division number, 'n', is mandatory
  :  * 'image' is mandatory since every page has a facsimile
- : 
- : The parameter $collection-uri is actually not necessary but introduced to keep
- : the structure of the API clear.
  :
  : Sample call to API: /api/textapi/ahikar/3r17c/3r1pq-147a/latest/item.json
  :
@@ -67,10 +64,7 @@ as element(object) {
         <n>{$page}</n>
         <content>{$server}/api/content/{commons:get-xml-uri($manifest-uri)}-{$page}.html</content>
         <content-type>application/xhtml+xml</content-type>
-        {
-            t-item:make-iso-language-elements($manifest-uri),
-            t-item:make-alt-language-elements($manifest-uri)
-        }
+        {t-item:make-language-elements($manifest-uri)}
         <x-langString>{t-item:get-language-string($manifest-uri)}</x-langString>
         <image>
             <id>{t-item:make-facsimile-id($manifest-uri, $page, $server)}</id>
@@ -88,23 +82,16 @@ as xs:string {
 };
 
 
-declare function t-item:make-iso-language-elements($manifest-uri as xs:string)
-as element(lang)* {
-    let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
-    let $iso-languages := 
-        $tei-xml//tei:language[@xml:base = "https://iso639-3.sil.org/code/"]/@ident/string()
-    for $lang in $iso-languages return
-        element lang {$lang}
-};
-
-
-declare function t-item:make-alt-language-elements($manifest-uri as xs:string)
-as element(lang)* {
+declare function t-item:make-language-elements($manifest-uri as xs:string)
+as element()+ {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
-    let $iso-languages := 
-        $tei-xml//tei:language[not(@xml:base = "https://iso639-3.sil.org/code/")]/@ident/string()
-    for $lang in $iso-languages return
-        element langAlt {$lang}
+    let $languages := $tei-xml//tei:language
+    return
+        for $lang in $languages return
+            if ($lang[@xml:base = "https://iso639-3.sil.org/code/"]) then
+                element lang {$lang/@ident/string()}
+            else
+                element langAlt {$lang/@ident/string()}
 };
 
 
diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
index 5f14afcc..821a5814 100644
--- a/exist-app/tests/tapi-item-tests.xqm
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -5,6 +5,7 @@ module namespace tit="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests";
 declare namespace http = "http://expath.org/ns/http-client";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "../modules/commons.xqm";
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
 import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
@@ -13,8 +14,7 @@ import module namespace t-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item
 
 declare
     %test:assertTrue
-function tit:is-endpoint-200()
-as xs:boolean {
+function tit:is-endpoint-200() {
     let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
     return
         tc:is-endpoint-http200($url)
@@ -43,22 +43,6 @@ as xs:string {
     t-item:make-facsimile-id($manifest-uri, $page, $tc:server)
 };
 
-declare
-    %test:args("ahiqar_agg") %test:assertXPath("count($result) = 2")
-    %test:args("ahiqar_agg") %test:assertXPath("$result = 'ara'")
-function tit:make-iso-language-elements($manifest-uri as xs:string)
-as element(lang)+ {
-    t-item:make-iso-language-elements($manifest-uri)
-};
-
-declare
-    %test:args("ahiqar_agg") %test:assertXPath("count($result) = 3")
-    %test:args("ahiqar_agg") %test:assertXPath("$result = 'syc-syrj'")
-function tit:make-alt-language-elements($manifest-uri as xs:string)
-as element(lang)+ {
-    t-item:make-alt-language-elements($manifest-uri)
-};
-
 declare
     %test:args("ahiqar_agg") %test:assertEquals("The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
                king of Assyria and Nineveh")
@@ -79,6 +63,7 @@ declare
     %test:assertXPath("map:contains($result, 'content')")
     %test:assertXPath("map:contains($result, 'content-type')")
     %test:assertXPath("map:contains($result, 'lang')")
+    %test:assertXPath("map:contains($result, 'langAlt')")
     %test:assertXPath("map:contains($result, 'image')")
 function tit:is-data-complete() as item() {
     let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
@@ -106,4 +91,13 @@ function tit:get-json($collection as xs:string,
     $page as xs:string) 
 as element(object){
     t-item:get-json($collection, $document, $page, $tc:server)
+};
+
+
+declare
+    %test:args("ahiqar_agg") %test:assertXPath("count($result) = 5")
+    %test:args("ahiqar_agg") %test:assertXPath("$result[local-name(.) = ('lang', 'langAlt')]")
+    %test:args("ahiqar_agg") %test:assertXPath("count($result[local-name(.) = 'lang']) = 2")
+function tit:make-language-elements($manifest-uri as xs:string) {
+    t-item:make-language-elements($manifest-uri)
 };
\ No newline at end of file
-- 
GitLab


From 0f32277953ad3ed943313584c9a87c3e595cc8c5 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 15:13:20 +0200
Subject: [PATCH 05/22] test: switch all tests on again

---
 exist-app/tests-runner.xq | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 592a01d5..209f2eeb 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -14,10 +14,10 @@ import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests
 
 let $test-results :=
     (
-        (:test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),:)
-        (:test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/coll-tests")),:)
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/coll-tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
-        (:test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),:)
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests"))
     )
-- 
GitLab


From 2c124ab7b3d2c917bd9bbeeec57fdd5ab7996e55 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 15:23:17 +0200
Subject: [PATCH 06/22] refactor: change namespaces

---
 exist-app/modules/tapi-collection.xqm     | 50 +++++++++++------------
 exist-app/modules/tapi-item.xqm           | 30 +++++++-------
 exist-app/modules/tapi-manifest.xqm       | 42 +++++++++----------
 exist-app/tests/tapi-collection-tests.xqm | 38 ++++++++---------
 exist-app/tests/tapi-item-tests.xqm       | 32 +++++++--------
 exist-app/tests/tapi-manifest-tests.xqm   | 22 +++++-----
 6 files changed, 107 insertions(+), 107 deletions(-)

diff --git a/exist-app/modules/tapi-collection.xqm b/exist-app/modules/tapi-collection.xqm
index e70bad22..08c40b87 100644
--- a/exist-app/modules/tapi-collection.xqm
+++ b/exist-app/modules/tapi-collection.xqm
@@ -6,7 +6,7 @@ xquery version "3.1";
  : /textapi/ahikar/3r9ps/collection.json
  :)
 
-module namespace t-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection";
+module namespace tapi-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection";
 
 declare namespace ore="http://www.openarchives.org/ore/terms/";
 declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
@@ -17,7 +17,7 @@ import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons"
 import module namespace requestr="http://exquery.org/ns/request";
 import module namespace rest="http://exquery.org/ns/restxq";
 
-declare variable $t-coll:server := 
+declare variable $tapi-coll:server := 
     if(requestr:hostname() = "existdb") then
         $commons:expath-pkg/*/@name => replace("/$", "")
     else
@@ -34,10 +34,10 @@ declare
     %rest:HEAD
     %rest:path("/textapi/ahikar/{$collection-uri}/collection.json")
     %output:method("json")
-function t-coll:endpoint($collection-uri as xs:string)
+function tapi-coll:endpoint($collection-uri as xs:string)
 as item()+ {
     $commons:responseHeader200,
-    t-coll:get-json($collection-uri, $t-coll:server)
+    tapi-coll:get-json($collection-uri, $tapi-coll:server)
 };
 
 (:~
@@ -53,13 +53,13 @@ as item()+ {
  : @param $server A string indicating the server. This parameter has been introduced to make this function testable.
  : @return An object element containing all necessary information
  :)
-declare function t-coll:get-json($collection-uri as xs:string,
+declare function tapi-coll:get-json($collection-uri as xs:string,
     $server as xs:string)
 as item()+ {
-    let $metadata-file := t-coll:get-metadata-file($collection-uri)
-    let $format-type := t-coll:get-format-type($metadata-file)
-    let $sequence := t-coll:make-sequence($collection-uri, $server)
-    let $annotationCollection-uri := t-coll:make-annotationCollection-uri($server, $collection-uri)
+    let $metadata-file := tapi-coll:get-metadata-file($collection-uri)
+    let $format-type := tapi-coll:get-format-type($metadata-file)
+    let $sequence := tapi-coll:make-sequence($collection-uri, $server)
+    let $annotationCollection-uri := tapi-coll:make-annotationCollection-uri($server, $collection-uri)
 
     return
     <object>
@@ -86,7 +86,7 @@ as item()+ {
     </object>
 };
 
-declare function t-coll:get-aggregation($collection-uri as xs:string)
+declare function tapi-coll:get-aggregation($collection-uri as xs:string)
 as document-node() {
     doc($commons:agg || $collection-uri || ".xml")
 };
@@ -102,7 +102,7 @@ as document-node() {
  : @return A list of ore:aggregates without the manifests to be excluded
  : 
  :)
-declare function t-coll:get-allowed-manifest-uris($aggregation-file as node())
+declare function tapi-coll:get-allowed-manifest-uris($aggregation-file as node())
 as xs:string+ {
     let $not-allowed :=
         (
@@ -113,29 +113,29 @@ as xs:string+ {
             $aggregate[@rdf:resource != $not-allowed]/@rdf:resource
     return
         for $uri in $allowed return
-            t-coll:remove-textgrid-prefix($uri)
+            tapi-coll:remove-textgrid-prefix($uri)
 };
 
-declare function t-coll:remove-textgrid-prefix($uri as xs:string)
+declare function tapi-coll:remove-textgrid-prefix($uri as xs:string)
 as xs:string {
     replace($uri, "textgrid:", "")
 };
 
-declare function t-coll:get-metadata-file($uri as xs:string)
+declare function tapi-coll:get-metadata-file($uri as xs:string)
 as document-node() {
     doc($commons:meta || $uri || ".xml")
 };
 
-declare function t-coll:make-sequence($collection-uri as xs:string,
+declare function tapi-coll:make-sequence($collection-uri as xs:string,
     $server as xs:string)
 as element(sequence)+ {
-    let $aggregation := t-coll:get-aggregation($collection-uri)
-    let $allowed-manifest-uris := t-coll:get-allowed-manifest-uris($aggregation/*)
+    let $aggregation := tapi-coll:get-aggregation($collection-uri)
+    let $allowed-manifest-uris := tapi-coll:get-allowed-manifest-uris($aggregation/*)
     
     for $manifest-uri in $allowed-manifest-uris return
-        let $manifest-metadata :=  t-coll:get-metadata-file($manifest-uri)
-        let $id := t-coll:make-id($server, $collection-uri, $manifest-uri)
-        let $type := t-coll:make-format-type($manifest-metadata)
+        let $manifest-metadata :=  tapi-coll:get-metadata-file($manifest-uri)
+        let $id := tapi-coll:make-id($server, $collection-uri, $manifest-uri)
+        let $type := tapi-coll:make-format-type($manifest-metadata)
         return
             <sequence>
                 <id>{$id}</id>
@@ -143,20 +143,20 @@ as element(sequence)+ {
             </sequence>
 };
 
-declare function t-coll:make-id($server as xs:string,
+declare function tapi-coll:make-id($server as xs:string,
     $collection-uri as xs:string,
     $manifest-uri as xs:string)
 as xs:string {
     $server || "/api/textapi/ahikar/" || $collection-uri || "/" || $manifest-uri || "/manifest.json"
 };
 
-declare function t-coll:get-format-type($metadata as document-node())
+declare function tapi-coll:get-format-type($metadata as document-node())
 as xs:string {
     $metadata//tgmd:format[1]/string()
-    => t-coll:make-format-type()
+    => tapi-coll:make-format-type()
 };
 
-declare function t-coll:make-format-type($tgmd-format as xs:string)
+declare function tapi-coll:make-format-type($tgmd-format as xs:string)
 as xs:string {
     switch ($tgmd-format)
         case "text/tg.aggregation+xml" return "collection"
@@ -164,7 +164,7 @@ as xs:string {
         default return "manifest"
 };
 
-declare function t-coll:make-annotationCollection-uri($server as xs:string,
+declare function tapi-coll:make-annotationCollection-uri($server as xs:string,
     $collection-uri as xs:string)
 as xs:string {
     $server || "/api/textapi/ahikar/" || $collection-uri || "/annotationCollection.json"
diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index 294916e5..c8f131c1 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -6,7 +6,7 @@ xquery version "3.1";
  : /textapi/ahikar/3r9ps/3rx15-8a/latest/item.json
  :)
 
-module namespace t-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item";
+module namespace tapi-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item";
 
 declare namespace ore="http://www.openarchives.org/ore/terms/";
 declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
@@ -18,7 +18,7 @@ import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons"
 import module namespace requestr="http://exquery.org/ns/request";
 import module namespace rest="http://exquery.org/ns/restxq";
 
-declare variable $t-item:server := 
+declare variable $tapi-item:server := 
     if(requestr:hostname() = "existdb") then
         $commons:expath-pkg/*/@name => replace("/$", "")
     else
@@ -43,38 +43,38 @@ declare
     %rest:HEAD
     %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}-{$page}/latest/item.json")
     %output:method("json")
-function t-item:endpoint($collection-uri as xs:string,
+function tapi-item:endpoint($collection-uri as xs:string,
     $manifest-uri as xs:string,
     $page as xs:string)
 as item()+ {
     $commons:responseHeader200,
-    t-item:get-json($collection-uri, $manifest-uri, $page, $t-item:server)
+    tapi-item:get-json($collection-uri, $manifest-uri, $page, $tapi-item:server)
 };
 
 
-declare function t-item:get-json($collection-uri as xs:string,
+declare function tapi-item:get-json($collection-uri as xs:string,
     $manifest-uri as xs:string,
     $page as xs:string,
     $server as xs:string)
 as element(object) {
     <object>
         <textapi>{$commons:version}</textapi>
-        <title>{t-item:make-title($manifest-uri)}</title>
+        <title>{tapi-item:make-title($manifest-uri)}</title>
         <type>page</type>
         <n>{$page}</n>
         <content>{$server}/api/content/{commons:get-xml-uri($manifest-uri)}-{$page}.html</content>
         <content-type>application/xhtml+xml</content-type>
-        {t-item:make-language-elements($manifest-uri)}
-        <x-langString>{t-item:get-language-string($manifest-uri)}</x-langString>
+        {tapi-item:make-language-elements($manifest-uri)}
+        <x-langString>{tapi-item:get-language-string($manifest-uri)}</x-langString>
         <image>
-            <id>{t-item:make-facsimile-id($manifest-uri, $page, $server)}</id>
+            <id>{tapi-item:make-facsimile-id($manifest-uri, $page, $server)}</id>
         </image>
         <annotationCollection>{$server}/api/textapi/ahikar/{$collection-uri}/{$manifest-uri}-{$page}/annotationCollection.json</annotationCollection>
     </object>
 };
 
 
-declare function t-item:make-title($manifest-uri)
+declare function tapi-item:make-title($manifest-uri)
 as xs:string {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     return
@@ -82,7 +82,7 @@ as xs:string {
 };
 
 
-declare function t-item:make-language-elements($manifest-uri as xs:string)
+declare function tapi-item:make-language-elements($manifest-uri as xs:string)
 as element()+ {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $languages := $tei-xml//tei:language
@@ -95,7 +95,7 @@ as element()+ {
 };
 
 
-declare function t-item:get-language-string($manifest-uri as xs:string)
+declare function tapi-item:get-language-string($manifest-uri as xs:string)
 as xs:string {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $langString :=
@@ -107,17 +107,17 @@ as xs:string {
 };
 
 
-declare function t-item:make-facsimile-id($manifest-uri as xs:string,
+declare function tapi-item:make-facsimile-id($manifest-uri as xs:string,
     $page as xs:string,
     $server as xs:string)
 as xs:string {
-    let $image-uri := t-item:get-facsimile-uri-for-page($manifest-uri, $page)
+    let $image-uri := tapi-item:get-facsimile-uri-for-page($manifest-uri, $page)
     return
         $server || "/api/images/" || $image-uri
 };
 
 
-declare function t-item:get-facsimile-uri-for-page($manifest-uri as xs:string,
+declare function tapi-item:get-facsimile-uri-for-page($manifest-uri as xs:string,
     $page as xs:string)
 as xs:string {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
diff --git a/exist-app/modules/tapi-manifest.xqm b/exist-app/modules/tapi-manifest.xqm
index b65e0373..f274050e 100644
--- a/exist-app/modules/tapi-manifest.xqm
+++ b/exist-app/modules/tapi-manifest.xqm
@@ -6,7 +6,7 @@ xquery version "3.1";
  : /textapi/ahikar/3r9ps/3rx15/manifest.json
  :)
 
-module namespace t-man="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest";
+module namespace tapi-mani="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest";
 
 declare namespace ore="http://www.openarchives.org/ore/terms/";
 declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
@@ -18,7 +18,7 @@ import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons"
 import module namespace requestr="http://exquery.org/ns/request";
 import module namespace rest="http://exquery.org/ns/restxq";
 
-declare variable $t-man:server := 
+declare variable $tapi-mani:server := 
     if(requestr:hostname() = "existdb") then
         $commons:expath-pkg/*/@name => replace("/$", "")
     else
@@ -33,40 +33,40 @@ declare
     %rest:HEAD
     %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}/manifest.json")
     %output:method("json")
-function t-man:endpoint($collection-uri as xs:string,
+function tapi-mani:endpoint($collection-uri as xs:string,
     $manifest-uri as xs:string)
 as item()+ {
     $commons:responseHeader200,
-    t-man:get-json($collection-uri, $manifest-uri, $t-man:server)
+    tapi-mani:get-json($collection-uri, $manifest-uri, $tapi-mani:server)
 };
 
 
-declare function t-man:get-json($collection-uri as xs:string,
+declare function tapi-mani:get-json($collection-uri as xs:string,
     $manifest-uri as xs:string,
     $server as xs:string)
 as element(object) {
     <object>
         <textapi>{$commons:version}</textapi>
         <id>{$server || "/api/textapi/ahikar/" || $collection-uri || "/" || $manifest-uri || "/manifest.json"}</id>
-        <label>{t-man:get-manifest-title($manifest-uri)}</label>
+        <label>{tapi-mani:get-manifest-title($manifest-uri)}</label>
         { 
-            t-man:make-editors($manifest-uri),
-            t-man:make-creation-date($manifest-uri),
-            t-man:make-origin($manifest-uri),
-            t-man:make-current-location($manifest-uri)
+            tapi-mani:make-editors($manifest-uri),
+            tapi-mani:make-creation-date($manifest-uri),
+            tapi-mani:make-origin($manifest-uri),
+            tapi-mani:make-current-location($manifest-uri)
         }
         <license>CC0-1.0</license>
         <annotationCollection>{$server}/api/textapi/ahikar/{$collection-uri}/{$manifest-uri}/annotationCollection.json</annotationCollection>
-        {t-man:make-sequences($collection-uri, $manifest-uri, $server)}
+        {tapi-mani:make-sequences($collection-uri, $manifest-uri, $server)}
     </object>
 };
 
 
-declare function t-man:make-sequences($collection-uri as xs:string,
+declare function tapi-mani:make-sequences($collection-uri as xs:string,
     $manifest-uri as xs:string,
     $server as xs:string)
 as element(sequence)+ {
-    let $valid-pages := t-man:get-valid-page-ids($manifest-uri)
+    let $valid-pages := tapi-mani:get-valid-page-ids($manifest-uri)
     return
         for $page in $valid-pages
         let $uri := "/api/textapi/ahikar/" || $collection-uri || "/" || $manifest-uri || "-" ||  $page || "/latest/item.json"
@@ -77,27 +77,27 @@ as element(sequence)+ {
             </sequence>
 };
 
-declare function t-man:get-valid-page-ids($manifest-uri as xs:string)
+declare function tapi-mani:get-valid-page-ids($manifest-uri as xs:string)
 as xs:string+ {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     return
         $tei-xml//tei:pb[@facs]/string(@n)
 };
 
-declare function t-man:get-manifest-title($manifest-uri as xs:string)
+declare function tapi-mani:get-manifest-title($manifest-uri as xs:string)
 as xs:string {
-    let $metadata-file := t-man:get-metadata-file($manifest-uri)
+    let $metadata-file := tapi-mani:get-metadata-file($manifest-uri)
     return
         $metadata-file//tgmd:title/string()
 };
 
-declare function t-man:get-metadata-file($manifest-uri as xs:string)
+declare function tapi-mani:get-metadata-file($manifest-uri as xs:string)
 as document-node() {
     doc($commons:tg-collection || "/meta/" || $manifest-uri || ".xml")
 };
 
 
-declare function t-man:make-editors($manifest-uri as xs:string)
+declare function tapi-mani:make-editors($manifest-uri as xs:string)
 as element(x-editor)+ {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $editors := $tei-xml//tei:titleStmt//tei:editor
@@ -116,7 +116,7 @@ as element(x-editor)+ {
 };
 
 
-declare function t-man:make-creation-date($manifest-uri as xs:string)
+declare function tapi-mani:make-creation-date($manifest-uri as xs:string)
 as element(x-date) {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $creation-date := $tei-xml//tei:history//tei:date
@@ -130,7 +130,7 @@ as element(x-date) {
 };
 
 
-declare function t-man:make-origin($manifest-uri as xs:string) as 
+declare function tapi-mani:make-origin($manifest-uri as xs:string) as 
 element(x-origin) {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $country := $tei-xml//tei:history//tei:country
@@ -149,7 +149,7 @@ element(x-origin) {
 };
 
 
-declare function t-man:make-current-location($manifest-uri as xs:string) as
+declare function tapi-mani:make-current-location($manifest-uri as xs:string) as
 element(x-location) {
     let $tei-xml := commons:get-tei-xml-for-manifest($manifest-uri)
     let $institution := $tei-xml//tei:msIdentifier//tei:institution
diff --git a/exist-app/tests/tapi-collection-tests.xqm b/exist-app/tests/tapi-collection-tests.xqm
index b34557fe..9023e813 100644
--- a/exist-app/tests/tapi-collection-tests.xqm
+++ b/exist-app/tests/tapi-collection-tests.xqm
@@ -9,10 +9,10 @@ import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons"
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
 import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
-import module namespace t-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection" at "../modules/tapi-collection.xqm";
+import module namespace tapi-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection" at "../modules/tapi-collection.xqm";
 
 declare variable $tct:restxq := "http://0.0.0.0:8080/exist/restxq";
-declare variable $tct:collection-uri := "test-collection.xml";
+declare variable $tct:collection-uri := "testapi-collection.xml";
 declare variable $tct:agg1-uri := "test-aggregation-1.xml";
 declare variable $tct:agg2-uri := "test-aggregation-2.xml";
 
@@ -22,7 +22,7 @@ function tct:_test-setup(){
     let $collection :=
         <rdf:RDF xmlns:ore="http://www.openarchives.org/ore/terms/" 
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
-            <rdf:Description rdf:about="textgrid:test-collection">
+            <rdf:Description rdf:about="textgrid:testapi-collection">
                 <ore:aggregates rdf:resource="textgrid:test-aggregation-1"/>
                 <ore:aggregates rdf:resource="textgrid:test-aggregation-2"/>
             </rdf:Description>
@@ -114,7 +114,7 @@ function tct:is-endpoint-available() {
 declare
     %test:args("ahiqar_collection") %test:assertXPath("$result//*[local-name(.) = 'aggregates']")
 function tct:get-aggregation($uri as xs:string) {
-    t-coll:get-aggregation($uri)
+    tapi-coll:get-aggregation($uri)
 };
 
 declare
@@ -130,7 +130,7 @@ function tct:get-allowed-manifest-uris-mock-up-input-included() {
             </rdf:Description>
         </rdf:RDF>
     return
-        t-coll:get-allowed-manifest-uris($collection-metadata) = "3rx14"
+        tapi-coll:get-allowed-manifest-uris($collection-metadata) = "3rx14"
 };
 
 declare
@@ -146,7 +146,7 @@ function tct:get-allowed-manifest-uris-mock-up-input-excluded() {
             </rdf:Description>
         </rdf:RDF>
     return
-        t-coll:get-allowed-manifest-uris($collection-metadata) = "3vp38"
+        tapi-coll:get-allowed-manifest-uris($collection-metadata) = "3vp38"
 };
 
 
@@ -154,22 +154,22 @@ declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json")
 function tct:make-id($colletion-uri as xs:string, $manifest-uri as xs:string)
 as xs:string {
-    t-coll:make-id($tct:restxq, $colletion-uri, $manifest-uri)
+    tapi-coll:make-id($tct:restxq, $colletion-uri, $manifest-uri)
 };
 
 
 declare
     %test:assertEquals("ahiqar_agg")
 function tct:get-allowed-manifest-uris-sample-input() {
-    let $collection-metadata := t-coll:get-aggregation("ahiqar_collection")
+    let $collection-metadata := tapi-coll:get-aggregation("ahiqar_collection")
     return
-        t-coll:get-allowed-manifest-uris($collection-metadata)
+        tapi-coll:get-allowed-manifest-uris($collection-metadata)
 };
 
 declare
     %test:args("ahiqar_collection") %test:assertXPath("$result[self::document-node()]")
 function tct:get-metadata-file($uri as xs:string) {
-    t-coll:get-metadata-file($uri)
+    tapi-coll:get-metadata-file($uri)
 };
 
 
@@ -177,7 +177,7 @@ declare
     %test:args("textgrid:1234") %test:assertEquals("1234")
     %test:args("1234") %test:assertEquals("1234")
 function tct:remove-textgrid-prefix($uri as xs:string) {
-    t-coll:remove-textgrid-prefix($uri)
+    tapi-coll:remove-textgrid-prefix($uri)
 };
 
 declare
@@ -185,32 +185,32 @@ declare
     %test:args("text/tg.edition+tg.aggregation+xml") %test:assertEquals("manifest")
     %test:args("test") %test:assertEquals("manifest")
 function tct:make-format-type($tgmd-format as xs:string) {
-    t-coll:make-format-type($tgmd-format)
+    tapi-coll:make-format-type($tgmd-format)
 };
 
 declare
     %test:assertEquals("manifest")
 function tct:get-format-type() {
-    let $metadata := t-coll:get-metadata-file("ahiqar_agg")
+    let $metadata := tapi-coll:get-metadata-file("ahiqar_agg")
     return
-        t-coll:get-format-type($metadata)
+        tapi-coll:get-format-type($metadata)
 };
 
 
 declare
     %test:args("ahiqar_collection") %test:assertXPath("$result//type[. = 'manifest']")
     %test:args("ahiqar_collection") %test:assertXPath("$result//id[matches(., 'ahiqar_agg/manifest.json')]")
-    %test:args("test-collection") %test:assertXPath("$result//id[matches(., 'test-aggregation-1/manifest.json')]")
-    %test:args("test-collection") %test:assertXPath("$result//id[matches(., 'test-aggregation-2/manifest.json')]")
+    %test:args("testapi-collection") %test:assertXPath("$result//id[matches(., 'test-aggregation-1/manifest.json')]")
+    %test:args("testapi-collection") %test:assertXPath("$result//id[matches(., 'test-aggregation-2/manifest.json')]")
 function tct:make-sequence($collection-uri as xs:string) {
-    t-coll:make-sequence($collection-uri, $tct:restxq)
+    tapi-coll:make-sequence($collection-uri, $tct:restxq)
 };
 
 declare
     %test:args("ahiqar_collection") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/annotationCollection.json")
 function tct:make-annotationCollection-uri($collection-uri as xs:string)
 as xs:string {
-    t-coll:make-annotationCollection-uri($tct:restxq, $collection-uri)
+    tapi-coll:make-annotationCollection-uri($tct:restxq, $collection-uri)
 };
 
 
@@ -218,7 +218,7 @@ declare
     %test:args("ahiqar_collection") %test:assertXPath("$result//title = 'The Story and Proverbs of Ahikar the Wise'")
     %test:args("ahiqar_collection") %test:assertXPath("$result//*/string() = 'http://0.0.0.0:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json' ")
 function tct:get-json($collection-uri as xs:string) {
-    t-coll:get-json($collection-uri, $tct:restxq)
+    tapi-coll:get-json($collection-uri, $tct:restxq)
 };
 
 
diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
index 821a5814..bf4ffb3c 100644
--- a/exist-app/tests/tapi-item-tests.xqm
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -1,6 +1,6 @@
 xquery version "3.1";
 
-module namespace tit="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests";
+module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests";
 
 declare namespace http = "http://expath.org/ns/http-client";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
@@ -9,12 +9,12 @@ import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons"
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
 import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
-import module namespace t-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item" at "../modules/tapi-item.xqm";
+import module namespace tapi-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item" at "../modules/tapi-item.xqm";
 
 
 declare
     %test:assertTrue
-function tit:is-endpoint-200() {
+function titemt:is-endpoint-200() {
     let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
     return
         tc:is-endpoint-http200($url)
@@ -22,33 +22,33 @@ function tit:is-endpoint-200() {
 
 declare
     %test:args("ahiqar_agg", "82a") %test:assertEquals("3r1nz")
-function tit:get-facsimile-uri-for-page($manifest-uri as xs:string,
+function titemt:get-facsimile-uri-for-page($manifest-uri as xs:string,
     $page as xs:string)
 as xs:string {
-    t-item:get-facsimile-uri-for-page($manifest-uri, $page)
+    tapi-item:get-facsimile-uri-for-page($manifest-uri, $page)
 };
 
 declare
     %test:args("ahiqar_agg") %test:assertEquals("Arabic, Classical Syriac, Eastern Syriac, Karshuni, Western Syriac")
-function tit:get-language-string($manifest-uri as xs:string)
+function titemt:get-language-string($manifest-uri as xs:string)
 as xs:string {
-    t-item:get-language-string($manifest-uri)
+    tapi-item:get-language-string($manifest-uri)
 };
 
 declare
     %test:args("ahiqar_agg", "82a") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/images/3r1nz")
-function tit:make-facsimile-id($manifest-uri as xs:string,
+function titemt:make-facsimile-id($manifest-uri as xs:string,
     $page as xs:string)
 as xs:string {
-    t-item:make-facsimile-id($manifest-uri, $page, $tc:server)
+    tapi-item:make-facsimile-id($manifest-uri, $page, $tc:server)
 };
 
 declare
     %test:args("ahiqar_agg") %test:assertEquals("The Proverbs or History of Aḥīḳar the wise, the scribe of Sanḥērībh,
                king of Assyria and Nineveh")
-function tit:make-title($manifest-uri as xs:string)
+function titemt:make-title($manifest-uri as xs:string)
 as xs:string {
-    t-item:make-title($manifest-uri)
+    tapi-item:make-title($manifest-uri)
 };
 
 
@@ -65,7 +65,7 @@ declare
     %test:assertXPath("map:contains($result, 'lang')")
     %test:assertXPath("map:contains($result, 'langAlt')")
     %test:assertXPath("map:contains($result, 'image')")
-function tit:is-data-complete() as item() {
+function titemt:is-data-complete() as item() {
     let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
     let $req := tc:make-request($url)
     return http:send-request($req)[2]
@@ -86,11 +86,11 @@ declare
     %test:assertXPath("$result//*[local-name(.) = 'content'] = 'http://0.0.0.0:8080/exist/restxq/api/content/ahiqar_sample-82a.html' ")
     (: checks if images connected to underlying pages are identified :)
     %test:assertXPath("$result//*[local-name(.) = 'id'] = 'http://0.0.0.0:8080/exist/restxq/api/images/3r1nz' ")
-function tit:get-json($collection as xs:string,
+function titemt:get-json($collection as xs:string,
     $document as xs:string,
     $page as xs:string) 
 as element(object){
-    t-item:get-json($collection, $document, $page, $tc:server)
+    tapi-item:get-json($collection, $document, $page, $tc:server)
 };
 
 
@@ -98,6 +98,6 @@ declare
     %test:args("ahiqar_agg") %test:assertXPath("count($result) = 5")
     %test:args("ahiqar_agg") %test:assertXPath("$result[local-name(.) = ('lang', 'langAlt')]")
     %test:args("ahiqar_agg") %test:assertXPath("count($result[local-name(.) = 'lang']) = 2")
-function tit:make-language-elements($manifest-uri as xs:string) {
-    t-item:make-language-elements($manifest-uri)
+function titemt:make-language-elements($manifest-uri as xs:string) {
+    tapi-item:make-language-elements($manifest-uri)
 };
\ No newline at end of file
diff --git a/exist-app/tests/tapi-manifest-tests.xqm b/exist-app/tests/tapi-manifest-tests.xqm
index 75c0d3cf..6891585f 100644
--- a/exist-app/tests/tapi-manifest-tests.xqm
+++ b/exist-app/tests/tapi-manifest-tests.xqm
@@ -9,7 +9,7 @@ import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons"
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
 import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
-import module namespace t-man="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest" at "../modules/tapi-manifest.xqm";
+import module namespace tapi-mani="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest" at "../modules/tapi-manifest.xqm";
 
 declare variable $tmt:restxq := "http://0.0.0.0:8080/exist/restxq";
 declare variable $tmt:manifest1 := "test-manifest1.xml";
@@ -141,7 +141,7 @@ function tmt:is-endpoint-200() {
 declare
     %test:args("ahiqar_agg") %test:assertXPath("$result//* = 'textgrid:ahiqar_agg.0'")
 function tmt:get-metadata-file($manifest-uri) {
-    t-man:get-metadata-file($manifest-uri)
+    tapi-mani:get-metadata-file($manifest-uri)
 };
 
 
@@ -149,13 +149,13 @@ declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json')]")
 function tmt:make-sequences($collection-uri as xs:string,
     $manifest-uri as xs:string) {
-    t-man:make-sequences($collection-uri, $manifest-uri, $tmt:restxq)
+    tapi-mani:make-sequences($collection-uri, $manifest-uri, $tmt:restxq)
 };
 
 declare
      %test:args("ahiqar_agg") %test:assertXPath("count($result) = 4")
 function tmt:get-valid-page-ids($manifest-uri as xs:string) {
-    t-man:get-valid-page-ids($manifest-uri)
+    tapi-mani:get-valid-page-ids($manifest-uri)
 };
 
 declare
@@ -163,13 +163,13 @@ declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json')]")
 function tmt:get-json($collection-uri as xs:string,
     $manifest-uri as xs:string) {
-    t-man:get-json($collection-uri, $manifest-uri, $tmt:restxq)
+    tapi-mani:get-json($collection-uri, $manifest-uri, $tmt:restxq)
 };
 
 declare
     %test:args("ahiqar_agg") %test:assertEquals("Beispieldatei zum Testen")
 function tmt:get-manifest-title($manifest-uri as xs:string) {
-    t-man:get-manifest-title($manifest-uri)
+    tapi-mani:get-manifest-title($manifest-uri)
 };
 
 declare
@@ -178,14 +178,14 @@ declare
     %test:args("test-manifest1") %test:assertXPath("count($result) = 1")
     %test:args("test-manifest1") %test:assertXPath("$result//name = 'none'")
 function tmt:make-editors($manifest-uri as xs:string) {
-    t-man:make-editors($manifest-uri)
+    tapi-mani:make-editors($manifest-uri)
 };
 
 declare
     %test:args("ahiqar_agg") %test:assertXPath("$result/string() = '18.10.1697'")
     %test:args("test-manifest1") %test:assertXPath("$result/string() = 'unknown'")
 function tmt:make-creation-date($manifest-uri as xs:string) {
-    t-man:make-creation-date($manifest-uri)
+    tapi-mani:make-creation-date($manifest-uri)
 };
 
 declare
@@ -194,7 +194,7 @@ declare
     %test:args("test-manifest2") %test:assertXPath("$result/string() = 'Iraq'")
     %test:args("test-manifest3") %test:assertXPath("$result/string() = 'Alqosh'")
 function tmt:make-origin($manifest-uri as xs:string) {
-    t-man:make-origin($manifest-uri)
+    tapi-mani:make-origin($manifest-uri)
 };
 
 declare
@@ -203,14 +203,14 @@ declare
     %test:args("test-manifest2") %test:assertXPath("$result/string() = 'University of Cambridge - Cambridge University Library'")
     %test:args("test-manifest3") %test:assertXPath("$result/string() = 'Great Britain'")
 function tmt:make-current-location($manifest-uri as xs:string) {
-    t-man:make-current-location($manifest-uri)
+    tapi-mani:make-current-location($manifest-uri)
 };
 
 declare
      %test:args("ahiqar_collection", "ahiqar_agg") %test:assertExists
 function tmt:get-json($collection-uri as xs:string,
     $manifest-uri as xs:string) {
-    t-man:get-json($collection-uri, $manifest-uri, $tmt:restxq)
+    tapi-mani:get-json($collection-uri, $manifest-uri, $tmt:restxq)
 };
 
 
-- 
GitLab


From fe5efbe2c7534acf737623c87cd7cb3aba72a938 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 21 Sep 2020 17:08:21 +0200
Subject: [PATCH 07/22] refactor: move functions that create HTML in separate
 module

---
 exist-app/modules/tapi-html.xqm     | 97 +++++++++++++++++++++++++++++
 exist-app/modules/tapi.xqm          | 78 +++--------------------
 exist-app/tests-runner.xq           |  6 +-
 exist-app/tests.xqm                 | 72 ++++++---------------
 exist-app/tests/tapi-html-tests.xqm | 74 ++++++++++++++++++++++
 5 files changed, 202 insertions(+), 125 deletions(-)
 create mode 100644 exist-app/modules/tapi-html.xqm
 create mode 100644 exist-app/tests/tapi-html-tests.xqm

diff --git a/exist-app/modules/tapi-html.xqm b/exist-app/modules/tapi-html.xqm
new file mode 100644
index 00000000..6c896991
--- /dev/null
+++ b/exist-app/modules/tapi-html.xqm
@@ -0,0 +1,97 @@
+xquery version "3.1";
+
+module namespace tapi-html="http://ahikar.sub.uni-goettingen.de/ns/tapi/html";
+
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+declare namespace xhtml="http://www.w3.org/1999/xhtml";
+
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
+import module namespace fragment="https://wiki.tei-c.org/index.php?title=Milestone-chunk.xquery" at "fragment.xqm";
+
+(:~
+ : Initiates the HTML serialization of a given page.
+ :
+ : @param $manifest-uri The unprefixed TextGrid URI of a document, e.g. '3rbmb'
+ : @param $page The page to be rendered. This has to be the string value of a tei:pb/@n in the given document, e.g. '1a'
+ : @return A div wrapper containing the rendered page
+ :)
+declare function tapi-html:get-html($tei-xml-uri as xs:string,
+    $page as xs:string)
+as element(div) {
+    let $tei-xml-base-uri := $commons:data || $tei-xml-uri || ".xml"
+    let $fragment :=
+        if ($page) then
+            tapi-html:get-page-fragment($tei-xml-base-uri, $page)
+        else
+            doc($tei-xml-base-uri)/*
+    return
+        tapi-html:get-html-from-fragment($fragment)
+};
+
+
+declare function tapi-html:get-page-fragment($tei-xml-base-uri as xs:string,
+    $page as xs:string)
+as element() {
+    let $node := doc($tei-xml-base-uri)/*
+        => tapi-html:add-IDs(),
+        $start-node := $node//tei:pb[@n = $page and @facs],
+        $end-node := tapi-html:get-end-node($start-node),
+        $wrap-in-first-common-ancestor-only := false(),
+        $include-start-and-end-nodes := false(),
+        $empty-ancestor-elements-to-include := ("")
+    return
+        fragment:get-fragment-from-doc(
+            $node,
+            $start-node,
+            $end-node,
+            $wrap-in-first-common-ancestor-only,
+            $include-start-and-end-nodes,
+            $empty-ancestor-elements-to-include)
+};
+
+
+declare function tapi-html:add-IDs($nodes as node()*)
+as node()* {
+    for $node in $nodes return
+        typeswitch ($node)
+        
+        case text() return
+            $node
+            
+        case comment() return
+            ()
+            
+        case processing-instruction() return
+            $node
+            
+        default return
+            element {QName("http://www.tei-c.org/ns/1.0", local-name($node))} {
+                attribute id {generate-id($node)},
+                $node/@*,
+                tapi-html:add-IDs($node/node())
+            }
+};
+
+
+declare function tapi-html:get-end-node($start-node as element(tei:pb))
+as element() {
+    let $following-pb := $start-node/following::tei:pb[1][@facs]
+    return
+        if($following-pb) then
+            $following-pb
+        else
+            $start-node/following::tei:ab[last()]
+};
+
+
+declare function tapi-html:get-html-from-fragment($fragment as element())
+as element(xhtml:div) {
+    let $stylesheet := doc("/db/apps/sade_assets/TEI-Stylesheets/html5/html5.xsl")
+    return
+        (: this wrapping is necessary in order to correctly set the namespace.
+        otherwise, error XQST0070 is raised during the tests. :)
+        element xhtml:div {
+            attribute class {"tei_body"},
+            transform:transform($fragment, $stylesheet, ())/xhtml:body//xhtml:div[@class = "tei_body"]/*
+        }
+};
diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index 8d640be7..a4c3a372 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -28,8 +28,13 @@ import module namespace fragment="https://wiki.tei-c.org/index.php?title=Milesto
 import module namespace functx="http://www.functx.com";
 import module namespace requestr="http://exquery.org/ns/request";
 import module namespace rest="http://exquery.org/ns/restxq";
+import module namespace tapi-html="http://ahikar.sub.uni-goettingen.de/ns/tapi/html" at "tapi-html.xqm";
 
-declare variable $tapi:server := if(requestr:hostname() = "existdb") then $commons:expath-pkg/*/@name => replace("/$", "") else "http://localhost:8094/exist/restxq";
+declare variable $tapi:server :=
+    if(requestr:hostname() = "existdb") then
+        $commons:expath-pkg/*/@name => replace("/$", "")
+    else
+        "http://localhost:8094/exist/restxq";
 
 (:~
  : Shows information about the currently installed application.
@@ -103,49 +108,7 @@ declare
 function tapi:content-rest($document as xs:string, $page as xs:string)
 as item()+ {
     $commons:responseHeader200,
-    tapi:content($document, $page)
-};
-
-
-(:~
- : Initiates the HTML serialization of a given page.
- :
- : @param $document The unprefixed TextGrid URI of a document, e.g. '3rbmb'
- : @param $page The page to be rendered. This has to be the string value of a tei:pb/@n in the given document, e.g. '1a'
- : @return A div wrapper containing the rendered page
- :)
-declare function tapi:content($document as xs:string, $page as xs:string)
-as element(div) {
-    let $documentPath := $commons:data || $document || ".xml"
-    let $TEI :=
-        if($page)
-        then
-            let $node := doc($documentPath)/* => tapi:add-IDs(),
-                $start-node := $node//tei:pb[@n = $page and @facs],
-                $end-node :=
-                    let $followingPb := $node//tei:pb[@n = $page and @facs]/following::tei:pb[1][@facs]
-                    return
-                        if($followingPb)
-                        then $followingPb
-                        else $node//tei:pb[@n = $page and @facs]/following::tei:ab[last()],
-                $wrap-in-first-common-ancestor-only := false(),
-                $include-start-and-end-nodes := false(),
-                $empty-ancestor-elements-to-include := ("")
-            return
-                fragment:get-fragment-from-doc(
-                    $node,
-                    $start-node,
-                    $end-node,
-                    $wrap-in-first-common-ancestor-only,
-                    $include-start-and-end-nodes,
-                    $empty-ancestor-elements-to-include)
-        else doc($documentPath)/*
-    let $stylesheet := doc("/db/apps/sade_assets/TEI-Stylesheets/html5/html5.xsl")
-    let $transform := transform:transform($TEI, $stylesheet, ())/xhtml:body//xhtml:div[@class = "tei_body"]
-    return
-        <div>
-            {$transform}
-        </div>
+    tapi-html:get-html($document, $page)
 };
 
 
@@ -297,30 +260,3 @@ as element(tei:text) {
 declare function tapi:get-format($uri as xs:string) as xs:string {
     doc($commons:meta || $uri || ".xml")//tgmd:format
 };
-
-
-declare function tapi:add-IDs($tei as element(tei:TEI)) as element(tei:TEI) {
-    tapi:add-IDs-recursion($tei)
-};
-
-declare function tapi:add-IDs-recursion($nodes as node()*) as node()* {
-    util:log-system-out($nodes),
-    for $node in $nodes return
-        typeswitch ($node)
-        
-        case text() return
-            $node
-            
-        case comment() return
-            ()
-            
-        case processing-instruction() return
-            $node
-            
-        default return
-            element {QName("http://www.tei-c.org/ns/1.0", local-name($node))} {
-                attribute id {generate-id($node)},
-                $node/@*,
-                tapi:add-IDs-recursion($node/node())
-            }
-};
diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 209f2eeb..992a2973 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -7,7 +7,8 @@ xquery version "3.1";
 import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tests/tapi-collection-tests.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace tests="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests.xqm";
-import module namespace tit="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tests/tapi-item-tests.xqm";
+import module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests" at "tests/tapi-html-tests.xqm";
+import module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tests/tapi-item-tests.xqm";
 import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tests/tapi-manifest-tests.xqm";
 import module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests" at "tests/collate-tests.xqm";
 import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
@@ -19,7 +20,8 @@ let $test-results :=
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests")),
-        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests"))
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests")),
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests"))
     )
 
 for $result in $test-results return
diff --git a/exist-app/tests.xqm b/exist-app/tests.xqm
index c9cab391..b5c52391 100644
--- a/exist-app/tests.xqm
+++ b/exist-app/tests.xqm
@@ -15,11 +15,9 @@ declare namespace tei="http://www.tei-c.org/ns/1.0";
 import module namespace anno="http://ahikar.sub.uni-goettingen.de/ns/annotations" at "modules/annotations.xqm";
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
 import module namespace tapi="http://ahikar.sub.uni-goettingen.de/ns/tapi" at "modules/tapi.xqm";
+import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "tests/test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 
-declare variable $tests:restxq := "http://0.0.0.0:8080/exist/restxq/";
-
-
 declare
     %test:setUp
 function tests:_test-setup() as xs:string+ {
@@ -78,24 +76,29 @@ declare
     (: check if repo.xml works :)
     %test:assertXPath("map:get($result, 'meta') => map:get('target') = 'ahikar'")
 function tests:api-info()  as item() {
-    let $url := $tests:restxq || "info"
-    let $req := <http:request href="{$url}" method="get">
-                        <http:header name="Connection" value="close"/>
-                   </http:request>
+    let $url := $tc:server || "/info"
+    let $req := tc:make-request($url)
     return http:send-request($req)[2] => util:base64-decode() => parse-json()
 };
 
 
+declare
+    %test:assertTrue
+function tests:is-html-api-available()
+as xs:boolean {
+    let $url := $tc:server || "/content/ahiqar_sample-82a.html"
+    return
+        tc:is-endpoint-http200($url)
+};
+
 declare
     (: check if tei:div is present.
      : no further tests are needed since the content has been tested while testing
      : the underlying function. :)
     %test:assertXPath("$result//*[@class = 'tei_body']")
 function tests:content-rest() as document-node() {
-    let $url := $tests:restxq || "/content/ahiqar_sample-82a.html"
-    let $req := <http:request href="{$url}" method="get">
-                        <http:header name="Connection" value="close"/>
-                   </http:request>
+    let $url := $tc:server || "/content/ahiqar_sample-82a.html"
+    let $req := tc:make-request($url)
     return http:send-request($req)[2]
 };
 
@@ -107,7 +110,7 @@ declare
     %test:assertExists
     %test:pending
 function tests:content-zip() as xs:base64Binary {
-    let $url := $tests:restxq || "/content/ahikar-plain-text.zip"
+    let $url := $tc:server || "/content/ahikar-plain-text.zip"
     let $req := <http:request href="{$url}" method="get">
                         <http:header name="Connection" value="close"/>
                    </http:request>
@@ -121,7 +124,7 @@ declare
      : the underlying function. :)
     %test:assertXPath("matches($result, '[\w]')")
 function tests:content-txt() as xs:string {
-    let $url := $tests:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_sample.txt"
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_sample.txt"
     let $req := <http:request href="{$url}" method="get">
                         <http:header name="Connection" value="close"/>
                    </http:request>
@@ -129,21 +132,6 @@ function tests:content-txt() as xs:string {
 };
 
 
-declare
-    %test:args("ahiqar_sample", "82a")
-    (: checks if there is text at all in the result :)
-    %test:assertXPath("$result//text()[matches(., '[\w]')]")
-    (: if a div[@class = 'tei_body'] is present, the transformation has been successfull :)
-    %test:assertXPath("$result//*[@class = 'tei_body']")
-    (: this is some text on 82a (and thus should be part of the result) :)
-    %test:assertXPath("$result//* = 'ܘܬܥܐܠܝ ܕܟܪܗ ܐܠܝ ܐܠܐܒܕ. ܘܢܟܬܒ ܟܒܪ'") 
-    (: this is some text on 83a which shouldn't be part of the result :)
-    %test:assertXPath("not($result//* = 'ܡܢ ܐܠܣܡܐ ܩܐܝܠܐ. ܒܚܝܬ ܐܬܟܠܬ ܐܘܠܐ ܥܠܝ ܐܠܐܨܢܐܡ' )")
-function tests:html-creation($document as xs:string, $page as xs:string) as element(div) {
-    tapi:content($document, $page)
-};
-
-
 declare
     %test:assertExists
 function tests:compress-text() as xs:base64Binary {
@@ -202,16 +190,14 @@ function tests:get-text-of-type($uri as xs:string, $type as xs:string) {
 declare
     %test:assertTrue
 function tests:is-txt-api-available() {
-    let $url := $tests:restxq || "content/ahiqar_sample.txt"
+    let $url := $tc:server || "/content/ahiqar_sample.txt"
     return
-        local:is-endpoint-http200($url)
+        tc:is-endpoint-http200($url)
 };
 
 declare function tests:txt() {
-    let $url := $tests:restxq || "textapi/ahiqar/ahiqar_collection/ahiqar_sample.txt"
-    let $req := <http:request href="{$url}" method="get">
-                        <http:header name="Connection" value="close"/>
-                   </http:request>
+    let $url := $tc:server || "textapi/ahiqar/ahiqar_collection/ahiqar_sample.txt"
+    let $req := tc:make-request($url)
     return http:send-request($req)[2] => util:base64-decode()
 };
 
@@ -343,21 +329,3 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:function tests:anno-get-uris($documentURI) {:)
 (:    anno:get-uris($documentURI):)
 (:};:)
-
-declare function local:is-endpoint-http200($url as xs:string) as xs:boolean {
-    let $http-status := local:get-http-status($url)
-    return
-        $http-status = "200"
-};
-
-declare function local:get-http-status($url as xs:string) as xs:string {
-    let $req := local:make-request($url)
-    return
-        http:send-request($req)[1]/@status
-};
-
-declare function local:make-request($url as xs:string) {
-    <http:request href="{$url}" method="get">
-        <http:header name="Connection" value="close"/>
-   </http:request>
-};
\ No newline at end of file
diff --git a/exist-app/tests/tapi-html-tests.xqm b/exist-app/tests/tapi-html-tests.xqm
new file mode 100644
index 00000000..c54e4c10
--- /dev/null
+++ b/exist-app/tests/tapi-html-tests.xqm
@@ -0,0 +1,74 @@
+xquery version "3.1";
+
+module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests";
+
+declare namespace tei="http://www.tei-c.org/ns/1.0";
+declare namespace xhtml="http://www.w3.org/1999/xhtml";
+
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "../modules/commons.xqm";
+import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
+import module namespace tapi-html="http://ahikar.sub.uni-goettingen.de/ns/tapi/html" at "../modules/tapi-html.xqm";
+
+
+declare
+    %test:assertXPath("$result//@id = 'N4'")
+function thtmlt:add-IDs()
+as node()+ {
+    let $manifest := doc($commons:data || "ahiqar_sample.xml")/*
+    return
+        tapi-html:add-IDs($manifest)
+};
+
+
+declare
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "82a") %test:assertXPath("$result[local-name(.) = 'pb']")
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "82a") %test:assertXPath("$result/@facs = 'textgrid:3r1p0'")
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "82a") %test:assertXPath("$result/@n = '82b'")
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "83b") %test:assertXPath("$result[local-name(.) = 'ab']")
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "83b") %test:assertXPath("matches($result, 'ܘܗܦܟܬ ܛܥܢܬ ܐܰܒܵܪܐ ܘܠܐ ܐܝܼܩܰܪ ܥܠ')")
+function thtmlt:get-end-node($tei-xml-base-uri as xs:string,
+    $page as xs:string)
+as item()+ {
+    let $node := doc($tei-xml-base-uri)/*
+    let $start-node := $node//tei:pb[@n = $page and @facs]
+    return
+        tapi-html:get-end-node($start-node)
+};
+
+
+declare
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "82a") %test:assertXPath("$result//*[local-name(.) = 'add'][@place = 'margin'] = 'حقًا'")
+function thtmlt:get-page-fragment($tei-xml-base-uri as xs:string,
+    $page as xs:string)
+as element() {
+    tapi-html:get-page-fragment($tei-xml-base-uri, $page)
+};
+    
+                        
+declare
+    %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml", "82a") %test:assertXPath("$result//text()[matches(., 'حقًا')]")
+function thtmlt:transform-fragment($tei-xml-base-uri as xs:string,
+    $page as xs:string)
+as element(xhtml:div) {
+    let $fragment := tapi-html:get-page-fragment($tei-xml-base-uri, $page)
+    return
+        tapi-html:get-html-from-fragment($fragment)
+};
+
+
+declare
+    %test:args("ahiqar_sample", "82a") %test:assertXPath("$result//text()[matches(., 'حقًا')]")
+    %test:args("ahiqar_sample", "82a")
+    (: checks if there is text at all in the result :)
+    %test:assertXPath("$result//text()[matches(., '[\w]')]")
+    (: if a div[@class = 'tei_body'] is present, the transformation has been successfull :)
+    %test:assertXPath("$result[@class = 'tei_body']")
+    (: this is some text on 82a (and thus should be part of the result) :)
+    %test:assertXPath("$result//* = 'ܘܬܥܐܠܝ ܕܟܪܗ ܐܠܝ ܐܠܐܒܕ. ܘܢܟܬܒ ܟܒܪ'") 
+    (: this is some text on 83a which shouldn't be part of the result :)
+    %test:assertXPath("not($result//* = 'ܡܢ ܐܠܣܡܐ ܩܐܝܠܐ. ܒܚܝܬ ܐܬܟܠܬ ܐܘܠܐ ܥܠܝ ܐܠܐܨܢܐܡ' )")
+function thtmlt:get-html($tei-xml-uri as xs:string,
+    $page as xs:string)
+as element(div) {
+    tapi-html:get-html($tei-xml-uri, $page)
+};
-- 
GitLab


From b1c079bff5af5b5c5b77c75785dc8cc02a47712f Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 07:46:33 +0200
Subject: [PATCH 08/22] refactor: move unit tests, choose better namespace,
 minor fixes

---
 exist-app/tests-runner.xq                     |  7 +-
 exist-app/tests/collate-tests.xqm             |  2 +-
 exist-app/{tests.xqm => tests/tapi-tests.xqm} | 72 +++++++++----------
 3 files changed, 41 insertions(+), 40 deletions(-)
 rename exist-app/{tests.xqm => tests/tapi-tests.xqm} (79%)

diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 992a2973..14299a3e 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -4,14 +4,15 @@ xquery version "3.1";
  : execution.
  :)
 
+import module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests" at "tests/collate-tests.xqm";
+import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
 import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tests/tapi-collection-tests.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
-import module namespace tests="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests.xqm";
 import module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests" at "tests/tapi-html-tests.xqm";
 import module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tests/tapi-item-tests.xqm";
 import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tests/tapi-manifest-tests.xqm";
-import module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests" at "tests/collate-tests.xqm";
-import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
+import module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests.xqm";
+
 
 let $test-results :=
     (
diff --git a/exist-app/tests/collate-tests.xqm b/exist-app/tests/collate-tests.xqm
index 77597760..b29b3165 100644
--- a/exist-app/tests/collate-tests.xqm
+++ b/exist-app/tests/collate-tests.xqm
@@ -4,7 +4,7 @@ module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests";
 
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 
-import module namespace coll="http://ahikar.sub.uni-goettingen.de/ns/collate" at "modules/collate.xqm";
+import module namespace coll="http://ahikar.sub.uni-goettingen.de/ns/collate" at "../modules/collate.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 
 declare variable $coll-tests:sample-file := local:open-file("ahiqar_sample");
diff --git a/exist-app/tests.xqm b/exist-app/tests/tapi-tests.xqm
similarity index 79%
rename from exist-app/tests.xqm
rename to exist-app/tests/tapi-tests.xqm
index b5c52391..5afd9713 100644
--- a/exist-app/tests.xqm
+++ b/exist-app/tests/tapi-tests.xqm
@@ -7,20 +7,20 @@ xquery version "3.1";
  : @version 0.1.0
  :)
 
-module namespace tests="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests";
+module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests";
 
 declare namespace http = "http://expath.org/ns/http-client";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 
-import module namespace anno="http://ahikar.sub.uni-goettingen.de/ns/annotations" at "modules/annotations.xqm";
+import module namespace anno="http://ahikar.sub.uni-goettingen.de/ns/annotations" at "../modules/annotations.xqm";
 import module namespace map="http://www.w3.org/2005/xpath-functions/map";
-import module namespace tapi="http://ahikar.sub.uni-goettingen.de/ns/tapi" at "modules/tapi.xqm";
-import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "tests/test-commons.xqm";
+import module namespace tapi="http://ahikar.sub.uni-goettingen.de/ns/tapi" at "../modules/tapi.xqm";
+import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons" at "test-commons.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 
 declare
     %test:setUp
-function tests:_test-setup() as xs:string+ {
+function tt:_test-setup() as xs:string+ {
     xmldb:create-collection("/db", "test-records"),
     xmldb:store("/db/test-records", "white-spaces.xml", <record><id>12     34 56
     78</id></record>),
@@ -64,7 +64,7 @@ function tests:_test-setup() as xs:string+ {
 
 declare
     %test:tearDown
-function tests:_test-teardown() as item() {
+function tt:_test-teardown() as item() {
     xmldb:remove("/db/test-records")
 };
 
@@ -75,7 +75,7 @@ declare
     %test:assertXPath("map:get($result, 'package') => map:get('title') = 'TextAPI for Ahikar'")
     (: check if repo.xml works :)
     %test:assertXPath("map:get($result, 'meta') => map:get('target') = 'ahikar'")
-function tests:api-info()  as item() {
+function tt:api-info()  as item() {
     let $url := $tc:server || "/info"
     let $req := tc:make-request($url)
     return http:send-request($req)[2] => util:base64-decode() => parse-json()
@@ -84,7 +84,7 @@ function tests:api-info()  as item() {
 
 declare
     %test:assertTrue
-function tests:is-html-api-available()
+function tt:is-html-api-available()
 as xs:boolean {
     let $url := $tc:server || "/content/ahiqar_sample-82a.html"
     return
@@ -96,7 +96,7 @@ declare
      : no further tests are needed since the content has been tested while testing
      : the underlying function. :)
     %test:assertXPath("$result//*[@class = 'tei_body']")
-function tests:content-rest() as document-node() {
+function tt:content-rest() as document-node() {
     let $url := $tc:server || "/content/ahiqar_sample-82a.html"
     let $req := tc:make-request($url)
     return http:send-request($req)[2]
@@ -109,7 +109,7 @@ declare
      : the underlying function. :)
     %test:assertExists
     %test:pending
-function tests:content-zip() as xs:base64Binary {
+function tt:content-zip() as xs:base64Binary {
     let $url := $tc:server || "/content/ahikar-plain-text.zip"
     let $req := <http:request href="{$url}" method="get">
                         <http:header name="Connection" value="close"/>
@@ -123,7 +123,7 @@ declare
      : no further tests are needed since the content has been tested while testing
      : the underlying function. :)
     %test:assertXPath("matches($result, '[\w]')")
-function tests:content-txt() as xs:string {
+function tt:content-txt() as xs:string {
     let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_sample.txt"
     let $req := <http:request href="{$url}" method="get">
                         <http:header name="Connection" value="close"/>
@@ -134,7 +134,7 @@ function tests:content-txt() as xs:string {
 
 declare
     %test:assertExists
-function tests:compress-text() as xs:base64Binary {
+function tt:compress-text() as xs:base64Binary {
     tapi:compress-to-zip()
 };
 
@@ -142,7 +142,7 @@ function tests:compress-text() as xs:base64Binary {
 declare
     %test:args("ahiqar_sample")
     %test:assertEquals("text/xml")
-function tests:tgmd-format($uri as xs:string) as xs:string {
+function tt:tgmd-format($uri as xs:string) as xs:string {
     tapi:get-format($uri)
 };
 
@@ -150,14 +150,14 @@ function tests:tgmd-format($uri as xs:string) as xs:string {
 declare
     %test:args("ahiqar_sample", "transcription")
     %test:assertXPath("$result[local-name(.) = 'text' and @type = 'transcription']")
-function tests:get-tei($document as xs:string, $type as xs:string) as element() {
+function tt:get-tei($document as xs:string, $type as xs:string) as element() {
     tapi:get-TEI-text($document, $type)
 };
 
 
 declare
     %test:assertXPath("$result/string() = '1234 5678'")
-function tests:remove-whitespaces() as document-node() {
+function tt:remove-whitespaces() as document-node() {
     let $doc := doc("/db/test-records/white-spaces.xml")
     return
         tapi:remove-whitespaces($doc)
@@ -165,37 +165,37 @@ function tests:remove-whitespaces() as document-node() {
 
 declare
     %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
-function tests:get-tei-file-name-of-edition($document as xs:string) {
+function tt:get-tei-file-name-of-edition($document as xs:string) {
     tapi:get-tei-file-name-of-edition($document)
 };
 
 declare
     %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
-function tests:get-edition-aggregates-without-uri-namespace($document as xs:string) {
+function tt:get-edition-aggregates-without-uri-namespace($document as xs:string) {
     tapi:get-edition-aggregates-without-uri-namespace($document)
 };
 
 declare
     %test:args("ahiqar_sample") %test:assertEquals("ahiqar_sample")
-function tests:find-xml-in-aggregates($aggregates as xs:string+) {
+function tt:find-xml-in-aggregates($aggregates as xs:string+) {
     tapi:find-xml-in-aggregates($aggregates)
 };
 
 declare 
     %test:args("ahiqar_sample", "transliteration") %test:assertXPath("$result[@type = 'transliteration']")
-function tests:get-text-of-type($uri as xs:string, $type as xs:string) {
+function tt:get-text-of-type($uri as xs:string, $type as xs:string) {
     tapi:get-text-of-type($uri, $type)
 };
 
 declare
     %test:assertTrue
-function tests:is-txt-api-available() {
+function tt:is-txt-api-available() {
     let $url := $tc:server || "/content/ahiqar_sample.txt"
     return
         tc:is-endpoint-http200($url)
 };
 
-declare function tests:txt() {
+declare function tt:txt() {
     let $url := $tc:server || "textapi/ahiqar/ahiqar_collection/ahiqar_sample.txt"
     let $req := tc:make-request($url)
     return http:send-request($req)[2] => util:base64-decode()
@@ -211,7 +211,7 @@ declare function tests:txt() {
 declare
     %test:args("ahiqar_sample", "data")
     %test:assertXPath("$result//*[local-name(.) = 'TEI']")
-function tests:anno-get-document($uri as xs:string, $type as xs:string) as document-node() {
+function tt:anno-get-document($uri as xs:string, $type as xs:string) as document-node() {
     anno:get-document($uri, $type)
 };
 
@@ -219,7 +219,7 @@ function tests:anno-get-document($uri as xs:string, $type as xs:string) as docum
 (:declare:)
 (:    %test:args("3r679", "114r"):)
 (:    %test:assertEquals("0"):)
-(:function tests:anno-determine-start-index-for-page($uri as xs:string, $page as xs:string) {:)
+(:function tt:anno-determine-start-index-for-page($uri as xs:string, $page as xs:string) {:)
 (:    anno:determine-start-index-for-page($uri, $page):)
 (:};:)
 (::)
@@ -227,14 +227,14 @@ function tests:anno-get-document($uri as xs:string, $type as xs:string) as docum
 (:declare:)
 (:    %test:args("3r131"):)
 (:    %test:assertEquals("16"):)
-(:function tests:anno-determine-start-index($uri as xs:string) {:)
+(:function tt:anno-determine-start-index($uri as xs:string) {:)
 (:    anno:determine-start-index($uri):)
 (:};:)
 (::)
 (:declare:)
 (:    %test:args("3r131"):)
 (:    %test:assertEquals("3r679"):)
-(:function tests:anno-get-parent-aggregation($uri as xs:string) {:)
+(:function tt:anno-get-parent-aggregation($uri as xs:string) {:)
 (:    anno:get-parent-aggregation($uri):)
 (:};:)
 (::)
@@ -242,7 +242,7 @@ function tests:anno-get-document($uri as xs:string, $type as xs:string) as docum
 (:declare:)
 (:    %test:args("3r131"):)
 (:    %test:assertEquals("114r", "114v"):)
-(:function tests:anno-get-pages-in-TEI($uri as xs:string) {:)
+(:function tt:anno-get-pages-in-TEI($uri as xs:string) {:)
 (:    anno:get-pages-in-TEI($uri):)
 (:};:)
 (::)
@@ -250,7 +250,7 @@ function tests:anno-get-document($uri as xs:string, $type as xs:string) as docum
 (:declare:)
 (:    %test:args("3r679"):)
 (:    %test:assertTrue:)
-(:function tests:anno-is-resource-edition($uri as xs:string) {:)
+(:function tt:anno-is-resource-edition($uri as xs:string) {:)
 (:    anno:is-resource-edition($uri):)
 (:};:)
 (::)
@@ -258,14 +258,14 @@ function tests:anno-get-document($uri as xs:string, $type as xs:string) as docum
 (:declare:)
 (:    %test:args("3r131"):)
 (:    %test:assertTrue:)
-(:function tests:anno-is-resource-xml($uri as xs:string) {:)
+(:function tt:anno-is-resource-xml($uri as xs:string) {:)
 (:    anno:is-resource-xml($uri):)
 (:};:)
 
 
 declare
     %test:assertEquals("A place's name.")
-function tests:anno-get-bodyValue() {
+function tt:anno-get-bodyValue() {
     let $annotation := doc("/db/test-records/sample-tei.xml")//tei:placeName
     return
         anno:get-bodyValue($annotation)
@@ -277,7 +277,7 @@ declare
     %test:assertFalse
 (:    %test:args("3r131"):)
 (:    %test:assertTrue:)
-function tests:anno-are-resources-available($resources as xs:string+) {
+function tt:anno-are-resources-available($resources as xs:string+) {
     anno:are-resources-available($resources)
 };
 
@@ -285,7 +285,7 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:declare:)
 (:    %test:args("3r131"):)
 (:    %test:assertEquals("Simon Birol, Aly Elrefaei"):)
-(:function tests:anno-get-creator($uri as xs:string) {:)
+(:function tt:anno-get-creator($uri as xs:string) {:)
 (:    anno:get-creator($uri):)
 (:};:)
 (::)
@@ -293,7 +293,7 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:declare:)
 (:    %test:args("3r131"):)
 (:    %test:assertEquals("Brit. Lib. Add. 7200"):)
-(:function tests:anno-get-metadata-title($uri as xs:string) {:)
+(:function tt:anno-get-metadata-title($uri as xs:string) {:)
 (:    anno:get-metadata-title($uri):)
 (:};:)
 (::)
@@ -301,7 +301,7 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:declare:)
 (:    %test:args("3r679"):)
 (:    %test:assertEquals("3r676", "3r672"):)
-(:function tests:anno-get-prev-xml-uris($uri as xs:string) {:)
+(:function tt:anno-get-prev-xml-uris($uri as xs:string) {:)
 (:    anno:get-prev-xml-uris($uri):)
 (:};:)
 (::)
@@ -309,7 +309,7 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:declare:)
 (:    %test:args("3r679"):)
 (:    %test:assertEquals("3r676", "3r672"):)
-(:function tests:anno-get-xmls-prev-in-collection($uri as xs:string) {:)
+(:function tt:anno-get-xmls-prev-in-collection($uri as xs:string) {:)
 (:    anno:get-xmls-prev-in-collection($uri):)
 (:};:)
 
@@ -317,7 +317,7 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:declare:)
 (:    %test:args("3r679", "114r", "next"):)
 (:    %test:assertEquals("114v"):)
-(:function tests:anno-get-prev-or-next-page($documentURI as xs:string,:)
+(:function tt:anno-get-prev-or-next-page($documentURI as xs:string,:)
 (:$page as xs:string, $type as xs:string) {:)
 (:    anno:get-prev-or-next-page($documentURI, $page, $type):)
 (:};:)
@@ -326,6 +326,6 @@ function tests:anno-are-resources-available($resources as xs:string+) {
 (:declare:)
 (:    %test:args("3r9ps"):)
 (:    %test:assertEquals("3r177", "3r178", "3r7vw", "3r7p1", "3r7p9", "3r7sk", "3r7tp", "3r7vd", "3r179", "3r7n0", "3r9vn", "3r9wf", "3rb3z", "3rbm9", "3rbmc", "3rx14", "3vp38"):)
-(:function tests:anno-get-uris($documentURI) {:)
+(:function tt:anno-get-uris($documentURI) {:)
 (:    anno:get-uris($documentURI):)
 (:};:)
-- 
GitLab


From cf49cdf83b6d85feb8dd909acf63a7b65d1965af Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 07:58:03 +0200
Subject: [PATCH 09/22] refactor: update namespace

---
 .../modules/{collate.xqm => tapi-txt.xqm}     | 104 ++++++-------
 exist-app/modules/tapi.xqm                    |  14 +-
 exist-app/tests-runner.xq                     |   6 +-
 ...ollate-tests.xqm => textapi-txt-tests.xqm} | 142 +++++++++---------
 4 files changed, 133 insertions(+), 133 deletions(-)
 rename exist-app/modules/{collate.xqm => tapi-txt.xqm} (54%)
 rename exist-app/tests/{collate-tests.xqm => textapi-txt-tests.xqm} (70%)

diff --git a/exist-app/modules/collate.xqm b/exist-app/modules/tapi-txt.xqm
similarity index 54%
rename from exist-app/modules/collate.xqm
rename to exist-app/modules/tapi-txt.xqm
index 635010eb..4248f53d 100644
--- a/exist-app/modules/collate.xqm
+++ b/exist-app/modules/tapi-txt.xqm
@@ -7,43 +7,43 @@ xquery version "3.1";
  : pipelines.
  :)
 
-module namespace coll="http://ahikar.sub.uni-goettingen.de/ns/collate";
+module namespace tapi-txt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt";
 
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 import module namespace fragment="https://wiki.tei-c.org/index.php?title=Milestone-chunk.xquery" at "fragment.xqm";
 
-declare variable $coll:textgrid := "/db/apps/sade/textgrid";
-declare variable $coll:data := $coll:textgrid || "/data";
-declare variable $coll:txt := $coll:textgrid || "/txt";
+declare variable $tapi-txt:textgrid := "/db/apps/sade/textgrid";
+declare variable $tapi-txt:data := $tapi-txt:textgrid || "/data";
+declare variable $tapi-txt:txt := $tapi-txt:textgrid || "/txt";
 
 
-declare function coll:main() 
+declare function tapi-txt:main() 
 as xs:string+ {
-    coll:create-txt-collection-if-not-available(),
-    for $text in coll:get-transcriptions-and-transliterations() return
-        let $relevant-text := coll:get-relevant-text($text)
-        let $file-name := coll:make-file-name($text)
+    tapi-txt:create-txt-collection-if-not-available(),
+    for $text in tapi-txt:get-transcriptions-and-transliterations() return
+        let $relevant-text := tapi-txt:get-relevant-text($text)
+        let $file-name := tapi-txt:make-file-name($text)
         return
-            xmldb:store($coll:txt, $file-name, $relevant-text, "text/plain")
+            xmldb:store($tapi-txt:txt, $file-name, $relevant-text, "text/plain")
 };
 
-declare function coll:create-txt-collection-if-not-available()
+declare function tapi-txt:create-txt-collection-if-not-available()
 as xs:string? {
-    if (xmldb:collection-available($coll:txt)) then
+    if (xmldb:collection-available($tapi-txt:txt)) then
         ()
     else
-        xmldb:create-collection($coll:textgrid, "txt")
+        xmldb:create-collection($tapi-txt:textgrid, "txt")
 };
 
-declare function coll:get-transcriptions-and-transliterations()
+declare function tapi-txt:get-transcriptions-and-transliterations()
 as element(tei:text)+ {
-    collection($coll:data)//tei:text[@type = ("transcription", "transliteration")]
-        [coll:has-text-milestone(.)]
+    collection($tapi-txt:data)//tei:text[@type = ("transcription", "transliteration")]
+        [tapi-txt:has-text-milestone(.)]
 };
 
-declare function coll:has-text-milestone($text as element(tei:text))
+declare function tapi-txt:has-text-milestone($text as element(tei:text))
 as xs:boolean {
     if ($text//tei:milestone) then
         true()
@@ -55,16 +55,16 @@ as xs:boolean {
  : An example for the file name is 
  : syriac-Brit_Lib_Add_7200-3r131-transcription.txt
  :)
-declare function coll:make-file-name($text as element(tei:text))
+declare function tapi-txt:make-file-name($text as element(tei:text))
 as xs:string {
-    let $lang-prefix := coll:get-language-prefix($text)
-    let $title-from-metadata := coll:create-metadata-title-for-file-name($text)
-    let $uri-plus-text-type := coll:make-file-name-suffix($text)
+    let $lang-prefix := tapi-txt:get-language-prefix($text)
+    let $title-from-metadata := tapi-txt:create-metadata-title-for-file-name($text)
+    let $uri-plus-text-type := tapi-txt:make-file-name-suffix($text)
     return
         $lang-prefix || "-" || $title-from-metadata || "-" || $uri-plus-text-type
 };
 
-declare function coll:get-language-prefix($text as element(tei:text))
+declare function tapi-txt:get-language-prefix($text as element(tei:text))
 as xs:string? {
     switch ($text/@type)
         case "transcription" return
@@ -85,9 +85,9 @@ as xs:string? {
         default return ()
 };
 
-declare function coll:create-metadata-title-for-file-name($text as element(tei:text))
+declare function tapi-txt:create-metadata-title-for-file-name($text as element(tei:text))
 as xs:string {
-    let $base-uri := coll:get-base-uri($text)
+    let $base-uri := tapi-txt:get-base-uri($text)
     let $metadata := doc($base-uri => replace("/data/", "/meta/"))
     return
         $metadata//tgmd:title
@@ -95,50 +95,50 @@ as xs:string {
         => replace("[_]+", "_")
 };
 
-declare function coll:get-base-uri($text as element(tei:text))
+declare function tapi-txt:get-base-uri($text as element(tei:text))
 as xs:string{
     base-uri($text)
 };
 
-declare function coll:make-file-name-suffix($text as element(tei:text))
+declare function tapi-txt:make-file-name-suffix($text as element(tei:text))
 as xs:string {
-    let $base-uri := coll:get-base-uri($text)
-    let $file-name := coll:get-file-name($base-uri)
+    let $base-uri := tapi-txt:get-base-uri($text)
+    let $file-name := tapi-txt:get-file-name($base-uri)
     let $type := $text/@type
     return
         $file-name || "-" || $type || ".txt"
 };
 
-declare function coll:get-file-name($base-uri as xs:string)
+declare function tapi-txt:get-file-name($base-uri as xs:string)
 as xs:string {
     tokenize($base-uri, "/")[last()]
     => substring-before(".xml")
 };
 
-declare function coll:get-relevant-text($text as element(tei:text))
+declare function tapi-txt:get-relevant-text($text as element(tei:text))
 as xs:string {
-    let $milestones := coll:get-milestones-in-text($text)
-    let $chunks := coll:get-chunks($milestones)
-    let $texts := coll:get-relevant-text-from-chunks($chunks)
+    let $milestones := tapi-txt:get-milestones-in-text($text)
+    let $chunks := tapi-txt:get-chunks($milestones)
+    let $texts := tapi-txt:get-relevant-text-from-chunks($chunks)
     return
         string-join($texts, " ")
 };
 
-declare function coll:get-milestones-in-text($text as element(tei:text))
+declare function tapi-txt:get-milestones-in-text($text as element(tei:text))
 as element(tei:milestone)+ {
     $text//tei:milestone
 };
 
-declare function coll:get-chunks($milestones as element(tei:milestone)+)
+declare function tapi-txt:get-chunks($milestones as element(tei:milestone)+)
 as element(tei:TEI)+ {
     for $milestone in $milestones return
-        coll:get-chunk($milestone)
+        tapi-txt:get-chunk($milestone)
 };
 
-declare function coll:get-chunk($milestone as element(tei:milestone))
+declare function tapi-txt:get-chunk($milestone as element(tei:milestone))
 as element(tei:TEI) {
     let $root := $milestone/root()
-    let $end-of-chunk := coll:get-end-of-chunk($milestone)
+    let $end-of-chunk := tapi-txt:get-end-of-chunk($milestone)
     return
         fragment:get-fragment-from-doc(
             $root,
@@ -149,15 +149,15 @@ as element(tei:TEI) {
             (""))
 };
 
-declare function coll:get-end-of-chunk($milestone as element(tei:milestone))
+declare function tapi-txt:get-end-of-chunk($milestone as element(tei:milestone))
 as node() {
-    if (coll:has-following-milestone($milestone)) then
-        coll:get-next-milestone($milestone)
+    if (tapi-txt:has-following-milestone($milestone)) then
+        tapi-txt:get-next-milestone($milestone)
     else
         $milestone/ancestor::tei:text[1]/tei:body/child::*[last()]
 };
 
-declare function coll:has-following-milestone($milestone as element(tei:milestone))
+declare function tapi-txt:has-following-milestone($milestone as element(tei:milestone))
 as xs:boolean {
     if ($milestone/following::tei:milestone[ancestor::tei:text[1] = $milestone/ancestor::tei:text[1]]) then
         true()
@@ -165,29 +165,29 @@ as xs:boolean {
         false()
 };
 
-declare function coll:get-next-milestone($milestone as element(tei:milestone))
+declare function tapi-txt:get-next-milestone($milestone as element(tei:milestone))
 as element(tei:milestone)? {
     $milestone/following::tei:milestone[ancestor::tei:text[1] = $milestone/ancestor::tei:text[1]][1]
 };
 
-declare function coll:get-relevant-text-from-chunks($chunks as element(tei:TEI)+)
+declare function tapi-txt:get-relevant-text-from-chunks($chunks as element(tei:TEI)+)
 as xs:string {
     let $texts :=
         for $chunk in $chunks return
-            coll:make-plain-text-from-chunk($chunk)
+            tapi-txt:make-plain-text-from-chunk($chunk)
     return
         string-join($texts, " ")
         => normalize-space()
 };
 
-declare function coll:make-plain-text-from-chunk($chunk as element(tei:TEI))
+declare function tapi-txt:make-plain-text-from-chunk($chunk as element(tei:TEI))
 as xs:string {
-    let $texts := coll:get-relevant-text-nodes($chunk)
+    let $texts := tapi-txt:get-relevant-text-nodes($chunk)
     let $prepared-texts :=
         for $text in $texts return
-            coll:prepare-plain-text-creation($text)
+            tapi-txt:prepare-plain-text-creation($text)
     return
-        coll:format-and-normalize-string($prepared-texts)
+        tapi-txt:format-and-normalize-string($prepared-texts)
 };
 
 (:~ 
@@ -201,7 +201,7 @@ as xs:string {
  : * catchwords (they simply serve to bind the books correctly and reduplicate text)
  : * note (they have been added later by another scribe)
  :)
-declare function coll:get-relevant-text-nodes($chunk as element(tei:TEI))
+declare function tapi-txt:get-relevant-text-nodes($chunk as element(tei:TEI))
 as text()+ {
     (($chunk//text()
         [not(parent::tei:sic)]
@@ -214,7 +214,7 @@ as text()+ {
         [not(parent::tei:note)]
 };
 
-declare function coll:prepare-plain-text-creation($text as text())
+declare function tapi-txt:prepare-plain-text-creation($text as text())
 as xs:string {
     if ($text/preceding-sibling::*[1][self::tei:lb[@break = "no"]]) then
         "@" || $text
@@ -222,7 +222,7 @@ as xs:string {
         $text
 };
 
-declare function coll:format-and-normalize-string($strings as xs:string+)
+declare function tapi-txt:format-and-normalize-string($strings as xs:string+)
 as xs:string {
     string-join($strings, " ")
     => replace(" @", "")
diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index a4c3a372..62987673 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -22,7 +22,7 @@ declare namespace test="http://exist-db.org/xquery/xqsuite";
 declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 declare namespace xhtml="http://www.w3.org/1999/xhtml";
 
-import module namespace coll="http://ahikar.sub.uni-goettingen.de/ns/collate" at "collate.xqm";
+import module namespace tapi-txt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt" at "tapi-txt.xqm";
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
 import module namespace fragment="https://wiki.tei-c.org/index.php?title=Milestone-chunk.xquery" at "fragment.xqm";
 import module namespace functx="http://www.functx.com";
@@ -95,20 +95,20 @@ declare function tapi:remove-whitespaces($doc as document-node()) as document-no
  :
  : Sample call to API: /content/3rbmb-1a.html
  :
- : @param $document The unprefixed TextGrid URI of a document, e.g. '3rbmb'
+ : @param $document The unprefixed TextGrid URI of a TEI/XML, e.g. '3rbmb'
  : @param $page The page to be rendered. This has to be the string value of a tei:pb/@n in the given document, e.g. '1a'
  : @return A response header as well as the rendered HTML page
  :)
 declare
     %rest:GET
     %rest:HEAD
-    %rest:path("/content/{$document}-{$page}.html")
+    %rest:path("/content/{$tei-xml-uri}-{$page}.html")
     %output:method("xml")
     %output:indent("no")
-function tapi:content-rest($document as xs:string, $page as xs:string)
+function tapi:content-rest($tei-xml-uri as xs:string, $page as xs:string)
 as item()+ {
     $commons:responseHeader200,
-    tapi-html:get-html($document, $page)
+    tapi-html:get-html($tei-xml-uri, $page)
 };
 
 
@@ -164,7 +164,7 @@ as item()+ {
     return
         ( 
             $commons:responseHeader200,
-            coll:make-plain-text-from-chunk($TEI)
+            tapi-txt:make-plain-text-from-chunk($TEI)
         )
 };
 
@@ -180,7 +180,7 @@ declare
     %rest:path("/content/ahikar-plain-text.zip")
     %output:method("binary")
 function tapi:text-rest() as item()+ {
-    let $prepare := coll:main()
+    let $prepare := tapi-txt:main()
     return
         $commons:responseHeader200,
         tapi:compress-to-zip()
diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 14299a3e..2b150985 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -4,20 +4,20 @@ xquery version "3.1";
  : execution.
  :)
 
-import module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests" at "tests/collate-tests.xqm";
+import module namespace ttt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests" at "tests/tapi-txt-tests.xqm";
 import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
 import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tests/tapi-collection-tests.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests" at "tests/tapi-html-tests.xqm";
 import module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tests/tapi-item-tests.xqm";
 import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tests/tapi-manifest-tests.xqm";
-import module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests.xqm";
+import module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests/tapi-tests.xqm";
 
 
 let $test-results :=
     (
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),
-        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/coll-tests")),
+        test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),
         test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests")),
diff --git a/exist-app/tests/collate-tests.xqm b/exist-app/tests/textapi-txt-tests.xqm
similarity index 70%
rename from exist-app/tests/collate-tests.xqm
rename to exist-app/tests/textapi-txt-tests.xqm
index b29b3165..2b719ffe 100644
--- a/exist-app/tests/collate-tests.xqm
+++ b/exist-app/tests/textapi-txt-tests.xqm
@@ -1,62 +1,62 @@
 xquery version "3.1";
 
-module namespace coll-tests="http://ahikar.sub.uni-goettingen.de/ns/coll-tests";
+module namespace ttt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests";
 
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 
-import module namespace coll="http://ahikar.sub.uni-goettingen.de/ns/collate" at "../modules/collate.xqm";
+import module namespace tapi-txt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt" at "../modules/tapi-txt.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 
-declare variable $coll-tests:sample-file := local:open-file("ahiqar_sample");
-declare variable $coll-tests:sample-transliteration := $coll-tests:sample-file//tei:text[@type = "transliteration"];
-declare variable $coll-tests:sample-transcription := $coll-tests:sample-file//tei:text[@type = "transcription"];
+declare variable $ttt:sample-file := local:open-file("ahiqar_sample");
+declare variable $ttt:sample-transliteration := $ttt:sample-file//tei:text[@type = "transliteration"];
+declare variable $ttt:sample-transcription := $ttt:sample-file//tei:text[@type = "transcription"];
 
 declare
     %test:args("ahiqar_sample") %test:assertExists
     %test:args("1234") %test:assertError("org.exist.xquery.XPathException")
-function coll-tests:open-file($uri as xs:string) as document-node() {
+function ttt:open-file($uri as xs:string) as document-node() {
     local:open-file($uri)
 };
 
 declare
     %test:assertXPath("count($result) = 2")
-function coll-tests:get-milestones-in-text()
+function ttt:get-milestones-in-text()
 as element(tei:milestone)* {
-    coll:get-milestones-in-text($coll-tests:sample-transliteration)
+    tapi-txt:get-milestones-in-text($ttt:sample-transliteration)
 };
 
 declare
     %test:assertExists
-function coll-tests:get-next-milestone-succecss()
+function ttt:get-next-milestone-succecss()
 as element(tei:milestone)? {
-    let $milestone := $coll-tests:sample-transliteration//tei:milestone[1]
+    let $milestone := $ttt:sample-transliteration//tei:milestone[1]
     return
-        coll:get-next-milestone($milestone)
+        tapi-txt:get-next-milestone($milestone)
 };
 
 declare
     %test:assertEmpty
-function coll-tests:get-next-milestone-fail()
+function ttt:get-next-milestone-fail()
 as element(tei:milestone)? {
-    let $milestone := $coll-tests:sample-transliteration//tei:milestone[2]
+    let $milestone := $ttt:sample-transliteration//tei:milestone[2]
     return
-        coll:get-next-milestone($milestone)
+        tapi-txt:get-next-milestone($milestone)
 };
 
 declare
     %test:assertExists
     %test:assertXPath("$result//*[local-name(.) = 'ab']")
-function coll-tests:get-chunk()
+function ttt:get-chunk()
 as element(tei:TEI) {
-    let $milestone := $coll-tests:sample-transliteration//tei:milestone[1]
+    let $milestone := $ttt:sample-transliteration//tei:milestone[1]
     return
-        coll:get-chunk($milestone)
+        tapi-txt:get-chunk($milestone)
 };
 
 
 declare
      %test:assertEquals("some text that should be used display some text without a space")
-function coll-tests:make-plain-text-from-chunk()
+function ttt:make-plain-text-from-chunk()
 as xs:string {
     let $chunk := 
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
@@ -90,13 +90,13 @@ as xs:string {
             </text>
         </TEI>
     return
-        coll:make-plain-text-from-chunk($chunk)
+        tapi-txt:make-plain-text-from-chunk($chunk)
 };
 
 
 declare
     %test:assertEquals("some text with")
-function coll-tests:prepare-plain-text-creation-no-lb()
+function ttt:prepare-plain-text-creation-no-lb()
 as xs:string {
     let $chunk :=
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
@@ -112,12 +112,12 @@ as xs:string {
         </TEI>
     let $text := $chunk//tei:ab/text()
     return
-        coll:prepare-plain-text-creation($text)
+        tapi-txt:prepare-plain-text-creation($text)
 };
 
 declare
     %test:assertEquals("@out a space.")
-function coll-tests:prepare-plain-text-creation-lb()
+function ttt:prepare-plain-text-creation-lb()
 as xs:string {
     let $chunk :=
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
@@ -133,12 +133,12 @@ as xs:string {
         </TEI>
     let $text := $chunk//tei:ab/text()
     return
-        coll:prepare-plain-text-creation($text)
+        tapi-txt:prepare-plain-text-creation($text)
 };
 
 declare
     %test:assertXPath("count($result) = 6")
-function coll-tests:get-relevant-text-nodes()
+function ttt:get-relevant-text-nodes()
 as text()+ {
     let $chunk := 
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
@@ -172,7 +172,7 @@ as text()+ {
             </text>
         </TEI>
     return
-        coll:get-relevant-text-nodes($chunk)
+        tapi-txt:get-relevant-text-nodes($chunk)
 };
 
 declare
@@ -182,15 +182,15 @@ declare
     %test:args("some
     
     new lines") %test:assertEquals("some new lines")
-function coll-tests:format-and-normalize-string($string as xs:string)
+function ttt:format-and-normalize-string($string as xs:string)
 as xs:string {
-    coll:format-and-normalize-string($string)
+    tapi-txt:format-and-normalize-string($string)
 };
 
 declare
     %test:assertTrue
-function coll-tests:create-txt-collection-if-not-available() {
-    let $create-collection := coll:create-txt-collection-if-not-available()
+function ttt:create-txt-collection-if-not-available() {
+    let $create-collection := tapi-txt:create-txt-collection-if-not-available()
     return
         if (xmldb:collection-available("/db/apps/sade/textgrid/txt/")) then
             true()
@@ -200,22 +200,22 @@ function coll-tests:create-txt-collection-if-not-available() {
 
 declare
     %test:assertXPath("count($result) gt 0")
-function coll-tests:get-transcriptions-and-transliterations()
+function ttt:get-transcriptions-and-transliterations()
 as element(tei:text)+ {
-    coll:get-transcriptions-and-transliterations()
+    tapi-txt:get-transcriptions-and-transliterations()
 };
 
 declare
     %test:args("ara") %test:assertEquals("arabic")
     %test:args("karshuni") %test:assertEquals("karshuni")
     %test:args("syc") %test:assertEquals("syriac")
-function coll-tests:get-language-prefix-transcriptions($lang as xs:string)
+function ttt:get-language-prefix-transcriptions($lang as xs:string)
 as xs:string {
     let $text := 
         <text xmlns="http://www.tei-c.org/ns/1.0" type="transcription"
         xml:lang="{$lang}" />
     return
-        coll:get-language-prefix($text)
+        tapi-txt:get-language-prefix($text)
 };
 
 declare
@@ -228,7 +228,7 @@ declare
     %test:args("syriac", "ara") %test:assertEquals("arabic")
     %test:args("syriac", "karshuni") %test:assertEquals("karshuni")
     %test:args("syriac", "syc") %test:assertEquals("syriac")
-function coll-tests:get-language-prefix-transliteration($lang-transliteration as xs:string,
+function ttt:get-language-prefix-transliteration($lang-transliteration as xs:string,
 $lang-transcription as xs:string)
 as xs:string {
     let $TEI :=
@@ -238,47 +238,47 @@ as xs:string {
         </TEI>
     let $text := $TEI/tei:text[1]
     return
-        coll:get-language-prefix($text)
+        tapi-txt:get-language-prefix($text)
 };
 
 declare
     %test:assertEquals("/db/apps/sade/textgrid/data/ahiqar_sample.xml")
-function coll-tests:get-base-uri()
+function ttt:get-base-uri()
 as xs:string {
-    coll:get-base-uri($coll-tests:sample-transcription)
+    tapi-txt:get-base-uri($ttt:sample-transcription)
 };
 
 declare
     %test:assertEquals("Beispieldatei_zum_Testen")
-function coll-tests:create-metadata-title-for-file-name()
+function ttt:create-metadata-title-for-file-name()
 as xs:string {
-    coll:create-metadata-title-for-file-name($coll-tests:sample-transcription)
+    tapi-txt:create-metadata-title-for-file-name($ttt:sample-transcription)
 };
 
 declare
     %test:assertEquals("karshuni-Beispieldatei_zum_Testen-ahiqar_sample-transcription.txt")
-function coll-tests:make-file-name()
+function ttt:make-file-name()
 as xs:string {
-    coll:make-file-name($coll-tests:sample-transcription)
+    tapi-txt:make-file-name($ttt:sample-transcription)
 };
 
 declare
     %test:assertEquals("ahiqar_sample-transcription.txt")
-function coll-tests:make-file-name-suffix()
+function ttt:make-file-name-suffix()
 as xs:string {
-    coll:make-file-name-suffix($coll-tests:sample-transcription)
+    tapi-txt:make-file-name-suffix($ttt:sample-transcription)
 };
 
 declare
     %test:args("/db/apps/sade/textgrid/data/ahiqar_sample.xml") %test:assertEquals("ahiqar_sample")
-function coll-tests:get-file-name($base-uri as xs:string)
+function ttt:get-file-name($base-uri as xs:string)
 as xs:string {
-    coll:get-file-name($base-uri)
+    tapi-txt:get-file-name($base-uri)
 };
 
 declare
     %test:assertEquals("text of the first narrative section some sayings")
-function coll-tests:get-relevant-text() {
+function ttt:get-relevant-text() {
     let $TEI :=
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
             <text>
@@ -292,37 +292,37 @@ function coll-tests:get-relevant-text() {
             </text>
         </TEI>
     return
-        coll:get-relevant-text($TEI/tei:text)
+        tapi-txt:get-relevant-text($TEI/tei:text)
 };
 
 declare
     %test:assertXPath("count($result) = 2")
-function coll-tests:get-chunks() {
-    let $milestones := coll:get-milestones-in-text($coll-tests:sample-transliteration)
+function ttt:get-chunks() {
+    let $milestones := tapi-txt:get-milestones-in-text($ttt:sample-transliteration)
     return
-        coll:get-chunks($milestones)
+        tapi-txt:get-chunks($milestones)
 };
 
 declare
     %test:assertXPath("$result[self::*[local-name(.) = 'milestone']]")
-function coll-tests:get-end-of-chunk-milestone() {
-    let $milestone := coll:get-milestones-in-text($coll-tests:sample-transliteration)[1]
+function ttt:get-end-of-chunk-milestone() {
+    let $milestone := tapi-txt:get-milestones-in-text($ttt:sample-transliteration)[1]
     return
-        coll:get-end-of-chunk($milestone)
+        tapi-txt:get-end-of-chunk($milestone)
 };
 
 declare
     %test:assertXPath("$result[self::*[local-name(.) = 'ab']]")
-function coll-tests:get-end-of-chunk-end-of-text() {
-    let $milestone := coll:get-milestones-in-text($coll-tests:sample-transliteration)[2]
+function ttt:get-end-of-chunk-end-of-text() {
+    let $milestone := tapi-txt:get-milestones-in-text($ttt:sample-transliteration)[2]
     return
-        coll:get-end-of-chunk($milestone)
+        tapi-txt:get-end-of-chunk($milestone)
 };
 
 
 declare
     %test:assertXPath("$result[self::*/string() = 'the end text']")
-function coll-tests:get-end-of-chunk-end-of-text-2() {
+function ttt:get-end-of-chunk-end-of-text-2() {
     let $TEI :=
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
             <text>
@@ -345,30 +345,30 @@ function coll-tests:get-end-of-chunk-end-of-text-2() {
     let $milestone := $TEI//tei:text[@type = "transliteration"]//tei:milestone
     
     return
-        coll:get-end-of-chunk($milestone)
+        tapi-txt:get-end-of-chunk($milestone)
 };
 
 declare
     %test:assertTrue
-function coll-tests:has-following-milestone-true()
+function ttt:has-following-milestone-true()
 as xs:boolean {
-    let $milestone := coll:get-milestones-in-text($coll-tests:sample-transliteration)[1]
+    let $milestone := tapi-txt:get-milestones-in-text($ttt:sample-transliteration)[1]
     return
-        coll:has-following-milestone($milestone)
+        tapi-txt:has-following-milestone($milestone)
 };
 
 declare
     %test:assertFalse
-function coll-tests:has-following-milestone-false()
+function ttt:has-following-milestone-false()
 as xs:boolean {
-    let $milestone := coll:get-milestones-in-text($coll-tests:sample-transliteration)[2]
+    let $milestone := tapi-txt:get-milestones-in-text($ttt:sample-transliteration)[2]
     return
-        coll:has-following-milestone($milestone)
+        tapi-txt:has-following-milestone($milestone)
 };
 
 declare
     %test:assertEquals("chunk1 relevant text more relevant text chunk2 relevant text more relevant text")
-function coll-tests:get-relevant-text-from-chunks()
+function ttt:get-relevant-text-from-chunks()
 as xs:string {
     let $chunk1 :=
         <TEI xmlns="http://www.tei-c.org/ns/1.0">
@@ -389,12 +389,12 @@ as xs:string {
             </text>
         </TEI>
     return
-        coll:get-relevant-text-from-chunks(($chunk1, $chunk2))
+        tapi-txt:get-relevant-text-from-chunks(($chunk1, $chunk2))
 };
 
 declare
     %test:assertTrue
-function coll-tests:has-text-milestone-succcess() {
+function ttt:has-text-milestone-succcess() {
     let $text:= 
         <text xmlns="http://www.tei-c.org/ns/1.0">
             <body>
@@ -402,12 +402,12 @@ function coll-tests:has-text-milestone-succcess() {
             </body>
         </text>
     return
-        coll:has-text-milestone($text)
+        tapi-txt:has-text-milestone($text)
 };
 
 declare
     %test:assertFalse
-function coll-tests:has-text-milestones-fail() {
+function ttt:has-text-milestones-fail() {
     let $text:= 
         <text xmlns="http://www.tei-c.org/ns/1.0">
             <body>
@@ -415,10 +415,10 @@ function coll-tests:has-text-milestones-fail() {
             </body>
         </text>
     return
-        coll:has-text-milestone($text)  
+        tapi-txt:has-text-milestone($text)  
 };
 
 declare function local:open-file($uri as xs:string)
 as document-node() {
-    doc($coll:data || "/" || $uri || ".xml")
+    doc($tapi-txt:data || "/" || $uri || ".xml")
 };
-- 
GitLab


From 43b79b8b56a2ed8bd223d1bf1df15a13e5a3cabf Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 09:07:52 +0200
Subject: [PATCH 10/22] refactor: rename module

---
 ...xtapi-txt-tests.xqm => tapi-txt-tests.xqm} | 71 +++++++++++++++++++
 1 file changed, 71 insertions(+)
 rename exist-app/tests/{textapi-txt-tests.xqm => tapi-txt-tests.xqm} (87%)

diff --git a/exist-app/tests/textapi-txt-tests.xqm b/exist-app/tests/tapi-txt-tests.xqm
similarity index 87%
rename from exist-app/tests/textapi-txt-tests.xqm
rename to exist-app/tests/tapi-txt-tests.xqm
index 2b719ffe..16d22def 100644
--- a/exist-app/tests/textapi-txt-tests.xqm
+++ b/exist-app/tests/tapi-txt-tests.xqm
@@ -422,3 +422,74 @@ declare function local:open-file($uri as xs:string)
 as document-node() {
     doc($tapi-txt:data || "/" || $uri || ".xml")
 };
+
+
+declare
+    %test:args("ahiqar_sample", "transcription")
+    %test:assertXPath("$result[local-name(.) = 'text' and @type = 'transcription']")
+function ttt:get-tei($document-uri as xs:string,
+    $type as xs:string)
+as element() {
+    tapi-txt:get-TEI-text($document-uri, $type)
+};
+
+declare
+    %test:args("ahiqar_sample")
+    %test:assertEquals("text/xml")
+function ttt:tgmd-format($uri as xs:string)
+as xs:string {
+    tapi-txt:get-format($uri)
+};
+
+
+declare
+    %test:args("ahiqar_sample")
+    %test:assertEquals("text/xml")
+function ttt:tgmd-format($uri as xs:string) as xs:string {
+    tapi-txt:get-format($uri)
+};
+
+
+
+declare
+    %test:args("ahiqar_sample", "transcription")
+    %test:assertXPath("$result[local-name(.) = 'text' and @type = 'transcription']")
+function ttt:get-tei($document as xs:string, $type as xs:string) as element() {
+    tapi-txt:get-TEI-text($document, $type)
+};
+
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
+function ttt:get-tei-xml-uri-from-edition($document as xs:string) {
+    tapi-txt:get-tei-xml-uri-from-edition($document)
+};
+
+
+declare
+    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
+function ttt:get-edition-aggregates-without-uri-namespace($document as xs:string) {
+    tapi-txt:get-edition-aggregates-without-uri-namespace($document)
+};
+
+
+declare
+    %test:args("ahiqar_sample") %test:assertEquals("ahiqar_sample")
+function ttt:get-tei-xml-from-aggregates($aggregates as xs:string+) {
+    tapi-txt:get-tei-xml-from-aggregates($aggregates)
+};
+
+
+declare 
+    %test:args("ahiqar_sample", "transliteration") %test:assertXPath("$result[@type = 'transliteration']")
+function ttt:get-text-of-type($uri as xs:string, $type as xs:string) {
+    tapi-txt:get-text-of-type($uri, $type)
+};
+
+declare
+    %test:args("ahiqar_sample") %test:assertTrue
+    %test:args("ahiqar_agg") %test:assertFalse
+function ttt:is-document-tei-xml($document-uri as xs:string)
+as xs:boolean {
+    tapi-txt:is-document-tei-xml($document-uri)
+};
-- 
GitLab


From b654a3ce16070105b2e965ad7f584227287b7871 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 09:08:27 +0200
Subject: [PATCH 11/22] refactor: move txt related functions to separate module

---
 exist-app/modules/commons.xqm      |   5 ++
 exist-app/modules/tapi-txt.xqm     |  85 +++++++++++++++++++++++
 exist-app/modules/tapi.xqm         | 104 ++++-------------------------
 exist-app/tests/commons-tests.xqm  |  10 ++-
 exist-app/tests/tapi-tests.xqm     |  64 +++---------------
 exist-app/tests/tapi-txt-tests.xqm |   7 ++
 6 files changed, 127 insertions(+), 148 deletions(-)

diff --git a/exist-app/modules/commons.xqm b/exist-app/modules/commons.xqm
index 2162a07e..f03f8300 100644
--- a/exist-app/modules/commons.xqm
+++ b/exist-app/modules/commons.xqm
@@ -41,4 +41,9 @@ as document-node() {
     let $xml-uri := commons:get-xml-uri($manifest-uri)
     return
         doc($commons:data || $xml-uri || ".xml")
+};
+
+declare function commons:open-tei-xml($tei-xml-uri as xs:string)
+as document-node() {
+    doc($commons:data || $tei-xml-uri || ".xml")
 };
\ No newline at end of file
diff --git a/exist-app/modules/tapi-txt.xqm b/exist-app/modules/tapi-txt.xqm
index 4248f53d..6dd2652f 100644
--- a/exist-app/modules/tapi-txt.xqm
+++ b/exist-app/modules/tapi-txt.xqm
@@ -9,9 +9,12 @@ xquery version "3.1";
 
 module namespace tapi-txt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt";
 
+declare namespace ore="http://www.openarchives.org/ore/terms/";
+declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
+import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
 import module namespace fragment="https://wiki.tei-c.org/index.php?title=Milestone-chunk.xquery" at "fragment.xqm";
 
 declare variable $tapi-txt:textgrid := "/db/apps/sade/textgrid";
@@ -229,3 +232,85 @@ as xs:string {
     => replace("[\p{P}\n+]", "")
     => replace("\s+", " ")
 };
+
+
+(:~
+ : Returns the tei:text of a document as indicated by the @type parameter.
+ : 
+ : Due to the structure of the Ahikar project we can pass either an edition or
+ : an XML file to the API endpoint for plain text creation.
+ : This function determines the correct file which serves as a basis for the plain text.
+ : 
+ : @param $document The URI of a resource
+ : @param $type Indicates the @type of tei:text to be processed
+ : @return The tei:text element to be serialized as plain text
+ :)
+declare function tapi-txt:get-TEI-text($document-uri as xs:string,
+    $type as xs:string)
+as element(tei:text) {
+    if (tapi-txt:is-document-tei-xml($document-uri)) then
+        commons:open-tei-xml($document-uri)//tei:text[@type = $type]
+    else
+        tapi-txt:get-tei-xml-uri-from-edition
+        => tapi-txt:get-text-of-type($type)
+};
+
+
+declare function tapi-txt:is-document-tei-xml($document-uri as xs:string) {
+    let $format := tapi-txt:get-format($document-uri)
+    return
+        if ($format = "text/xml") then
+            true()
+        else
+            false()
+};
+
+
+(:~
+ : Returns the TextGrid metadata type of a resource.
+ : 
+ : @param $uri The URI of the resource
+ : @return The resource's format as tgmd:format
+ :)
+declare function tapi-txt:get-format($uri as xs:string) as xs:string {
+    doc($commons:meta || $uri || ".xml")//tgmd:format
+};
+
+
+declare function tapi-txt:get-tei-xml-uri-from-edition($document-uri as xs:string)
+as xs:string {
+    let $aggregates := tapi-txt:get-edition-aggregates-without-uri-namespace($document-uri)
+    return
+        tapi-txt:get-tei-xml-from-aggregates($aggregates)
+};
+
+
+declare function tapi-txt:get-edition-aggregates-without-uri-namespace($document-uri as xs:string)
+as xs:string+ {
+    let $edition := commons:get-aggregation($document-uri)
+    for $agg in $edition//ore:aggregates/@rdf:resource return
+        replace($agg, "textgrid:", "")
+};
+
+
+declare function tapi-txt:get-tei-xml-from-aggregates($aggregates as xs:string+)
+as xs:string {
+    for $agg in $aggregates return
+        if (tapi-txt:get-format($agg) = "text/xml") then
+            $agg
+        else
+            ()
+};
+
+
+declare function tapi-txt:get-text-of-type($uri as xs:string,
+    $type as xs:string)
+as element(tei:text) {
+    commons:open-tei-xml($uri)//tei:text[@type = $type]
+};
+
+
+declare function tapi-txt:compress-to-zip()
+as xs:base64Binary* {
+    compression:zip(xs:anyURI($commons:tg-collection || "/txt/"), false())
+};
\ No newline at end of file
diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index 62987673..5c3f0745 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -13,19 +13,13 @@ xquery version "3.1";
 
 module namespace tapi="http://ahikar.sub.uni-goettingen.de/ns/tapi";
 
-declare namespace expkg="http://expath.org/ns/pkg";
-declare namespace ore="http://www.openarchives.org/ore/terms/";
 declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
-declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 declare namespace test="http://exist-db.org/xquery/xqsuite";
-declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 declare namespace xhtml="http://www.w3.org/1999/xhtml";
 
 import module namespace tapi-txt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt" at "tapi-txt.xqm";
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
-import module namespace fragment="https://wiki.tei-c.org/index.php?title=Milestone-chunk.xquery" at "fragment.xqm";
-import module namespace functx="http://www.functx.com";
 import module namespace requestr="http://exquery.org/ns/request";
 import module namespace rest="http://exquery.org/ns/restxq";
 import module namespace tapi-html="http://ahikar.sub.uni-goettingen.de/ns/tapi/html" at "tapi-html.xqm";
@@ -46,7 +40,7 @@ declare
     %rest:HEAD
     %rest:path("/info")
     %output:method("json")
-function tapi:info-rest()
+function tapi:endpoint-info()
 as item()+ {
     $commons:responseHeader200,
     tapi:info()
@@ -105,7 +99,8 @@ declare
     %rest:path("/content/{$tei-xml-uri}-{$page}.html")
     %output:method("xml")
     %output:indent("no")
-function tapi:content-rest($tei-xml-uri as xs:string, $page as xs:string)
+function tapi:endpoint-html($tei-xml-uri as xs:string,
+    $page as xs:string)
 as item()+ {
     $commons:responseHeader200,
     tapi-html:get-html($tei-xml-uri, $page)
@@ -128,7 +123,7 @@ declare
     %rest:path("/images/{$uri}")
     %rest:produces("image/jpeg")
     %output:method("binary")
-function tapi:images-rest($uri as xs:string)
+function tapi:endpoint-image($uri as xs:string)
 as item()+ {
     $commons:responseHeader200,
     hc:send-request(
@@ -151,20 +146,20 @@ as item()+ {
 declare
     %rest:GET
     %rest:HEAD
-    %rest:path("/content/{$document}.txt")
+    %rest:path("/content/{$document-uri}.txt")
     %rest:query-param("type", "{$type}", "transcription")
     %output:method("text")
-function tapi:text-rest($document as xs:string, $type)
+function tapi:endpoint-txt($document-uri as xs:string,
+    $type)
 as item()+ {
-    let $text := tapi:get-TEI-text($document, $type)
-    let $TEI :=
+    let $pseudo-chunk :=
         element tei:TEI {
-            $text
+            tapi-txt:get-TEI-text($document-uri, $type)
         }
     return
         ( 
             $commons:responseHeader200,
-            tapi-txt:make-plain-text-from-chunk($TEI)
+            tapi-txt:make-plain-text-from-chunk($pseudo-chunk)
         )
 };
 
@@ -179,84 +174,9 @@ declare
     %rest:HEAD
     %rest:path("/content/ahikar-plain-text.zip")
     %output:method("binary")
-function tapi:text-rest() as item()+ {
+function tapi:endpoint-zip() as item()+ {
     let $prepare := tapi-txt:main()
     return
         $commons:responseHeader200,
-        tapi:compress-to-zip()
-};
-
-
-(:~
- : Compressing all manuscripts available to ZIP.
- : 
- : @return the zipped files as xs:base64Binary
- :)
-declare function tapi:compress-to-zip()
-as xs:base64Binary* {
-    compression:zip(xs:anyURI($commons:tg-collection || "/txt/"), false())
-};
-
-
-(:~
- : Returns the tei:text of a document as indicated by the @type parameter.
- : 
- : Due to the structure of the Ahikar project we can pass either an edition or
- : an XML file to the API endpoint for plain text creation.
- : This function determines the correct file which serves as a basis for the plain text.
- : 
- : @param $document The URI of a resource
- : @param $type Indicates the @type of tei:text to be processed
- : @return The tei:text element to be serialized as plain text
- :)
-declare function tapi:get-TEI-text($document as xs:string, $type as xs:string)
-as element(tei:text) {
-    let $format := tapi:get-format($document)
-    return
-        if ($format = "text/xml") then
-            doc($commons:data || $document || ".xml")//tei:text[@type = $type]
-        (: in this case the document is an edition which forces us to pick the
-        text/xml file belonging to it :)
-        else
-            let $xml := tapi:get-tei-file-name-of-edition($document)
-            return
-                 tapi:get-text-of-type($xml, $type)
-};
-
-declare function tapi:get-tei-file-name-of-edition($document as xs:string)
-as xs:string {
-    let $aggregates := tapi:get-edition-aggregates-without-uri-namespace($document)
-    return
-        tapi:find-xml-in-aggregates($aggregates)
-};
-
-declare function tapi:get-edition-aggregates-without-uri-namespace($document as xs:string)
-as xs:string+ {
-    let $edition := doc($commons:agg || $document || ".xml")
-    for $agg in $edition//ore:aggregates/@rdf:resource return
-        replace($agg, "textgrid:", "")
-};
-
-declare function tapi:find-xml-in-aggregates($aggregates as xs:string+)
-as xs:string {
-    for $agg in $aggregates return
-        if (tapi:get-format($agg) = "text/xml") then
-            $agg
-        else
-            ()
-};
-
-declare function tapi:get-text-of-type($uri as xs:string, $type as xs:string)
-as element(tei:text) {
-    doc($commons:data || $uri || ".xml")//tei:text[@type = $type]
-};
-
-(:~
- : Returns the TextGrid metadata type of a resource.
- : 
- : @param $uri The URI of the resource
- : @return The resource's format as tgmd:format
- :)
-declare function tapi:get-format($uri as xs:string) as xs:string {
-    doc($commons:meta || $uri || ".xml")//tgmd:format
+        tapi-txt:compress-to-zip()
 };
diff --git a/exist-app/tests/commons-tests.xqm b/exist-app/tests/commons-tests.xqm
index f33acf42..1fd68394 100644
--- a/exist-app/tests/commons-tests.xqm
+++ b/exist-app/tests/commons-tests.xqm
@@ -29,4 +29,12 @@ declare
                king of Assyria and Nineveh'")
 function ct:get-tei-xml-for-manifest($manifest-uri) {
     commons:get-tei-xml-for-manifest($manifest-uri)
-};
\ No newline at end of file
+};
+
+
+declare
+    %test:args("ahiqar_sample") %test:assertXPath("$result//*[local-name(.) = 'TEI']")
+function ct:open-tei-xml($tei-xml-uri as xs:string)
+as document-node() {
+    commons:open-tei-xml($tei-xml-uri)
+};
diff --git a/exist-app/tests/tapi-tests.xqm b/exist-app/tests/tapi-tests.xqm
index 5afd9713..c45b756e 100644
--- a/exist-app/tests/tapi-tests.xqm
+++ b/exist-app/tests/tapi-tests.xqm
@@ -132,61 +132,6 @@ function tt:content-txt() as xs:string {
 };
 
 
-declare
-    %test:assertExists
-function tt:compress-text() as xs:base64Binary {
-    tapi:compress-to-zip()
-};
-
-
-declare
-    %test:args("ahiqar_sample")
-    %test:assertEquals("text/xml")
-function tt:tgmd-format($uri as xs:string) as xs:string {
-    tapi:get-format($uri)
-};
-
-
-declare
-    %test:args("ahiqar_sample", "transcription")
-    %test:assertXPath("$result[local-name(.) = 'text' and @type = 'transcription']")
-function tt:get-tei($document as xs:string, $type as xs:string) as element() {
-    tapi:get-TEI-text($document, $type)
-};
-
-
-declare
-    %test:assertXPath("$result/string() = '1234 5678'")
-function tt:remove-whitespaces() as document-node() {
-    let $doc := doc("/db/test-records/white-spaces.xml")
-    return
-        tapi:remove-whitespaces($doc)
-};
-
-declare
-    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
-function tt:get-tei-file-name-of-edition($document as xs:string) {
-    tapi:get-tei-file-name-of-edition($document)
-};
-
-declare
-    %test:args("ahiqar_agg") %test:assertEquals("ahiqar_sample")
-function tt:get-edition-aggregates-without-uri-namespace($document as xs:string) {
-    tapi:get-edition-aggregates-without-uri-namespace($document)
-};
-
-declare
-    %test:args("ahiqar_sample") %test:assertEquals("ahiqar_sample")
-function tt:find-xml-in-aggregates($aggregates as xs:string+) {
-    tapi:find-xml-in-aggregates($aggregates)
-};
-
-declare 
-    %test:args("ahiqar_sample", "transliteration") %test:assertXPath("$result[@type = 'transliteration']")
-function tt:get-text-of-type($uri as xs:string, $type as xs:string) {
-    tapi:get-text-of-type($uri, $type)
-};
-
 declare
     %test:assertTrue
 function tt:is-txt-api-available() {
@@ -202,6 +147,15 @@ declare function tt:txt() {
 };
 
 
+declare
+    %test:assertXPath("$result/string() = '1234 5678'")
+function tt:remove-whitespaces() as document-node() {
+    let $doc := doc("/db/test-records/white-spaces.xml")
+    return
+        tapi:remove-whitespaces($doc)
+};
+
+
 (:  
  : *****************
  : * AnnotationAPI * 
diff --git a/exist-app/tests/tapi-txt-tests.xqm b/exist-app/tests/tapi-txt-tests.xqm
index 16d22def..c7be439a 100644
--- a/exist-app/tests/tapi-txt-tests.xqm
+++ b/exist-app/tests/tapi-txt-tests.xqm
@@ -493,3 +493,10 @@ function ttt:is-document-tei-xml($document-uri as xs:string)
 as xs:boolean {
     tapi-txt:is-document-tei-xml($document-uri)
 };
+
+declare
+    %test:assertExists
+function ttt:compress-text()
+as xs:base64Binary {
+    tapi-txt:compress-to-zip()
+};
-- 
GitLab


From 1c3611b5334cf8fa6b8ec47c9a2dd490822d074b Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 09:39:25 +0200
Subject: [PATCH 12/22] refactor: move RESTXQ endpoints to single module,
 remove dead code

---
 exist-app/modules/tapi-collection.xqm     | 26 -------
 exist-app/modules/tapi-item.xqm           | 39 ----------
 exist-app/modules/tapi-manifest.xqm       | 27 -------
 exist-app/modules/tapi.xqm                | 64 +++++++++++++++++
 exist-app/tests/tapi-collection-tests.xqm | 35 ++-------
 exist-app/tests/tapi-item-tests.xqm       | 29 --------
 exist-app/tests/tapi-manifest-tests.xqm   | 39 +---------
 exist-app/tests/tapi-tests.xqm            | 87 +++++++++++++++++++++++
 8 files changed, 158 insertions(+), 188 deletions(-)

diff --git a/exist-app/modules/tapi-collection.xqm b/exist-app/modules/tapi-collection.xqm
index 08c40b87..e372b8ec 100644
--- a/exist-app/modules/tapi-collection.xqm
+++ b/exist-app/modules/tapi-collection.xqm
@@ -9,36 +9,10 @@ xquery version "3.1";
 module namespace tapi-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection";
 
 declare namespace ore="http://www.openarchives.org/ore/terms/";
-declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
 declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
-import module namespace requestr="http://exquery.org/ns/request";
-import module namespace rest="http://exquery.org/ns/restxq";
-
-declare variable $tapi-coll:server := 
-    if(requestr:hostname() = "existdb") then
-        $commons:expath-pkg/*/@name => replace("/$", "")
-    else
-        "http://localhost:8094/exist/restxq";
-
-(:~
- : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#collection
- : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#collection-object
- : @param $collection-uri The unprefixed TextGrid URI of a collection, e.g. '3r132'
- : @return A collection object as JSON
- :)
-declare
-    %rest:GET
-    %rest:HEAD
-    %rest:path("/textapi/ahikar/{$collection-uri}/collection.json")
-    %output:method("json")
-function tapi-coll:endpoint($collection-uri as xs:string)
-as item()+ {
-    $commons:responseHeader200,
-    tapi-coll:get-json($collection-uri, $tapi-coll:server)
-};
 
 (:~
  : Returns information about the main collection for the project. This encompasses
diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index c8f131c1..1493eeb8 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -8,48 +8,9 @@ xquery version "3.1";
 
 module namespace tapi-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item";
 
-declare namespace ore="http://www.openarchives.org/ore/terms/";
-declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
-declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
-declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
-import module namespace requestr="http://exquery.org/ns/request";
-import module namespace rest="http://exquery.org/ns/restxq";
-
-declare variable $tapi-item:server := 
-    if(requestr:hostname() = "existdb") then
-        $commons:expath-pkg/*/@name => replace("/$", "")
-    else
-        "http://localhost:8094/exist/restxq";
-
-(:~
- : Returns information about a given page in a document. This is mainly compliant
- : with the SUB TextAPI, but has the following additions:
- :  * the division number, 'n', is mandatory
- :  * 'image' is mandatory since every page has a facsimile
- :
- : Sample call to API: /api/textapi/ahikar/3r17c/3r1pq-147a/latest/item.json
- :
- : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#item
- : @param $collection-uri The unprefixed TextGrid URI of a collection, e.g. '3r17c'
- : @param $manifest-uri The unprefixed TextGrid URI of a document, e.g. '3r1pq'
- : @param $page A page number as encoded in a tei:pb/@n, e.g. '147a'
- : @return Information about a page
- :)
-declare
-    %rest:GET
-    %rest:HEAD
-    %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}-{$page}/latest/item.json")
-    %output:method("json")
-function tapi-item:endpoint($collection-uri as xs:string,
-    $manifest-uri as xs:string,
-    $page as xs:string)
-as item()+ {
-    $commons:responseHeader200,
-    tapi-item:get-json($collection-uri, $manifest-uri, $page, $tapi-item:server)
-};
 
 
 declare function tapi-item:get-json($collection-uri as xs:string,
diff --git a/exist-app/modules/tapi-manifest.xqm b/exist-app/modules/tapi-manifest.xqm
index f274050e..6a3205af 100644
--- a/exist-app/modules/tapi-manifest.xqm
+++ b/exist-app/modules/tapi-manifest.xqm
@@ -8,37 +8,10 @@ xquery version "3.1";
 
 module namespace tapi-mani="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest";
 
-declare namespace ore="http://www.openarchives.org/ore/terms/";
-declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
-declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 declare namespace tei="http://www.tei-c.org/ns/1.0";
 declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
-import module namespace requestr="http://exquery.org/ns/request";
-import module namespace rest="http://exquery.org/ns/restxq";
-
-declare variable $tapi-mani:server := 
-    if(requestr:hostname() = "existdb") then
-        $commons:expath-pkg/*/@name => replace("/$", "")
-    else
-        "http://localhost:8094/exist/restxq";
-        
-
-(:~
- : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#manifest
- :)
-declare
-    %rest:GET
-    %rest:HEAD
-    %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}/manifest.json")
-    %output:method("json")
-function tapi-mani:endpoint($collection-uri as xs:string,
-    $manifest-uri as xs:string)
-as item()+ {
-    $commons:responseHeader200,
-    tapi-mani:get-json($collection-uri, $manifest-uri, $tapi-mani:server)
-};
 
 
 declare function tapi-mani:get-json($collection-uri as xs:string,
diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index 5c3f0745..e0d3aa5e 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -18,6 +18,9 @@ declare namespace tei="http://www.tei-c.org/ns/1.0";
 declare namespace test="http://exist-db.org/xquery/xqsuite";
 declare namespace xhtml="http://www.w3.org/1999/xhtml";
 
+import module namespace tapi-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection" at "tapi-collection.xqm";
+import module namespace tapi-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item" at "tapi-item.xqm";
+import module namespace tapi-mani="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest" at "tapi-manifest.xqm";
 import module namespace tapi-txt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt" at "tapi-txt.xqm";
 import module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons" at "commons.xqm";
 import module namespace requestr="http://exquery.org/ns/request";
@@ -80,6 +83,67 @@ declare function tapi:remove-whitespaces($doc as document-node()) as document-no
 };
 
 
+(:~
+ : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#collection
+ : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#collection-object
+ : @param $collection-uri The unprefixed TextGrid URI of a collection, e.g. '3r132'
+ : @return A collection object as JSON
+ :)
+declare
+    %rest:GET
+    %rest:HEAD
+    %rest:path("/textapi/ahikar/{$collection-uri}/collection.json")
+    %output:method("json")
+function tapi:endpoint-collection($collection-uri as xs:string)
+as item()+ {
+    $commons:responseHeader200,
+    tapi-coll:get-json($collection-uri, $tapi:server)
+};
+
+
+(:~
+ : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#manifest
+ :)
+declare
+    %rest:GET
+    %rest:HEAD
+    %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}/manifest.json")
+    %output:method("json")
+function tapi:endpoint-manifest($collection-uri as xs:string,
+    $manifest-uri as xs:string)
+as item()+ {
+    $commons:responseHeader200,
+    tapi-mani:get-json($collection-uri, $manifest-uri, $tapi:server)
+};
+
+
+(:~
+ : Returns information about a given page in a document. This is mainly compliant
+ : with the SUB TextAPI, but has the following additions:
+ :  * the division number, 'n', is mandatory
+ :  * 'image' is mandatory since every page has a facsimile
+ :
+ : Sample call to API: /api/textapi/ahikar/3r17c/3r1pq-147a/latest/item.json
+ :
+ : @see https://subugoe.pages.gwdg.de/emo/text-api/page/specs/#item
+ : @param $collection-uri The unprefixed TextGrid URI of a collection, e.g. '3r17c'
+ : @param $manifest-uri The unprefixed TextGrid URI of a document, e.g. '3r1pq'
+ : @param $page A page number as encoded in a tei:pb/@n, e.g. '147a'
+ : @return Information about a page
+ :)
+declare
+    %rest:GET
+    %rest:HEAD
+    %rest:path("/textapi/ahikar/{$collection-uri}/{$manifest-uri}-{$page}/latest/item.json")
+    %output:method("json")
+function tapi:endpoint-item($collection-uri as xs:string,
+    $manifest-uri as xs:string,
+    $page as xs:string)
+as item()+ {
+    $commons:responseHeader200,
+    tapi-item:get-json($collection-uri, $manifest-uri, $page, $tapi:server)
+};
+
 (:~
  : Returns an HTML rendering of a given page.
  : 
diff --git a/exist-app/tests/tapi-collection-tests.xqm b/exist-app/tests/tapi-collection-tests.xqm
index 9023e813..594e835a 100644
--- a/exist-app/tests/tapi-collection-tests.xqm
+++ b/exist-app/tests/tapi-collection-tests.xqm
@@ -11,7 +11,6 @@ import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace tapi-coll="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection" at "../modules/tapi-collection.xqm";
 
-declare variable $tct:restxq := "http://0.0.0.0:8080/exist/restxq";
 declare variable $tct:collection-uri := "testapi-collection.xml";
 declare variable $tct:agg1-uri := "test-aggregation-1.xml";
 declare variable $tct:agg2-uri := "test-aggregation-2.xml";
@@ -103,14 +102,6 @@ function tct:_test-teardown() {
     xmldb:remove($commons:meta, $tct:agg2-uri)
 };
 
-declare
-    %test:assertTrue
-function tct:is-endpoint-available() {
-    let $url := $tct:restxq || "/textapi/ahikar/ahiqar_collection/collection.json"
-    return
-        tc:is-endpoint-http200($url)
-};
-
 declare
     %test:args("ahiqar_collection") %test:assertXPath("$result//*[local-name(.) = 'aggregates']")
 function tct:get-aggregation($uri as xs:string) {
@@ -154,7 +145,7 @@ declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json")
 function tct:make-id($colletion-uri as xs:string, $manifest-uri as xs:string)
 as xs:string {
-    tapi-coll:make-id($tct:restxq, $colletion-uri, $manifest-uri)
+    tapi-coll:make-id($tc:server, $colletion-uri, $manifest-uri)
 };
 
 
@@ -203,14 +194,14 @@ declare
     %test:args("testapi-collection") %test:assertXPath("$result//id[matches(., 'test-aggregation-1/manifest.json')]")
     %test:args("testapi-collection") %test:assertXPath("$result//id[matches(., 'test-aggregation-2/manifest.json')]")
 function tct:make-sequence($collection-uri as xs:string) {
-    tapi-coll:make-sequence($collection-uri, $tct:restxq)
+    tapi-coll:make-sequence($collection-uri, $tc:server)
 };
 
 declare
     %test:args("ahiqar_collection") %test:assertEquals("http://0.0.0.0:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/annotationCollection.json")
 function tct:make-annotationCollection-uri($collection-uri as xs:string)
 as xs:string {
-    tapi-coll:make-annotationCollection-uri($tct:restxq, $collection-uri)
+    tapi-coll:make-annotationCollection-uri($tc:server, $collection-uri)
 };
 
 
@@ -218,23 +209,5 @@ declare
     %test:args("ahiqar_collection") %test:assertXPath("$result//title = 'The Story and Proverbs of Ahikar the Wise'")
     %test:args("ahiqar_collection") %test:assertXPath("$result//*/string() = 'http://0.0.0.0:8080/exist/restxq/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json' ")
 function tct:get-json($collection-uri as xs:string) {
-    tapi-coll:get-json($collection-uri, $tct:restxq)
-};
-
-
-declare
-    (: check if all parts are present.
-     : no further tests are needed since the content has been tested while testing
-     : the underlying function. :)
-    %test:assertXPath("map:contains($result, 'title')")
-    %test:assertXPath("map:contains($result, 'collector')")
-    %test:assertXPath("map:contains($result, 'description')")
-    %test:assertXPath("map:contains($result, 'sequence')")
-function tct:endpoint()
-as item() {
-    let $url := $tct:restxq || "/textapi/ahikar/ahiqar_collection/collection.json"
-    let $req := <http:request href="{$url}" method="get">
-                        <http:header name="Connection" value="close"/>
-                   </http:request>
-    return http:send-request($req)[2] => util:base64-decode() => parse-json()
+    tapi-coll:get-json($collection-uri, $tc:server)
 };
diff --git a/exist-app/tests/tapi-item-tests.xqm b/exist-app/tests/tapi-item-tests.xqm
index bf4ffb3c..cba63492 100644
--- a/exist-app/tests/tapi-item-tests.xqm
+++ b/exist-app/tests/tapi-item-tests.xqm
@@ -12,14 +12,6 @@ import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:o
 import module namespace tapi-item="http://ahikar.sub.uni-goettingen.de/ns/tapi/item" at "../modules/tapi-item.xqm";
 
 
-declare
-    %test:assertTrue
-function titemt:is-endpoint-200() {
-    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
-    return
-        tc:is-endpoint-http200($url)
-};
-
 declare
     %test:args("ahiqar_agg", "82a") %test:assertEquals("3r1nz")
 function titemt:get-facsimile-uri-for-page($manifest-uri as xs:string,
@@ -52,27 +44,6 @@ as xs:string {
 };
 
 
-declare
-    (: check if all parts are present.
-     : no further tests are needed since the content has been tested while testing
-     : the underlying function. :)
-    %test:assertXPath("map:contains($result, 'textapi')")
-    %test:assertXPath("map:contains($result, 'title')")
-    %test:assertXPath("map:contains($result, 'type')")
-    %test:assertXPath("map:contains($result, 'n')")
-    %test:assertXPath("map:contains($result, 'content')")
-    %test:assertXPath("map:contains($result, 'content-type')")
-    %test:assertXPath("map:contains($result, 'lang')")
-    %test:assertXPath("map:contains($result, 'langAlt')")
-    %test:assertXPath("map:contains($result, 'image')")
-function titemt:is-data-complete() as item() {
-    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
-    let $req := tc:make-request($url)
-    return http:send-request($req)[2]
-        => util:base64-decode()
-        => parse-json()
-};
-
 declare
     %test:args("ahiqar_collection", "ahiqar_agg", "82a")
     (: checks if the correct file has been opened :)
diff --git a/exist-app/tests/tapi-manifest-tests.xqm b/exist-app/tests/tapi-manifest-tests.xqm
index 6891585f..946bb722 100644
--- a/exist-app/tests/tapi-manifest-tests.xqm
+++ b/exist-app/tests/tapi-manifest-tests.xqm
@@ -11,7 +11,6 @@ import module namespace tc="http://ahikar.sub.uni-goettingen.de/ns/tests/commons
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
 import module namespace tapi-mani="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest" at "../modules/tapi-manifest.xqm";
 
-declare variable $tmt:restxq := "http://0.0.0.0:8080/exist/restxq";
 declare variable $tmt:manifest1 := "test-manifest1.xml";
 declare variable $tmt:manifest2 := "test-manifest2.xml";
 declare variable $tmt:manifest3 := "test-manifest3.xml";
@@ -130,14 +129,6 @@ function tmt:_test-teardown() {
     xmldb:remove($commons:data, $tmt:tei3)
 };
 
-declare
-    %test:assertTrue
-function tmt:is-endpoint-200() {
-    let $url := $tmt:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
-    return
-        tc:is-endpoint-http200($url)
-};
-
 declare
     %test:args("ahiqar_agg") %test:assertXPath("$result//* = 'textgrid:ahiqar_agg.0'")
 function tmt:get-metadata-file($manifest-uri) {
@@ -149,7 +140,7 @@ declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json')]")
 function tmt:make-sequences($collection-uri as xs:string,
     $manifest-uri as xs:string) {
-    tapi-mani:make-sequences($collection-uri, $manifest-uri, $tmt:restxq)
+    tapi-mani:make-sequences($collection-uri, $manifest-uri, $tc:server)
 };
 
 declare
@@ -163,7 +154,7 @@ declare
     %test:args("ahiqar_collection", "ahiqar_agg") %test:assertXPath("$result//id[matches(., '/api/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json')]")
 function tmt:get-json($collection-uri as xs:string,
     $manifest-uri as xs:string) {
-    tapi-mani:get-json($collection-uri, $manifest-uri, $tmt:restxq)
+    tapi-mani:get-json($collection-uri, $manifest-uri, $tc:server)
 };
 
 declare
@@ -210,30 +201,6 @@ declare
      %test:args("ahiqar_collection", "ahiqar_agg") %test:assertExists
 function tmt:get-json($collection-uri as xs:string,
     $manifest-uri as xs:string) {
-    tapi-mani:get-json($collection-uri, $manifest-uri, $tmt:restxq)
+    tapi-mani:get-json($collection-uri, $manifest-uri, $tc:server)
 };
 
-
-declare
-    (: check if all parts are present.
-     : no further tests are needed since the content has been tested while testing
-     : the underlying function. :)
-    %test:assertXPath("map:contains($result, 'textapi')")
-    %test:assertXPath("map:contains($result, 'id')")
-    %test:assertXPath("map:contains($result, 'label')")
-    %test:assertXPath("map:contains($result, 'x-editor')")
-    %test:assertXPath("map:contains($result, 'x-date')")
-    %test:assertXPath("map:contains($result, 'x-origin')")
-    %test:assertXPath("map:contains($result, 'x-location')")
-    %test:assertXPath("map:contains($result, 'license')")
-    %test:assertXPath("map:contains($result, 'annotationCollection')")
-    %test:assertXPath("map:contains($result, 'sequence')")
-function tmt:endpoint()
-as item() {
-    let $url := $tmt:restxq || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
-    let $req := tc:make-request($url)
-    return
-        http:send-request($req)[2]
-        => util:base64-decode()
-        => parse-json()
-};
diff --git a/exist-app/tests/tapi-tests.xqm b/exist-app/tests/tapi-tests.xqm
index c45b756e..646cb6ff 100644
--- a/exist-app/tests/tapi-tests.xqm
+++ b/exist-app/tests/tapi-tests.xqm
@@ -155,6 +155,93 @@ function tt:remove-whitespaces() as document-node() {
         tapi:remove-whitespaces($doc)
 };
 
+declare
+    %test:assertTrue
+function tt:is-collection-endpoint-http200() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/collection.json"
+    return
+        tc:is-endpoint-http200($url)
+};
+
+
+declare
+    (: check if all parts are present.
+     : no further tests are needed since the content has been tested while testing
+     : the underlying function. :)
+    %test:assertXPath("map:contains($result, 'title')")
+    %test:assertXPath("map:contains($result, 'collector')")
+    %test:assertXPath("map:contains($result, 'description')")
+    %test:assertXPath("map:contains($result, 'sequence')")
+function tt:endpoint-collection()
+as item() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/collection.json"
+    let $req := <http:request href="{$url}" method="get">
+                        <http:header name="Connection" value="close"/>
+                   </http:request>
+    return http:send-request($req)[2] => util:base64-decode() => parse-json()
+};
+
+declare
+    %test:assertTrue
+function tt:is-manifest-endpoint-http200() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
+    return
+        tc:is-endpoint-http200($url)
+};
+
+declare
+    (: check if all parts are present.
+     : no further tests are needed since the content has been tested while testing
+     : the underlying function. :)
+    %test:assertXPath("map:contains($result, 'textapi')")
+    %test:assertXPath("map:contains($result, 'id')")
+    %test:assertXPath("map:contains($result, 'label')")
+    %test:assertXPath("map:contains($result, 'x-editor')")
+    %test:assertXPath("map:contains($result, 'x-date')")
+    %test:assertXPath("map:contains($result, 'x-origin')")
+    %test:assertXPath("map:contains($result, 'x-location')")
+    %test:assertXPath("map:contains($result, 'license')")
+    %test:assertXPath("map:contains($result, 'annotationCollection')")
+    %test:assertXPath("map:contains($result, 'sequence')")
+function tt:endpoint-manifest()
+as item() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg/manifest.json"
+    let $req := tc:make-request($url)
+    return
+        http:send-request($req)[2]
+        => util:base64-decode()
+        => parse-json()
+};
+
+declare
+    %test:assertTrue
+function tt:is-item-endpoint-http200() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
+    return
+        tc:is-endpoint-http200($url)
+};
+
+declare
+    (: check if all parts are present.
+     : no further tests are needed since the content has been tested while testing
+     : the underlying function. :)
+    %test:assertXPath("map:contains($result, 'textapi')")
+    %test:assertXPath("map:contains($result, 'title')")
+    %test:assertXPath("map:contains($result, 'type')")
+    %test:assertXPath("map:contains($result, 'n')")
+    %test:assertXPath("map:contains($result, 'content')")
+    %test:assertXPath("map:contains($result, 'content-type')")
+    %test:assertXPath("map:contains($result, 'lang')")
+    %test:assertXPath("map:contains($result, 'langAlt')")
+    %test:assertXPath("map:contains($result, 'image')")
+function tt:endpoint-item() as item() {
+    let $url := $tc:server || "/textapi/ahikar/ahiqar_collection/ahiqar_agg-82a/latest/item.json"
+    let $req := tc:make-request($url)
+    return http:send-request($req)[2]
+        => util:base64-decode()
+        => parse-json()
+};
+
 
 (:  
  : *****************
-- 
GitLab


From 8eb94690abbaa83fcd02144a7e4bb5a2cb8646c4 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 09:43:49 +0200
Subject: [PATCH 13/22] docs: bump version number, update CHANGELOG

---
 CHANGELOG.md               | 8 ++++++++
 exist-app/build.properties | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e148e46c..e2f30678 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [1.11.0] - 2020-09-22
+
+### Changed
+
+- In order to improve the clearity of the application, `tapi.xqm` now only holds the RESTXQ endpoints of the TextAPI.
+All further functionality has been moved to separate module and furnished with tests.
+- The test runner has been designed to be self-reporting, i.e. only faulty results are displayed fully.
+
 ## [1.10.0] - 2020-09-18
 
 ### Added
diff --git a/exist-app/build.properties b/exist-app/build.properties
index e6276c19..1daf5a4b 100644
--- a/exist-app/build.properties
+++ b/exist-app/build.properties
@@ -1,5 +1,5 @@
 project.name=https://ahikar-test.sub.uni-goettingen.de/
-project.version=1.10.0
+project.version=1.11.0
 project.title=TextAPI for Ahikar
 project.abbrev=ahikar-test
 project.processorversion=5.2.0
-- 
GitLab


From 2ea5b421879b651be218d5efabaca082c9210967 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 10:28:34 +0200
Subject: [PATCH 14/22] fix: update trigger API

---
 exist-app/modules/testtrigger.xqm | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/exist-app/modules/testtrigger.xqm b/exist-app/modules/testtrigger.xqm
index afaa51df..d1004775 100644
--- a/exist-app/modules/testtrigger.xqm
+++ b/exist-app/modules/testtrigger.xqm
@@ -6,7 +6,6 @@ xquery version "3.1";
  : since at this point the RESTXQ API isn't fired up yet which causes the tests to throw errors.
  : 
  : @author Michelle Weidling
- : @version 0.1.0
  : @since 0.4.0
  :)
 
@@ -14,7 +13,14 @@ module namespace testtrigger="http://ahikar.sub.uni-goettingen.de/ns/testtrigger
 
 import module namespace rest="http://exquery.org/ns/restxq";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
-import module namespace tests="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "../tests.xqm";
+
+import module namespace ttt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests" at "../tests/tapi-txt-tests.xqm";
+import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "../tests/commons-tests.xqm";
+import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "../tests/tapi-collection-tests.xqm";
+import module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests" at "../tests/tapi-html-tests.xqm";
+import module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "../tests/tapi-item-tests.xqm";
+import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "../tests/tapi-manifest-tests.xqm";
+import module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "../tests/tapi-tests.xqm";
 
 (:~
  : Triggers the tests for the Ahikar backend. Called by the CI.
@@ -33,7 +39,16 @@ as item()? {
     then error(QName("error://1", "deploy"), "Deploy token incorrect.")
   else
     let $sysout := util:log-system-out("TextAPI and package installation done. running tests…")
-    let $tests := test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests"))
+    let $tests :=
+        (
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/tests")),
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests")),
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/commons-tests")),
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests")),
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests")),
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests")),
+            test:suite(util:list-functions("http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests"))
+        )
     
     let $fileSeparator := util:system-property("file.separator")
     let $system-path := system:get-exist-home() || $fileSeparator
-- 
GitLab


From 66969d79d24eaf063cade863a985b3d3f0db1728 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 11:10:55 +0200
Subject: [PATCH 15/22] ci: extend test result inspection

---
 .gitignore                           |  1 +
 .gitlab-ci.yml                       |  8 +++-----
 get-unit-test-failures-and-errors.sh | 13 +++++++++++++
 3 files changed, 17 insertions(+), 5 deletions(-)
 create mode 100755 get-unit-test-failures-and-errors.sh

diff --git a/.gitignore b/.gitignore
index 72a43116..22b4373e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,6 +15,7 @@ docker/frontend
 
 # exclude local scripts
 *.sh
+!get-unit-test-failures-and-errors.sh
 
 # exlude data for commitizen
 node_modules
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f488f9df..15cf117a 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -43,11 +43,9 @@ test_exist_app:
     - curl http://localhost:8080/exist/restxq/trigger-tests
     - sleep 60
     - bash exist-app/test/bin/shutdown.sh
-    - failures=$(xmllint --xpath '/tests/testsuites/testsuite/@failures' exist-app/test/test-results.xml | egrep -o "[0-9]+")
-    - echo "There is/are currently $failures failures."
-    # one test fails on purpose to ensure the test suite is running correctly.
-    # thus we always have 1 failing test which indicates that everything is fine.
-    - if [[ "$failures" -gt 1 ]]; then exit 1; else exit 0; fi
+    - failures=$(./get-unit-test-failures-and-errors.sh)
+    - echo -e "\033[1;33mThere is/are currently $failures failures or errors.\033[0m"
+    - if [[ "$failures" -gt 0 ]]; then exit 1; else exit 0; fi
   artifacts:
     paths:
       - exist-app/test/test-results.xml
diff --git a/get-unit-test-failures-and-errors.sh b/get-unit-test-failures-and-errors.sh
new file mode 100755
index 00000000..187d793a
--- /dev/null
+++ b/get-unit-test-failures-and-errors.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+failures=$(xmllint --xpath '/tests/testsuites/testsuite/@failures' exist-app/local/test-results.xml | egrep -o "[0-9]+")
+errors=$(xmllint --xpath '/tests/testsuites/testsuite/@errors' exist-app/local/test-results.xml | egrep -o "[0-9]+")
+
+problem_sum=0
+
+for FAILURE in $failures $errors
+do
+    let problem_sum+=$FAILURE
+done
+
+echo $problem_sum
-- 
GitLab


From cb27da779d0f54a4c77ab7a4f91aa38d6265355c Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 11:29:35 +0200
Subject: [PATCH 16/22] fix: fix error in path

---
 .gitignore                           | 1 +
 get-unit-test-failures-and-errors.sh | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index 22b4373e..eb0c115b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
 exist-app/build/**
 exist-app/expath-pkg.xml
 exist-app/**/*.xar
+eXist-app/test/*
 
 # env with secrets
 **/ahikar.env
diff --git a/get-unit-test-failures-and-errors.sh b/get-unit-test-failures-and-errors.sh
index 187d793a..ee76a04b 100755
--- a/get-unit-test-failures-and-errors.sh
+++ b/get-unit-test-failures-and-errors.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 
-failures=$(xmllint --xpath '/tests/testsuites/testsuite/@failures' exist-app/local/test-results.xml | egrep -o "[0-9]+")
-errors=$(xmllint --xpath '/tests/testsuites/testsuite/@errors' exist-app/local/test-results.xml | egrep -o "[0-9]+")
+failures=$(xmllint --xpath '/tests/testsuites/testsuite/@failures' exist-app/test/test-results.xml | egrep -o "[0-9]+")
+errors=$(xmllint --xpath '/tests/testsuites/testsuite/@errors' exist-app/test/test-results.xml | egrep -o "[0-9]+")
 
 problem_sum=0
 
-- 
GitLab


From 916b77e3c57189173a97c70dc27d47387494103d Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 11:42:40 +0200
Subject: [PATCH 17/22] test: fix dependency between tests

---
 .gitignore                         |  1 -
 exist-app/tests/tapi-txt-tests.xqm | 21 ++++++++++-----------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index eb0c115b..22b4373e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,6 @@
 exist-app/build/**
 exist-app/expath-pkg.xml
 exist-app/**/*.xar
-eXist-app/test/*
 
 # env with secrets
 **/ahikar.env
diff --git a/exist-app/tests/tapi-txt-tests.xqm b/exist-app/tests/tapi-txt-tests.xqm
index c7be439a..eb82e337 100644
--- a/exist-app/tests/tapi-txt-tests.xqm
+++ b/exist-app/tests/tapi-txt-tests.xqm
@@ -11,6 +11,14 @@ declare variable $ttt:sample-file := local:open-file("ahiqar_sample");
 declare variable $ttt:sample-transliteration := $ttt:sample-file//tei:text[@type = "transliteration"];
 declare variable $ttt:sample-transcription := $ttt:sample-file//tei:text[@type = "transcription"];
 
+declare
+    %test:setUp
+function ttt:_test-setup()
+as xs:string+ {
+    tapi-txt:main()
+};
+
+
 declare
     %test:args("ahiqar_sample") %test:assertExists
     %test:args("1234") %test:assertError("org.exist.xquery.XPathException")
@@ -434,23 +442,14 @@ as element() {
 };
 
 declare
-    %test:args("ahiqar_sample")
-    %test:assertEquals("text/xml")
+    %test:args("ahiqar_sample") %test:assertEquals("text/xml")
+    %test:args("ahiqar_agg") %test:assertEquals("text/tg.edition+tg.aggregation+xml")
 function ttt:tgmd-format($uri as xs:string)
 as xs:string {
     tapi-txt:get-format($uri)
 };
 
 
-declare
-    %test:args("ahiqar_sample")
-    %test:assertEquals("text/xml")
-function ttt:tgmd-format($uri as xs:string) as xs:string {
-    tapi-txt:get-format($uri)
-};
-
-
-
 declare
     %test:args("ahiqar_sample", "transcription")
     %test:assertXPath("$result[local-name(.) = 'text' and @type = 'transcription']")
-- 
GitLab


From 289d6c1754fd72ef61ff1c957f8548715c9b3346 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Tue, 22 Sep 2020 15:28:29 +0200
Subject: [PATCH 18/22] test: fix wrong uris

---
 exist-app/tests/tapi-manifest-tests.xqm | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/exist-app/tests/tapi-manifest-tests.xqm b/exist-app/tests/tapi-manifest-tests.xqm
index 946bb722..1142ebff 100644
--- a/exist-app/tests/tapi-manifest-tests.xqm
+++ b/exist-app/tests/tapi-manifest-tests.xqm
@@ -124,9 +124,9 @@ function tmt:_test-teardown() {
     xmldb:remove($commons:agg, $tmt:manifest1),
     xmldb:remove($commons:agg, $tmt:manifest2),
     xmldb:remove($commons:agg, $tmt:manifest3),
-    xmldb:remove($commons:data, $tmt:tei1),
-    xmldb:remove($commons:data, $tmt:tei2),
-    xmldb:remove($commons:data, $tmt:tei3)
+    xmldb:remove($commons:data, $tmt:tei1-uri),
+    xmldb:remove($commons:data, $tmt:tei2-uri),
+    xmldb:remove($commons:data, $tmt:tei3-uri)
 };
 
 declare
-- 
GitLab


From c6efd05d02aada33bd9e2a78162cd9e8a78b9b23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathias=20G=C3=B6bel?= <goebel@sub.uni-goettingen.de>
Date: Thu, 8 Oct 2020 12:13:38 +0200
Subject: [PATCH 19/22] add note on dba access for tests

---
 exist-app/tests-runner.xq | 1 +
 1 file changed, 1 insertion(+)

diff --git a/exist-app/tests-runner.xq b/exist-app/tests-runner.xq
index 2b150985..d559f434 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests-runner.xq
@@ -2,6 +2,7 @@ xquery version "3.1";
 (:~
  : Script providing access to the test functions (XQSuite) for local unit test
  : execution.
+ : Elevated rights (dba/admin) are required for some tests.
  :)
 
 import module namespace ttt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests" at "tests/tapi-txt-tests.xqm";
-- 
GitLab


From c51954468c755d9b1e953127dadd83bed8a69f85 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathias=20G=C3=B6bel?= <goebel@sub.uni-goettingen.de>
Date: Thu, 8 Oct 2020 12:13:52 +0200
Subject: [PATCH 20/22] removed unused namespaces

---
 exist-app/modules/commons.xqm | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/exist-app/modules/commons.xqm b/exist-app/modules/commons.xqm
index f03f8300..77c61d90 100644
--- a/exist-app/modules/commons.xqm
+++ b/exist-app/modules/commons.xqm
@@ -3,10 +3,7 @@ xquery version "3.1";
 module namespace commons="http://ahikar.sub.uni-goettingen.de/ns/commons";
 
 declare namespace ore="http://www.openarchives.org/ore/terms/";
-declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
 declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
-declare namespace tei="http://www.tei-c.org/ns/1.0";
-declare namespace tgmd="http://textgrid.info/namespaces/metadata/core/2010";
 
 declare variable $commons:expath-pkg := doc("../expath-pkg.xml");
 declare variable $commons:version := $commons:expath-pkg/*/@version;
-- 
GitLab


From b08413a7d0fb56725b56a166d7653ce07642b589 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathias=20G=C3=B6bel?= <goebel@sub.uni-goettingen.de>
Date: Thu, 8 Oct 2020 12:33:06 +0200
Subject: [PATCH 21/22] correct module description

---
 exist-app/modules/annotations.xqm | 2 +-
 exist-app/modules/tapi-html.xqm   | 4 ++++
 exist-app/modules/tapi-item.xqm   | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/exist-app/modules/annotations.xqm b/exist-app/modules/annotations.xqm
index 5dbc3c3c..e10bcc44 100644
--- a/exist-app/modules/annotations.xqm
+++ b/exist-app/modules/annotations.xqm
@@ -1,7 +1,7 @@
 xquery version "3.1";
 
 (:~
- : This module provides the TextAPI for Ahikar.
+ : This module provides the Annotation Layer via TextAPI for Ahikar.
  :
  : @author Michelle Weidling
  : @version 1.8.1
diff --git a/exist-app/modules/tapi-html.xqm b/exist-app/modules/tapi-html.xqm
index 6c896991..4fd7d05a 100644
--- a/exist-app/modules/tapi-html.xqm
+++ b/exist-app/modules/tapi-html.xqm
@@ -1,4 +1,8 @@
 xquery version "3.1";
+(: 
+ : This module is for preparing the HTML serialization of a
+ : given TEI document or fragment.
+ :)
 
 module namespace tapi-html="http://ahikar.sub.uni-goettingen.de/ns/tapi/html";
 
diff --git a/exist-app/modules/tapi-item.xqm b/exist-app/modules/tapi-item.xqm
index 1493eeb8..f38fb7e7 100644
--- a/exist-app/modules/tapi-item.xqm
+++ b/exist-app/modules/tapi-item.xqm
@@ -1,7 +1,7 @@
 xquery version "3.1";
 
 (: 
- : This module handles calls to the API on manifest level, e.g.
+ : This module handles calls to the API on item level, e.g.
  : 
  : /textapi/ahikar/3r9ps/3rx15-8a/latest/item.json
  :)
-- 
GitLab


From ca740ae4f2c9b842be82b4e6d8a58bfef63a321e Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Thu, 8 Oct 2020 15:53:00 +0200
Subject: [PATCH 22/22] tests:move runner into test collection

---
 exist-app/{ => tests}/tests-runner.xq | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
 rename exist-app/{ => tests}/tests-runner.xq (81%)

diff --git a/exist-app/tests-runner.xq b/exist-app/tests/tests-runner.xq
similarity index 81%
rename from exist-app/tests-runner.xq
rename to exist-app/tests/tests-runner.xq
index d559f434..0fb6c1e7 100644
--- a/exist-app/tests-runner.xq
+++ b/exist-app/tests/tests-runner.xq
@@ -5,14 +5,14 @@ xquery version "3.1";
  : Elevated rights (dba/admin) are required for some tests.
  :)
 
-import module namespace ttt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests" at "tests/tapi-txt-tests.xqm";
-import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "tests/commons-tests.xqm";
-import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tests/tapi-collection-tests.xqm";
+import module namespace ttt="http://ahikar.sub.uni-goettingen.de/ns/tapi/txt/tests" at "tapi-txt-tests.xqm";
+import module namespace ct="http://ahikar.sub.uni-goettingen.de/ns/commons-tests" at "commons-tests.xqm";
+import module namespace tct="http://ahikar.sub.uni-goettingen.de/ns/tapi/collection/tests" at "tapi-collection-tests.xqm";
 import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";
-import module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests" at "tests/tapi-html-tests.xqm";
-import module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tests/tapi-item-tests.xqm";
-import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tests/tapi-manifest-tests.xqm";
-import module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tests/tapi-tests.xqm";
+import module namespace thtmlt="http://ahikar.sub.uni-goettingen.de/ns/tapi/html/tests" at "tapi-html-tests.xqm";
+import module namespace titemt="http://ahikar.sub.uni-goettingen.de/ns/tapi/item/tests" at "tapi-item-tests.xqm";
+import module namespace tmt="http://ahikar.sub.uni-goettingen.de/ns/tapi/manifest/tests" at "tapi-manifest-tests.xqm";
+import module namespace tt="http://ahikar.sub.uni-goettingen.de/ns/tapi/tests" at "tapi-tests.xqm";
 
 
 let $test-results :=
-- 
GitLab