Skip to content

Commit

Permalink
test: Make json ld comparison robust to json formatting changes (#3320)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone authored Jul 15, 2024
1 parent 63f8426 commit e1280d5
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,11 @@ object RdfFormatUtilSpec {
def parseToJsonLDDocument(
rdfStr: String,
rdfFormat: RdfFormat,
flatJsonLD: Boolean = false,
): JsonLDDocument =
rdfFormat match {
case JsonLD =>
// Use JsonLDUtil to parse JSON-LD.
JsonLDUtil.parseJsonLD(jsonLDString = rdfStr, flatten = flatJsonLD)
JsonLDUtil.parseJsonLD(rdfStr)

case nonJsonLD: NonJsonLD =>
val model: JenaModel = JenaModelFactory.makeEmptyModel
Expand All @@ -274,6 +273,6 @@ object RdfFormatUtilSpec {
.lang(RdfFormatUtil.rdfFormatToJenaParsingLang(nonJsonLD))
.errorHandler(jena.riot.system.ErrorHandlerFactory.errorHandlerStrictNoLogging)
.parse(model.getDataset)
JsonLDUtil.fromRdfModel(model, flatJsonLD)
JsonLDUtil.fromRdfModel(model)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import zio.test.*

import org.knora.webapi.E2EZSpec
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.rdf.JsonLDUtil
import org.knora.webapi.slice.admin.domain.model.ListProperties.ListIri

object ListsV2E2Spec extends E2EZSpec {
Expand Down Expand Up @@ -45,128 +46,129 @@ object ListsV2E2Spec extends E2EZSpec {
} yield {
assertTrue(
response.status.code == 200,
bodyStr == """{
| "rdfs:label": "a list that is not used",
| "knora-api:attachedToProject": {
| "@id": "http://rdfh.ch/projects/0001"
| },
| "knora-api:hasSubListNode": [
| {
| "rdfs:label": "node 1",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:hasSubListNode": [
| {
| "rdfs:label": "child of node 1",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 0,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList011"
| },
| {
| "rdfs:label": "List012",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 1,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList012"
| },
| {
| "rdfs:label": "List013",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 2,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList013"
| },
| {
| "rdfs:label": "List014",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:hasSubListNode": [
| {
| "rdfs:label": "first child of node 014",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 0,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList0141"
| },
| {
| "rdfs:label": "second child of node 014",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 0,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList0142"
| }
| ],
| "knora-api:listNodePosition": 3,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList014"
| },
| {
| "rdfs:label": "List015",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 4,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList015"
| }
| ],
| "knora-api:listNodePosition": 0,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList01"
| },
| {
| "rdfs:label": "node 2",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 1,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList02"
| },
| {
| "rdfs:label": "node 3",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:hasSubListNode": {
| "rdfs:label": "child of node 3",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 0,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList031"
| },
| "knora-api:listNodePosition": 2,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList03"
| }
| ],
| "knora-api:isRootNode": true,
| "rdfs:comment": "a list that is not in used in ontology or data",
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList",
| "@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#"
| }
|}""".stripMargin,
JsonLDUtil.parseJsonLD(bodyStr) == JsonLDUtil.parseJsonLD("""
{
"rdfs:label": "a list that is not used",
"knora-api:attachedToProject": {
"@id": "http://rdfh.ch/projects/0001"
},
"knora-api:hasSubListNode": [
{
"rdfs:label": "node 1",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:hasSubListNode": [
{
"rdfs:label": "child of node 1",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 0,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList011"
},
{
"rdfs:label": "List012",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 1,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList012"
},
{
"rdfs:label": "List013",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 2,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList013"
},
{
"rdfs:label": "List014",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:hasSubListNode": [
{
"rdfs:label": "first child of node 014",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 0,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList0141"
},
{
"rdfs:label": "second child of node 014",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 0,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList0142"
}
],
"knora-api:listNodePosition": 3,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList014"
},
{
"rdfs:label": "List015",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 4,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList015"
}
],
"knora-api:listNodePosition": 0,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList01"
},
{
"rdfs:label": "node 2",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 1,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList02"
},
{
"rdfs:label": "node 3",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:hasSubListNode": {
"rdfs:label": "child of node 3",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 0,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList031"
},
"knora-api:listNodePosition": 2,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList03"
}
],
"knora-api:isRootNode": true,
"rdfs:comment": "a list that is not in used in ontology or data",
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList",
"@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#"
}
}"""),
)
}
},
Expand All @@ -188,25 +190,25 @@ object ListsV2E2Spec extends E2EZSpec {
bodyStr <- response.body.asString
} yield assertTrue(
response.status.code == 200,
bodyStr == """{
| "rdfs:label": "node 1",
| "knora-api:hasRootNode": {
| "@id": "http://rdfh.ch/lists/0001/notUsedList"
| },
| "knora-api:listNodePosition": 0,
| "@type": "knora-api:ListNode",
| "@id": "http://rdfh.ch/lists/0001/notUsedList01",
| "@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#"
| }
|}""".stripMargin,
JsonLDUtil.parseJsonLD(bodyStr) == JsonLDUtil.parseJsonLD("""
{
"rdfs:label": "node 1",
"knora-api:hasRootNode": {
"@id": "http://rdfh.ch/lists/0001/notUsedList"
},
"knora-api:listNodePosition": 0,
"@type": "knora-api:ListNode",
"@id": "http://rdfh.ch/lists/0001/notUsedList01",
"@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#"
}
}"""),
)
},
),
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ object JsonLDUtil {
* @param flatten `true` if a flat JSON-LD document should be returned.
* @return a [[JsonLDDocument]].
*/
def parseJsonLD(jsonLDString: String, flatten: Boolean = false): JsonLDDocument = {
def parseJsonLD(jsonLDString: String): JsonLDDocument = {
// Parse the string into a jakarta.json.JsonStructure.
val stringReader = new StringReader(jsonLDString)
val jsonReader: JsonReader = Json.createReader(stringReader)
Expand All @@ -1236,16 +1236,7 @@ object JsonLDUtil {
val compactedJsonObject: JsonObject = JsonLd.compact(titaniumDocument, emptyContext).get

// Convert the resulting jakarta.json.JsonObject to a JsonLDDocument.
val jsonLDDocument: JsonLDDocument = javaxJsonObjectToJsonLDDocument(compactedJsonObject)

// Was flat JSON-LD requested?
if (flatten) {
// Yes. Flatten the document.
jsonLDDocument.flattened
} else {
// No. Leave it as is.
jsonLDDocument
}
javaxJsonObjectToJsonLDDocument(compactedJsonObject)
}

/**
Expand Down

0 comments on commit e1280d5

Please sign in to comment.