-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support JSON-LD ontology v2 change requests (#3451)
- Loading branch information
Showing
9 changed files
with
193 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
webapi/src/main/scala/org/knora/webapi/slice/ontology/api/OntologyApiModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright © 2021 - 2025 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.slice.ontology.api | ||
import zio.* | ||
|
||
import org.knora.webapi.slice.URModule | ||
import org.knora.webapi.slice.resourceinfo.domain.IriConverter | ||
|
||
object OntologyApiModule extends URModule[IriConverter, OntologyV2RequestParser] { self => | ||
|
||
val layer: URLayer[self.Dependencies, self.Provided] = | ||
ZLayer.makeSome[self.Dependencies, self.Provided](OntologyV2RequestParser.layer) | ||
} |
53 changes: 53 additions & 0 deletions
53
webapi/src/main/scala/org/knora/webapi/slice/ontology/api/OntologyV2RequestParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright © 2021 - 2025 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.slice.ontology.api | ||
|
||
import org.apache.jena.vocabulary.RDFS | ||
import zio.* | ||
|
||
import java.util.UUID | ||
import scala.language.implicitConversions | ||
|
||
import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Complex.* | ||
import org.knora.webapi.messages.SmartIri | ||
import org.knora.webapi.messages.v2.responder.ontologymessages.ChangeOntologyMetadataRequestV2 | ||
import org.knora.webapi.slice.admin.domain.model.User | ||
import org.knora.webapi.slice.common.jena.JenaConversions.given_Conversion_String_Property | ||
import org.knora.webapi.slice.common.jena.ModelOps | ||
import org.knora.webapi.slice.common.jena.ModelOps.* | ||
import org.knora.webapi.slice.common.jena.ResourceOps.* | ||
import org.knora.webapi.slice.resourceinfo.domain.IriConverter | ||
|
||
final case class OntologyV2RequestParser(iriConverter: IriConverter) { | ||
|
||
def changeOntologyMetadataRequestV2( | ||
jsonLd: String, | ||
apiRequestId: UUID, | ||
requestingUser: User, | ||
): IO[String, ChangeOntologyMetadataRequestV2] = ZIO.scoped { | ||
for { | ||
model <- ModelOps.fromJsonLd(jsonLd) | ||
r <- ZIO.fromEither(model.singleRootResource) | ||
ontologyIri: SmartIri <- | ||
ZIO.fromOption(r.uri).orElseFail("No IRI found").flatMap(iriConverter.asSmartIri(_).mapError(_.getMessage)) | ||
label <- ZIO.fromEither(r.objectStringOption(RDFS.label)) | ||
comment <- ZIO.fromEither(r.objectStringOption(RDFS.comment)) | ||
lastModificationDate <- ZIO.fromEither(r.objectInstant(LastModificationDate)) | ||
} yield ChangeOntologyMetadataRequestV2( | ||
ontologyIri, | ||
label, | ||
comment, | ||
lastModificationDate, | ||
apiRequestId, | ||
requestingUser, | ||
) | ||
} | ||
|
||
} | ||
|
||
object OntologyV2RequestParser { | ||
val layer = ZLayer.derive[OntologyV2RequestParser] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,11 @@ import zio.Chunk | |
import zio.NonEmptyChunk | ||
|
||
import dsp.valueobjects.LanguageCode | ||
import org.knora.webapi.TestDataFactory.Project.systemProjectIri | ||
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionADM | ||
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionsDataADM | ||
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2 | ||
import org.knora.webapi.slice.admin.api.model.Project | ||
import org.knora.webapi.slice.admin.domain.model.* | ||
import org.knora.webapi.slice.admin.domain.model.Email | ||
import org.knora.webapi.slice.admin.domain.model.FamilyName | ||
|
@@ -23,13 +27,39 @@ import org.knora.webapi.slice.admin.domain.model.SystemAdmin | |
import org.knora.webapi.slice.admin.domain.model.UserIri | ||
import org.knora.webapi.slice.admin.domain.model.UserStatus | ||
import org.knora.webapi.slice.admin.domain.model.Username | ||
import org.knora.webapi.slice.admin.domain.service.KnoraGroupRepo | ||
import org.knora.webapi.slice.admin.domain.service.KnoraProjectRepo | ||
|
||
/** | ||
* Helps in creating value objects for tests. | ||
*/ | ||
object TestDataFactory { | ||
object Project { | ||
val systemProjectIri: IRI = KnoraProjectRepo.builtIn.SystemProject.id.value // built-in project | ||
} | ||
|
||
object User { | ||
/* represents the user profile of 'root' as found in admin-data.ttl */ | ||
val rootUser: User = | ||
org.knora.webapi.slice.admin.domain.model.User( | ||
id = "http://rdfh.ch/users/root", | ||
username = "root", | ||
email = "[email protected]", | ||
givenName = "System", | ||
familyName = "Administrator", | ||
status = true, | ||
lang = "de", | ||
password = Option("$2a$12$7XEBehimXN1rbhmVgQsyve08.vtDmKK7VMin4AdgCEtE4DWgfQbTK"), | ||
groups = Seq.empty[Group], | ||
projects = Seq.empty[Project], | ||
permissions = PermissionsDataADM( | ||
groupsPerProject = Map( | ||
systemProjectIri -> List(KnoraGroupRepo.builtIn.SystemAdmin.id.value), | ||
), | ||
administrativePermissionsPerProject = Map.empty[IRI, Set[PermissionADM]], | ||
), | ||
) | ||
|
||
val testUser: KnoraUser = KnoraUser( | ||
UserIri.unsafeFrom("http://rdfh.ch/users/exists"), | ||
Username.unsafeFrom("testuser"), | ||
|
77 changes: 77 additions & 0 deletions
77
webapi/src/test/scala/org/knora/webapi/slice/ontology/api/OntologyV2RequestParserSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright © 2021 - 2025 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.slice.ontology.api | ||
import zio.* | ||
import zio.test.* | ||
import zio.test.check | ||
|
||
import java.time.Instant | ||
|
||
import org.knora.webapi.TestDataFactory | ||
import org.knora.webapi.messages.StringFormatter | ||
import org.knora.webapi.messages.v2.responder.ontologymessages.ChangeOntologyMetadataRequestV2 | ||
import org.knora.webapi.slice.common.JsonLdTestUtil.JsonLdTransformations | ||
import org.knora.webapi.slice.resourceinfo.domain.IriConverter | ||
|
||
object OntologyV2RequestParserSpec extends ZIOSpecDefault { | ||
private val sf = StringFormatter.getInitializedTestInstance | ||
|
||
private val parser = ZIO.serviceWithZIO[OntologyV2RequestParser] | ||
private val user = TestDataFactory.User.rootUser | ||
|
||
private val changeOntologyMetadataRequestV2Suite = | ||
suite("ChangeOntologyMetadataRequestV2") { | ||
test("should parse correct jsonLd") { | ||
val instant = Instant.parse("2017-12-19T15:23:42.166Z") | ||
val jsonLd: String = | ||
""" | ||
|{ | ||
| "@id" : "http://0.0.0.0:3333/ontology/0001/anything/v2", | ||
| "@type" : "owl:Ontology", | ||
| "knora-api:lastModificationDate" : { | ||
| "@type" : "xsd:dateTimeStamp", | ||
| "@value" : "2017-12-19T15:23:42.166Z" | ||
| }, | ||
| "rdfs:label" : { | ||
| "@language" : "en", | ||
| "@value" : "Some Label" | ||
| }, | ||
| "rdfs:comment" : { | ||
| "@language" : "en", | ||
| "@value" : "Some Comment" | ||
| }, | ||
| "@context" : { | ||
| "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", | ||
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#", | ||
| "owl" : "http://www.w3.org/2002/07/owl#", | ||
| "rdfs" : "http://www.w3.org/2000/01/rdf-schema#", | ||
| "xsd" : "http://www.w3.org/2001/XMLSchema#", | ||
| "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#" | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
check(JsonLdTransformations.allGen) { t => | ||
for { | ||
uuid <- Random.nextUUID | ||
req <- parser(_.changeOntologyMetadataRequestV2(t(jsonLd), uuid, user)) | ||
} yield assertTrue( | ||
req == ChangeOntologyMetadataRequestV2( | ||
sf.toSmartIri("http://0.0.0.0:3333/ontology/0001/anything/v2"), | ||
Some("Some Label"), | ||
Some("Some Comment"), | ||
instant, | ||
uuid, | ||
user, | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
val spec = suite("OntologyV2RequestParser")(changeOntologyMetadataRequestV2Suite) | ||
.provide(OntologyV2RequestParser.layer, IriConverter.layer, StringFormatter.test) | ||
} |