/
SearchV1R2RSpec.scala
351 lines (220 loc) · 12.7 KB
/
SearchV1R2RSpec.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
/*
* Copyright © 2015-2021 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.v1
import akka.actor.ActorSystem
import akka.http.javadsl.model.StatusCodes
import akka.http.scaladsl.testkit.RouteTestTimeout
import org.knora.webapi._
import org.knora.webapi.exceptions.InvalidApiJsonException
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.routing.v1.SearchRouteV1
import org.scalatest.Assertion
import spray.json._
import scala.concurrent.ExecutionContextExecutor
/**
* End-to-end test specification for the search endpoint. This specification uses the Spray Testkit as documented
* here: http://spray.io/documentation/1.2.2/spray-testkit/
*/
class SearchV1R2RSpec extends R2RSpec {
override def testConfigSource: String =
"""
|# akka.loglevel = "DEBUG"
|# akka.stdout-loglevel = "DEBUG"
""".stripMargin
private val searchPath = new SearchRouteV1(routeData).knoraApiPath
implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(settings.defaultTimeout)
implicit val ec: ExecutionContextExecutor = system.dispatcher
override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/all_data/anything-data.ttl", name = "http://www.knora.org/data/0001/anything"),
RdfDataObject(path = "test_data/demo_data/images-demo-data.ttl", name = "http://www.knora.org/data/00FF/images"),
RdfDataObject(path = "test_data/all_data/incunabula-data.ttl", name = "http://www.knora.org/data/0803/incunabula")
)
/**
* Checks for the number of expected results to be returned.
*
* @param responseJson the response send back by the search route.
* @param expectedNumber the expected number of results for the query.
* @return an assertion that the actual amount of results corresponds with the expected number of results.
*/
def checkNumberOfHits(responseJson: String, expectedNumber: Int): Assertion = {
val response: Map[String, JsValue] = responseAs[String].parseJson.asJsObject.fields
val nhits = response.getOrElse("nhits", throw InvalidApiJsonException(s"No member 'nhits' given"))
nhits match {
case JsString(hits) =>
assert(hits == expectedNumber.toString, s"expected $expectedNumber result, but $hits given")
case _ => throw InvalidApiJsonException(s"'nhits' is not a number")
}
}
"The Search Endpoint" should {
"perform an extended search for the pages of a book whose seqnum is lower than or equals 10" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23partOf&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23seqnum&compop=EQ&compop=LT_EQ&searchval=http%3A%2F%2Frdfh.ch%2F0803%2F5e77e98d2603&searchval=10"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23page"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 10)
}
}
"perform an extended search for books that have been published on the first of March 1497 (Julian Calendar)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23pubdate&compop=EQ&searchval=JULIAN:1497-03-01"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23book"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 2)
}
}
"perform an extended search for books that have not been published on the first of March 1497 (Julian Calendar)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23pubdate&compop=!EQ&searchval=JULIAN:1497-03-01"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23book"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
// this is the negation of the query condition above, hence the size of the result set must be 19 (total of incunabula:book) minus 2 (number of results from query above)
checkNumberOfHits(responseAs[String], 17)
}
}
"perform an extended search for books that have been published before 1497 (Julian Calendar)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23pubdate&compop=LT&searchval=JULIAN:1497"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23book"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 15)
}
}
"perform an extended search for books that have been published 1497 or later (Julian Calendar)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23pubdate&compop=GT_EQ&searchval=JULIAN:1497"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23book"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 4)
}
}
"perform an extended search for books that have been published after 1497 (Julian Calendar)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23pubdate&compop=GT&searchval=JULIAN:1497"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23book"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 1)
}
}
"perform an extended search for books that have been published 1497 or before (Julian Calendar)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23pubdate&compop=LT_EQ&searchval=JULIAN:1497"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0803%2Fincunabula%23book"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
// this is the negation of the query condition above, hence the size of the result set must be 19 (total of incunabula:book) minus 1 (number of results from query above)
checkNumberOfHits(responseAs[String], 18)
}
}
"perform an extended search for an anything:Thing that has a Boolean value set to true" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasBoolean&compop=EQ&searchval=true"
val filter = "&show_nrows=25&start_at=0&filter_by_project=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 2)
}
}
"perform an extended search for an anything:Thing that has a Boolean value that is not false" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasBoolean&compop=!EQ&searchval=false"
val filter = "&show_nrows=25&start_at=0&filter_by_project=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 2)
}
}
"perform an extended search for an anything:Thing that has a Boolean value (EXISTS)" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasBoolean&compop=EXISTS&searchval="
val filter = "&show_nrows=25&start_at=0&filter_by_project=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 2)
}
}
val props_two_lists_one =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasListItem&compop=EQ&searchval=http%3A%2F%2Frdfh.ch%2Flists%2F0001%2FtreeList01"
"perform an extended search for an anything:Thing that has two list values on one of the lists (EQ)" in {
val filter = "&show_nrows=25&start_at=0&filter_by_project=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001"
Get("/v1/search/?searchtype=extended" + props_two_lists_one + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 2)
}
}
val props_two_lists_two =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasOtherListItem&compop=EQ&searchval=http%3A%2F%2Frdfh.ch%2Flists%2F0001%2FotherTreeList02"
"perform an extended search for an anything:Thing that has two list values on the other lists (EQ)" in {
val filter = "&show_nrows=25&start_at=0&filter_by_project=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001"
Get("/v1/search/?searchtype=extended" + props_two_lists_two + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 1)
}
}
"perform an extended search for an anything:Thing that has two list values on both lists (EQ)" in {
val filter = "&show_nrows=25&start_at=0&filter_by_project=http%3A%2F%2Frdfh.ch%2Fprojects%2F0001"
Get("/v1/search/?searchtype=extended" + props_two_lists_one + props_two_lists_two + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 1)
}
}
"perform an extended search for an anything:Thing with a timestamp" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasTimeStamp&compop=GT&searchval=2019-08-30T10%3A45%3A26.365863Z"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23Thing"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 1)
}
}
"perform an extended search for an anything:Thing with a Geoname id" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasGeoname&compop=EQ&searchval=2661604"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23Thing"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 1)
}
}
"perform an extended search for an anything:Thing with a URI" in {
val props =
"&property_id=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23hasUri&compop=EQ&searchval=http%3A%2F%2Fwww.google.ch"
val filter =
"&show_nrows=25&start_at=0&filter_by_restype=http%3A%2F%2Fwww.knora.org%2Fontology%2F0001%2Fanything%23Thing"
Get("/v1/search/?searchtype=extended" + props + filter) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
checkNumberOfHits(responseAs[String], 1)
}
}
}
}