(in-package #:topic-maps.xtm.tests)
(defun orr-current-p (map)
(and (ik-current-p map)
(every (lambda (o) (let ((r (item-reifier o)))
(or (null r) (and (eq r (canonical-topic r)) (eq map (topic-parent r)) (eq o (topic-reified r)))))) (all-occurrences map))))
(defun orr-model (map)
(let ((topics (topic-map-topics map)))
(mm-fields "existingModel" (ik-model map) "canonicalOccurrenceReifierInverseEQ" (if (orr-current-p map) :true :false)
"occurrenceReifiers" (coerce (loop for o in (all-occurrences map) collect
(mm-fields "itemIdentifiers" (coerce (item-identifiers o) 'vector)
"reifierTopic" (when (item-reifier o) (1+ (position (item-reifier o) topics :test #'eq)))
"reifierItemIdentifiers" (coerce (when (item-reifier o) (item-identifiers (item-reifier o))) 'vector)
"inverseReifiedEQ" (if (or (null (item-reifier o)) (eq o (topic-reified (item-reifier o)))) :true :false))) 'vector))))
(defun orr-observe (raw key)
(let ((parser (symbol-function 'topic-maps.xml:parse-octets))
(reader (symbol-function 'topic-maps.xtm::read-occurrence))
(calls 0) (same nil) (held nil) (counts nil) (map nil))
(unwind-protect
(progn
(setf (symbol-function 'topic-maps.xml:parse-octets)
(lambda (input &rest args) (incf calls) (setf same (eq raw input)) (apply parser input args)))
(setf (symbol-function 'topic-maps.xtm::read-occurrence)
(lambda (node owner model)
(let ((o (funcall reader node owner model)))
(pushnew o held :test #'eq) (when (item-reifier o) (pushnew (item-reifier o) held :test #'eq))
(push (length (all-occurrences model)) counts) o)))
(setf map (deserialize-octets raw :document-iri *document* :source-key key)))
(setf (symbol-function 'topic-maps.xml:parse-octets) parser
(symbol-function 'topic-maps.xtm::read-occurrence) reader))
(values map held (nreverse counts) calls same)))
(defun orr-held-p (map held)
(every (lambda (i) (member (topic-maps.tmdm::canonical-item i) (topic-maps.tmdm::map-information-items map) :test #'eq)) held))
(defun orr-pure (label map held)
(let ((before (ik-state map)) (cx (mm-cxtm map)))
(check (format nil "occurrence-reifier:~A:pure-repeated-CXTM-identifiers-reifiers-handles" label)
(and (equalp cx (mm-cxtm map)) (equal before (ik-state map)) (orr-current-p map) (orr-held-p map held))) cx))
(defun orr-condition-from (raw key &optional resolver)
;; Observe a real failed native assignment on its actual Map. Ignore inner
;; disposable preflight calls; propagate the original native condition object.
(let ((saved (symbol-function 'set-item-reifier)) (depth 0) (frames nil) (c nil))
(unwind-protect
(progn
(setf (symbol-function 'set-item-reifier)
(lambda (item topic)
(incf depth)
(unwind-protect
(let* ((map (topic-maps.tmdm::item-topic-map item)) (before (ik-state map))
(prior (and topic (topic-reified topic))))
(handler-case (funcall saved item topic)
(error (condition)
(when (= depth 1)
(push (mm-fields "assignedItemKind" (string (type-of item))
"priorReifiedItemKind" (when prior (string (type-of prior)))
"requestedReifierII" (coerce (when topic (item-identifiers topic)) 'vector)
"nativeAssignmentUnchanged" (if (equal before (ik-state map)) :true :false)) frames))
(error condition))))
(decf depth))))
(setf c (condition-from (lambda () (deserialize-octets raw :document-iri *document* :source-key key :resource-resolver resolver)))))
(setf (symbol-function 'set-item-reifier) saved))
(values c (nreverse frames))))
(defun occurrence-reifier-original-contracts ()
(setf *occurrence-reifier-outputs* nil)
(let ((previous (copy-list *cases*)))
(dolist (name '("occurrence-reifier" "occurrence-duplicate-reifier"))
(let* ((id (format nil "xtm2/in/~A.xtm" name))
(*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
(baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name))) (expected (file-bytes baseline))
(case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id)
:document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
:clauses '("XTM 4.3.2,4.3.4,4.5,4.15" "TMDM 5.1,5.3.4-5.3.5,5.6,6.2,6.5")))
(map nil) (held nil) (counts nil) (calls 0) (same nil) (actual nil) (c nil))
(check (format nil "occurrence-reifier-original:~A:unique-new-case" name)
(null (find id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))
(setf *cases* (append *cases* (list case)))
(handler-case
(progn
(multiple-value-setq (map held counts calls same) (orr-observe raw id))
(let* ((o (first (all-occurrences map))) (r (by-id map "reifier")))
(check (format nil "occurrence-reifier-original:~A:exact-native-value-parent-type-scope-reifier-and-inverse" name)
(and (= 3 (length (topic-map-topics map))) (= 1 (length (all-occurrences map)))
(null (topic-map-associations map)) (null (name-reifier-names map))
(eq (by-id map "topic") (occurrence-parent o)) (eq (by-id map "occtype") (occurrence-type o))
(equal "Testing resource data." (occurrence-value o)) (equal (string-datatype) (occurrence-datatype o))
(null (occurrence-scope o)) (null (item-identifiers o)) (eq r (item-reifier o)) (eq o (topic-reified r))
(equal (list (concatenate 'string *document* "#reifier")) (item-identifiers r))
(every (lambda (topic) (and (= 1 (length (item-identifiers topic)))
(null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)))) (topic-map-topics map))
(orr-current-p map) (orr-held-p map held))))
(check (format nil "occurrence-reifier-original:~A:original-octets-one-qualified-parser-and-actual-reads" name)
(and (= 1 calls) same (equalp raw copy) (equal counts (if (equal name "occurrence-reifier") '(1) '(1 1)))))
(when (equal name "occurrence-duplicate-reifier")
(check "occurrence-reifier-original:duplicate:old-Occurrence-handle-replaced-and-resolvable"
(some (lambda (i) (and (typep i 'occurrence) (not (eq i (canonical-occurrence i))))) held)))
(setf actual (orr-pure name map held))
(let ((other (deserialize-octets raw :document-iri *document* :source-key "different:Occurrence-Reifier-provenance")))
(check (format nil "occurrence-reifier-original:~A:source-key-independent" name)
(and (equalp (orr-model map) (orr-model other)) (equalp actual (mm-cxtm other))))))
(error (condition) (setf c condition)))
(record-original-result case actual expected c)
(push (mm-fields "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw)
"sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"qualifiedParserCalls" calls "originalOctetsEQ" (if same :true :false) "model" (when map (orr-model map))
"occurrenceCountsAfterEachRead" (coerce counts 'vector) "observedNativeHandles" (length held)
"oldHandlesCanonicalAndLive" (if (and map (orr-held-p map held)) :true :false)
"actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
"actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
"firstDifferentByte" (when actual (mismatch actual expected)) "condition" (mm-condition c)
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *occurrence-reifier-outputs*)
(when c (error c))
(check (format nil "occurrence-reifier-original:~A:complete-original-CXTM-byte-exact" name) (equalp actual expected))))
(orr-negative-original)
(check "occurrence-reifier-original:three-requested-cases-once-all-prior-PASS-preserved"
(and (every (lambda (case) (and (member case *cases* :test #'eq) (eq :pass (topic-maps.test-foundation:test-case-status case)))) previous)
(same-identifiers-p (mapcar #'topic-maps.test-foundation:test-case-id (set-difference *cases* previous :test #'eq))
'("xtm2/in/occurrence-reifier.xtm" "xtm2/in/occurrence-duplicate-reifier.xtm" "xtm2/invalid/reifier-collision.xtm"))
(every (lambda (case) (= 1 (count (topic-maps.test-foundation:test-case-id case) *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))) *cases*))))
(setf *occurrence-reifier-outputs* (nreverse *occurrence-reifier-outputs*)))
(defun orr-negative-original ()
(let* ((id "xtm2/invalid/reifier-collision.xtm")
(*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
(spec '("reifier-collision" reification-error :reifier-conflict "Topic already reifies a different information item." nil nil "XTM 4.3.2,4.15; TMDM 5.3.4-5.3.5,6.2: Map and Occurrence are different items"))
(case (topic-maps.test-foundation:make-test-case :id id :category :invalid :source-pathname (corpus-path id)
:document-iri *document* :expected-outcome :ctm-rejection :clauses (list (seventh spec))))
(c nil) (frames nil))
(multiple-value-setq (c frames) (orr-condition-from raw id))
(record-negative-original-result case spec c *document* id)
(setf *cases* (append *cases* (list case)))
(push (mm-fields "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw)
"sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"condition" (mm-condition c) "actualNativeAssignments" (coerce frames 'vector)
"intendedCauseReached" (if (intended-negative-p spec c *document* id) :true :false)
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *occurrence-reifier-outputs*)
(check "occurrence-reifier-negative-original:actual-Map-Occurrence-collision-and-exact-native-condition"
(and (intended-negative-p spec c *document* id) (equalp copy raw) (= 1 (length frames))
(let ((members (topic-maps.test-foundation::json-object-members (first frames))))
(and (equal "OCCURRENCE" (cdr (assoc "assignedItemKind" members :test #'equal)))
(equal "TOPIC-MAP" (cdr (assoc "priorReifiedItemKind" members :test #'equal)))
(eq :true (cdr (assoc "nativeAssignmentUnchanged" members :test #'equal)))))))
(check "occurrence-reifier-negative-original:no-preliminary-Unsupported-registered-PASS"
(and (not (typep c 'xtm-unsupported)) (eq :pass (topic-maps.test-foundation:test-case-status case))))))
(defun orr-xml (reifier &key (ids nil) (type "#t") scope (value "V") (datatype nil) resource)
(format nil "~A~A~A"
(if reifier (format nil " reifier='~A'" reifier) "") (ik-ids ids) type
(if scope (format nil "~{~}" scope) "")
(or resource (format nil "~A" (if datatype (format nil " datatype='~A'" datatype) "") value))))
(defun orr-probe (label body verify &optional replacement)
(let* ((raw (bytes (document body))) (copy (copy-seq raw)))
(multiple-value-bind (map held counts calls same) (orr-observe raw label)
(check (format nil "occurrence-reifier:~A:productive-reader-original-bytes-native-EQ-and-inverses" label)
(and (= 1 calls) same (equalp copy raw) (orr-current-p map) (orr-held-p map held)))
(funcall verify map)
(when replacement
(check (format nil "occurrence-reifier:~A:old-handles-resolve-after-normalization" label)
(some (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)))
(let ((cx (orr-pure label map held)))
(push (mm-fields "label" label "documentIRI" *document* "sourceKey" label "sourceText" (document body)
"model" (orr-model map) "CXTM" (babel:octets-to-string cx :encoding :utf-8)
"readOccurrenceCounts" (coerce counts 'vector) "oldHandlesCanonicalAndLive" :true) *occurrence-reifier-controls*)) map)))
(defun orr-error (label body class code &key resolver map-reifier)
(multiple-value-bind (c frames) (orr-condition-from (bytes (if map-reifier (map-reifier-xml map-reifier body) (document body))) label resolver)
(let ((actual (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c))
((typep c 'reification-error) (reification-error-code c))
((typep c 'item-identifier-error) (item-identifier-error-code c)))))
(check (format nil "occurrence-reifier-error:~A:exact-class-code-intended-cause" label)
(and (eq (type-of c) class) (eq code actual)))
(when (typep c 'topic-maps.xtm:xtm-error)
(check (format nil "occurrence-reifier-error:~A:original-document-source-position-and-cause" label)
(and (equal *document* (error-document-iri c)) (equal label (error-source-key c))
(if (typep c 'xtm-xml-error) (topic-maps.xtm:error-cause c)
(and (integerp (error-line c)) (integerp (error-column c)))))))
(push (mm-fields "label" label "documentIRI" *document* "sourceKey" label "sourceText" (if map-reifier (map-reifier-xml map-reifier body) (document body))
"condition" (mm-condition c) "nativeConditionHasNoSourcePositionSlots" (if (typep c 'topic-maps.xtm:xtm-error) :false :true)
"actualNativeAssignments" (coerce frames 'vector)) *occurrence-reifier-controls*)) c))
(defun occurrence-reifier-tests ()
(setf *occurrence-reifier-controls* nil)
(dolist (position '("unknown" "before" "after"))
(let ((body (format nil "~A" (orr-xml "#r"))))
(orr-probe (concatenate 'string "definition-" position)
(concatenate 'string (if (equal position "before") "" "") body
(if (equal position "after") "" ""))
(lambda (map)
(check (format nil "occurrence-reifier:definition-~A:one-II-reifier-topic-no-SI-SL" position)
(let ((o (first (all-occurrences map))) (r (by-id map "r")))
(and (= 3 (length (topic-map-topics map))) (eq r (item-reifier o)) (eq o (topic-reified r))
(equal (list (concatenate 'string *document* "#r")) (item-identifiers r))
(null (topic-subject-identifiers r)) (null (topic-subject-locators r)) (null (item-identifiers o)))))))))
(dolist (category '("itemIdentity" "subjectIdentifier"))
(let ((iri "https://reifier.example.invalid/object#r"))
(orr-probe (concatenate 'string "lookup-" category)
(format nil "<~A href='~A'/>~A" category iri (orr-xml iri))
(lambda (map)
(let ((r (by-id map "known")))
(check (format nil "occurrence-reifier:lookup-~A:II-only-lookup-TMDM-cross-merge-and-identifier-preservation" category)
(and (= 3 (length (topic-map-topics map))) (eq r (item-reifier (first (all-occurrences map))))
(same-identifiers-p (list (concatenate 'string *document* "#known") iri) (item-identifiers r))
(equal (when (equal category "subjectIdentifier") (list iri)) (topic-subject-identifiers r))
(null (topic-subject-locators r)))))))))
(dolist (ref '("urn:reifier" "../%E9%A6%AC#r"))
(orr-probe (concatenate 'string "resolved-IRI:" ref) (format nil "~A" (orr-xml ref :ids '("urn:occurrence-II")))
(lambda (map)
(let* ((o (first (all-occurrences map))) (r (item-reifier o)))
(check (format nil "occurrence-reifier:resolved-IRI:~A:separate-II-and-Reifier" ref)
(and (equal '("urn:occurrence-II") (item-identifiers o))
(equal (list (mm-child-iri ref)) (item-identifiers r))
(not (equal (item-identifiers o) (item-identifiers r))) (null (topic-subject-identifiers r)) (null (topic-subject-locators r))))))))
(orr-probe "same-Occurrence-two-Reifiers-and-item-identifiers"
(format nil "~A~A" (orr-xml "#r" :ids '("urn:o1")) (orr-xml "#u" :ids '("urn:o2")))
(lambda (map)
(let ((o (first (all-occurrences map))))
(check "occurrence-reifier:two-Reifiers:one-native-Occurrence-one-Reifier-Topic-all-identifiers"
(and (= 1 (length (all-occurrences map))) (= 3 (length (topic-map-topics map)))
(same-identifiers-p '("urn:o1" "urn:o2") (item-identifiers o))
(eq (by-id map "r") (by-id map "u")) (eq o (topic-reified (by-id map "u")))
(same-identifiers-p (list (concatenate 'string *document* "#r") (concatenate 'string *document* "#u")) (item-identifiers (item-reifier o))))))) t)
(dolist (order '("unreified-first" "reified-first"))
(orr-probe order
(format nil "~A~A" (orr-xml (when (equal order "reified-first") "#r")) (orr-xml (when (equal order "unreified-first") "#r")))
(lambda (map) (check (concatenate 'string "occurrence-reifier:" order ":normalization-retains-Reifier")
(and (= 1 (length (all-occurrences map))) (eq (by-id map "r") (item-reifier (first (all-occurrences map))))))) t))
(orr-probe "resourceRef-IRI-value-with-Reification"
(format nil "~A" (orr-xml "#r" :resource ""))
(lambda (map) (let ((o (first (all-occurrences map))))
(check "occurrence-reifier:resourceRef:resolved-value-datatype-and-reified-inverse"
(and (equal (mm-child-iri "../resource") (occurrence-value o)) (equal (iri-datatype) (occurrence-datatype o))
(eq o (topic-reified (by-id map "r"))))))))
(orr-late-merges)
(orr-negative-tests)
(orr-import-test)
(setf *occurrence-reifier-controls* (nreverse *occurrence-reifier-controls*)))
(defun orr-late-merges ()
(dolist (category '("subjectIdentifier" "subjectLocator" "itemIdentity"))
(let* ((label (concatenate 'string "late-" category))
(body (concatenate 'string
(format nil "~A~A"
(orr-xml "#r" :ids '("urn:o1") :type "#t" :scope '("#s"))
(orr-xml "#u" :ids '("urn:o2") :type "#tt" :scope '("#ss")))
(format nil "<~A href='urn:owner'/><~A href='urn:owner'/><~A href='urn:type'/><~A href='urn:type'/><~A href='urn:scope'/><~A href='urn:scope'/><~A href='urn:reifier'/><~A href='urn:reifier'/>" category category category category category category category category))))
(orr-probe label body
(lambda (map) (let ((o (first (all-occurrences map))))
(check (format nil "occurrence-reifier:~A:late-owner-type-scope-Reifier-identity-merges" label)
(and (= 1 (length (all-occurrences map))) (eq (by-id map "p") (by-id map "q"))
(eq (by-id map "t") (by-id map "tt")) (eq (by-id map "s") (by-id map "ss"))
(eq (by-id map "r") (by-id map "u")) (eq o (topic-reified (by-id map "u")))
(same-identifiers-p '("urn:o1" "urn:o2") (item-identifiers o))
(eq (by-id map "p") (occurrence-parent o)) (eq (by-id map "t") (occurrence-type o))
(equal (list (by-id map "s")) (occurrence-scope o)))))) t))))
(defun orr-negative-tests ()
(dolist (row '(("bad-percent" "%ZZ" :invalid-percent-escape) ("bad-percent-UTF8" "%C3%28" :invalid-percent-utf8) ("bad-IRI" "x#y#z" :invalid-iri)))
(orr-error (first row) (format nil "~A" (orr-xml (second row))) 'xtm-rejection (third row)))
(dolist (row '(("extra-attribute" "V" xtm-rejection :unexpected-attribute)
("reifier-on-type" "V" xtm-rejection :unexpected-attribute)
("reifier-on-Topic" "" xtm-rejection :unexpected-attribute)
("reifier-on-scope" "V" xtm-rejection :unexpected-attribute)
("malformed-XML" "" xtm-xml-error :xml-not-well-formed)))
(apply #'orr-error row))
(orr-error "two-different-Occurrences-same-Reifier"
(format nil "~A~A" (orr-xml "#r" :value "A") (orr-xml "#r" :value "B")) 'reification-error :reifier-conflict)
(orr-error "Name-and-Occurrence-same-Reifier"
(format nil "N~A" (orr-xml "#r")) 'reification-error :reifier-conflict)
(orr-error "Map-and-Occurrence-same-Reifier"
(concatenate 'string "" (orr-xml "#r") "")
'reification-error :reifier-conflict
:map-reifier "#r")
(dolist (category '("subjectIdentifier" "subjectLocator" "itemIdentity"))
(orr-error (concatenate 'string "late-conflicting-Reifier-merge-" category)
(format nil "~A~A<~A href='urn:shared'/><~A href='urn:shared'/>"
(orr-xml "#r" :value "A") (orr-xml "#u" :value "B") category category) 'reification-error :reifier-conflict))
(orr-error "Occurrence-own-II-cannot-be-Reifier-Topic-II"
(format nil "~A" (orr-xml "urn:same" :ids '("urn:same"))) 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier)
(orr-error "anyType-remains-Unsupported" (format nil "~A" (orr-xml "#r" :datatype "http://www.w3.org/2001/XMLSchema#anyType")) 'xtm-unsupported :resource-data-datatype)
(orr-error "Association-Reification-remains-Unsupported" "" 'xtm-unsupported :unsupported-construct)
(orr-error "Role-Reification-remains-Unsupported" "" 'xtm-unsupported :unsupported-construct))
(defun orr-import-test ()
(let* ((child (mm-child-iri "child.xtm"))
(raw (bytes (document (format nil "~A" (orr-xml "#r" :ids '("urn:imported-Occurrence") :scope '("#s")))))))
(mm-call "Occurrence-Reification-import" (document (concatenate 'string (mm-directive "child.xtm") ""))
(list (list child raw "child:Reification"))
(lambda (map imports calls) (declare (ignore calls))
(let ((o (first (all-occurrences map))) (r (mm-local map child "r")))
(check "occurrence-reifier:import:native-parent-II-Reifier-inverse-source-preservation"
(and (orr-current-p map) (eq (by-id map "local") (occurrence-parent o)) (eq r (item-reifier o)) (eq o (topic-reified r))
(equal '("urn:imported-Occurrence") (item-identifiers o)) (null (item-identifiers map))
(every (lambda (record) (let ((source (first record)) (resolve (second record)))
(and (orr-current-p source) (eq o (funcall resolve (first (all-occurrences source))))
(eq r (funcall resolve (item-reifier (first (all-occurrences source)))))))) imports))))
(push (mm-fields "label" "native-Reification-import" "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"model" (orr-model map) "sourceMapPreserved" :true) *occurrence-reifier-controls*)))))