Advertisement
Guest User

RDF Describer API Spec

a guest
May 22nd, 2010
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.34 KB | None | 0 0
  1. //package ...
  2.  
  3. import org.openrdf.repository.sail.SailRepository
  4. import org.openrdf.sail.memory.MemoryStore
  5.  
  6. import spock.lang.*
  7.  
  8.  
  9. class DescriberSpec extends Specification {
  10.  
  11.     def repo
  12.     def conn
  13.  
  14.     static ORG_URI = "http://example.org"
  15.  
  16.     def setup() {
  17.         repo = new SailRepository(new MemoryStore())
  18.         repo.initialize()
  19.         conn = repo.getConnection()
  20.     }
  21.  
  22.     def cleanup() {
  23.         conn.close()
  24.         repo.shutDown()
  25.     }
  26.  
  27.     def "should build and read rdf"() {
  28.         given:
  29.         def describer = newDescriber()
  30.  
  31.         when: "we make a lot of statements"
  32.         def p = describer.newDescription("$ORG_URI/persons/some_body#person")
  33.         p.addType("foaf:Person")
  34.         p.addValue("foaf:name", "Some Body")
  35.         p.addValue("foaf:firstName", "Some")
  36.         p.addValue("foaf:surname", "Body")
  37.         p.addRel("foaf:homepage", "$ORG_URI/persons/some_body")
  38.         p.addValue("rdfs:comment", "I am somebody.", "@en")
  39.         def img = p.addRel("foaf:depiction", "$ORG_URI/img/some_body.png")
  40.         img.addType("foaf:Image")
  41.         img.addRel("foaf:thumbnail", "$ORG_URI/img/some_body-64x64.png")
  42.         def cv = p.addRev("cv:aboutPerson")
  43.         cv.addType("cv:CV")
  44.         def work1 = cv.addRel("cv:hasWorkHistory")
  45.         work1.addValue("cv:startDate", "1999-01-01T00:00:00Z", "xsd:dateTime")
  46.         work1.addRel("cv:employedIn", "http://example.com/about#")
  47.         def work2 = cv.addRel("cv:hasWorkHistory")
  48.         work2.addValue("cv:startDate", "2000-01-01T00:00:00Z", "xsd:dateTime")
  49.         work2.addRel("cv:employedIn", "http://example.net/about#")
  50.  
  51.         then: "we can find the added data"
  52.         p.about == "http://example.org/persons/some_body#person"
  53.         p.type.about == "http://xmlns.com/foaf/0.1/Person"
  54.         p.getValue("foaf:name") == "Some Body"
  55.         img = p.getRel("foaf:depiction")
  56.         img.getRel("foaf:thumbnail").about == "http://example.org/img/some_body-64x64.png"
  57.         cv = p.getRev("cv:aboutPerson")
  58.         cv.getRels("cv:hasWorkHistory").collect { it.getRel("cv:employedIn").about }.sort() ==
  59.                 ["http://example.com/about#", "http://example.net/about#"]
  60.     }
  61.  
  62.     def "should find subjects and objects"() {
  63.         given:
  64.         def describer = newDescriber()
  65.         def s = "$ORG_URI/persons/some_body#person"
  66.         def o = "$ORG_URI/img/some_body.png"
  67.  
  68.         when: "a relation is added"
  69.         def p = describer.newDescription(s)
  70.         p.addRel("foaf:depiction", o)
  71.  
  72.         then: "subject and object are found via the added relation"
  73.         describer.subjectDescriptions("foaf:depiction", o).collect { it.about } == [s]
  74.         describer.objectDescriptions(s, "foaf:depiction").collect { it.about } == [o]
  75.  
  76.         and: "any reference finds them"
  77.         describer.subjectDescriptions(null, o).collect { it.about } == [s]
  78.         describer.objectDescriptions(s, null).collect { it.about } == [o]
  79.  
  80.         and: "there is only one of each"
  81.         describer.subjectDescriptions(null, null).collect { it.about } == [s]
  82.         describer.objectDescriptions(null, null).collect { it.about } == [o]
  83.     }
  84.  
  85.     def newDescriber() {
  86.         return new Describer(conn).
  87.             setPrefix('foaf', "http://xmlns.com/foaf/0.1/").
  88.             setPrefix('cv', "http://purl.org/captsolo/resume-rdf/0.2/cv#")
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement