-
Notifications
You must be signed in to change notification settings - Fork 18
/
ListsADME2ESpec.scala
556 lines (421 loc) · 25.1 KB
/
ListsADME2ESpec.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright © 2015-2019 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.e2e.admin
import akka.actor.ActorSystem
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.testkit.RouteTestTimeout
import scala.concurrent.Await
import com.typesafe.config.{Config, ConfigFactory}
import org.knora.webapi.messages.admin.responder.listsmessages._
import org.knora.webapi.messages.store.triplestoremessages.{RdfDataObject, StringLiteralV2, TriplestoreJsonProtocol}
import org.knora.webapi.messages.v1.responder.sessionmessages.SessionJsonProtocol
import org.knora.webapi.messages.v1.routing.authenticationmessages.CredentialsADM
import org.knora.webapi.util.{AkkaHttpUtils, MutableTestIri}
import org.knora.webapi.{E2ESpec, SharedListsTestDataADM, SharedTestDataADM, SharedTestDataV1}
import scala.concurrent.duration._
object ListsADME2ESpec {
val config: Config = ConfigFactory.parseString(
"""
akka.loglevel = "DEBUG"
akka.stdout-loglevel = "DEBUG"
""".stripMargin)
}
/**
* End-to-End (E2E) test specification for testing users endpoint.
*/
class ListsADME2ESpec extends E2ESpec(ListsADME2ESpec.config) with SessionJsonProtocol with TriplestoreJsonProtocol with ListADMJsonProtocol {
implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(5.seconds)
override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/demo_data/images-demo-data.ttl", name = "http://www.knora.org/data/00FF/images"),
RdfDataObject(path = "test_data/all_data/anything-data.ttl", name = "http://www.knora.org/data/0001/anything")
)
val rootCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.rootUser,
"test"
)
val normalUserCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.normalUser,
"test"
)
val anythingUserCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.anythingUser1,
"test"
)
val anythingAdminUserCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.anythingAdminUser,
"test"
)
private val treeListInfo: ListRootNodeInfoADM = SharedListsTestDataADM.treeListInfo
private val treeListNodes: Seq[ListChildNodeADM] = SharedListsTestDataADM.treeListChildNodes
"The Lists Route (/admin/lists)" when {
"used to query information about lists" should {
"return all lists" in {
val request = Get(baseApiUrl + s"/admin/lists") ~> addCredentials(BasicHttpCredentials(rootCreds.email, rootCreds.password))
val response: HttpResponse = singleAwaitingRequest(request)
// println(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val lists: Seq[ListNodeInfoADM] = AkkaHttpUtils.httpResponseToJson(response).fields("lists").convertTo[Seq[ListNodeInfoADM]]
// log.debug("lists: {}", lists)
lists.size should be (7)
}
"return all lists belonging to the images project" in {
val request = Get(baseApiUrl + s"/admin/lists?projectIri=http%3A%2F%2Frdfh.ch%2Fprojects%2F00FF") ~> addCredentials(rootCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val lists: Seq[ListNodeInfoADM] = AkkaHttpUtils.httpResponseToJson(response).fields("lists").convertTo[Seq[ListNodeInfoADM]]
// log.debug("lists: {}", lists)
lists.size should be (4)
}
"return all lists belonging to the anything project" in {
val request = Get(baseApiUrl + s"/admin/lists?projectIri=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001") ~> addCredentials(rootCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val lists: Seq[ListNodeInfoADM] = AkkaHttpUtils.httpResponseToJson(response).fields("lists").convertTo[Seq[ListNodeInfoADM]]
// log.debug("lists: {}", lists)
lists.size should be (2)
}
"return basic list information" in {
val request = Get(baseApiUrl + s"/admin/lists/infos/http%3A%2F%2Frdfh.ch%2Flists%2F0001%2FtreeList") ~> addCredentials(rootCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val receivedListInfo: ListRootNodeInfoADM = AkkaHttpUtils.httpResponseToJson(response).fields("listinfo").convertTo[ListRootNodeInfoADM]
val expectedListInfo: ListRootNodeInfoADM = SharedListsTestDataADM.treeListInfo
receivedListInfo.sorted should be (expectedListInfo.sorted)
}
"return a complete list" in {
val request = Get(baseApiUrl + s"/admin/lists/http%3A%2F%2Frdfh.ch%2Flists%2F0001%2FtreeList") ~> addCredentials(rootCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// println(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val receivedList: ListADM = AkkaHttpUtils.httpResponseToJson(response).fields("list").convertTo[ListADM]
receivedList.listinfo.sorted should be (treeListInfo.sorted)
receivedList.children.map(_.sorted) should be (treeListNodes.map(_.sorted))
}
}
"given a custom Iri" should {
"create a list with the provided custom Iri" in {
val request = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, SharedTestDataADM.createListWithCustomIriRequest)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.OK)
val receivedList: ListADM = AkkaHttpUtils.httpResponseToJson(response).fields("list").convertTo[ListADM]
val listInfo = receivedList.listinfo
listInfo.id should be (SharedTestDataADM.customListIRI)
val labels: Seq[StringLiteralV2] = listInfo.labels.stringLiterals
labels.size should be (1)
labels.head should be (StringLiteralV2(value = "New list with a custom IRI", language = Some("en")))
}
"return a DuplicateValueException during list creation when the supplied list IRI is not unique" in {
// duplicate list IRI
val params =
s"""
|{
| "id": "${SharedTestDataADM.customListIRI}",
| "projectIri": "${SharedTestDataADM.ANYTHING_PROJECT_IRI}",
| "labels": [{ "value": "New List", "language": "en"}],
| "comments": []
|}
""".stripMargin
val request = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.BadRequest)
val errorMessage : String = Await.result(Unmarshal(response.entity).to[String], 1.second)
val invalidIri: Boolean = errorMessage.contains(s"IRI: '${SharedTestDataADM.customListIRI}' already exists, try another one.")
invalidIri should be(true)
}
}
"used to modify list information" should {
val newListIri = new MutableTestIri
val firstChildIri = new MutableTestIri
val secondChildIri = new MutableTestIri
val thirdChildIri = new MutableTestIri
"create a list" in {
val request = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, SharedTestDataADM.createListRequest)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val receivedList: ListADM = AkkaHttpUtils.httpResponseToJson(response).fields("list").convertTo[ListADM]
val listInfo = receivedList.listinfo
listInfo.projectIri should be (SharedTestDataADM.ANYTHING_PROJECT_IRI)
val labels: Seq[StringLiteralV2] = listInfo.labels.stringLiterals
labels.size should be (1)
labels.head should be (StringLiteralV2(value = "Neue Liste", language = Some("de")))
val comments = receivedList.listinfo.comments.stringLiterals
comments.isEmpty should be (true)
val children = receivedList.children
children.size should be (0)
// store list IRI for next test
newListIri.set(listInfo.id)
}
"return a ForbiddenException if the user creating the list is not project or system admin" in {
val params =
s"""
|{
| "projectIri": "${SharedTestDataADM.ANYTHING_PROJECT_IRI}",
| "labels": [{ "value": "Neue Liste", "language": "de"}],
| "comments": []
|}
""".stripMargin
val request = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.Forbidden)
}
"return a BadRequestException during list creation when payload is not correct" in {
// no project IRI
val params01 =
s"""
|{
| "projectIri": "",
| "labels": [{ "value": "Neue Liste", "language": "de"}],
| "comments": []
|}
""".stripMargin
val request01 = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, params01))
val response01: HttpResponse = singleAwaitingRequest(request01)
// println(s"response: ${response01.toString}")
response01.status should be(StatusCodes.BadRequest)
// invalid project IRI
val params02 =
s"""
|{
| "projectIri": "notvalidIRI",
| "labels": [{ "value": "Neue Liste", "language": "de"}],
| "comments": []
|}
""".stripMargin
val request02 = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, params02))
val response02: HttpResponse = singleAwaitingRequest(request02)
// println(s"response: ${response02.toString}")
response02.status should be(StatusCodes.BadRequest)
// missing label
val params03 =
s"""
|{
| "projectIri": "${SharedTestDataADM.ANYTHING_PROJECT_IRI}",
| "labels": [],
| "comments": []
|}
""".stripMargin
val request03 = Post(baseApiUrl + s"/admin/lists", HttpEntity(ContentTypes.`application/json`, params03))
val response03: HttpResponse = singleAwaitingRequest(request03)
// println(s"response: ${response03.toString}")
response03.status should be(StatusCodes.BadRequest)
}
"update basic list information" in {
val params = SharedTestDataADM.updateListInfoRequest(newListIri.get)
val encodedListUrl = java.net.URLEncoder.encode(newListIri.get, "utf-8")
val request = Put(baseApiUrl + s"/admin/lists/infos/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val receivedListInfo: ListRootNodeInfoADM = AkkaHttpUtils.httpResponseToJson(response).fields("listinfo").convertTo[ListRootNodeInfoADM]
receivedListInfo.projectIri should be (SharedTestDataADM.ANYTHING_PROJECT_IRI)
val labels: Seq[StringLiteralV2] = receivedListInfo.labels.stringLiterals
labels.size should be (2)
val comments = receivedListInfo.comments.stringLiterals
comments.size should be (2)
}
"return a ForbiddenException if the user updating the list is not project or system admin" in {
val params =
s"""
|{
| "listIri": "${newListIri.get}",
| "projectIri": "${SharedTestDataADM.ANYTHING_PROJECT_IRI}",
| "labels": [{ "value": "Neue geönderte Liste", "language": "de"}, { "value": "Changed list", "language": "en"}],
| "comments": [{ "value": "Neuer Kommentar", "language": "de"}, { "value": "New comment", "language": "en"}]
|}
""".stripMargin
val encodedListUrl = java.net.URLEncoder.encode(newListIri.get, "utf-8")
val request = Put(baseApiUrl + s"/admin/lists/infos/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// log.debug(s"response: ${response.toString}")
response.status should be(StatusCodes.Forbidden)
}
"return a BadRequestException during list change when payload is not correct" in {
val encodedListUrl = java.net.URLEncoder.encode(newListIri.get, "utf-8")
// empty list IRI
val params01 =
s"""
|{
| "listIri": "",
| "projectIri": "${SharedTestDataADM.ANYTHING_PROJECT_IRI}",
| "labels": [{ "value": "Neue geönderte Liste", "language": "de"}, { "value": "Changed list", "language": "en"}],
| "comments": [{ "value": "Neuer Kommentar", "language": "de"}, { "value": "New comment", "language": "en"}]
|}
""".stripMargin
val request01 = Put(baseApiUrl + s"/admin/lists/infos/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params01)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response01: HttpResponse = singleAwaitingRequest(request01)
// log.debug(s"response: ${response.toString}")
response01.status should be(StatusCodes.BadRequest)
// empty project
val params02 =
s"""
|{
| "listIri": "${newListIri.get}",
| "projectIri": "",
| "labels": [{ "value": "Neue geönderte Liste", "language": "de"}, { "value": "Changed list", "language": "en"}],
| "comments": [{ "value": "Neuer Kommentar", "language": "de"}, { "value": "New comment", "language": "en"}]
|}
""".stripMargin
val request02 = Put(baseApiUrl + s"/admin/lists/infos/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params02)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response02: HttpResponse = singleAwaitingRequest(request02)
// log.debug(s"response: ${response.toString}")
response02.status should be(StatusCodes.BadRequest)
// empty parameters
val params03 =
s"""
|{
| "listIri": "${newListIri.get}",
| "projectIri": "${SharedTestDataADM.ANYTHING_PROJECT_IRI}",
| "labels": [],
| "comments": []
|}
""".stripMargin
val request03 = Put(baseApiUrl + s"/admin/lists/infos/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params03)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response03: HttpResponse = singleAwaitingRequest(request03)
// log.debug(s"response: ${response.toString}")
response03.status should be(StatusCodes.BadRequest)
}
"add child to list - to the root node" in {
val encodedListUrl = java.net.URLEncoder.encode(newListIri.get, "utf-8")
val name = "first"
val label = "New First Child List Node Value"
val comment = "New First Child List Node Comment"
val params = SharedTestDataADM.addChildListNodeRequest(
parentNodeIri = newListIri.get,
name = name,
label = label,
comment = comment
)
val request = Post(baseApiUrl + s"/admin/lists/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// println(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val received: ListNodeInfoADM = AkkaHttpUtils.httpResponseToJson(response).fields("nodeinfo").convertTo[ListNodeInfoADM]
// check correct node info
val childNodeInfo = received match {
case info: ListChildNodeInfoADM => info
case something => fail(s"expecting ListChildNodeInfoADM but got ${something.getClass.toString} instead.")
}
// check labels
val labels: Seq[StringLiteralV2] = childNodeInfo.labels.stringLiterals
labels.size should be (1)
labels.sorted should be (Seq(StringLiteralV2(value = label, language = Some("en"))))
// check comments
val comments = childNodeInfo.comments.stringLiterals
comments.size should be (1)
comments.sorted should be (Seq(StringLiteralV2(value = comment, language = Some("en"))))
// check position
val position = childNodeInfo.position
position should be (0)
// check has root node
val rootNode = childNodeInfo.hasRootNode
rootNode should be (newListIri.get)
firstChildIri.set(childNodeInfo.id)
}
"add second child to list - to the root node" in {
val encodedListUrl = java.net.URLEncoder.encode(newListIri.get, "utf-8")
val name = "second"
val label = "New Second Child List Node Value"
val comment = "New Second Child List Node Comment"
val params = SharedTestDataADM.addChildListNodeRequest(
parentNodeIri = newListIri.get,
name = name,
label = label,
comment = comment
)
val request = Post(baseApiUrl + s"/admin/lists/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// println(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val received: ListNodeInfoADM = AkkaHttpUtils.httpResponseToJson(response).fields("nodeinfo").convertTo[ListNodeInfoADM]
// check correct node info
val childNodeInfo = received match {
case info: ListChildNodeInfoADM => info
case something => fail(s"expecting ListChildNodeInfoADM but got ${something.getClass.toString} instead.")
}
// check labels
val labels: Seq[StringLiteralV2] = childNodeInfo.labels.stringLiterals
labels.size should be (1)
labels.sorted should be (Seq(StringLiteralV2(value = label, language = Some("en"))))
// check comments
val comments = childNodeInfo.comments.stringLiterals
comments.size should be (1)
comments.sorted should be (Seq(StringLiteralV2(value = comment, language = Some("en"))))
// check position
val position = childNodeInfo.position
position should be (1)
// check has root node
val rootNode = childNodeInfo.hasRootNode
rootNode should be (newListIri.get)
secondChildIri.set(childNodeInfo.id)
}
"add child to second child node" in {
val encodedListUrl = java.net.URLEncoder.encode(secondChildIri.get, "utf-8")
val name = "third"
val label = "New Third Child List Node Value"
val comment = "New Third Child List Node Comment"
val params = SharedTestDataADM.addChildListNodeRequest(
parentNodeIri = secondChildIri.get,
name = name,
label = label,
comment = comment
)
val request = Post(baseApiUrl + s"/admin/lists/" + encodedListUrl, HttpEntity(ContentTypes.`application/json`, params)) ~> addCredentials(anythingAdminUserCreds.basicHttpCredentials)
val response: HttpResponse = singleAwaitingRequest(request)
// println(s"response: ${response.toString}")
response.status should be(StatusCodes.OK)
val received: ListNodeInfoADM = AkkaHttpUtils.httpResponseToJson(response).fields("nodeinfo").convertTo[ListNodeInfoADM]
// check correct node info
val childNodeInfo = received match {
case info: ListChildNodeInfoADM => info
case something => fail(s"expecting ListChildNodeInfoADM but got ${something.getClass.toString} instead.")
}
// check labels
val labels: Seq[StringLiteralV2] = childNodeInfo.labels.stringLiterals
labels.size should be (1)
labels.sorted should be (Seq(StringLiteralV2(value = label, language = Some("en"))))
// check comments
val comments = childNodeInfo.comments.stringLiterals
comments.size should be (1)
comments.sorted should be (Seq(StringLiteralV2(value = comment, language = Some("en"))))
// check position
val position = childNodeInfo.position
position should be (0)
// check has root node
val rootNode = childNodeInfo.hasRootNode
rootNode should be (newListIri.get)
thirdChildIri.set(childNodeInfo.id)
}
"add flat nodes" ignore {
}
"add hierarchical nodes" ignore {
}
"change node order" ignore {
}
"delete node if not in use" ignore {
}
}
}
}