~ tests are better organized
authorAlexandre Bertails <bertails@gmail.com>
Sat, 27 Aug 2011 19:24:52 -0400
changeset 9 9a148d184ac2
parent 8 a0f7ac3d2722
child 10 6874dd1a9497
~ tests are better organized
src/main/resources/People/Berners-Lee/card
src/main/scala/Main.scala
src/main/scala/util.scala
src/test/scala/Test.scala
src/test/scala/utiltest.scala
--- a/src/main/resources/People/Berners-Lee/card	Sat Aug 27 18:40:08 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,160 +0,0 @@
-
-<!-- Processed by Id: cwm.py,v 1.197 2007/12/13 15:38:39 syosi Exp -->
-<!--     using base file:///devel/WWW/People/Berners-Lee/card.n3-->
-
-
-<rdf:RDF xmlns="http://xmlns.com/foaf/0.1/"
-    xmlns:cc="http://creativecommons.org/ns#"
-    xmlns:cert="http://www.w3.org/ns/auth/cert#"
-    xmlns:con="http://www.w3.org/2000/10/swap/pim/contact#"
-    xmlns:dc="http://purl.org/dc/elements/1.1/"
-    xmlns:dct="http://purl.org/dc/terms/"
-    xmlns:doap="http://usefulinc.com/ns/doap#"
-    xmlns:foaf="http://xmlns.com/foaf/0.1/"
-    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
-    xmlns:owl="http://www.w3.org/2002/07/owl#"
-    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
-    xmlns:rsa="http://www.w3.org/ns/auth/rsa#"
-    xmlns:s="http://www.w3.org/2000/01/rdf-schema#">
-
-    <rdf:Description rdf:about="../../DesignIssues/Overview.html">
-        <dc:title>Design Issues for the World Wide Web</dc:title>
-        <maker rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <PersonalProfileDocument rdf:about="">
-        <cc:license rdf:resource="http://creativecommons.org/licenses/by-nc/3.0/"/>
-        <dc:title>Tim Berners-Lee's FOAF file</dc:title>
-        <maker rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-        <primaryTopic rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </PersonalProfileDocument>
-
-    <rdf:Description rdf:about="http://dig.csail.mit.edu/2005/ajar/ajaw/data#Tabulator">
-        <doap:developer rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://dig.csail.mit.edu/2007/01/camp/data#course">
-        <maker rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <PersonalProfileDocument rdf:about="http://dig.csail.mit.edu/2008/webdav/timbl/foaf.rdf">
-        <cc:license rdf:resource="http://creativecommons.org/licenses/by-nc/3.0/"/>
-        <dc:title>Tim Berners-Lee's editable FOAF file</dc:title>
-        <maker rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-        <primaryTopic rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </PersonalProfileDocument>
-
-    <rdf:Description rdf:about="http://dig.csail.mit.edu/breadcrumbs/blog/4">
-        <dc:title>timbl's blog</dc:title>
-        <s:seeAlso rdf:resource="http://dig.csail.mit.edu/breadcrumbs/blog/feed/4"/>
-        <maker rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://dig.csail.mit.edu/data#DIG">
-        <member rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://wiki.ontoworld.org/index.php/_IRW2006">
-        <dc:title>Identity, Reference and the Web workshop 2006</dc:title>
-        <con:participant rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://www.ecs.soton.ac.uk/~dt2/dlstuff/www2006_data#panel-panelk01">
-        <s:label>The Next Wave of the Web (Plenary Panel)</s:label>
-        <con:participant rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://www.w3.org/2000/10/swap/data#Cwm">
-        <doap:developer rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://www.w3.org/2011/Talks/0331-hyderabad-tbl/data#talk">
-        <dct:title>Designing the Web for an Open Society</dct:title>
-        <maker rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <con:Male rdf:about="http://www.w3.org/People/Berners-Lee/card#i">
-        <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
-        <s:label>Tim Berners-Lee</s:label>
-        <s:seeAlso rdf:resource="http://dig.csail.mit.edu/2008/webdav/timbl/foaf.rdf"/>
-        <s:seeAlso rdf:resource="http://www.w3.org/2007/11/Talks/search/query?date=All+past+and+future+talks&#38;event=None&#38;activity=None&#38;name=Tim+Berners-Lee&#38;country=None&#38;language=None&#38;office=None&#38;rdfOnly=yes&#38;submit=Submit"/>
-        <con:assistant rdf:resource="http://www.w3.org/People/Berners-Lee/card#amy"/>
-        <con:homePage rdf:resource="./"/>
-        <con:office rdf:parseType="Resource">
-            <con:address rdf:parseType="Resource">
-                <con:city>Cambridge</con:city>
-                <con:country>USA</con:country>
-                <con:postalCode>02139</con:postalCode>
-                <con:street>32 Vassar Street</con:street>
-                <con:street2>MIT CSAIL Room 32-G524</con:street2>
-            </con:address>
-            <con:phone rdf:resource="tel:+1-617-253-5702"/>
-            <geo:location rdf:parseType="Resource">
-                <geo:lat>42.361860</geo:lat>
-                <geo:long>-71.091840</geo:long>
-            </geo:location>
-        </con:office>
-        <con:preferredURI>http://www.w3.org/People/Berners-Lee/card#i</con:preferredURI>
-        <con:publicHomePage rdf:resource="./"/>
-        <owl:sameAs rdf:resource="http://identi.ca/user/45563"/>
-        <owl:sameAs rdf:resource="http://www.advogato.org/person/timbl/foaf.rdf#me"/>
-        <owl:sameAs rdf:resource="http://www4.wiwiss.fu-berlin.de/bookmashup/persons/Tim+Berners-Lee"/>
-        <owl:sameAs rdf:resource="http://www4.wiwiss.fu-berlin.de/dblp/resource/person/100007"/>
-        <account rdf:resource="http://en.wikipedia.org/User:Timbl"/>
-        <account rdf:resource="http://identi.ca/timbl"/>
-        <account rdf:resource="http://twitter.com/timberners_lee"/>
-        <based_near rdf:parseType="Resource">
-            <geo:lat>42.361860</geo:lat>
-            <geo:long>-71.091840</geo:long>
-        </based_near>
-        <family_name>Berners-Lee</family_name>
-        <givenname>Timothy</givenname>
-        <homepage rdf:resource="http://www.w3.org/People/Berners-Lee/"/>
-        <img rdf:resource="http://www.w3.org/Press/Stock/Berners-Lee/2001-europaeum-eighth.jpg"/>
-        <mbox rdf:resource="mailto:timbl@w3.org"/>
-        <mbox_sha1sum>965c47c5a70db7407210cef6e4e6f5374a525c5c</mbox_sha1sum>
-        <name>Timothy Berners-Lee</name>
-        <nick>TimBL</nick>
-        <nick>timbl</nick>
-        <openid rdf:resource="http://www.w3.org/People/Berners-Lee/"/>
-        <phone rdf:resource="tel:+1-(617)-253-5702"/>
-        <title>Sir</title>
-        <weblog rdf:resource="http://dig.csail.mit.edu/breadcrumbs/blog/4"/>
-        <workplaceHomepage rdf:resource="http://www.w3.org/"/>
-    </con:Male>
-
-    <rdf:Description rdf:about="http://www.w3.org/data#W3C">
-        <member rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-    </rdf:Description>
-
-    <rdf:Description rdf:about="http://www4.wiwiss.fu-berlin.de/booksMeshup/books/006251587X">
-        <dc:creator rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-        <dc:title>Weaving the Web: The Original Design and Ultimate Destiny of the World Wide Web</dc:title>
-    </rdf:Description>
-
-    <rdf:Description>
-        <rdf:type rdf:resource="http://www.w3.org/ns/auth/rsa#RSAPublicKey"/>
-        <cert:identity rdf:resource="http://www.w3.org/People/Berners-Lee/card#i"/>
-        <rsa:modulus rdf:parseType="Resource">
-            <cert:hex>d7 a0 e9 1e ed dd cc 90 5d 5e cc d1 e4 12 ab 0c 
-5b db e1 18 fa 99 b7 13 2d 91 54 52 f0 b0 9a f5 
-eb c0 09 6c a1 db de ec 32 72 3f 5d dd 2b 05 56 
-4e 2c e6 7e ff ba 8e 86 77 8e 11 4a 02 a3 90 7c 
-2e 6c 6b 28 cf 16 fe e7 7d 0e f0 c4 4d 2e 3c cd 
-3e 0b 6e 8c fd d1 97 e3 aa 86 ec 19 99 80 72 9a 
-f4 45 1f 79 99 bc e5 5e b3 4b d5 a5 35 04 70 46 
-37 00 f7 30 8e 37 2b db 6e 07 5e 0b b8 a8 db a9 
-36 86 fa 4a e5 13 17 a4 43 82 bb 09 d0 92 94 c1 
-68 5b 10 97 ff d5 9c 44 6a e5 67 fa ec e6 b6 aa 
-27 89 79 06 b5 24 a6 49 89 bd 48 cf ea ec 61 d1 
-2c c0 b6 3d db 88 5d 2d ad b0 b3 58 c6 66 aa 93 
-f5 a4 43 fb 91 fc 2a 3d c6 99 eb 46 15 9b 05 c5 
-75 8c 9f 13 ed 28 44 09 4c c5 39 e5 82 e1 1d e3 
-6c 67 33 a6 7b 51 25 ef 40 7b 32 9e f5 e9 22 ca 
-57 46 a5 ff c6 7b 65 0b 4a e3 66 10 fc a0 cd 7b</cert:hex>
-        </rsa:modulus>
-        <rsa:public_exponent rdf:parseType="Resource">
-            <cert:decimal>65537</cert:decimal>
-        </rsa:public_exponent>
-    </rdf:Description>
-</rdf:RDF>
--- a/src/main/scala/Main.scala	Sat Aug 27 18:40:08 2011 -0400
+++ b/src/main/scala/Main.scala	Sat Aug 27 19:24:52 2011 -0400
@@ -73,7 +73,7 @@
           val (fos, _) = foo()
           bodyModel.write(fos, "RDF/XML-ABBREV", baseURI)
           fos.close()
