Skip to content

Commit

Permalink
improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Jun 11, 2024
1 parent 8050d42 commit 4690b44
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

}
Expand Down Expand Up @@ -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> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|PREFIX owl: <http://www.w3.org/2002/07/owl#>
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
|
|INSERT DATA {
| GRAPH <fooGraph> {
| <fooResource> rdf:type <fooClass> ;
| knora-base:isDeleted false ;
| knora-base:attachedToUser <fooUser> ;
| knora-base:attachedToProject <fooProject> ;
| rdfs:label \"\"\"fooLabel\"\"\" ;
| knora-base:hasPermissions \"fooPermissions\" ;
| knora-base:creationDate \"2024-01-01T10:00:00.673298Z\"^^xsd:dateTime .
|
|
| # Value: fooValue
| # Property: fooProperty
|
|
| <fooValue> rdf:type <http://api.knora.org/ontology/knora-api/v2#IntValue> ;
| knora-base:isDeleted false ;
| knora-base:valueHasString \"\"\"42\"\"\" ;
| knora-base:valueHasUUID \"$uuidEncoded\" .
|
|
|
|
| <fooValue> knora-base:valueHasInteger 42 .
|
|
|
|
|
|
| <fooValue> knora-base:attachedToUser <fooCreator> ;
| knora-base:hasPermissions \"fooValuePermissions\"^^xsd:string ;
| knora-base:valueHasOrder 1 ;
| knora-base:valueCreationDate \"2024-01-01T10:00:00.673298Z\"^^xsd:dateTime .
|
|
|
|
| <fooResource> <fooProperty> <fooValue> .
|
|
|
|
| }
|}
|""".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: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|PREFIX owl: <http://www.w3.org/2002/07/owl#>
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
|
|INSERT DATA {
| GRAPH <fooGraph> {
| <fooResource> rdf:type <fooClass> ;
| knora-base:isDeleted false ;
| knora-base:attachedToUser <fooUser> ;
| knora-base:attachedToProject <fooProject> ;
| rdfs:label \"\"\"fooLabel\"\"\" ;
| knora-base:hasPermissions "fooPermissions" ;
| knora-base:creationDate "2024-01-01T10:00:00.673298Z"^^xsd:dateTime .
|
|
|
|
|
|
| <fooResource> <fooLinkProperty> <fooLinkTarget> .
|
|
| <fooLinkValue> rdf:type knora-base:LinkValue ;
| rdf:subject <fooResource> ;
| rdf:predicate <fooLinkProperty> ;
| rdf:object <fooLinkTarget> ;
| 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 <fooLinkCreator> ;
| knora-base:hasPermissions "fooLinkPermissions" ;
| knora-base:valueHasUUID "$valueUuid" .
|
|
| <fooResource> <fooLinkPropertyValue> <fooLinkValue> .
|
| }
|}
|""".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))
}

0 comments on commit 4690b44

Please sign in to comment.