Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Migrate resources routes to Tapir (DEV-4656) #3521

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.knora.webapi.e2e.v2

import org.apache.pekko
import spray.json.JsString
import zio.ZIO

import java.net.URLEncoder
import java.time.Instant
Expand All @@ -31,10 +32,11 @@ import org.knora.webapi.messages.ValuesValidator
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.rdf.*
import org.knora.webapi.models.*
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.routing.v2.OntologiesRouteV2
import org.knora.webapi.routing.v2.ResourcesRouteV2
import org.knora.webapi.sharedtestdata.SharedOntologyTestDataADM
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.testservices.TestClientService
import org.knora.webapi.util.*

import pekko.http.scaladsl.model.*
Expand All @@ -61,8 +63,6 @@ class OntologyV2R2RSpec extends R2RSpec {

private val ontologiesPath =
DSPApiDirectives.handleErrors(appConfig)(OntologiesRouteV2().makeRoute)
private val resourcesPath =
DSPApiDirectives.handleErrors(appConfig)(ResourcesRouteV2(appConfig).makeRoute)

implicit val ec: ExecutionContextExecutor = system.dispatcher

Expand Down Expand Up @@ -2820,23 +2820,16 @@ class OntologyV2R2RSpec extends R2RSpec {
| "freetest" : "${SharedOntologyTestDataADM.FREETEST_ONTOLOGY_IRI_LocalHost}#"
| }
|}""".stripMargin

Post(
"/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, createResourceWithValues),
) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> resourcesPath ~> check {
val responseStr = responseAs[String]
assert(status == StatusCodes.OK, responseStr)

val responseJsonDoc = JsonLDUtil.parseJsonLD(responseStr)
val validationFun: (String, => Nothing) => String = (s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
val resourceIri: IRI = responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
assert(resourceIri.toSmartIri.isKnoraDataIri)

val responseJsonDoc2 = JsonLDUtil.parseJsonLD(responseStr)
val label = responseJsonDoc2.body.value.get(OntologyConstants.Rdfs.Label).get
assert(label == JsonLDString(resourceLabel))
}
val _ = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.checkResponseOK(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + "/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, createResourceWithValues),
) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)),
),
),
)

// payload to test cardinality can't be deleted
val cardinalityCantBeDeletedPayload = AddCardinalitiesRequest.make(
Expand Down Expand Up @@ -3098,19 +3091,16 @@ class OntologyV2R2RSpec extends R2RSpec {
| "freetest" : "${SharedOntologyTestDataADM.FREETEST_ONTOLOGY_IRI_LocalHost}#"
| }
|}""".stripMargin

Post(
"/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, createResourceWithValues),
) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)) ~> resourcesPath ~> check {
val responseStr = responseAs[String]
assert(status == StatusCodes.OK, responseStr)
val responseJsonDoc = JsonLDUtil.parseJsonLD(responseStr)
val validationFun: (String, => Nothing) => String = (s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
val resourceIri: IRI =
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
assert(resourceIri.toSmartIri.isKnoraDataIri)
}
val _ = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.checkResponseOK(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + "/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, createResourceWithValues),
) ~> addCredentials(BasicHttpCredentials(anythingUsername, password)),
),
),
)