-          Ok ~> ResponseString("")
+          Created
         }
         case POST(_) => {
           /* http://openjena.org/ARQ/javadoc/com/hp/hpl/jena/update/UpdateFactory.html */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/util.scala	Sat Aug 27 19:24:52 2011 -0400
@@ -0,0 +1,49 @@
+package org.w3.readwriteweb
+
+import javax.servlet._
+import javax.servlet.http._
+import unfiltered.request._
+import unfiltered.response._
+import unfiltered.jetty._
+
+import java.io._
+import scala.io.Source
+
+import org.slf4j.{Logger, LoggerFactory}
+
+import com.hp.hpl.jena.rdf.model._
+import com.hp.hpl.jena.query._
+import com.hp.hpl.jena.update._
+
+import unfiltered.request._
+import unfiltered.response._
+import unfiltered.jetty._
+
+package object util {
+  
+  val defaultLang = "RDF/XML-ABBREV"
+
+  class MSAuthorVia(value:String) extends ResponseHeader("MS-Author-Via", List(value))
+  object ViaSPARQL extends MSAuthorVia("SPARQL")
+  
+  object ResponseModel {
+    def apply(model:Model, base:String, lang:String = defaultLang):ResponseStreamer =
+      new ResponseStreamer {
+        def stream(os:OutputStream):Unit = model.write(os, lang, base)
+      }
+  }
+  
+  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 reader = new StringReader(s)
+    val m = ModelFactory.createDefaultModel()
+    m.read(reader, base, lang)
+    m
+  }
+
+}
\ No newline at end of file
--- a/src/test/scala/Test.scala	Sat Aug 27 18:40:08 2011 -0400
+++ b/src/test/scala/Test.scala	Sat Aug 27 19:24:52 2011 -0400
@@ -29,42 +29,7 @@
   }
   
   def setup = { _.filter(new ReadWriteWeb(base).read) }
