-
Notifications
You must be signed in to change notification settings - Fork 18
/
OntologyFormatsE2ESpec.scala
335 lines (290 loc) · 13.1 KB
/
OntologyFormatsE2ESpec.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* 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.e2e.v2
import org.apache.pekko
import org.scalatest.Inspectors.forEvery
import spray.json._
import java.net.URLEncoder
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import org.knora.webapi._
import org.knora.webapi.e2e.ClientTestDataCollector
import org.knora.webapi.e2e.TestDataFileContent
import org.knora.webapi.e2e.TestDataFilePath
import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Complex
import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Simple
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.sharedtestdata.SharedOntologyTestDataADM
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.util._
import pekko.http.scaladsl.model._
import pekko.http.scaladsl.model.headers.Accept
class OntologyFormatsE2ESpec extends E2ESpec {
override lazy val rdfDataObjects: List[RdfDataObject] =
List(
RdfDataObject("test_data/project_ontologies/freetest-onto.ttl", "http://www.knora.org/ontology/0001/freetest"),
RdfDataObject("test_data/project_ontologies/minimal-onto.ttl", "http://www.knora.org/ontology/0001/minimal"),
)
/**
* Represents an HTTP GET test that requests ontology information.
*
* @param urlPath the URL path to be used in the request.
* @param fileBasename the basename of the test data file containing the expected response.
* @param maybeClientTestDataBasename the basename of the client test data file, if any, to be collected by
* [[org.knora.webapi.e2e.ClientTestDataCollector]].
* @param disableWrite if true, this [[HttpGetTest]] will not write the expected response file when `writeFile` is called.
* This is useful if two tests share the same file.
*/
private case class HttpGetTest(
urlPath: String,
fileBasename: String,
clientTestDataBasename: Option[String] = None,
) {
private val jsonLd = "jsonld"
private val jsonLdReceived = s"received.$jsonLd"
private def makeFile(suffix: String): Path =
Paths.get("..", "test_data", "generated_test_data", "ontologyR2RV2", s"$fileBasename.$suffix")
/**
* If `maybeClientTestDataBasename` is defined, stores the response string in [[org.knora.webapi.e2e.ClientTestDataCollector]].
*/
def storeClientTestData(responseStr: String): Unit =
clientTestDataBasename match {
case Some(clientTestDataBasename) => CollectClientTestData(clientTestDataBasename, responseStr)
case None => ()
}
/**
* Reads the expected response file.
*
* @param mediaType the media type of the response.
* @return the contents of the file.
*/
def readFile(mediaType: MediaType.NonBinary): String =
FileUtil.readTextFile(makeFile(mediaType.fileExtensions.head))
def fileExists: Boolean =
Files.exists(makeFile(jsonLd))
def writeReceived(responseStr: String): Unit = {
val newOutputFile = makeFile(jsonLdReceived)
Files.createDirectories(newOutputFile.getParent)
FileUtil.writeTextFile(newOutputFile, responseStr)
()
}
}
private val clientTestDataPath: Seq[String] = Seq("v2", "ontologies")
private val clientTestDataCollector = new ClientTestDataCollector(appConfig)
private def CollectClientTestData(fileName: String, fileContent: String): Unit =
clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = fileName,
fileExtension = "json",
),
text = fileContent,
),
)
private def urlEncodeIri(iri: IRI): String =
URLEncoder.encode(iri, "UTF-8")
private def checkTestCase(httpGetTest: HttpGetTest) = {
val responseJsonLd = getResponse(httpGetTest.urlPath, RdfMediaTypes.`application/ld+json`)
val responseTtl = getResponse(httpGetTest.urlPath, RdfMediaTypes.`text/turtle`)
val responseRdfXml = getResponse(httpGetTest.urlPath, RdfMediaTypes.`application/rdf+xml`)
if (!httpGetTest.fileExists) {
httpGetTest.writeReceived(responseJsonLd)
throw new AssertionError(s"No approved data available in file ${httpGetTest.fileBasename}")
}
val approvedJsonLd = httpGetTest.readFile(RdfMediaTypes.`application/ld+json`)
if (JsonParser(responseJsonLd) != JsonParser(approvedJsonLd)) {
httpGetTest.writeReceived(responseJsonLd)
throw new AssertionError(
s"""|
|The response did not equal the approved data.
|
|Response:
|
|$responseJsonLd
|
|
|${"=" * 120}
|
|
|Approved data:
|
|$approvedJsonLd
|
|""".stripMargin,
)
}
assert(parseTurtle(responseTtl) == parseJsonLd(responseJsonLd))
assert(parseRdfXml(responseRdfXml) == parseJsonLd(responseJsonLd))
httpGetTest.storeClientTestData(responseJsonLd)
}
private def getResponse(url: String, mediaType: MediaType.NonBinary) = {
val request = Get(s"$baseApiUrl$url").addHeader(Accept(mediaType))
val response = singleAwaitingRequest(request)
val responseStr = responseToString(response)
assert(response.status == StatusCodes.OK, responseStr)
responseStr
}
private object TestCases {
private val knoraApiOntologySimple =
HttpGetTest(
urlPath = s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Simple.KnoraApiOntologyIri)}",
fileBasename = "knoraApiOntologySimple",
)
private val knoraApiOntologyComplex =
HttpGetTest(
urlPath = s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Complex.KnoraApiOntologyIri)}",
fileBasename = "knoraApiOntologyWithValueObjects",
clientTestDataBasename = Some("knora-api-ontology"),
)
private val salsahGuiOntology =
HttpGetTest(urlPath = "/ontology/salsah-gui/v2", fileBasename = "salsahGuiOntology")
private val standoffOntology =
HttpGetTest(urlPath = "/ontology/standoff/v2", fileBasename = "standoffOntologyWithValueObjects")
private val knoraApiDateSegmentSimple =
HttpGetTest(
urlPath = s"/v2/ontologies/classes/${urlEncodeIri(KnoraApiV2Simple.Date)}",
fileBasename = "knoraApiDate",
)
private val knoraApiDateSegmentComplex =
HttpGetTest(
urlPath = s"/v2/ontologies/classes/${urlEncodeIri(KnoraApiV2Complex.DateValue)}",
fileBasename = "knoraApiDateValue",
)
private val knoraApiHasColorSegmentSimple =
HttpGetTest(
urlPath = s"/v2/ontologies/properties/${urlEncodeIri(KnoraApiV2Simple.HasColor)}",
fileBasename = "knoraApiSimpleHasColor",
)
private val knoraApiHasColorSegmentComplex =
HttpGetTest(
urlPath = s"/v2/ontologies/properties/${urlEncodeIri(KnoraApiV2Complex.HasColor)}",
fileBasename = "knoraApiWithValueObjectsHasColor",
)
private val anythingOntologyMetadata =
HttpGetTest(
urlPath = s"/v2/ontologies/metadata/${urlEncodeIri(SharedTestDataADM.anythingProjectIri)}",
fileBasename = "anythingOntologyMetadata",
clientTestDataBasename = Some("get-ontologies-project-anything-response"),
)
private val anythingOntologySimple =
HttpGetTest(
urlPath =
s"/v2/ontologies/allentities/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost_SIMPLE)}",
fileBasename = "anythingOntologySimple",
)
private val anythingOntologyComplex =
HttpGetTest(
urlPath =
s"/v2/ontologies/allentities/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost)}",
fileBasename = "anythingOntologyWithValueObjects",
clientTestDataBasename = Some("anything-ontology"),
)
private val anythingThingWithAllLanguages =
HttpGetTest(
urlPath =
s"/v2/ontologies/classes/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_THING_RESOURCE_CLASS_LocalHost)}?allLanguages=true",
fileBasename = "anythingThingWithAllLanguages",
clientTestDataBasename = Some("get-class-anything-thing-with-allLanguages-response"),
)
private val anythingOntologyThingSimple =
HttpGetTest(
urlPath =
s"/v2/ontologies/classes/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_THING_RESOURCE_CLASS_LocalHost_SIMPLE)}",
fileBasename = "anythingThingSimple",
)
private val anythingOntologyThingComplex =
HttpGetTest(
urlPath =
s"/v2/ontologies/classes/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_THING_RESOURCE_CLASS_LocalHost)}",
fileBasename = "anythingThing",
)
private val anythingOntologyHasListItemSimple =
HttpGetTest(
urlPath =
s"/v2/ontologies/properties/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_HasListItem_PROPERTY_LocalHost_SIMPLE)}",
fileBasename = "anythingHasListItemSimple",
)
private val anythingOntologyHasListItemComplex =
HttpGetTest(
urlPath =
s"/v2/ontologies/properties/${urlEncodeIri(SharedOntologyTestDataADM.ANYTHING_HasListItem_PROPERTY_LocalHost)}",
fileBasename = "anythingHasListItem",
clientTestDataBasename = Some("get-property-listValue-response"),
)
val testCases = Seq(
// built-in ontologies
knoraApiOntologySimple,
knoraApiOntologyComplex,
salsahGuiOntology,
standoffOntology,
// class segments of built-in ontologies
knoraApiDateSegmentSimple,
knoraApiDateSegmentComplex,
// property segments of built-in ontologies
knoraApiHasColorSegmentSimple,
knoraApiHasColorSegmentComplex,
// project ontologies
anythingOntologyMetadata,
anythingOntologySimple,
anythingOntologyComplex,
anythingThingWithAllLanguages,
// class segments of project ontologies
anythingOntologyThingSimple,
anythingOntologyThingComplex,
// property segments of project ontologies
anythingOntologyHasListItemSimple,
anythingOntologyHasListItemComplex,
)
}
"The Ontologies v2 Endpoint" should {
"serve the ontologies in JSON-LD, turtle and RDF-XML" in {
forEvery(TestCases.testCases) { testCase =>
checkTestCase(testCase)
}
}
"serve the knora-api ontology in the simple schema on two separate endpoints" in {
val ontologyAllEntitiesResponseJson = getResponse(
s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Simple.KnoraApiOntologyIri)}",
RdfMediaTypes.`application/ld+json`,
)
val knoraApiResponseJson = getResponse(s"/ontology/knora-api/simple/v2", RdfMediaTypes.`application/ld+json`)
assert(JsonParser(ontologyAllEntitiesResponseJson) == JsonParser(knoraApiResponseJson))
val ontologyAllEntitiesResponseTurtle = getResponse(
s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Simple.KnoraApiOntologyIri)}",
RdfMediaTypes.`text/turtle`,
)
val knoraApiResponseTurtle = getResponse(s"/ontology/knora-api/simple/v2", RdfMediaTypes.`text/turtle`)
assert(parseTurtle(ontologyAllEntitiesResponseTurtle) == parseTurtle(knoraApiResponseTurtle))
val ontologyAllEntitiesResponseRdfXml = getResponse(
s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Simple.KnoraApiOntologyIri)}",
RdfMediaTypes.`application/rdf+xml`,
)
val knoraApiResponseRdfXml = getResponse(s"/ontology/knora-api/simple/v2", RdfMediaTypes.`application/rdf+xml`)
assert(parseRdfXml(ontologyAllEntitiesResponseRdfXml) == parseRdfXml(knoraApiResponseRdfXml))
}
"serve the knora-api in the complex schema on two separate endpoints" in {
val ontologyAllEntitiesResponseJson = getResponse(
s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Complex.KnoraApiOntologyIri)}",
RdfMediaTypes.`application/ld+json`,
)
val knoraApiResponseJson = getResponse(s"/ontology/knora-api/v2", RdfMediaTypes.`application/ld+json`)
assert(JsonParser(ontologyAllEntitiesResponseJson) == JsonParser(knoraApiResponseJson))
val ontologyAllEntitiesResponseTurtle = getResponse(
s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Complex.KnoraApiOntologyIri)}",
RdfMediaTypes.`text/turtle`,
)
val knoraApiResponseTurtle = getResponse(s"/ontology/knora-api/v2", RdfMediaTypes.`text/turtle`)
assert(parseTurtle(ontologyAllEntitiesResponseTurtle) == parseTurtle(knoraApiResponseTurtle))
val ontologyAllEntitiesResponseRdfXml = getResponse(
s"/v2/ontologies/allentities/${urlEncodeIri(KnoraApiV2Complex.KnoraApiOntologyIri)}",
RdfMediaTypes.`application/rdf+xml`,
)
val knoraApiResponseRdfXml = getResponse(s"/ontology/knora-api/v2", RdfMediaTypes.`application/rdf+xml`)
assert(parseRdfXml(ontologyAllEntitiesResponseRdfXml) == parseRdfXml(knoraApiResponseRdfXml))
}
}
}