From 4690b4456593cfc738df489b9862927db551d363 Mon Sep 17 00:00:00 2001 From: Balduin Landolt <33053745+BalduinLandolt@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:58:10 +0200 Subject: [PATCH] improve testing --- .../twirl/SparqlTemplateLinkUpdate.scala | 3 +- .../resources/CreateResourceV2Handler.scala | 4 +- .../sparql/v2/createNewResource.scala.txt | 19 +- .../repo/service/ResourcesRepoLiveSpec.scala | 216 +++++++++++++++++- 4 files changed, 221 insertions(+), 21 deletions(-) diff --git a/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateLinkUpdate.scala b/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateLinkUpdate.scala index 2ecf6df0d8..68248028ec 100644 --- a/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateLinkUpdate.scala +++ b/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateLinkUpdate.scala @@ -52,12 +52,13 @@ case class SparqlTemplateLinkUpdate( ) final case class NewLinkValueInfo( - linkPropertyIri: SmartIri, + linkPropertyIri: IRI, newLinkValueIri: IRI, linkTargetIri: IRI, newReferenceCount: Int, newLinkValueCreator: IRI, newLinkValuePermissions: String, + valueUuid: String, ) final case class NewValueInfo( diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala index 7fad37830a..c418d7f235 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala @@ -11,6 +11,7 @@ import zio.* import java.time.Instant import dsp.errors.* +import dsp.valueobjects.UuidUtil import org.knora.webapi.* import org.knora.webapi.config.AppConfig import org.knora.webapi.core.MessageRelay @@ -834,12 +835,13 @@ final case class CreateResourceV2Handler( for { newValueIri <- makeUnusedValueIri(resourceIri) } yield NewLinkValueInfo( - linkPropertyIri = OntologyConstants.KnoraBase.HasStandoffLinkTo.toSmartIri, + linkPropertyIri = OntologyConstants.KnoraBase.HasStandoffLinkTo, newLinkValueIri = newValueIri, linkTargetIri = targetIri, newReferenceCount = initialReferenceCount, newLinkValueCreator = KnoraUserRepo.builtIn.SystemUser.id.value, newLinkValuePermissions = standoffLinkValuePermissions, + valueUuid = UuidUtil.makeRandomBase64EncodedUuid, ) } ZIO.collectAll(standoffLinkUpdatesFutures) diff --git a/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt b/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt index 0614eca5ca..f09e9782d9 100644 --- a/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt +++ b/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt @@ -63,7 +63,6 @@ INSERT DATA { <@newValueInfo.newValueIri> rdf:subject <@newValueInfo.resourceIri> ; rdf:predicate <@newValueInfo.propertyIri.stripSuffix("Value")> ; rdf:object <@linkValueContentV2.referredResourceIri> ; - # knora-base:valueHasString "@linkValueContentV2.referredResourceIri"^^xsd:string ; knora-base:valueHasRefCount 1 . } @@ -308,20 +307,10 @@ INSERT DATA { knora-base:valueHasString "@linkUpdate.linkTargetIri" ; knora-base:valueHasRefCount @linkUpdate.newReferenceCount ; knora-base:isDeleted false ; - knora-base:valueCreationDate "@creationDate"^^xsd:dateTime . - - <@linkUpdate.newLinkValueIri> knora-base:attachedToUser <@linkUpdate.newLinkValueCreator> ; - knora-base:hasPermissions "@linkUpdate.newLinkValuePermissions" . - - @* - - If this template is being used to create a single text value containing standoff links, the new LinkValue - we are creating may be a new version of an existing LinkValue (linkUpdate.linkValueExists will be true). - In that case, the WHERE clause will have already bound a SPARQL variable ?linkValue@linkValueIndex, - containing the IRI of the existing LinkValue. Therefore, add a triple indicating that the new LinkValue - is a new version of the existing one, and copy the UUID from the existing one. - *@ - <@linkUpdate.newLinkValueIri> knora-base:valueHasUUID "@{UuidUtil.base64Encode(UUID.randomUUID)}" . + knora-base:valueCreationDate "@creationDate"^^xsd:dateTime ; + knora-base:attachedToUser <@linkUpdate.newLinkValueCreator> ; + knora-base:hasPermissions "@linkUpdate.newLinkValuePermissions" ; + knora-base:valueHasUUID "@linkUpdate.valueUuid" . @* Attach the new LinkValue to its containing resource. *@ <@resourceIri> <@{linkUpdate.linkPropertyIri}Value> <@linkUpdate.newLinkValueIri> . diff --git a/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala b/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala index 0358164dbe..51125aabdb 100644 --- a/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala @@ -7,15 +7,25 @@ package org.knora.webapi.slice.resources.repo.service import zio.* import zio.test.* -import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Update -import org.knora.webapi.slice.resourceinfo.domain.InternalIri + import java.time.Instant +import java.util.UUID + +import dsp.valueobjects.UuidUtil +import org.knora.webapi.ApiV2Complex +import org.knora.webapi.messages.StringFormatter +import org.knora.webapi.messages.twirl.NewLinkValueInfo +import org.knora.webapi.messages.twirl.NewValueInfo +import org.knora.webapi.messages.v2.responder.valuemessages.IntegerValueContentV2 +import org.knora.webapi.slice.resourceinfo.domain.InternalIri +import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Update object ResourcesRepoLiveSpec extends ZIOSpecDefault { + def spec: Spec[Environment & (TestEnvironment & Scope), Any] = tests.provide(Layer.layer) - def spec = + val tests: Spec[StringFormatter, Nothing] = suite("ResourcesRepoLiveSpec")( - test("Create new resource query") { + test("Create new resource query without values") { val tripleQuotes = "\"\"\"" val graphIri = InternalIri("fooGraph") @@ -69,7 +79,205 @@ object ResourcesRepoLiveSpec extends ZIOSpecDefault { creatorIri = userIri, ) + assertTrue(expected.sparql == result.sparql) + }, + test("Create new resource query with values") { + val graphIri = InternalIri("fooGraph") + val projectIri = "fooProject" + val userIri = "fooUser" + val resourceIri = "fooResource" + val resourceClassIri = "fooClass" + val label = "fooLabel" + val creationDate = Instant.parse("2024-01-01T10:00:00.673298Z") + val permissions = "fooPermissions" + + val valueIri = "fooValue" + val valueCreator = "fooCreator" + val valuePermissions = "fooValuePermissions" + val valueCreationDate = Instant.parse("2024-01-01T10:00:00.673298Z") + + val uuid = UUID.randomUUID() + val uuidEncoded = UuidUtil.base64Encode(uuid) + + val resourceDefinition = ResourceReadyToCreate( + resourceIri = resourceIri, + resourceClassIri = resourceClassIri, + resourceLabel = label, + creationDate = creationDate, + permissions = permissions, + newValueInfos = Seq( + NewValueInfo( + resourceIri = resourceIri, + propertyIri = "fooProperty", + value = IntegerValueContentV2(ApiV2Complex, 42), + newValueIri = valueIri, + newValueUUID = uuid, + valueCreator = valueCreator, + valuePermissions = valuePermissions, + creationDate = valueCreationDate, + valueHasOrder = 1, + ), + ), + linkUpdates = Seq.empty, + ) + + val expected = + Update(s"""| + |PREFIX rdf: + |PREFIX rdfs: + |PREFIX owl: + |PREFIX xsd: + |PREFIX knora-base: + | + |INSERT DATA { + | GRAPH { + | rdf:type ; + | knora-base:isDeleted false ; + | knora-base:attachedToUser ; + | knora-base:attachedToProject ; + | rdfs:label \"\"\"fooLabel\"\"\" ; + | knora-base:hasPermissions \"fooPermissions\" ; + | knora-base:creationDate \"2024-01-01T10:00:00.673298Z\"^^xsd:dateTime . + | + | + | # Value: fooValue + | # Property: fooProperty + | + | + | rdf:type ; + | knora-base:isDeleted false ; + | knora-base:valueHasString \"\"\"42\"\"\" ; + | knora-base:valueHasUUID \"$uuidEncoded\" . + | + | + | + | + | knora-base:valueHasInteger 42 . + | + | + | + | + | + | + | knora-base:attachedToUser ; + | knora-base:hasPermissions \"fooValuePermissions\"^^xsd:string ; + | knora-base:valueHasOrder 1 ; + | knora-base:valueCreationDate \"2024-01-01T10:00:00.673298Z\"^^xsd:dateTime . + | + | + | + | + | . + | + | + | + | + | } + |} + |""".stripMargin) + val result = ResourcesRepoLive.createNewResourceQuery( + dataGraphIri = graphIri, + resourceToCreate = resourceDefinition, + projectIri = projectIri, + creatorIri = userIri, + ) + + assertTrue(expected.sparql == result.sparql) + }, + test("Create new resource query with links") { + val graphIri = InternalIri("fooGraph") + val projectIri = "fooProject" + val userIri = "fooUser" + val resourceIri = "fooResource" + val resourceClassIri = "fooClass" + val label = "fooLabel" + val creationDate = Instant.parse("2024-01-01T10:00:00.673298Z") + val permissions = "fooPermissions" + + val linkPropertyIri = "fooLinkProperty" + val linkTargetIri = "fooLinkTarget" + val linkValueIri = "fooLinkValue" + val linkCreator = "fooLinkCreator" + val linkPermissions = "fooLinkPermissions" + val valueUuid = UuidUtil.makeRandomBase64EncodedUuid + + val resourceDefinition = ResourceReadyToCreate( + resourceIri = resourceIri, + resourceClassIri = resourceClassIri, + resourceLabel = label, + creationDate = creationDate, + permissions = permissions, + newValueInfos = Seq.empty, + linkUpdates = Seq( + NewLinkValueInfo( + linkPropertyIri = linkPropertyIri, + newLinkValueIri = linkValueIri, + linkTargetIri = linkTargetIri, + newReferenceCount = 1, + newLinkValueCreator = linkCreator, + newLinkValuePermissions = linkPermissions, + valueUuid = valueUuid, + ), + ), + ) + + val expected = + Update(s"""| + |PREFIX rdf: + |PREFIX rdfs: + |PREFIX owl: + |PREFIX xsd: + |PREFIX knora-base: + | + |INSERT DATA { + | GRAPH { + | rdf:type ; + | knora-base:isDeleted false ; + | knora-base:attachedToUser ; + | knora-base:attachedToProject ; + | rdfs:label \"\"\"fooLabel\"\"\" ; + | knora-base:hasPermissions "fooPermissions" ; + | knora-base:creationDate "2024-01-01T10:00:00.673298Z"^^xsd:dateTime . + | + | + | + | + | + | + | . + | + | + | rdf:type knora-base:LinkValue ; + | rdf:subject ; + | rdf:predicate ; + | rdf:object ; + | knora-base:valueHasString "fooLinkTarget" ; + | knora-base:valueHasRefCount 1 ; + | knora-base:isDeleted false ; + | knora-base:valueCreationDate "2024-01-01T10:00:00.673298Z"^^xsd:dateTime ; + | knora-base:attachedToUser ; + | knora-base:hasPermissions "fooLinkPermissions" ; + | knora-base:valueHasUUID "$valueUuid" . + | + | + | . + | + | } + |} + |""".stripMargin) + val result = ResourcesRepoLive.createNewResourceQuery( + dataGraphIri = graphIri, + resourceToCreate = resourceDefinition, + projectIri = projectIri, + creatorIri = userIri, + ) + assertTrue(expected.sparql == result.sparql) }, ) + +} + +object Layer { + val layer: ULayer[StringFormatter] = ZLayer.fromZIO(ZIO.succeed(StringFormatter.getInitializedTestInstance)) }