/
ValuesV2R2RSpec.scala
207 lines (174 loc) · 7.86 KB
/
ValuesV2R2RSpec.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
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.e2e.v2
import akka.actor.ActorSystem
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import akka.http.scaladsl.testkit.RouteTestTimeout
import scala.concurrent.ExecutionContextExecutor
import dsp.errors.AssertionException
import org.knora.webapi._
import org.knora.webapi.e2e.ClientTestDataCollector
import org.knora.webapi.e2e.TestDataFileContent
import org.knora.webapi.e2e.TestDataFilePath
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.rdf._
import org.knora.webapi.messages.util.search.SparqlQueryConstants
import org.knora.webapi.routing.v2.SearchRouteV2
import org.knora.webapi.routing.v2.ValuesRouteV2
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.util.MutableTestIri
/**
* Tests creating a still image file value using a mock Sipi.
*/
class ValuesV2R2RSpec extends R2RSpec {
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
private val valuesPath = new ValuesRouteV2(routeData).makeRoute
private val searchPath = new SearchRouteV2(routeData).makeRoute
implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(
appConfig.defaultTimeoutAsDuration
)
implicit val ec: ExecutionContextExecutor = system.dispatcher
/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers(Some(system))
private val aThingPictureIri = "http://rdfh.ch/0001/a-thing-picture"
private val anythingUserEmail = SharedTestDataADM.anythingUser1.email
private val password = SharedTestDataADM.testPass
private val stillImageFileValueIri = new MutableTestIri
// Directory path for generated client test data
private val clientTestDataPath: Seq[String] = Seq("v2", "values")
// Collects client test data
private val clientTestDataCollector = new ClientTestDataCollector(appConfig)
override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/all_data/anything-data.ttl", name = "http://www.knora.org/data/0001/anything")
)
private def getResourceWithValues(
resourceIri: IRI,
propertyIrisForGravsearch: Seq[SmartIri],
userEmail: String
): JsonLDDocument = {
// Make a Gravsearch query from a template.
val gravsearchQuery: String = org.knora.webapi.messages.twirl.queries.gravsearch.txt
.getResourceWithSpecifiedProperties(
resourceIri = resourceIri,
propertyIris = propertyIrisForGravsearch
)
.toString()
// Run the query.
Post(
"/v2/searchextended",
HttpEntity(SparqlQueryConstants.`application/sparql-query`, gravsearchQuery)
) ~> addCredentials(BasicHttpCredentials(userEmail, password)) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
responseToJsonLDDocument(response)
}
}
private def getValuesFromResource(resource: JsonLDDocument, propertyIriInResult: SmartIri): JsonLDArray =
resource.requireArray(propertyIriInResult.toString)
private def getValueFromResource(
resource: JsonLDDocument,
propertyIriInResult: SmartIri,
expectedValueIri: IRI
): JsonLDObject = {
val resourceIri: IRI = resource.requireStringWithValidation(JsonLDKeywords.ID, stringFormatter.validateAndEscapeIri)
val propertyValues: JsonLDArray =
getValuesFromResource(resource = resource, propertyIriInResult = propertyIriInResult)
val matchingValues: Seq[JsonLDObject] = propertyValues.value.collect {
case jsonLDObject: JsonLDObject
if jsonLDObject.requireStringWithValidation(
JsonLDKeywords.ID,
stringFormatter.validateAndEscapeIri
) == expectedValueIri =>
jsonLDObject
}
if (matchingValues.isEmpty) {
throw AssertionException(
s"Property <$propertyIriInResult> of resource <$resourceIri> does not have value <$expectedValueIri>"
)
}
if (matchingValues.size > 1) {
throw AssertionException(
s"Property <$propertyIriInResult> of resource <$resourceIri> has more than one value with the IRI <$expectedValueIri>"
)
}
matchingValues.head
}
private def getValue(
resourceIri: IRI,
propertyIriForGravsearch: SmartIri,
propertyIriInResult: SmartIri,
expectedValueIri: IRI,
userEmail: String
): JsonLDObject = {
val resource: JsonLDDocument = getResourceWithValues(
resourceIri = resourceIri,
propertyIrisForGravsearch = Seq(propertyIriForGravsearch),
userEmail = userEmail
)
getValueFromResource(
resource = resource,
propertyIriInResult = propertyIriInResult,
expectedValueIri = expectedValueIri
)
}
"The values v2 endpoint" should {
"update a still image file value using a mock Sipi" in {
val resourceIri: IRI = aThingPictureIri
val internalFilename = "IQUO3t1AABm-FSLC0vNvVpr.jp2"
val jsonLDEntity =
s"""{
| "@id" : "$resourceIri",
| "@type" : "anything:ThingPicture",
| "knora-api:hasStillImageFileValue" : {
| "@id" : "http://rdfh.ch/0001/a-thing-picture/values/goZ7JFRNSeqF-dNxsqAS7Q",
| "@type" : "knora-api:StillImageFileValue",
| "knora-api:fileValueHasFilename" : "$internalFilename"
| },
| "@context" : {
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}""".stripMargin
clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "update-still-image-file-value-request",
fileExtension = "json"
),
text = jsonLDEntity
)
)
Put("/v2/values", HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password)
) ~> valuesPath ~> check {
assert(status == StatusCodes.OK, response.toString)
val responseJsonDoc = responseToJsonLDDocument(response)
val valueIri: IRI =
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, stringFormatter.validateAndEscapeIri)
stillImageFileValueIri.set(valueIri)
val valueType: SmartIri =
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.TYPE, stringFormatter.toSmartIriWithErr)
valueType should ===(OntologyConstants.KnoraApiV2Complex.StillImageFileValue.toSmartIri)
val savedValue: JsonLDObject = getValue(
resourceIri = resourceIri,
propertyIriForGravsearch = OntologyConstants.KnoraApiV2Complex.HasStillImageFileValue.toSmartIri,
propertyIriInResult = OntologyConstants.KnoraApiV2Complex.HasStillImageFileValue.toSmartIri,
expectedValueIri = stillImageFileValueIri.get,
userEmail = anythingUserEmail
)
savedValue.requireString(OntologyConstants.KnoraApiV2Complex.FileValueHasFilename) should ===(internalFilename)
savedValue.requireInt(OntologyConstants.KnoraApiV2Complex.StillImageFileValueHasDimX) should ===(512)
savedValue.requireInt(OntologyConstants.KnoraApiV2Complex.StillImageFileValueHasDimY) should ===(256)
}
}
}
}