Skip to content

Commit

Permalink
fix(standoffLinks) standoffLinkValues + test (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
SepidehAlassi committed Jan 21, 2021
1 parent efcd451 commit 6f14aee
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
Expand Up @@ -789,13 +789,13 @@ class ValuesResponderV2(responderData: ResponderData) extends Responder(responde
// standoff links to that IRI.
val initialReferenceCounts: Map[IRI, Int] = allStandoffLinkTargetsGrouped.mapValues(_.size)

for {
newValueIri: IRI <- makeUnusedValueIri(createMultipleValuesRequest.resourceIri)

// For each standoff link target IRI, construct a SparqlTemplateLinkUpdate to create a hasStandoffLinkTo property
// and one LinkValue with its initial reference count.
standoffLinkUpdates: Seq[SparqlTemplateLinkUpdate] = initialReferenceCounts.toSeq.map {
case (targetIri, initialReferenceCount) =>
// For each standoff link target IRI, construct a SparqlTemplateLinkUpdate to create a hasStandoffLinkTo property
// and one LinkValue with its initial reference count.
val standoffLinkUpdatesFutures: Seq[Future[SparqlTemplateLinkUpdate]] = initialReferenceCounts.toSeq.map {
case (targetIri, initialReferenceCount) =>
for {
newValueIri <- makeUnusedValueIri(createMultipleValuesRequest.resourceIri)
} yield
SparqlTemplateLinkUpdate(
linkPropertyIri = OntologyConstants.KnoraBase.HasStandoffLinkTo.toSmartIri,
directLinkExists = false,
Expand All @@ -810,8 +810,9 @@ class ValuesResponderV2(responderData: ResponderData) extends Responder(responde
newLinkValueCreator = OntologyConstants.KnoraAdmin.SystemUser,
newLinkValuePermissions = standoffLinkValuePermissions
)
}

}
for {
standoffLinkUpdates: Seq[SparqlTemplateLinkUpdate] <- Future.sequence(standoffLinkUpdatesFutures)
// Generate SPARQL INSERT statements based on those SparqlTemplateLinkUpdates.
sparqlInsert = org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.generateInsertStatementsForStandoffLinks(
Expand Down
Expand Up @@ -1945,6 +1945,55 @@ class ResourcesRouteV2E2ESpec extends E2ESpec(ResourcesRouteV2E2ESpec.config) {
val resourceJsonLDDoc = JsonLDUtil.parseJsonLD(resourceComplexGetResponseAsString)
assert(resourceJsonLDDoc.body.value.contains(OntologyConstants.KnoraApiV2Complex.HasStandoffLinkToValue))
}

"create a resource containing a text value with multiple standoff links" in {
val jsonLDEntity =
"""{
| "@type": "anything:Thing",
| "anything:hasText": {
| "@type": "knora-api:TextValue",
| "knora-api:textValueAsXml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<text>\n This text links to another <a class=\"salsah-link\" href=\"http://rdfh.ch/0001/another-thing\">thing</a> and a <a class=\"salsah-link\" href=\"http://rdfh.ch/0001/a-blue-thing\">blue thing</a>.\n</text>",
| "knora-api:textValueHasMapping": {
| "@id": "http://rdfh.ch/standoff/mappings/StandardMapping"
| }
| },
| "knora-api:attachedToProject": {
| "@id": "http://rdfh.ch/projects/0001"
| },
| "rdfs:label": "thing_with_mutiple_standoffLinks",
| "@context": {
| "anything": "http://0.0.0.0:3333/ontology/0001/anything/v2#",
| "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
| "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
| "knora-api": "http://api.knora.org/ontology/knora-api/v2#"
| }
| }""".stripMargin

val request = Post(s"$baseApiUrl/v2/resources", HttpEntity(RdfMediaTypes.`application/ld+json`, jsonLDEntity)) ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password))
val response: HttpResponse = singleAwaitingRequest(request)
assert(response.status == StatusCodes.OK, response.toString)
val responseJsonDoc: JsonLDDocument = responseToJsonLDDocument(response)
val resourceIri: IRI =
responseJsonDoc.body.requireStringWithValidation(JsonLDKeywords.ID, stringFormatter.validateAndEscapeIri)
assert(resourceIri.toSmartIri.isKnoraDataIri)

// Request the newly created resource in the complex schema, and check that it matches the ontology.
val resourceComplexGetRequest = Get(s"$baseApiUrl/v2/resources/${URLEncoder.encode(resourceIri, "UTF-8")}") ~> addCredentials(
BasicHttpCredentials(anythingUserEmail, password))
val resourceComplexGetResponse: HttpResponse = singleAwaitingRequest(resourceComplexGetRequest)
val resourceComplexGetResponseAsString = responseToString(resourceComplexGetResponse)

// Check that it has multiple property knora-api:hasStandoffLinkToValue.
val resourceJsonLDDoc: JsonLDDocument = JsonLDUtil.parseJsonLD(resourceComplexGetResponseAsString)
val numberOfStandofHasLinkValue = resourceJsonLDDoc.body.value
.get(OntologyConstants.KnoraApiV2Complex.HasStandoffLinkToValue)
.get
.asInstanceOf[JsonLDArray]
.value
.size
assert(numberOfStandofHasLinkValue == 2)
}
}
}

Expand Down

0 comments on commit 6f14aee

Please sign in to comment.