Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ object RdfFormat:
override val cliOptions: List[String] = List("nt", "ntriples")
override val jenaLang: Lang = RDFLanguages.NTRIPLES

case object Turtle extends RdfFormat.Jena.Writeable, RdfFormat.Jena.Readable:
override val fullName: String = "Turtle"
override val cliOptions: List[String] = List("ttl", "turtle")
override val jenaLang: Lang = RDFLanguages.TURTLE

case object TriG extends RdfFormat.Jena.Writeable, RdfFormat.Jena.Readable:
override val fullName: String = "TriG"
override val cliOptions: List[String] = List("trig")
override val jenaLang: Lang = RDFLanguages.TRIG

case object RdfProto extends RdfFormat.Jena.Writeable, RdfFormat.Jena.Readable:
override val fullName: String = "RDF Protobuf"
override val cliOptions: List[String] = List("jenaproto", "jena-proto")
override val jenaLang: Lang = RDFLanguages.RDFPROTO

case object Thrift extends RdfFormat.Jena.Writeable, RdfFormat.Jena.Readable:
override val fullName: String = "RDF Thrift"
override val cliOptions: List[String] = List("jenathrift", "jena-thrift")
override val jenaLang: Lang = RDFLanguages.RDFTHRIFT

case object RdfXml extends RdfFormat.Jena.Readable:
override val fullName: String = "RDF/XML"
override val cliOptions: List[String] = List("rdfxml", "rdf-xml")
override val jenaLang: Lang = RDFLanguages.RDFXML

case object JsonLd extends RdfFormat.Jena.Readable:
override val fullName: String = "JSON-LD"
override val cliOptions: List[String] = List("jsonld", "json-ld")
override val jenaLang: Lang = RDFLanguages.JSONLD

// We do not ever want to write or read from Jelly to Jelly
// So better not have it as Writeable or Readable, just mark that it's integrated into Jena
case object JellyBinary extends RdfFormat.Jena:
Expand All @@ -40,7 +70,8 @@ object RdfFormat:
override val cliOptions: List[String] = List("jelly-text")
val extension = ".jelly.txt"

private val rdfFormats: List[RdfFormat] = List(NQuads, NTriples, JellyBinary, JellyText)
private val rdfFormats: List[RdfFormat] =
List(NQuads, NTriples, JellyBinary, JellyText, Turtle, TriG, RdfProto, Thrift, RdfXml, JsonLd)

def all: List[RdfFormat] = rdfFormats

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package eu.neverblink.jelly.cli.command.helpers

import eu.ostrzyciel.jelly.convert.jena.riot.JellyLanguage
import org.apache.jena.rdf.model.{Model, ModelFactory, ResourceFactory}
import org.apache.jena.riot.{RDFDataMgr, RDFLanguages}
import org.apache.jena.riot.{Lang, RDFDataMgr, RDFLanguages}

import java.io.{ByteArrayInputStream, ByteArrayOutputStream}

Expand All @@ -18,13 +18,13 @@ object DataGenHelper:
*/
def generateTripleModel(nTriples: Int): Model =
val model = ModelFactory.createDefaultModel()
val subStr = "http://example.org/subject"
val predStr = "http://example.org/predicate"
val objStr = "http://example.org/object"
val subStr = "http://example.org/subject/index"
val predStr = "http://example.org/predicate/index"
val objStr = "http://example.org/object/index"
val tripleList = (1 to nTriples).map { i =>
val sub = ResourceFactory.createResource(s"$subStr/$i")
val pred = ResourceFactory.createProperty(s"$predStr/$i")
val obj = ResourceFactory.createResource(s"$objStr/$i")
val sub = ResourceFactory.createResource(s"$subStr$i")
val pred = ResourceFactory.createProperty(s"$predStr$i")
val obj = ResourceFactory.createResource(s"$objStr$i")
val stat = ResourceFactory.createStatement(sub, pred, obj)
model.add(stat)
}
Expand Down Expand Up @@ -60,22 +60,25 @@ object DataGenHelper:
* @return
* String
*/
def generateNQuadString(nTriples: Int): String =
def generateJenaString(nTriples: Int, jenaLang: Lang = RDFLanguages.NQUADS): String =
val model = generateTripleModel(nTriples)
val outputStream = new ByteArrayOutputStream()
RDFDataMgr.write(outputStream, model, RDFLanguages.NQUADS)
RDFDataMgr.write(outputStream, model, jenaLang)
outputStream.toString

/** This method generates an NQuad input stream with nTriples
*
* @param nTriples
* number of triples to generate
* @return
* String
* ByteArrayInputStream
*/
def generateNQuadInputStream(nTriples: Int): ByteArrayInputStream =
def generateJenaInputStream(
nTriples: Int,
jenaLang: Lang = RDFLanguages.NQUADS,
): ByteArrayInputStream =
val model = generateTripleModel(nTriples)
val outputStream = new ByteArrayOutputStream()
RDFDataMgr.write(outputStream, model, RDFLanguages.NQUADS)
RDFDataMgr.write(outputStream, model, jenaLang)
val nQuadStream = new ByteArrayInputStream(outputStream.toByteArray)
nQuadStream
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.scalatest.wordspec.AnyWordSpec
import java.io.FileOutputStream
import java.nio.file.{Files, Path}
import java.util.UUID.randomUUID
import scala.util.Using

object TestFixtureHelper

Expand All @@ -28,11 +29,13 @@ trait TestFixtureHelper extends BeforeAndAfterAll:
private def getFileExtension(format: Lang = RDFLanguages.NQUADS): String =
format.getFileExtensions.get(0)