-
-  val timBL = host / "People/Berners-Lee/card#i"
-  
-  "a GET on TimBL's FOAF profile" should {
-    val (via, body) = Http(timBL >+ { req =>
-      (req >:> { _("MS-Author-Via").head }, req as_str)
-    } )
-    "return an non empty document" in {
-      body must not be empty
-    }
-    """have the header "MS-Author-Via" set to SPARQL""" in {
-      via must_== "SPARQL"
-    }
-  }
-
     
-  val insertQuery =
-"""
-PREFIX foaf: <http://xmlns.com/foaf/0.1/>
-INSERT DATA { <http://dig.csail.xvm.mit.edu/2007/wiki/people/JoeLambda#JL> foaf:name "Joe Lambda" }
-"""
-        
-  "POSTing an INSERT query on Joe's URI (which does not exist yet)" should {
-    "return a 200" in {
-      val httpCode:Int = Http(joe.post(insertQuery) get_statusCode)
-      httpCode must_== 200
-    }
-    "create the corresponding file on disk" in {
-      joeOnDisk must exist
-    }
-    "create a valid rdf document with exactly one triple" in {
-      val model = Http(joe as_model(joe.path) )
-      model.size must_== 1
-    }
-  }
-  
   val joeRDF =
 """
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
@@ -74,21 +39,50 @@
   </foaf:Person>
 </rdf:RDF>
 """
