/
ValuesV1R2RSpec.scala
410 lines (356 loc) · 17 KB
/
ValuesV1R2RSpec.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright © 2021 - 2022 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.v1
import java.net.URLEncoder
import akka.actor.ActorSystem
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, StatusCodes}
import akka.http.scaladsl.testkit.RouteTestTimeout
import org.knora.webapi._
import org.knora.webapi.http.directives.DSPApiDirectives
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.v1.responder.valuemessages.ApiValueV1JsonProtocol._
import org.knora.webapi.routing.v1.ValuesRouteV1
import org.knora.webapi.sharedtestdata.SharedTestDataV1
import org.knora.webapi.util.{AkkaHttpUtils, MutableTestIri}
import spray.json._
/**
* Tests the values route.
*/
class ValuesV1R2RSpec extends R2RSpec {
override def testConfigSource: String =
"""
# akka.loglevel = "DEBUG"
# akka.stdout-loglevel = "DEBUG"
""".stripMargin
private val valuesPath = DSPApiDirectives.handleErrors(system)(new ValuesRouteV1(routeData).knoraApiPath)
implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(settings.defaultTimeout)
private val integerValueIri = new MutableTestIri
private val timeValueIri = new MutableTestIri
private val textValueIri = new MutableTestIri
private val linkValueIri = new MutableTestIri
private val textValueWithLangIri = new MutableTestIri
private val boringComment = "This is a boring comment."
override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/all_data/anything-data.ttl", name = "http://www.knora.org/data/0001/anything")
)
private val anythingUser = SharedTestDataV1.anythingUser1
private val anythingUserEmail = anythingUser.userData.email.get
private val testPass = "test"
private val mappingIri = "http://rdfh.ch/standoff/mappings/StandardMapping"
"The Values Endpoint" should {
"add an integer value to a resource" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasInteger",
| "int_value": 1234
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
integerValueIri.set(valueIri)
}
}
"change an integer value" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasInteger",
| "int_value": 4321
|}
""".stripMargin
Put(
s"/v1/values/${URLEncoder.encode(integerValueIri.get, "UTF-8")}",
HttpEntity(ContentTypes.`application/json`, params)
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
integerValueIri.set(valueIri)
}
}
"mark an integer value as deleted" in {
Delete(
s"/v1/values/${URLEncoder.encode(integerValueIri.get, "UTF-8")}?deleteComment=deleted%20for%20testing"
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
}
}
"add a time value to a resource" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasTimeStamp",
| "time_value": "2019-08-28T14:40:17.215927Z"
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
timeValueIri.set(valueIri)
}
}
"change a time value" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasTimeStamp",
| "time_value": "2019-08-28T14:45:37.756142Z"
|}
""".stripMargin
Put(
s"/v1/values/${URLEncoder.encode(timeValueIri.get, "UTF-8")}",
HttpEntity(ContentTypes.`application/json`, params)
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
timeValueIri.set(valueIri)
}
}
"get a link value" in {
Get(
s"/v1/links/${URLEncoder.encode("http://rdfh.ch/0001/contained-thing-1", "UTF-8")}/${URLEncoder
.encode("http://www.knora.org/ontology/0001/anything#isPartOfOtherThing", "UTF-8")}/${URLEncoder.encode("http://rdfh.ch/0001/containing-thing", "UTF-8")}"
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val linkValue = AkkaHttpUtils.httpResponseToJson(response).fields("value").asJsObject.fields
assert(
linkValue("subjectIri").asInstanceOf[JsString].value == "http://rdfh.ch/0001/contained-thing-1" &&
linkValue("predicateIri")
.asInstanceOf[JsString]
.value == "http://www.knora.org/ontology/0001/anything#isPartOfOtherThing" &&
linkValue("objectIri").asInstanceOf[JsString].value == "http://rdfh.ch/0001/containing-thing" &&
linkValue("referenceCount").asInstanceOf[JsNumber].value.toInt == 1
)
}
}
"not add an empty text value to a resource" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasText",
| "richtext_value": {"utf8str":""}
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.BadRequest, response.toString)
}
}
"add a text value containing a standoff reference to another resource" in {
val xmlStr =
"""<?xml version="1.0" encoding="UTF-8"?>
|<text>
| This text links to another <a class="salsah-link" href="http://rdfh.ch/0001/another-thing">resource</a>.
|</text>
""".stripMargin
val params =
s"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasText",
| "richtext_value": {"xml": ${xmlStr.toJson.compactPrint}, "mapping_id": "$mappingIri"}
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
// check for standoff link in value creation response
assert(
responseJson("value")
.asInstanceOf[JsObject]
.fields("xml")
.toString
.contains("http://rdfh.ch/0001/another-thing"),
"standoff link target is not contained in value creation response"
)
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
textValueIri.set(valueIri)
}
}
"change a text value containing a standoff reference to another resource" in {
val xmlStr =
"""<?xml version="1.0" encoding="UTF-8"?>
|<text>
| This new version of the text links to another <a class="salsah-link" href="http://rdfh.ch/0001/a-thing-with-text-values">resource</a>.
|</text>
""".stripMargin
val params =
s"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasText",
| "richtext_value": {"xml": ${xmlStr.toJson.compactPrint}, "mapping_id": "$mappingIri"}
|}
""".stripMargin
Put(
s"/v1/values/${URLEncoder.encode(textValueIri.get, "UTF-8")}",
HttpEntity(ContentTypes.`application/json`, params)
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
// check for standoff link in value creation response
assert(
responseJson("value")
.asInstanceOf[JsObject]
.fields("xml")
.toString
.contains("http://rdfh.ch/0001/a-thing-with-text-values"),
"standoff link target is not contained in value creation response"
)
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
textValueIri.set(valueIri)
}
}
"get the version history of a value" in {
Get(
s"/v1/values/history/${URLEncoder.encode("http://rdfh.ch/0001/a-thing", "UTF-8")}/${URLEncoder
.encode("http://www.knora.org/ontology/0001/anything#hasText", "UTF-8")}/${URLEncoder.encode(textValueIri.get, "UTF-8")}"
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val versionHistory: JsValue = AkkaHttpUtils.httpResponseToJson(response).fields("valueVersions")
val (mostRecentVersion, originalVersion) = versionHistory match {
case JsArray(Vector(mostRecent, original)) => (mostRecent.asJsObject.fields, original.asJsObject.fields)
case _ => fail("expected an array, but got something else.")
}
assert(
mostRecentVersion("previousValue").asInstanceOf[JsString].value == originalVersion("valueObjectIri")
.asInstanceOf[JsString]
.value
)
assert(originalVersion("previousValue") == JsNull)
}
}
"mark as deleted a text value containing a standoff reference to another resource" in {
Delete(
s"/v1/values/${URLEncoder.encode(textValueIri.get, "UTF-8")}?deleteComment=deleted%20for%20testing"
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
}
}
"add a link value to a resource" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasOtherThing",
| "link_value": "http://rdfh.ch/0001/another-thing"
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
linkValueIri.set(valueIri)
}
}
"mark a link value as deleted" in {
Delete(
s"/v1/values/${URLEncoder.encode(linkValueIri.get, "UTF-8")}?deleteComment=deleted%20for%20testing"
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
}
}
"add a link value with a comment to a resource" in {
val params =
s"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing",
| "prop": "http://www.knora.org/ontology/0001/anything#hasOtherThing",
| "link_value": "http://rdfh.ch/0001/another-thing",
| "comment":"$boringComment"
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
linkValueIri.set(valueIri)
}
}
"get a link value with a comment" in {
Get(
s"/v1/links/${URLEncoder.encode("http://rdfh.ch/0001/a-thing", "UTF-8")}/${URLEncoder
.encode("http://www.knora.org/ontology/0001/anything#hasOtherThing", "UTF-8")}/${URLEncoder.encode("http://rdfh.ch/0001/another-thing", "UTF-8")}"
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseObj = AkkaHttpUtils.httpResponseToJson(response).fields
val comment = responseObj("comment").asInstanceOf[JsString].value
val linkValue = responseObj("value").asJsObject.fields
assert(
linkValue("subjectIri").asInstanceOf[JsString].value == "http://rdfh.ch/0001/a-thing" &&
linkValue("predicateIri")
.asInstanceOf[JsString]
.value == "http://www.knora.org/ontology/0001/anything#hasOtherThing" &&
linkValue("objectIri").asInstanceOf[JsString].value == "http://rdfh.ch/0001/another-thing" &&
linkValue("referenceCount").asInstanceOf[JsNumber].value.toInt == 1 &&
comment == boringComment
)
}
}
"add a text value with language to a resource" in {
val params =
"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing-with-text-valuesLanguage",
| "prop": "http://www.knora.org/ontology/0001/anything#hasText",
| "richtext_value": {"utf8str":"Guten Tag", "language": "de"}
|}
""".stripMargin
Post("/v1/values", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, testPass)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
assert(responseJson("value").asInstanceOf[JsObject].fields("utf8str").toString.contains("Guten Tag"))
assert(responseJson("value").asInstanceOf[JsObject].fields("language").toString.contains("de"))
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
textValueWithLangIri.set(valueIri)
}
}
"change the previous text value with German language to Persian" in {
val params =
s"""
|{
| "res_id": "http://rdfh.ch/0001/a-thing-with-text-valuesLanguage",
| "prop": "http://www.knora.org/ontology/0001/anything#hasText",
| "richtext_value": {"utf8str": "Salam", "language": "fa"}
|}
""".stripMargin
Put(
s"/v1/values/${URLEncoder.encode(textValueWithLangIri.get, "UTF-8")}",
HttpEntity(ContentTypes.`application/json`, params)
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, testPass)) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJson: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
assert(responseJson("value").asInstanceOf[JsObject].fields("utf8str").toString.contains("Salam"))
assert(responseJson("value").asInstanceOf[JsObject].fields("language").toString.contains("fa"))
val valueIri: IRI = responseJson("id").asInstanceOf[JsString].value
textValueWithLangIri.set(valueIri)
}
}
}
}