-
Notifications
You must be signed in to change notification settings - Fork 18
/
InputOntologyV2Spec.scala
260 lines (246 loc) · 10.4 KB
/
InputOntologyV2Spec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.messages.v2.responder.ontologymessages
import java.time.Instant
import dsp.errors.BadRequestException
import org.knora.webapi.ApiV2Complex
import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.IriConversions.*
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.store.triplestoremessages.SmartIriLiteralV2
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2
import org.knora.webapi.messages.util.rdf.JsonLDUtil
import org.knora.webapi.messages.v2.responder.ontologymessages.OwlCardinality.KnoraCardinalityInfo
import org.knora.webapi.slice.ontology.domain.model.Cardinality.ZeroOrOne
/**
* Tests [[InputOntologyV2]].
*/
class InputOntologyV2Spec extends CoreSpec {
import InputOntologyV2Spec.*
"InputOntologyV2" should {
"parse a property definition" in {
val params =
"""
|{
| "@id" : "http://0.0.0.0:3333/ontology/0001/anything/v2",
| "@type" : "owl:Ontology",
| "knora-api:lastModificationDate" : {
| "@type" : "xsd:dateTimeStamp",
| "@value" : "2017-12-19T15:23:42.166Z"
| },
| "@graph" : [ {
| "@id" : "anything:hasName",
| "@type" : "owl:ObjectProperty",
| "knora-api:subjectType" : {
| "@id" : "anything:Thing"
| },
| "knora-api:objectType" : {
| "@id" : "knora-api:TextValue"
| },
| "rdfs:comment" : [ {
| "@language" : "en",
| "@value" : "The name of a 'Thing'"
| }, {
| "@language" : "de",
| "@value" : "Der Name eines Dinges"
| } ],
| "rdfs:label" : [ {
| "@language" : "en",
| "@value" : "has name"
| }, {
| "@language" : "de",
| "@value" : "hat Namen"
| } ],
| "rdfs:subPropertyOf" : [ {
| "@id" : "knora-api:hasValue"
| }, {
| "@id" : "http://schema.org/name"
| } ]
| } ],
| "@context" : {
| "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
| "owl" : "http://www.w3.org/2002/07/owl#",
| "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
| "xsd" : "http://www.w3.org/2001/XMLSchema#",
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}
""".stripMargin
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
paramsAsInput should ===(PropertyDef)
}
"parse a class definition" in {
val params =
s"""
|{
| "@id" : "http://0.0.0.0:3333/ontology/0001/anything/v2",
| "@type" : "owl:Ontology",
| "knora-api:lastModificationDate" : {
| "@type" : "xsd:dateTimeStamp",
| "@value" : "2017-12-19T15:23:42.166Z"
| },
| "@graph" : [ {
| "@id" : "anything:WildThing",
| "@type" : "owl:Class",
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "wild thing"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "A thing that is wild"
| },
| "rdfs:subClassOf" : [ {
| "@id" : "anything:Thing"
| }, {
| "@type": "owl:Restriction",
| "owl:maxCardinality": 1,
| "owl:onProperty": {
| "@id" : "anything:hasName"
| }
| } ]
| } ],
| "@context" : {
| "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
| "owl" : "http://www.w3.org/2002/07/owl#",
| "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
| "xsd" : "http://www.w3.org/2001/XMLSchema#",
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}
""".stripMargin
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
paramsAsInput should ===(ClassDef)
}
"reject an entity definition in the wrong ontology" in {
val params =
s"""
|{
| "@id" : "http://0.0.0.0:3333/ontology/0001/incunabula/v2",
| "@type" : "owl:Ontology",
| "knora-api:lastModificationDate" : {
| "@type" : "xsd:dateTimeStamp",
| "@value" : "2017-12-19T15:23:42.166Z"
| },
| "@graph" : [ {
| "@id" : "anything:WildThing",
| "@type" : "owl:Class",
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "wild thing"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "A thing that is wild"
| },
| "rdfs:subClassOf" : [ {
| "@id" : "anything:Thing"
| }, {
| "@type": "owl:Restriction",
| "owl:maxCardinality": 1,
| "owl:onProperty": {
| "@id" : "anything:hasName"
| }
| } ]
| } ],
| "@context" : {
| "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
| "owl" : "http://www.w3.org/2002/07/owl#",
| "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
| "xsd" : "http://www.w3.org/2001/XMLSchema#",
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}
""".stripMargin
assertThrows[BadRequestException] {
InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params))
}
}
}
}
object InputOntologyV2Spec {
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val PropertyDef = InputOntologyV2(
ontologyMetadata = OntologyMetadataV2(
ontologyIri = "http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri,
label = None,
lastModificationDate = Some(Instant.parse("2017-12-19T15:23:42.166Z")),
),
properties = Map(
"http://0.0.0.0:3333/ontology/0001/anything/v2#hasName".toSmartIri -> PropertyInfoContentV2(
propertyIri = "http://0.0.0.0:3333/ontology/0001/anything/v2#hasName".toSmartIri,
predicates = Map(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type".toSmartIri -> PredicateInfoV2(
predicateIri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".toSmartIri,
objects = Seq(SmartIriLiteralV2("http://www.w3.org/2002/07/owl#ObjectProperty".toSmartIri)),
),
"http://api.knora.org/ontology/knora-api/v2#subjectType".toSmartIri -> PredicateInfoV2(
predicateIri = "http://api.knora.org/ontology/knora-api/v2#subjectType".toSmartIri,
objects = Seq(SmartIriLiteralV2("http://0.0.0.0:3333/ontology/0001/anything/v2#Thing".toSmartIri)),
),
"http://api.knora.org/ontology/knora-api/v2#objectType".toSmartIri -> PredicateInfoV2(
predicateIri = "http://api.knora.org/ontology/knora-api/v2#objectType".toSmartIri,
objects = Seq(SmartIriLiteralV2("http://api.knora.org/ontology/knora-api/v2#TextValue".toSmartIri)),
),
"http://www.w3.org/2000/01/rdf-schema#label".toSmartIri -> PredicateInfoV2(
predicateIri = "http://www.w3.org/2000/01/rdf-schema#label".toSmartIri,
objects = Seq(
StringLiteralV2.from("has name", Some("en")),
StringLiteralV2.from("hat Namen", Some("de")),
),
),
"http://www.w3.org/2000/01/rdf-schema#comment".toSmartIri -> PredicateInfoV2(
predicateIri = "http://www.w3.org/2000/01/rdf-schema#comment".toSmartIri,
objects = Seq(
StringLiteralV2.from("The name of a 'Thing'", Some("en")),
StringLiteralV2.from("Der Name eines Dinges", Some("de")),
),
),
),
subPropertyOf = Set(
"http://api.knora.org/ontology/knora-api/v2#hasValue".toSmartIri,
"http://schema.org/name".toSmartIri,
),
ontologySchema = ApiV2Complex,
),
),
)
val ClassDef = InputOntologyV2(
classes = Map(
"http://0.0.0.0:3333/ontology/0001/anything/v2#WildThing".toSmartIri -> ClassInfoContentV2(
predicates = Map(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type".toSmartIri -> PredicateInfoV2(
predicateIri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".toSmartIri,
objects = Seq(SmartIriLiteralV2("http://www.w3.org/2002/07/owl#Class".toSmartIri)),
),
"http://www.w3.org/2000/01/rdf-schema#label".toSmartIri -> PredicateInfoV2(
predicateIri = "http://www.w3.org/2000/01/rdf-schema#label".toSmartIri,
objects = Seq(StringLiteralV2.from("wild thing", Some("en"))),
),
"http://www.w3.org/2000/01/rdf-schema#comment".toSmartIri -> PredicateInfoV2(
predicateIri = "http://www.w3.org/2000/01/rdf-schema#comment".toSmartIri,
objects = Seq(StringLiteralV2.from("A thing that is wild", Some("en"))),
),
),
classIri = "http://0.0.0.0:3333/ontology/0001/anything/v2#WildThing".toSmartIri,
ontologySchema = ApiV2Complex,
directCardinalities = Map(
"http://0.0.0.0:3333/ontology/0001/anything/v2#hasName".toSmartIri -> KnoraCardinalityInfo(
ZeroOrOne,
),
),
subClassOf = Set("http://0.0.0.0:3333/ontology/0001/anything/v2#Thing".toSmartIri),
),
),
ontologyMetadata = OntologyMetadataV2(
ontologyIri = "http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri,
lastModificationDate = Some(Instant.parse("2017-12-19T15:23:42.166Z")),
),
properties = Map(),
)
}