// payload to ask if cardinality can be removed from TestClassTwo
val cardinalityCanBeDeletedPayload = AddCardinalitiesRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.knora.webapi.messages.util.rdf.JsonLDKeywords
import org.knora.webapi.messages.util.rdf.JsonLDUtil
import org.knora.webapi.messages.util.search.SparqlQueryConstants
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.routing.v2.ResourcesRouteV2
import org.knora.webapi.routing.v2.StandoffRouteV2
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.slice.search.api.SearchApiRoutes
Expand All @@ -54,8 +53,6 @@ class SearchRouteV2R2RSpec extends R2RSpec {
.runOrThrow(ZIO.serviceWith[SearchApiRoutes](_.routes))
.reduce(_ ~ _)

private val resourcePath =
DSPApiDirectives.handleErrors(appConfig)(ResourcesRouteV2(appConfig).makeRoute)
private val standoffPath =
DSPApiDirectives.handleErrors(appConfig)(StandoffRouteV2().makeRoute)

Expand Down Expand Up @@ -7345,19 +7342,25 @@ class SearchRouteV2R2RSpec extends R2RSpec {
| }
|}""".stripMargin

Post("/v2/resources", HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
) ~> resourcePath ~> check {
val resourceCreateResponseStr = responseAs[String]
assert(status == StatusCodes.OK, resourceCreateResponseStr)
val resourceCreateResponseAsJsonLD: JsonLDDocument = JsonLDUtil.parseJsonLD(resourceCreateResponseStr)
val validationFun: (String, => Nothing) => String =
(s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
val resourceIri: IRI =
resourceCreateResponseAsJsonLD.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
assert(resourceIri.toSmartIri.isKnoraDataIri)
hamletResourceIri.set(resourceIri)
}
val resourceCreateResponseStr = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.getResponseString(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + "/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity),
) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
),
),
),
)
val resourceCreateResponseAsJsonLD: JsonLDDocument = JsonLDUtil.parseJsonLD(resourceCreateResponseStr)
val validationFun: (String, => Nothing) => String =
(s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
val resourceIri: IRI =
resourceCreateResponseAsJsonLD.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
assert(resourceIri.toSmartIri.isKnoraDataIri)
hamletResourceIri.set(resourceIri)
}

"search for the large text and its markup and receive it as XML, and check that it matches the original XML" ignore { // depends on previous test
Expand Down Expand Up @@ -7399,7 +7402,6 @@ class SearchRouteV2R2RSpec extends R2RSpec {

"find a resource with two different incoming links" in {
// Create the target resource.

val targetResource: String =
"""{
| "@type" : "anything:BlueThing",
Expand All @@ -7415,20 +7417,16 @@ class SearchRouteV2R2RSpec extends R2RSpec {
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}""".stripMargin

val targetResourceIri: IRI =
Post(s"/v2/resources", HttpEntity(RdfMediaTypes.`application/ld+json`, targetResource)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
) ~> resourcePath ~> check {
val createTargetResourceResponseStr = responseAs[String]
assert(response.status == StatusCodes.OK, createTargetResourceResponseStr)
val responseJsonDoc: JsonLDDocument = responseToJsonLDDocument(response)
val validationFun: (String, => Nothing) => String =
(s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
}

assert(targetResourceIri.toSmartIri.isKnoraDataIri)
val targetResourceIri = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.getResponseJsonLD(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + s"/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, targetResource),
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, password)),
).map(_.body.getRequiredString(JsonLDKeywords.ID).getOrElse(throw AssertionError("No IRI returned"))),
),
)

val sourceResource1: String =
s"""{
Expand All @@ -7451,20 +7449,16 @@ class SearchRouteV2R2RSpec extends R2RSpec {
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}""".stripMargin

val sourceResource1Iri: IRI =
Post(s"/v2/resources", HttpEntity(RdfMediaTypes.`application/ld+json`, sourceResource1)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
) ~> resourcePath ~> check {
val createSourceResource1ResponseStr = responseAs[String]
assert(response.status == StatusCodes.OK, createSourceResource1ResponseStr)
val responseJsonDoc: JsonLDDocument = responseToJsonLDDocument(response)
val validationFun: (String, => Nothing) => String =
(s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
}

assert(sourceResource1Iri.toSmartIri.isKnoraDataIri)
val _ = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.checkResponseOK(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + "/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, sourceResource1),
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, password)),
),
),
)

val sourceResource2: String =
s"""{
Expand All @@ -7488,19 +7482,16 @@ class SearchRouteV2R2RSpec extends R2RSpec {
| }
|}""".stripMargin

val sourceResource2Iri: IRI =
Post(s"/v2/resources", HttpEntity(RdfMediaTypes.`application/ld+json`, sourceResource2)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
) ~> resourcePath ~> check {
val createSourceResource2ResponseStr = responseAs[String]
assert(response.status == StatusCodes.OK, createSourceResource2ResponseStr)
val responseJsonDoc: JsonLDDocument = responseToJsonLDDocument(response)
val validationFun: (String, => Nothing) => String =
(s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
}

assert(sourceResource2Iri.toSmartIri.isKnoraDataIri)
val _ = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.checkResponseOK(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + "/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, sourceResource2),
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, password)),
),
),
)

val gravsearchQuery =
s"""
Expand Down Expand Up @@ -7762,23 +7753,19 @@ class SearchRouteV2R2RSpec extends R2RSpec {
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
| }
|}""".stripMargin

Post(s"/v2/resources", HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password),
) ~> resourcePath ~> check {
val responseStr = responseAs[String]
assert(status == StatusCodes.OK, responseStr)
val resourceCreateResponseAsJsonLD: JsonLDDocument = JsonLDUtil.parseJsonLD(responseStr)
val validationFun: (String, => Nothing) => String =
(s, e) => Iri.validateAndEscapeIri(s).getOrElse(e)
val resourceIri: IRI =
resourceCreateResponseAsJsonLD.body.requireStringWithValidation(JsonLDKeywords.ID, validationFun)
assert(resourceIri.toSmartIri.isKnoraDataIri)
timeTagResourceIri.set(resourceIri)
}
val resourceIri = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[TestClientService](
_.getResponseJsonLD(
Post(
appConfig.knoraApi.internalKnoraApiBaseUrl + s"/v2/resources",
HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity),
) ~> addCredentials(BasicHttpCredentials(anythingUserEmail, password)),
).map(_.body.getRequiredString(JsonLDKeywords.ID).getOrElse(throw AssertionError("No IRI returned"))),
),
)
timeTagResourceIri.set(resourceIri)

// Search for the resource.

val gravsearchQuery =
"""
|PREFIX knora-api: <http://api.knora.org/ontology/knora-api/v2#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class CardinalitiesV2E2ESpec extends E2ESpec {
)
}

// first adding the the cardinalities to the *super*, then to the *sub* class
// first adding the cardinalities to the *super*, then to the *sub* class
val clsAndProps = List(
(superClassName, superClassProperty1),
(superClassName, superClassProperty2),
Expand Down

This file was deleted.

Loading