+ test for PUT + beIsomorphicWith Matcher
authorAlexandre Bertails <bertails@gmail.com>
Sat, 27 Aug 2011 18:40:08 -0400
changeset 8 a0f7ac3d2722
parent 7 e6bf64866274
child 9 9a148d184ac2
+ test for PUT + beIsomorphicWith Matcher
src/main/scala/Main.scala
src/test/scala/Test.scala
src/test/scala/utiltest.scala
--- a/src/main/scala/Main.scala	Sat Aug 27 17:54:12 2011 -0400
+++ b/src/main/scala/Main.scala	Sat Aug 27 18:40:08 2011 -0400
@@ -16,8 +16,7 @@
 import com.hp.hpl.jena.update._
 import com.hp.hpl.jena.shared.JenaException
 
-class MSAuthorVia(value:String) extends ResponseHeader("MS-Author-Via", List(value))
-object ViaSPARQL extends MSAuthorVia("SPARQL")
+import org.w3.readwriteweb.util._
 
 // holds some Unfiltered plans
 class ReadWriteWeb(base:File) {
@@ -67,23 +66,14 @@
       req match {
         case GET(_) => {
           val model:Model = loadModel(fileOnDisk)
-          Ok ~> ViaSPARQL ~> new ResponseStreamer {
-            def stream(os:OutputStream):Unit = {
-              val lang = "RDF/XML-ABBREV" // "TURTLE"
-              model.write(os, lang, baseURI)
-            }
-          }
+          Ok ~> ViaSPARQL ~> ResponseModel(model, baseURI)
         }
         case PUT(_) => {
-          val bodyModel = {
-            val m = ModelFactory.createDefaultModel()
-            m.read(Body.stream(req), baseURI)
-            m
-          }
+          val bodyModel = modelFromInputStream(Body.stream(req), baseURI)
           val (fos, _) = foo()
           bodyModel.write(fos, "RDF/XML-ABBREV", baseURI)
           fos.close()
-          Ok ~> ResponseString("don't know what to return")
+          Ok ~> ResponseString("")
         }
         case POST(_) => {
           /* http://openjena.org/ARQ/javadoc/com/hp/hpl/jena/update/UpdateFactory.html */
--- a/src/test/scala/Test.scala	Sat Aug 27 17:54:12 2011 -0400
+++ b/src/test/scala/Test.scala	Sat Aug 27 18:40:08 2011 -0400
@@ -11,6 +11,7 @@
 import com.hp.hpl.jena.query._
 import com.hp.hpl.jena.update._
 
+import org.w3.readwriteweb.util._
 import org.w3.readwriteweb.utiltest._
 
 object ReadWriteWebSpec extends Specification with unfiltered.spec.jetty.Served {
@@ -77,16 +78,16 @@
 //        <foaf:openid rdf:resource="/2007/wiki/people/JoeLambda" />
 //    <foaf:img rdf:resource="/2007/wiki/people/JoeLambda/images/me.jpg" />
 
-//  "PUTing an RDF document on Joe's URI (which now does exist)" should {
-//    "return a 200" in {
-//      val httpCode:Int = Http(joe.post(joeRDF) get_statusCode)
-//      httpCode must_== 200
-//    }
-//    "create a valid rdf document with exactly XXX triple" in {
-//      val model = Http(joe as_model(joe.path))
-//      model.size must_== 1
-//    }
-//  }
+  "PUTing an RDF document on Joe's URI (which now does exist)" should {
+    "return a 200" in {
+      val httpCode:Int = Http(joe.put(joeRDF) get_statusCode)
+      httpCode must_== 200
+    }
+    "return an RDF document isomorphic with the original one" in {
+      val model = Http(joe as_model(joe.path))
+      model must beIsomorphicWith (modelFromString(joeRDF, joe.path))
+    }
+  }
     
     
     
--- a/src/test/scala/utiltest.scala	Sat Aug 27 17:54:12 2011 -0400
+++ b/src/test/scala/utiltest.scala	Sat Aug 27 18:40:08 2011 -0400
@@ -21,8 +21,20 @@
 
 import dispatch._
 
+import org.specs.matcher.Matcher
+
+import org.w3.readwriteweb.util._
+
 package object utiltest {
-
+  
+  def beIsomorphicWith(that:Model):Matcher[Model] =
+    new Matcher[Model] {
+      def apply(otherModel: => Model) =
+        (that isIsomorphicWith otherModel,
+         "Model A is isomorphic to model B",
+         "%s not isomorphic with %s" format (otherModel.toString, that.toString))
+  }
+  
   class RequestW(req:Request) {
 
     def as_model(base:String, lang:String = "RDF/XML-ABBREV"):Handler[Model] =
@@ -31,6 +43,8 @@
     def post(body:String):Request =
       (req <<< body).copy(method="POST")
       
+    def put(body:String):Request = req <<< body
+      
     def get_statusCode:Handler[Int] = new Handler(req, (c, r, e) => c, null)
     
     def get:Request = req.copy(method="GET")
@@ -39,18 +53,6 @@
   
   implicit def wrapRequest(req:Request):RequestW = new RequestW(req)
   
-  def modelFromInputStream(is:InputStream, base:String, lang:String = "RDF/XML-ABBREV"):Model = {
-    val m = ModelFactory.createDefaultModel()
-    m.read(is, base, lang)
-    m
-  }
-  
-  def modelFromString(s:String, base:String, lang:String = "RDF/XML-ABBREV"):Model = {
-    val m = ModelFactory.createDefaultModel()
-    m.read(s, base, lang)
-    m
-  }
-