/
OntologyV2R2RSpec.scala
981 lines (808 loc) · 58.5 KB
/
OntologyV2R2RSpec.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
package org.knora.webapi.e2e.v2
import java.io.File
import java.net.URLEncoder
import java.time.Instant
import akka.actor.ActorSystem
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.{Accept, BasicHttpCredentials}
import akka.http.scaladsl.testkit.RouteTestTimeout
import org.eclipse.rdf4j.model.Model
import org.knora.webapi._
import org.knora.webapi.exceptions.AssertionException
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util._
import org.knora.webapi.messages.v2.responder.ontologymessages.{InputOntologyV2, TestResponseParsingModeV2}
import org.knora.webapi.messages.{OntologyConstants, SmartIri, StringFormatter}
import org.knora.webapi.routing.v2.OntologiesRouteV2
import org.knora.webapi.sharedtestdata.{SharedOntologyTestDataADM, SharedTestDataADM}
import org.knora.webapi.util._
import spray.json._
import scala.concurrent.ExecutionContextExecutor
object OntologyV2R2RSpec {
private val imagesUserProfile = SharedTestDataADM.imagesUser01
private val imagesUsername = imagesUserProfile.email
private val imagesProjectIri = SharedTestDataADM.IMAGES_PROJECT_IRI
private val anythingUserProfile = SharedTestDataADM.anythingAdminUser
private val anythingUsername = anythingUserProfile.email
private val superUserProfile = SharedTestDataADM.superUser
private val superUsername = superUserProfile.email
private val password = "test"
}
/**
* End-to-end test specification for API v2 ontology routes.
*/
class OntologyV2R2RSpec extends R2RSpec {
import OntologyV2R2RSpec._
override def testConfigSource: String =
"""
|# akka.loglevel = "DEBUG"
|# akka.stdout-loglevel = "DEBUG"
""".stripMargin
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
private val ontologiesPath = new OntologiesRouteV2(routeData).knoraApiPath
implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(settings.defaultTimeout)
implicit val ec: ExecutionContextExecutor = system.dispatcher
// If true, the existing expected response files are overwritten with the HTTP GET responses from the server.
// If false, the responses from the server are compared to the contents fo the expected response files.
private val writeGetTestResponses = false
override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/ontologies/example-box.ttl", name = "http://www.knora.org/ontology/shared/example-box"),
RdfDataObject(path = "test_data/ontologies/minimal-onto.ttl", name = "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 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, disableWrite: Boolean = false) {
def makeFile(mediaType: MediaType.NonBinary): File = {
val fileSuffix = mediaType.fileExtensions.head
new File(s"test_data/ontologyR2RV2/$fileBasename.$fileSuffix")
}
/**
* Writes the expected response file.
*
* @param responseStr the contents of the file to be written.
* @param mediaType the media type of the response.
*/
def writeFile(responseStr: String, mediaType: MediaType.NonBinary): Unit = {
if (!disableWrite) {
// Per default only read access is allowed in the bazel sandbox.
// This workaround allows to save test output.
val testOutputDir = sys.env("TEST_UNDECLARED_OUTPUTS_DIR")
val file = makeFile(mediaType)
val newOutputFile = new File(testOutputDir, file.getPath)
newOutputFile.getParentFile.mkdirs()
FileUtil.writeTextFile(newOutputFile, responseStr)
}
}
/**
* 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))
}
}
// URL-encoded IRIs for use as URL segments in HTTP GET tests.
private val imagesProjectSegment = URLEncoder.encode(imagesProjectIri, "UTF-8")
private val knoraApiSimpleOntologySegment = URLEncoder.encode(OntologyConstants.KnoraApiV2Simple.KnoraApiOntologyIri, "UTF-8")
private val knoraApiWithValueObjectsOntologySegment = URLEncoder.encode(OntologyConstants.KnoraApiV2Complex.KnoraApiOntologyIri, "UTF-8")
private val incunabulaOntologySimpleSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0803/incunabula/simple/v2", "UTF-8")
private val incunabulaOntologyWithValueObjectsSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0803/incunabula/v2", "UTF-8")
private val knoraApiDateSegment = URLEncoder.encode("http://api.knora.org/ontology/knora-api/simple/v2#Date", "UTF-8")
private val knoraApiDateValueSegment = URLEncoder.encode("http://api.knora.org/ontology/knora-api/v2#DateValue", "UTF-8")
private val knoraApiSimpleHasColorSegment = URLEncoder.encode("http://api.knora.org/ontology/knora-api/simple/v2#hasColor", "UTF-8")
private val knoraApiWithValueObjectsHasColorSegment = URLEncoder.encode("http://api.knora.org/ontology/knora-api/v2#hasColor", "UTF-8")
private val incunabulaSimplePubdateSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0803/incunabula/simple/v2#pubdate", "UTF-8")
private val incunabulaWithValueObjectsPubDateSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0803/incunabula/v2#pubdate", "UTF-8")
private val incunabulaWithValueObjectsPageSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0803/incunabula/v2#page", "UTF-8")
private val incunabulaWithValueObjectsBookSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0803/incunabula/v2#book", "UTF-8")
private val boxOntologyWithValueObjectsSegment = URLEncoder.encode("http://api.knora.org/ontology/shared/example-box/v2", "UTF-8")
private val minimalOntologyWithValueObjects = URLEncoder.encode("http://0.0.0.0:3333/ontology/0001/minimal/v2", "UTF-8")
private val anythingOntologyWithValueObjects = URLEncoder.encode("http://0.0.0.0:3333/ontology/0001/anything/v2", "UTF-8")
// The URLs and expected response files for each HTTP GET test.
private val httpGetTests = Seq(
HttpGetTest("/v2/ontologies/metadata", "allOntologyMetadata"),
HttpGetTest(s"/v2/ontologies/metadata/$imagesProjectSegment", "imagesOntologyMetadata"),
HttpGetTest(s"/v2/ontologies/allentities/$knoraApiSimpleOntologySegment", "knoraApiOntologySimple"),
HttpGetTest("/ontology/knora-api/simple/v2", "knoraApiOntologySimple", disableWrite = true),
HttpGetTest(s"/v2/ontologies/allentities/$knoraApiWithValueObjectsOntologySegment", "knoraApiOntologyWithValueObjects"),
HttpGetTest("/ontology/knora-api/v2", "knoraApiOntologyWithValueObjects", disableWrite = true),
HttpGetTest("/ontology/salsah-gui/v2", "salsahGuiOntology"),
HttpGetTest("/ontology/standoff/v2", "standoffOntologyWithValueObjects"),
HttpGetTest(s"/v2/ontologies/allentities/$incunabulaOntologySimpleSegment", "incunabulaOntologySimple"),
HttpGetTest(s"/v2/ontologies/allentities/$incunabulaOntologyWithValueObjectsSegment", "incunabulaOntologyWithValueObjects"),
HttpGetTest(s"/v2/ontologies/classes/$knoraApiDateSegment", "knoraApiDate"),
HttpGetTest(s"/v2/ontologies/classes/$knoraApiDateValueSegment", "knoraApiDateValue"),
HttpGetTest(s"/v2/ontologies/properties/$knoraApiSimpleHasColorSegment", "knoraApiSimpleHasColor"),
HttpGetTest(s"/v2/ontologies/properties/$knoraApiWithValueObjectsHasColorSegment", "knoraApiWithValueObjectsHasColor"),
HttpGetTest(s"/v2/ontologies/properties/$incunabulaSimplePubdateSegment", "incunabulaSimplePubDate"),
HttpGetTest(s"/v2/ontologies/properties/$incunabulaWithValueObjectsPubDateSegment", "incunabulaWithValueObjectsPubDate"),
HttpGetTest(s"/v2/ontologies/classes/$incunabulaWithValueObjectsPageSegment/$incunabulaWithValueObjectsBookSegment", "incunabulaPageAndBookWithValueObjects"),
HttpGetTest(s"/v2/ontologies/allentities/$boxOntologyWithValueObjectsSegment", "boxOntologyWithValueObjects"),
HttpGetTest(s"/v2/ontologies/allentities/$minimalOntologyWithValueObjects", "minimalOntologyWithValueObjects"),
HttpGetTest(s"/v2/ontologies/allentities/$anythingOntologyWithValueObjects", "anythingOntologyWithValueObjects")
)
// The media types that will be used in HTTP Accept headers in HTTP GET tests.
private val mediaTypesForGetTests = Seq(
RdfMediaTypes.`application/ld+json`,
RdfMediaTypes.`text/turtle`,
RdfMediaTypes.`application/rdf+xml`
)
private val fooIri = new MutableTestIri
private var fooLastModDate: Instant = Instant.now
private var anythingLastModDate: Instant = Instant.parse("2017-12-19T15:23:42.166Z")
private val uselessIri = new MutableTestIri
private var uselessLastModDate: Instant = Instant.now
private def getPropertyIrisFromResourceClassResponse(responseJsonDoc: JsonLDDocument): Set[SmartIri] = {
val classDef = responseJsonDoc.body.requireArray("@graph").value.head.asInstanceOf[JsonLDObject]
classDef.value(OntologyConstants.Rdfs.SubClassOf).asInstanceOf[JsonLDArray].value.collect {
case obj: JsonLDObject if !obj.isIri => obj.requireIriInObject(OntologyConstants.Owl.OnProperty, stringFormatter.toSmartIriWithErr)
}.toSet
}
"The Ontologies v2 Endpoint" should {
"serve ontology data in different formats" in {
// Iterate over the HTTP GET tests.
for (httpGetTest <- httpGetTests) {
// Do each test with each media type.
for (mediaType <- mediaTypesForGetTests) {
Get(httpGetTest.urlPath).addHeader(Accept(mediaType)) ~> ontologiesPath ~> check {
val responseStr: String = responseAs[String]
// Are we writing expected response files?
if (writeGetTestResponses) {
// Yes. But only write RDF/XML files if they're semantically different from the ones that we already
// have, to avoid writing new files into Git that differ only in their blank node IDs.
mediaType match {
case RdfMediaTypes.`application/rdf+xml` =>
val existingFile: File = httpGetTest.makeFile(mediaType)
if (existingFile.exists()) {
val parsedResponse: Model = parseRdfXml(responseStr)
val parsedExistingFile: Model = parseRdfXml(httpGetTest.readFile(mediaType))
if (parsedResponse != parsedExistingFile) {
httpGetTest.writeFile(responseStr, mediaType)
}
} else {
httpGetTest.writeFile(responseStr, mediaType)
}
case _ =>
httpGetTest.writeFile(responseStr, mediaType)
}
} else {
// No. Compare the received response with the expected response.
mediaType match {
case RdfMediaTypes.`application/ld+json` =>
assert(JsonParser(responseStr) == JsonParser(httpGetTest.readFile(mediaType)))
case RdfMediaTypes.`text/turtle` =>
assert(parseTurtle(responseStr) == parseTurtle(httpGetTest.readFile(mediaType)))
case RdfMediaTypes.`application/rdf+xml` =>
assert(parseRdfXml(responseStr) == parseRdfXml(httpGetTest.readFile(mediaType)))
case _ => throw AssertionException(s"Unsupported media type for test: $mediaType")
}
}
}
}
}
}
"create an empty ontology called 'foo' with a project code" in {
val label = "The foo ontology"
val params = SharedTestDataADM.createOntology(imagesProjectIri, label)
Post("/v2/ontologies", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(imagesUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
val metadata = responseJsonDoc.body
val ontologyIri = metadata.value("@id").asInstanceOf[JsonLDString].value
assert(ontologyIri == "http://0.0.0.0:3333/ontology/00FF/foo/v2")
fooIri.set(ontologyIri)
assert(metadata.value(OntologyConstants.Rdfs.Label) == JsonLDString(label))
val lastModDate = metadata.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
fooLastModDate = lastModDate
}
}
"change the metadata of 'foo'" in {
val newLabel = "The modified foo ontology"
val params = SharedTestDataADM.changeOntologyMetadata(fooIri.get, newLabel, fooLastModDate)
Put("/v2/ontologies/metadata", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(imagesUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
val metadata = responseJsonDoc.body
val ontologyIri = metadata.value("@id").asInstanceOf[JsonLDString].value
assert(ontologyIri == fooIri.get)
assert(metadata.value(OntologyConstants.Rdfs.Label) == JsonLDString(newLabel))
val lastModDate = metadata.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
assert(lastModDate.isAfter(fooLastModDate))
fooLastModDate = lastModDate
}
}
"delete the 'foo' ontology" in {
val fooIriEncoded = URLEncoder.encode(fooIri.get, "UTF-8")
val lastModificationDate = URLEncoder.encode(fooLastModDate.toString, "UTF-8")
Delete(s"/v2/ontologies/$fooIriEncoded?lastModificationDate=$lastModificationDate") ~> addCredentials(BasicHttpCredentials(imagesUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
}
}
"create a property anything:hasName as a subproperty of knora-api:hasValue and schema:name" in {
val params = SharedTestDataADM.createProperty(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties should ===(paramsAsInput.properties)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"change the rdfs:label of a property" in {
val params = SharedTestDataADM.changePropertyLabel(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Put("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties.head._2.predicates(OntologyConstants.Rdfs.Label.toSmartIri).objects should ===(paramsAsInput.properties.head._2.predicates(OntologyConstants.Rdfs.Label.toSmartIri).objects)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"change the rdfs:comment of a property" in {
val params = SharedTestDataADM.changePropertyComment(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Put("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties.head._2.predicates(OntologyConstants.Rdfs.Comment.toSmartIri).objects should ===(paramsAsInput.properties.head._2.predicates(OntologyConstants.Rdfs.Comment.toSmartIri).objects)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"add an rdfs:comment to a link property that has no rdfs:comment" in {
val params = SharedTestDataADM.addCommentToPropertyThatHasNoComment(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Put("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties.head._2.predicates(OntologyConstants.Rdfs.Comment.toSmartIri).objects should ===(paramsAsInput.properties.head._2.predicates(OntologyConstants.Rdfs.Comment.toSmartIri).objects)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"create a class anything:WildThing that is a subclass of anything:Thing, with a direct cardinality for anything:hasName" in {
val params = SharedTestDataADM.createClassWithCardinalities(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/classes", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes should ===(paramsAsInput.classes)
// Check that cardinalities were inherited from anything:Thing.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://0.0.0.0:3333/ontology/0001/anything/v2#hasDecimal".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"create a class anything:Nothing with no properties" in {
val params = SharedTestDataADM.createClassWithoutCardinalities(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/classes", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes should ===(paramsAsInput.classes)
// Check that cardinalities were inherited from knora-api:Resource.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://api.knora.org/ontology/knora-api/v2#attachedToUser".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"change the labels of a class" in {
val params = SharedTestDataADM.changeClassLabel(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Put("/v2/ontologies/classes", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.predicates(OntologyConstants.Rdfs.Label.toSmartIri).objects should ===(paramsAsInput.classes.head._2.predicates.head._2.objects)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"change the comments of a class" in {
val params = SharedTestDataADM.changeClassComment(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Put("/v2/ontologies/classes", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.predicates(OntologyConstants.Rdfs.Comment.toSmartIri).objects should ===(paramsAsInput.classes.head._2.predicates.head._2.objects)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"create a property anything:hasOtherNothing with knora-api:objectType anything:Nothing" 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" : "$anythingLastModDate"
| },
| "@graph" : [ {
| "@id" : "anything:hasOtherNothing",
| "@type" : "owl:ObjectProperty",
| "knora-api:subjectType" : {
| "@id" : "anything:Nothing"
| },
| "knora-api:objectType" : {
| "@id" : "anything:Nothing"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "Refers to the other Nothing of a Nothing"
| },
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "has nothingness"
| },
| "rdfs:subPropertyOf" : {
| "@id" : "knora-api:hasLinkTo"
| }
| } ],
| "@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
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties should ===(paramsAsInput.properties)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"add a cardinality for the property anything:hasOtherNothing to the class anything:Nothing" in {
val params = SharedTestDataADM.addCardinality(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
val paramsWithAddedLinkValueCardinality = paramsAsInput.copy(
classes = paramsAsInput.classes.map {
case (classIri, classDef) =>
val hasOtherNothingValueCardinality = "http://0.0.0.0:3333/ontology/0001/anything/v2#hasOtherNothingValue".toSmartIri ->
classDef.directCardinalities("http://0.0.0.0:3333/ontology/0001/anything/v2#hasOtherNothing".toSmartIri)
classIri -> classDef.copy(
directCardinalities = classDef.directCardinalities + hasOtherNothingValueCardinality
)
}
)
Post("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.directCardinalities should ===(paramsWithAddedLinkValueCardinality.classes.head._2.directCardinalities)
// Check that cardinalities were inherited from knora-api:Resource.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://api.knora.org/ontology/knora-api/v2#attachedToUser".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"remove the cardinality for the property anything:hasOtherNothing from the class anything:Nothing" in {
val params = SharedTestDataADM.removeCardinalityOfProperty(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
Put("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.directCardinalities.isEmpty should ===(true)
// Check that cardinalities were inherited from knora-api:Resource.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://api.knora.org/ontology/knora-api/v2#attachedToUser".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"delete the property anything:hasOtherNothing" in {
val propertySegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0001/anything/v2#hasOtherNothing", "UTF-8")
val lastModificationDate = URLEncoder.encode(anythingLastModDate.toString, "UTF-8")
Delete(s"/v2/ontologies/properties/$propertySegment?lastModificationDate=$lastModificationDate") ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
responseJsonDoc.requireStringWithValidation("@id", stringFormatter.toSmartIriWithErr) should ===("http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri)
val newAnythingLastModDate = responseJsonDoc.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"create a property anything:hasNothingness with knora-api:subjectType anything:Nothing" 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" : "$anythingLastModDate"
| },
| "@graph" : [ {
| "@id" : "anything:hasNothingness",
| "@type" : "owl:ObjectProperty",
| "knora-api:subjectType" : {
| "@id" : "anything:Nothing"
| },
| "knora-api:objectType" : {
| "@id" : "knora-api:BooleanValue"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "Indicates whether a Nothing has nothingness"
| },
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "has nothingness"
| },
| "rdfs:subPropertyOf" : {
| "@id" : "knora-api:hasValue"
| }
| } ],
| "@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
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties should ===(paramsAsInput.properties)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"add a cardinality for the property anything:hasNothingness to the class anything:Nothing" 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" : "$anythingLastModDate"
| },
| "@graph" : [ {
| "@id" : "anything:Nothing",
| "@type" : "owl:Class",
| "rdfs:subClassOf" : {
| "@type": "owl:Restriction",
| "owl:maxCardinality" : 1,
| "owl:onProperty" : {
| "@id" : "anything:hasNothingness"
| }
| }
| } ],
| "@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
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.directCardinalities should ===(paramsAsInput.classes.head._2.directCardinalities)
// Check that cardinalities were inherited from knora-api:Resource.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://api.knora.org/ontology/knora-api/v2#attachedToUser".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"create a property anything:hasEmptiness with knora-api:subjectType anything:Nothing" 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" : "$anythingLastModDate"
| },
| "@graph" : [ {
| "@id" : "anything:hasEmptiness",
| "@type" : "owl:ObjectProperty",
| "knora-api:subjectType" : {
| "@id" : "anything:Nothing"
| },
| "knora-api:objectType" : {
| "@id" : "knora-api:BooleanValue"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "Indicates whether a Nothing has emptiness"
| },
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "has emptiness"
| },
| "rdfs:subPropertyOf" : {
| "@id" : "knora-api:hasValue"
| }
| } ],
| "@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
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Post("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties should ===(paramsAsInput.properties)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"change the cardinalities of the class anything:Nothing, replacing anything:hasNothingness with anything:hasEmptiness" in {
val params = SharedTestDataADM.replaceClassCardinalities(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(params)).unescape
Put("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.directCardinalities should ===(paramsAsInput.classes.head._2.directCardinalities)
// Check that cardinalities were inherited from knora-api:Resource.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://api.knora.org/ontology/knora-api/v2#attachedToUser".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"delete the property anything:hasNothingness" in {
val propertySegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0001/anything/v2#hasNothingness", "UTF-8")
val lastModificationDate = URLEncoder.encode(anythingLastModDate.toString, "UTF-8")
Delete(s"/v2/ontologies/properties/$propertySegment?lastModificationDate=$lastModificationDate") ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
responseJsonDoc.requireStringWithValidation("@id", stringFormatter.toSmartIriWithErr) should ===("http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri)
val newAnythingLastModDate = responseJsonDoc.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"remove all cardinalities from the class anything:Nothing" in {
val params = SharedTestDataADM.removeAllClassCardinalities(SharedOntologyTestDataADM.ANYTHING_ONTOLOGY_IRI_LocalHost, anythingLastModDate)
Put("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.classes.head._2.directCardinalities.isEmpty should ===(true)
// Check that cardinalities were inherited from knora-api:Resource.
getPropertyIrisFromResourceClassResponse(responseJsonDoc) should contain("http://api.knora.org/ontology/knora-api/v2#attachedToUser".toSmartIri)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"delete the property anything:hasEmptiness" in {
val propertySegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0001/anything/v2#hasEmptiness", "UTF-8")
val lastModificationDate = URLEncoder.encode(anythingLastModDate.toString, "UTF-8")
Delete(s"/v2/ontologies/properties/$propertySegment?lastModificationDate=$lastModificationDate") ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
responseJsonDoc.requireStringWithValidation("@id", stringFormatter.toSmartIriWithErr) should ===("http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri)
val newAnythingLastModDate = responseJsonDoc.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"delete the class anything:Nothing" in {
val classSegment = URLEncoder.encode("http://0.0.0.0:3333/ontology/0001/anything/v2#Nothing", "UTF-8")
val lastModificationDate = URLEncoder.encode(anythingLastModDate.toString, "UTF-8")
Delete(s"/v2/ontologies/classes/$classSegment?lastModificationDate=$lastModificationDate") ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
responseJsonDoc.requireStringWithValidation("@id", stringFormatter.toSmartIriWithErr) should ===("http://0.0.0.0:3333/ontology/0001/anything/v2".toSmartIri)
val newAnythingLastModDate = responseJsonDoc.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
assert(newAnythingLastModDate.isAfter(anythingLastModDate))
anythingLastModDate = newAnythingLastModDate
}
}
"create a shared ontology and put a property in it" in {
val label = "The useless ontology"
val createOntologyJson =
s"""
|{
| "knora-api:ontologyName": "useless",
| "knora-api:attachedToProject": {
| "@id": "${OntologyConstants.KnoraAdmin.DefaultSharedOntologiesProject}"
| },
| "knora-api:isShared": true,
| "rdfs:label": "$label",
| "@context": {
| "rdfs": "${OntologyConstants.Rdfs.RdfsPrefixExpansion}",
| "knora-api": "${OntologyConstants.KnoraApiV2Complex.KnoraApiV2PrefixExpansion}"
| }
|}
""".stripMargin
Post("/v2/ontologies", HttpEntity(RdfMediaTypes.`application/ld+json`, createOntologyJson)) ~> addCredentials(BasicHttpCredentials(superUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
val metadata = responseJsonDoc.body
val ontologyIri = metadata.value("@id").asInstanceOf[JsonLDString].value
assert(ontologyIri == "http://api.knora.org/ontology/shared/useless/v2")
uselessIri.set(ontologyIri)
assert(metadata.value(OntologyConstants.Rdfs.Label) == JsonLDString(label))
val lastModDate = metadata.requireDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.LastModificationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
uselessLastModDate = lastModDate
}
val createPropertyJson =
s"""
|{
| "@id" : "${uselessIri.get}",
| "@type" : "owl:Ontology",
| "knora-api:lastModificationDate" : {
| "@type" : "xsd:dateTimeStamp",
| "@value" : "$uselessLastModDate"
| },
| "@graph" : [ {
| "@id" : "useless:hasSharedName",
| "@type" : "owl:ObjectProperty",
| "knora-api:objectType" : {
| "@id" : "knora-api:TextValue"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "Represents a name"
| },
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "has shared name"
| },
| "rdfs:subPropertyOf" : {
| "@id" : "knora-api:hasValue"
| }
| } ],
| "@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#",
| "useless" : "${uselessIri.get}#"
| }
|}
""".stripMargin
// Convert the submitted JSON-LD to an InputOntologyV2, without SPARQL-escaping, so we can compare it to the response.
val paramsAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(JsonLDUtil.parseJsonLD(createPropertyJson)).unescape
Post("/v2/ontologies/properties", HttpEntity(RdfMediaTypes.`application/ld+json`, createPropertyJson)) ~> addCredentials(BasicHttpCredentials(superUsername, password)) ~> ontologiesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
// Convert the response to an InputOntologyV2 and compare the relevant part of it to the request.
val responseAsInput: InputOntologyV2 = InputOntologyV2.fromJsonLD(responseJsonDoc, parsingMode = TestResponseParsingModeV2).unescape
responseAsInput.properties should ===(paramsAsInput.properties)
// Check that the ontology's last modification date was updated.
val newAnythingLastModDate = responseAsInput.ontologyMetadata.lastModificationDate.get
assert(newAnythingLastModDate.isAfter(uselessLastModDate))
uselessLastModDate = newAnythingLastModDate
}
}
}
}