From b35cd21d84d8e2449b5388455f0c32e4bcd33e46 Mon Sep 17 00:00:00 2001 From: Kirill Yankov Date: Thu, 8 Feb 2024 11:57:10 +0100 Subject: [PATCH] fix for #156 --- .../org/dbpedia/databus/SparqlClient.scala | 23 ++++++++++++------- .../dbpedia/databus/DatabusScalatraTest.scala | 14 +++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/scala/org/dbpedia/databus/SparqlClient.scala b/src/main/scala/org/dbpedia/databus/SparqlClient.scala index bf21d77..ba88bd2 100644 --- a/src/main/scala/org/dbpedia/databus/SparqlClient.scala +++ b/src/main/scala/org/dbpedia/databus/SparqlClient.scala @@ -201,19 +201,22 @@ object RdfConversions { ShaclValidator.get() .validate(Shapes.parse(shacl), model.getGraph) ) + def validateWithShacl(file: Array[Byte], modelLang: Lang, shaclGraph: Graph, fileCtx: Option[util.Context]): Try[ValidationReport] = + for { + (model, _) <- readModel(file, modelLang, fileCtx) + re <- validateWithShacl(model, shaclGraph) + } yield re def validateWithShacl(file: Array[Byte], shaclData: Array[Byte], fileCtx: Option[util.Context], shaclCtx: Option[util.Context], modelLang: Lang): Try[ValidationReport] = for { (shaclGra, _) <- readModel(shaclData, DefaultShaclLang, shaclCtx) - (model, _) <- readModel(file, modelLang, fileCtx) - re <- validateWithShacl(model, shaclGra.getGraph) + re <- validateWithShacl(file, modelLang, shaclGra.getGraph, fileCtx) } yield re def validateWithShacl(file: Array[Byte], fileCtx: Option[util.Context], shaclUri: String, modelLang: Lang): Try[ValidationReport] = for { shaclGra <- Try(RDFDataMgr.loadGraph(shaclUri)) - (model, _) <- readModel(file, modelLang, fileCtx) - re <- validateWithShacl(model, shaclGra) + re <- validateWithShacl(file, modelLang, shaclGra, fileCtx) } yield re def langToFormat(lang: Lang): RDFFormat = lang match { @@ -427,10 +430,14 @@ object RdfConversions { import org.apache.jena.riot.SysRIOT.fmtMessage - override def warning(message: String, line: Long, col: Long): Unit = { - warnings = warnings :+ Warning(fmtMessage(message, line, col)) - defaultEH.warning(message, line, col) - } + override def warning(message: String, line: Long, col: Long): Unit = + // Fix for https://github.com/dbpedia/databus/issues/156, need to convert this to error + if (message.contains("Spaces are not legal in URIs/IRIs")) { + error(message, line, col) + } else { + warnings = warnings :+ Warning(fmtMessage(message, line, col)) + defaultEH.warning(message, line, col) + } override def error(message: String, line: Long, col: Long): Unit = defaultEH.error(message, line, col) diff --git a/src/test/scala/org/dbpedia/databus/DatabusScalatraTest.scala b/src/test/scala/org/dbpedia/databus/DatabusScalatraTest.scala index 3b123a0..d305d6c 100644 --- a/src/test/scala/org/dbpedia/databus/DatabusScalatraTest.scala +++ b/src/test/scala/org/dbpedia/databus/DatabusScalatraTest.scala @@ -83,6 +83,20 @@ class DatabusScalatraTest extends ScalatraFlatSpec with BeforeAndAfter { } + "Shacl validation" should "report problems in input" in { + + val file = "report_syntax_err.jsonld" + val sha = "test.shacl" + val bytes = Paths.get(getClass.getClassLoader.getResource(file).getFile).toFile + val shacl = Paths.get(getClass.getClassLoader.getResource(sha).getFile).toFile + + post("/databus/shacl/validate", Map.empty, Map("shacl" -> shacl, "graph" -> bytes)) { + status should equal(400) + body should include("Bad IRI") + } + + } + "File read" should "return 404" in { get("/databus/graph/read?repo=kuckuck&path=pa/not_existing.jsonld") {