-
Notifications
You must be signed in to change notification settings - Fork 18
/
TriplestoreServiceLiveSpec.scala
225 lines (188 loc) · 6.63 KB
/
TriplestoreServiceLiveSpec.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
/*
* Copyright © 2021 - 2023 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.store.triplestore
import akka.testkit.ImplicitSender
import scala.concurrent.duration._
import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.store.triplestoremessages._
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Select
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Update
class TriplestoreServiceLiveSpec extends CoreSpec with ImplicitSender {
override implicit val timeout: FiniteDuration = 30.seconds
override lazy val rdfDataObjects: List[RdfDataObject] = List(
RdfDataObject("test_data/project_data/anything-data.ttl", "http://www.knora.org/data/0001/anything")
)
val countTriplesQuery: String =
"""
SELECT (COUNT(*) AS ?no)
WHERE
{
?s ?p ?o .
}
"""
val namedGraphQuery: String =
"""
SELECT ?namedGraph ?s ?p ?o ?lang
WHERE {
{
GRAPH ?namedGraph {
BIND(IRI("http://www.knora.org/ontology/0001/anything#Thing") as ?s)
?s ?p ?obj
BIND(str(?obj) as ?o)
BIND(lang(?obj) as ?lang)
}
}
}
""".stripMargin
val insertQuery: String =
"""
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix sub: <http://subotic.org/#>
INSERT DATA
{
GRAPH <http://subotic.org/graph>
{
<http://ivan> sub:tries "something" ;
sub:hopes "success" ;
rdf:type sub:Me .
}
}
"""
val graphDataContent: String =
"""
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix jedi: <http://jedi.org/#>
<http://luke> jedi:tries "force for the first time" ;
jedi:hopes "to power the lightsaber" ;
rdf:type jedi:Skywalker .
"""
val checkInsertQuery: String =
"""
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix sub: <http://subotic.org/#>
SELECT *
WHERE {
GRAPH <http://subotic.org/graph>
{
?s rdf:type sub:Me .
?s ?p ?o .
}
}
"""
val revertInsertQuery: String =
"""
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix sub: <http://subotic.org/#>
WITH <http://subotic.org/graph>
DELETE { ?s ?p ?o }
WHERE
{
?s rdf:type sub:Me .
?s ?p ?o .
}
"""
val searchURI: String = "<http://jena.apache.org/text#query>"
val textSearchQueryFusekiValueHasString: String =
s"""
PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
SELECT DISTINCT *
WHERE {
?iri <http://jena.apache.org/text#query> 'test' .
?iri knora-base:valueHasString ?literal .
}
"""
val textSearchQueryFusekiDRFLabel: String =
s"""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT *
WHERE {
?iri <http://jena.apache.org/text#query> 'Papa' .
?iri rdfs:label ?literal .
}
"""
var afterLoadCount: Int = -1
var afterChangeCount: Int = -1
var afterChangeRevertCount: Int = -1
"The TriplestoreService" should {
"reset the data after receiving a 'ResetTriplestoreContent' request" in {
UnsafeZioRun.runOrThrow(
TriplestoreService
.resetTripleStoreContent(rdfDataObjects)
.timeout(java.time.Duration.ofMinutes(5))
)
val msg = UnsafeZioRun.runOrThrow(TriplestoreService.query(Select(countTriplesQuery)))
afterLoadCount = msg.results.bindings.head.rowMap("no").toInt
(afterLoadCount > 0) should ===(true)
}
"provide data receiving a Named Graph request" in {
val actual = UnsafeZioRun.runOrThrow(TriplestoreService.query(Select(namedGraphQuery)))
actual.results.bindings.nonEmpty should ===(true)
}
"execute an update" in {
val countTriplesBefore = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(countTriplesQuery))
.map(_.results.bindings.head.rowMap("no").toInt)
)
countTriplesBefore should ===(afterLoadCount)
UnsafeZioRun.runOrThrow(TriplestoreService.query(Update(insertQuery)))
val checkInsertActual = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(checkInsertQuery))
.map(_.results.bindings.size)
)
checkInsertActual should ===(3)
afterChangeCount = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(countTriplesQuery))
.map(_.results.bindings.head.rowMap("no").toInt)
)
(afterChangeCount - afterLoadCount) should ===(3)
}
"revert back " in {
val countTriplesBefore = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(countTriplesQuery))
.map(_.results.bindings.head.rowMap("no").toInt)
)
countTriplesBefore should ===(afterChangeCount)
UnsafeZioRun.runOrThrow(TriplestoreService.query(Update(revertInsertQuery)))
val countTriplesQueryActual = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(countTriplesQuery))
.map(_.results.bindings.head.rowMap("no").toInt)
)
countTriplesQueryActual should ===(afterLoadCount)
val checkInsertActual = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(checkInsertQuery))
.map(_.results.bindings.size)
)
checkInsertActual should ===(0)
}
"execute the search with the lucene index for 'knora-base:valueHasString' properties" in {
within(1000.millis) {
val actual = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(textSearchQueryFusekiValueHasString))
.map(_.results.bindings.size)
)
actual should ===(3)
}
}
"execute the search with the lucene index for 'rdfs:label' properties" in {
within(1000.millis) {
val actual = UnsafeZioRun.runOrThrow(
TriplestoreService
.query(Select(textSearchQueryFusekiDRFLabel))
.map(_.results.bindings.size)
)
actual should ===(1)
}
}
}
}