def withFullQuadFile(testCode: (String) => Any): Unit =
val extension = getFileExtension(RDFLanguages.NQUADS)
def withFullJenaFile(testCode: (String) => Any, jenaLang: Lang = RDFLanguages.NQUADS): Unit =
val extension = getFileExtension(jenaLang)
val tempFile = Files.createTempFile(tmpDir, randomUUID.toString, f".${extension}")
val model = DataGenHelper.generateTripleModel(testCardinality)
RDFDataMgr.write(new FileOutputStream(tempFile.toFile), model, RDFLanguages.NQUADS)
Using(new FileOutputStream(tempFile.toFile)) { fileOutputStream =>
RDFDataMgr.write(fileOutputStream, model, jenaLang)
}
try {
testCode(tempFile.toString)
} finally { tempFile.toFile.delete() }
Expand Down Expand Up @@ -65,8 +68,8 @@ trait TestFixtureHelper extends BeforeAndAfterAll:
testCode(tempFile.toString)
} finally { tempFile.toFile.delete() }

def withEmptyQuadFile(testCode: (String) => Any): Unit =
val extension = getFileExtension(RDFLanguages.NQUADS)
def withEmptyJenaFile(testCode: (String) => Any, jenaLang: Lang = RDFLanguages.NQUADS): Unit =
val extension = getFileExtension(jenaLang)
val tempFile = Files.createTempFile(tmpDir, randomUUID.toString, f".${extension}")
try {
testCode(tempFile.toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eu.neverblink.jelly.cli.command.rdf
import com.google.protobuf.InvalidProtocolBufferException
import eu.neverblink.jelly.cli.*
import eu.neverblink.jelly.cli.command.helpers.*
import org.apache.jena.riot.RDFLanguages
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

Expand All @@ -18,7 +19,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
"rdf from-jelly command" should {
"handle conversion of Jelly to NTriples" when {
"a file to output stream" in withFullJellyFile { j =>
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
val nQuadString = DataGenHelper.generateJenaString(testCardinality)
val (out, err) =
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", j))
val sortedOut = out.split("\n").map(_.trim).sorted
Expand All @@ -29,7 +30,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
"input stream to output stream" in {
val input = DataGenHelper.generateJellyInputStream(testCardinality)
RdfFromJelly.setStdIn(input)
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
val nQuadString = DataGenHelper.generateJenaString(testCardinality)
val (out, err) = RdfFromJelly.runTestCommand(
List("rdf", "from-jelly", "--out-format", RdfFormat.NQuads.cliOptions.head),
)
Expand All @@ -38,8 +39,8 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
sortedOut should contain theSameElementsAs sortedQuads
}
"a file to file" in withFullJellyFile { j =>
withEmptyQuadFile { q =>
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
withEmptyJenaFile { q =>
val nQuadString = DataGenHelper.generateJenaString(testCardinality)
val (out, err) =
RdfFromJelly.runTestCommand(
List("rdf", "from-jelly", j, "--to", q),
Expand All @@ -54,7 +55,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
}
"a file to file when defaulting to nQuads" in withFullJellyFile { j =>
withEmptyRandomFile { q =>
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
val nQuadString = DataGenHelper.generateJenaString(testCardinality)
val (out, err) =
RdfFromJelly.runTestCommand(
List("rdf", "from-jelly", j, "--to", q),
Expand All @@ -67,10 +68,10 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
out.length should be(0)
}
}
"an input stream to file" in withEmptyQuadFile { q =>
"an input stream to file" in withEmptyJenaFile { q =>
val input = DataGenHelper.generateJellyInputStream(testCardinality)
RdfFromJelly.setStdIn(input)
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
val nQuadString = DataGenHelper.generateJenaString(testCardinality)
val (out, err) =
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", "--to", q))
val sortedOut = Using.resource(Source.fromFile(q)) { content =>
Expand Down Expand Up @@ -175,7 +176,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
exception.code should be(1)
}
"output file cannot be created" in withFullJellyFile { j =>
withEmptyQuadFile { q =>
withEmptyJenaFile { q =>
Paths.get(q).toFile.setWritable(false)
val exception =
intercept[ExitException] {
Expand All @@ -193,7 +194,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:

}
"deserializing error occurs" in withFullJellyFile { j =>
withEmptyQuadFile { q =>
withEmptyJenaFile { q =>
RdfFromJelly.runTestCommand(
List("rdf", "from-jelly", j, "--to", q),
)
Expand All @@ -211,7 +212,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
}
}
"parsing error occurs with debug set" in withFullJellyFile { j =>
withEmptyQuadFile { q =>
withEmptyJenaFile { q =>
RdfFromJelly.runTestCommand(
List("rdf", "from-jelly", j, "--to", q),
)
Expand All @@ -229,7 +230,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
}
}
"invalid output format supplied" in withFullJellyFile { j =>
withEmptyQuadFile { q =>
withEmptyJenaFile { q =>
val exception =
intercept[ExitException] {
RdfFromJelly.runTestCommand(
Expand Down Expand Up @@ -265,5 +266,32 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
exception.code should be(1)
}
}
"readable but not writable format supplied" in withFullJellyFile { j =>
withEmptyJenaFile(
testCode = { q =>
val exception =
intercept[ExitException] {
RdfFromJelly.runTestCommand(
List(
"rdf",
"from-jelly",
j,
"--to",
q,
"--out-format",
RdfFormat.RdfXml.cliOptions.head,
),
)
}
val msg = InvalidFormatSpecified(
RdfFormat.RdfXml.cliOptions.head,
RdfFromJellyPrint.validFormatsString,
)
RdfFromJelly.getErrString should include(msg.getMessage)
exception.code should be(1)
},
jenaLang = RDFLanguages.RDFXML,
)
}
}
}
Loading