Skip to content

Commit

Permalink
refactor.spec.kind
Browse files Browse the repository at this point in the history
  • Loading branch information
siers committed Nov 7, 2024
1 parent cc42d41 commit bb52134
Showing 1 changed file with 36 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package org.knora.webapi.e2e.v2
import org.apache.pekko.http.scaladsl.model.HttpEntity
import org.apache.pekko.http.scaladsl.model.StatusCodes
import org.apache.pekko.http.scaladsl.model.headers.BasicHttpCredentials
import org.apache.pekko.http.scaladsl.server.RouteConcatenation.*
import zio.ZIO

import scala.concurrent.ExecutionContextExecutor

Expand All @@ -19,29 +17,23 @@ import dsp.valueobjects.Iri
import org.knora.webapi.*
import org.knora.webapi.messages.IriConversions.*
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.OntologyConstants.{KnoraApiV2Complex => OntConsts}
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.UnsafeZioRun
import org.knora.webapi.routing.v2.ValuesRouteV2
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.slice.search.api.SearchApiRoutes
import org.knora.webapi.util.MutableTestIri
import org.apache.pekko.http.scaladsl.model.HttpResponse

/**
* Tests creating a still image file value using a mock Sipi.
*/
class ValuesV2R2RSpec extends R2RSpec {
class ValuesV2R2RSpec extends E2ESpec {

private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance

private val valuesPath = ValuesRouteV2().makeRoute
private val searchPath = UnsafeZioRun
.runOrThrow(ZIO.serviceWith[SearchApiRoutes](_.routes))
.reduce(_ ~ _)

implicit val ec: ExecutionContextExecutor = system.dispatcher

private val aThingPictureIri = "http://rdfh.ch/0001/a-thing-picture"
Expand Down Expand Up @@ -70,14 +62,14 @@ class ValuesV2R2RSpec extends R2RSpec {
.getResourceWithSpecifiedProperties(resourceIri, propertyIrisForGravsearch)
.toString()

// Run the query.
Post(
"/v2/searchextended",
val request = Post(
baseApiUrl + "/v2/searchextended",
HttpEntity(SparqlQueryConstants.`application/sparql-query`, gravsearchQuery),
) ~> addCredentials(BasicHttpCredentials(userEmail, password)) ~> searchPath ~> check {
assert(status == StatusCodes.OK, response.toString)
responseToJsonLDDocument(response)
}
) ~> addCredentials(BasicHttpCredentials(userEmail, password))

val response: HttpResponse = singleAwaitingRequest(request)
assert(response.status == StatusCodes.OK)
responseToJsonLDDocument(response)
}

private def getValuesFromResource(resource: JsonLDDocument, propertyIriInResult: SmartIri): JsonLDArray =
Expand Down Expand Up @@ -141,7 +133,7 @@ class ValuesV2R2RSpec extends R2RSpec {
"update a still image file value using a mock Sipi" in {
val resourceIri: IRI = aThingPictureIri
val internalFilename = "IQUO3t1AABm-FSLC0vNvVpr.jp2"

// val internalFilename = "De6XyNL4H71-D9QxghOuOPJ.jp2"
val jsonLDEntity =
s"""{
| "@id" : "$resourceIri",
Expand All @@ -157,36 +149,33 @@ class ValuesV2R2RSpec extends R2RSpec {
| }
|}""".stripMargin

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, validationFun)
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,
val header = addHeader("X-Asset-Ingested", "true")
val request =
Put(baseApiUrl + "/v2/values", HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
)

savedValue
.getRequiredString(OntologyConstants.KnoraApiV2Complex.FileValueHasFilename)
.fold(msg => throw BadRequestException(msg), identity) should ===(internalFilename)
savedValue
.getRequiredInt(OntologyConstants.KnoraApiV2Complex.StillImageFileValueHasDimX)
.fold(e => throw BadRequestException(e), identity) should ===(512)
savedValue
.getRequiredInt(OntologyConstants.KnoraApiV2Complex.StillImageFileValueHasDimY)
.fold(e => throw BadRequestException(e), identity) should ===(256)
}
val response: HttpResponse = singleAwaitingRequest(request)
assert(response.status == StatusCodes.OK)
val responseJsonDoc = responseToJsonLDDocument(response)
val valueIri: IRI =
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
stillImageFileValueIri.set(valueIri)
val valueType: SmartIri =
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.TYPE, stringFormatter.toSmartIriWithErr)
valueType should ===(OntConsts.StillImageFileValue.toSmartIri)

val savedValue: JsonLDObject = getValue(
resourceIri = resourceIri,
propertyIriForGravsearch = OntConsts.HasStillImageFileValue.toSmartIri,
propertyIriInResult = OntConsts.HasStillImageFileValue.toSmartIri,
expectedValueIri = stillImageFileValueIri.get,
userEmail = anythingUserEmail,
)

savedValue.getRequiredString(OntConsts.FileValueHasFilename).toOption should contain(internalFilename)
savedValue.getRequiredInt(OntConsts.StillImageFileValueHasDimX).toOption should contain(512)
savedValue.getRequiredInt(OntConsts.StillImageFileValueHasDimY).toOption should contain(256)
}
}
}

0 comments on commit bb52134

Please sign in to comment.