+  
+  val initialModel = modelFromString(joeRDF, joe.path)
 
 //        <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 {
+  "PUTing an RDF document on Joe's URI (which does not exist yet)" should {
+    "return a 201" in {
       val httpCode:Int = Http(joe.put(joeRDF) get_statusCode)
+      httpCode must_== 201
+    }
+    "create a document on disk" in {
+      joeOnDisk must exist
+    }
+  }
+  
+  "Joe's URI" should {
+    "now exist and be isomorphic with the original document" in {
+      val (statusCode, via, model) = Http(joe >++ { req => (req.get_statusCode,
+                                                            req.get_header("MS-Author-Via"),
+                                                            req as_model(joe.path))
+                                                  } )
+      statusCode must_== 200
+      via must_== "SPARQL"
+      model must beIsomorphicWith (initialModel)
+    }
+  }
+  
+  val insertQuery =
+"""
+PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+INSERT DATA { </2007/wiki/people/JoeLambda#JL> foaf:openid </2007/wiki/people/JoeLambda> }
+"""
+  
+  "POSTing an INSERT query on Joe's URI (which does not exist yet)" should {
+    "succeed" in {
+      val httpCode:Int = Http(joe.post(insertQuery) get_statusCode)
       httpCode must_== 200
     }
-    "return an RDF document isomorphic with the original one" in {
+    "produce a graph with one more triple than the original one" in {
       val model = Http(joe as_model(joe.path))
-      model must beIsomorphicWith (modelFromString(joeRDF, joe.path))
+      model.size must_== (initialModel.size + 1)
     }
   }
-    
-    
+  
     
 }
--- a/src/test/scala/utiltest.scala	Sat Aug 27 18:40:08 2011 -0400
+++ b/src/test/scala/utiltest.scala	Sat Aug 27 19:24:52 2011 -0400
@@ -47,8 +47,16 @@
       
     def get_statusCode:Handler[Int] = new Handler(req, (c, r, e) => c, null)
     
+    def get_header(header:String):Handler[String] = req >:> { _(header).head }
+    
     def get:Request = req.copy(method="GET")
     
+    def >++ [A, B, C] (block: Request => (Handler[A], Handler[B], Handler[C])) = {
+      Handler(req, { (code, res, opt_ent) =>
+        val (a, b, c) = block( /\ )
+          (a.block(code, res, opt_ent), b.block(code,res,opt_ent), c.block(code,res,opt_ent))
+      } )
+    }
   }
   
   implicit def wrapRequest(req:Request):RequestW = new RequestW(req)