From ea01c9cfeb2c7a9ae031366b200518db8e87c311 Mon Sep 17 00:00:00 2001 From: Alexandre Bertails Date: Wed, 21 Nov 2012 16:21:39 -0500 Subject: [PATCH] first test for LDPS --- .../main/scala/plantain/PlantainStore.scala | 20 ++-- .../scala/plantain/PlantainURISyntax.scala | 4 + .../src/main/scala/plantain/plantain.scala | 2 +- .../plantain/PlantainGraphStoreTest.scala | 112 +++++++++++++++++- rdf/src/main/scala/syntax/NodeSyntax.scala | 5 + rdf/src/main/scala/syntax/TripleSyntax.scala | 6 + rdf/src/main/scala/syntax/UriSyntax.scala | 4 + 7 files changed, 137 insertions(+), 16 deletions(-) diff --git a/plantain/src/main/scala/plantain/PlantainStore.scala b/plantain/src/main/scala/plantain/PlantainStore.scala index 45731efb9..cc54f7284 100644 --- a/plantain/src/main/scala/plantain/PlantainStore.scala +++ b/plantain/src/main/scala/plantain/PlantainStore.scala @@ -37,19 +37,13 @@ trait LDPC[Rdf <: RDF] { trait LDPS[Rdf <: RDF] { def baseUri: Rdf#URI def shutdown(): Unit - def createLDPC(uri: URI): Future[LDPC[Rdf]] - def getLDPC(uri: URI): Future[LDPC[Rdf]] - def deleteLDPC(uri: URI): Future[Unit] + def createLDPC(uri: Rdf#URI): Future[LDPC[Rdf]] + def getLDPC(uri: Rdf#URI): Future[LDPC[Rdf]] + def deleteLDPC(uri: Rdf#URI): Future[Unit] } -object PlantainLDPR { - - - -} - /** * it's important for the uris in the graph to be absolute * this invariant is assumed by the sparql engine (TripleSource) @@ -58,13 +52,15 @@ case class PlantainLDPR(uri: URI, graph: Graph) extends LDPR[Plantain] { /* the graph such that all URIs are relative to $uri */ def relativeGraph: Graph = { - graph.triples.foldLeft(Graph.empty){ (current, triple) => current + triple.relativize(uri) } + graph.triples.foldLeft(Graph.empty){ (current, triple) => current + triple.relativizeAgainst(uri) } } } class PlantainLDPC(val uri: URI, actorRef: ActorRef)(implicit timeout: Timeout) extends LDPC[Plantain] { + override def toString: String = uri.toString + def execute[A](script: Plantain#Script[A]): Future[A] = { (actorRef ? Coordinated(Script(script))).asInstanceOf[Future[A]] } @@ -97,7 +93,7 @@ class PlantainLDPCActor(baseUri: URI, root: Path) extends Actor { } case GetLDPR(uri, k) => { val ldpr = LDPRs(uri.lastPathSegment) - run(coordinated, k(ldpr.graph)) + run(coordinated, k(ldpr.relativeGraph)) } case DeleteLDPR(uri, a) => { LDPRs.remove(uri.lastPathSegment) @@ -188,7 +184,7 @@ class PlantainLDPSActor(baseUri: URI, root: Path, system: ActorSystem)(implicit val ldpc = new PlantainLDPC(uri, ldpcActorRef) sender ! ldpc } - case coordinated @ Coordinated(GetLDPC(uri)) => coordinated atomic { implicit t => + case coordinated @ Coordinated(DeleteLDPC(uri)) => coordinated atomic { implicit t => val ldpcActorRefOpt = LDPCs.remove(uri) ldpcActorRefOpt foreach { ldpcActorRef => system.stop(ldpcActorRef) } sender ! () diff --git a/plantain/src/main/scala/plantain/PlantainURISyntax.scala b/plantain/src/main/scala/plantain/PlantainURISyntax.scala index 3e506e84b..55dd4c2b6 100644 --- a/plantain/src/main/scala/plantain/PlantainURISyntax.scala +++ b/plantain/src/main/scala/plantain/PlantainURISyntax.scala @@ -46,6 +46,10 @@ class PlantainURISyntax(val uri: URI) extends AnyVal with syntax.URISyntax[Plant URI(uri.underlying.relativize(other.underlying)) } + def relativizeAgainst(other: Plantain#URI)(implicit ops: RDFOps[Plantain]): Plantain#URI = { + URI(other.underlying.relativize(uri.underlying)) + } + def lastPathSegment: String = uri.underlying.toString.replaceFirst(".*/([^/?]+).*", "$1") } diff --git a/plantain/src/main/scala/plantain/plantain.scala b/plantain/src/main/scala/plantain/plantain.scala index c30595562..c369ca1d9 100644 --- a/plantain/src/main/scala/plantain/plantain.scala +++ b/plantain/src/main/scala/plantain/plantain.scala @@ -239,7 +239,7 @@ sealed trait Node { } catch { case iae: IllegalArgumentException => new SesameURI { - override def equals(o: Any): Boolean = o.isInstanceOf[URI] && o.asInstanceOf[URI].toString == uriS + override def equals(o: Any): Boolean = o.isInstanceOf[SesameURI] && o.asInstanceOf[SesameURI].toString == uriS def getLocalName: String = uriS def getNamespace: String = "" override def hashCode: Int = uriS.hashCode diff --git a/plantain/src/test/scala/plantain/PlantainGraphStoreTest.scala b/plantain/src/test/scala/plantain/PlantainGraphStoreTest.scala index f86ae37ea..f78f7942b 100644 --- a/plantain/src/test/scala/plantain/PlantainGraphStoreTest.scala +++ b/plantain/src/test/scala/plantain/PlantainGraphStoreTest.scala @@ -1,8 +1,114 @@ package org.w3.banana.plantain import org.w3.banana._ +import org.scalatest._ +import org.scalatest.matchers._ import Plantain._ +import LDPCommand._ +import scala.concurrent.ExecutionContext.Implicits.global -//class PlantainGraphStoreTest extends GraphStoreTest[Plantain]({ -// PlantainStore(null, null) -//}) +class PlantainLDPSTest extends LDPSTest[Plantain]({ + PlantainLDPS(null, null) +}) + +class LDPSTest[Rdf <: RDF]( + ldps: LDPS[Rdf])( + implicit diesel: Diesel[Rdf], + reader: RDFReader[Rdf, RDFXML]) extends WordSpec with MustMatchers with BeforeAndAfterAll { + + import diesel._ + import ops._ + + val foaf = FOAFPrefix[Rdf] + + override def afterAll(): Unit = { + ldps.shutdown() + } + + val graph: Rdf#Graph = ( + URI("#me") + -- foaf.name ->- "Alexandre".lang("fr") + -- foaf.title ->- "Mr" + ).graph + + val graph2: Rdf#Graph = ( + URI("#me") + -- foaf.name ->- "Alexandre".lang("fr") + -- foaf.knows ->- ( + URI("http://bblfish.net/#hjs") + -- foaf.name ->- "Henry Story" + -- foaf.currentProject ->- URI("http://webid.info/") + ) + ).graph + + val foo: Rdf#Graph = ( + URI("http://example.com/foo") + -- rdf("foo") ->- "foo" + -- rdf("bar") ->- "bar" + ).graph + + "CreateLDPR should create an LDPR with the given graph -- with given uri" in { + val ldpcUri = URI("http://example.com/foo") + val ldprUri = URI("http://example.com/foo/betehess") + val script = for { + ldpc <- ldps.createLDPC(ldpcUri) + _ <- ldpc.execute(createLDPR(Some(ldprUri), graph)) + rGraph <- ldpc.execute(getLDPR(ldprUri)) + _ <- ldps.deleteLDPC(ldpcUri) + } yield { + assert(rGraph isIsomorphicWith graph) + } + script.getOrFail(scala.concurrent.duration.Duration.create("10s")) + } + +// "getNamedGraph should retrieve the graph added with appendToGraph" in { +// val u1 = URI("http://example.com/foo/betehess") +// val u2 = URI("http://example.com/foo/alexandre") +// val r = for { +// ldpc <- ldps.createLDPC(URI("http://example.com/foo")) +// _ <- ldpc.execute() +// _ <- graphStore.appendToGraph(u1, graph) +// _ <- graphStore.appendToGraph(u2, graph2) +// rGraph <- graphStore.getGraph(u1) +// rGraph2 <- graphStore.getGraph(u2) +// } yield { +// assert(rGraph isIsomorphicWith graph) +// assert(rGraph2 isIsomorphicWith graph2) +// } +// r.getOrFail() +// } + +// "appendToGraph should be equivalent to graph union" in { +// val u = URI("http://example.com/graph") +// val r = for { +// _ <- graphStore.removeGraph(u) +// _ <- graphStore.appendToGraph(u, graph) +// _ <- graphStore.appendToGraph(u, graph2) +// rGraph <- graphStore.getGraph(u) +// } yield { +// assert(rGraph isIsomorphicWith union(List(graph, graph2))) +// } +// r.getOrFail() +// } +// +// "patchGraph should delete and insert triples as expected" in { +// val u = URI("http://example.com/graph") +// val r = for { +// _ <- graphStore.removeGraph(u) +// _ <- graphStore.appendToGraph(u, foo) +// _ <- graphStore.patchGraph(u, +// (URI("http://example.com/foo") -- rdf("foo") ->- "foo").graph.toIterable, +// (URI("http://example.com/foo") -- rdf("baz") ->- "baz").graph) +// rGraph <- graphStore.getGraph(u) +// } yield { +// val expected = ( +// URI("http://example.com/foo") +// -- rdf("bar") ->- "bar" +// -- rdf("baz") ->- "baz" +// ).graph +// assert(rGraph isIsomorphicWith expected) +// } +// r.getOrFail() +// } + +} diff --git a/rdf/src/main/scala/syntax/NodeSyntax.scala b/rdf/src/main/scala/syntax/NodeSyntax.scala index 07035e3db..09ca9ec13 100644 --- a/rdf/src/main/scala/syntax/NodeSyntax.scala +++ b/rdf/src/main/scala/syntax/NodeSyntax.scala @@ -17,4 +17,9 @@ class NodeSyntax[Rdf <: RDF](val node: Rdf#Node) extends AnyVal { foldNode(node)(_.relativize(baseUri), bn => bn, lit => lit) } + def relativizeAgainst(baseUri: Rdf#URI)(implicit ops: RDFOps[Rdf]): Rdf#Node = { + import ops.uriSyntax + ops.foldNode(node)(_.relativizeAgainst(baseUri), bn => bn, lit => lit) + } + } diff --git a/rdf/src/main/scala/syntax/TripleSyntax.scala b/rdf/src/main/scala/syntax/TripleSyntax.scala index d99f2ba9b..e51caf313 100644 --- a/rdf/src/main/scala/syntax/TripleSyntax.scala +++ b/rdf/src/main/scala/syntax/TripleSyntax.scala @@ -16,4 +16,10 @@ class TripleSyntax[Rdf <: RDF](val triple: Rdf#Triple) extends AnyVal { ops.makeTriple(s.relativize(baseUri), p, o.relativize(baseUri)) } + def relativizeAgainst(baseUri: Rdf#URI)(implicit ops: RDFOps[Rdf]): Rdf#Triple = { + import ops.nodeSyntax + val (s, p, o) = ops.fromTriple(triple) + ops.makeTriple(s.relativizeAgainst(baseUri), p, o.relativizeAgainst(baseUri)) + } + } diff --git a/rdf/src/main/scala/syntax/UriSyntax.scala b/rdf/src/main/scala/syntax/UriSyntax.scala index 91354cc8c..8d4fc63d7 100644 --- a/rdf/src/main/scala/syntax/UriSyntax.scala +++ b/rdf/src/main/scala/syntax/UriSyntax.scala @@ -25,6 +25,8 @@ trait URISyntax[Rdf <: RDF] extends Any { def relativize(other: Rdf#URI)(implicit ops: RDFOps[Rdf]): Rdf#URI + def relativizeAgainst(other: Rdf#URI)(implicit ops: RDFOps[Rdf]): Rdf#URI + def lastPathSegment: String } @@ -74,6 +76,8 @@ class URISyntaxDefault[Rdf <: RDF](val uri: Rdf#URI) extends AnyVal with URISynt def relativize(other: Rdf#URI)(implicit ops: RDFOps[Rdf]): Rdf#URI = URIHelper.relativize(uri, other)(ops) + def relativizeAgainst(other: Rdf#URI)(implicit ops: RDFOps[Rdf]): Rdf#URI = URIHelper.relativize(other, uri)(ops) + def lastPathSegment: String = uri.toString.replaceFirst(".*/([^/?]+).*", "$1") }