Skip to content

Commit

Permalink
fixed tests for core.rest namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniogarrote committed Jun 18, 2010
1 parent 7e0fbd4 commit de86a52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
17 changes: 16 additions & 1 deletion src/plaza/examples/webapp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[plaza.triple-spaces.core]
[plaza.triple-spaces.distributed-server]
[plaza.rest core])
(:require [compojure.route :as route]))
(:require [compojure.route :as route]
[clojure.contrib.str-utils2 :as str2]))


;; We will use jena
Expand All @@ -33,6 +34,7 @@
:wikipedia_entry {:uri foaf:holdsAccount :range rdfs:Resource}))

(tbox-register-schema :celebrity ComputationCelebrity-schema)
(tbox-register-schema :foaf-agent foaf:Agent-schema)

;; We create a Triple Space for the resources

Expand All @@ -53,6 +55,19 @@
(GET "/" [] "<h1>Testing plaza...</h1>")
(spawn-rest-resource! :celebrity "/Celebrity/:id" :celebrities)
(spawn-rest-collection-resource! :celebrity "/Celebrity" :celebrities)
(spawn-rest-resource! :foaf-agent "/CustomIds/:name" :resource
:id-property-alias :name
:id-property-uri foaf:name
:allowed-methods [:get]
:id-match-fn (fn [req env]
(let [pattern (str (:resource-qname-prefix env) (:resource-qname-local env))]
(str2/replace pattern ":name" (get (:params req) "name")))))
(spawn-rest-collection-resource! :foaf-agent "/CustomIds" :resource
:allowed-methods [:post]
:id-gen-fn (fn [req env]
(let [prefix (:resource-qname-prefix env)
local (:resource-qname-local env)]
{:id (get (:params req) "name") :uri (str prefix local "/" (get (:params req) "name"))})))
(route/not-found "Page not found"))

;; Running the application
Expand Down
23 changes: 12 additions & 11 deletions src/plaza/rest/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,14 @@
(defn random-resource-id [ns]
(random-uuid))

(defn build-triples-from-resource-map [uri mapping]
(let [resource-uri (if (seq? uri) (apply rdf-resource uri) (rdf-resource uri))]
(reduce (fn [acum [p o]]
(if (keyword? o)
acum
(conj acum [resource-uri p o])))
[] mapping)))
(defn build-triples-from-resource-map
([uri mapping]
(let [resource-uri (if (seq? uri) (apply rdf-resource uri) (rdf-resource uri))]
(reduce (fn [acum [p o]]
(if (keyword? o)
acum
(conj acum [resource-uri p o])))
[] mapping))))

(defn build-query-from-resource-map [mapping resource-type]
(concat [[?s rdf:type resource-type]]
Expand Down Expand Up @@ -593,7 +594,7 @@
(let [prefix (:resource-qname-prefix environment)
local (:resource-qname-local environment)
port (if (= (:server-port request) 80) "" (str ":" (:server-port request)))
id (random-resource-id)]
id (random-uuid)]
{:id id :uri (str (str prefix port local) "/" id)}))

(defmacro wrap-request [kind prefix local request & body]
Expand Down Expand Up @@ -1062,9 +1063,9 @@
(defn handle-post-collection [request environment]
(let [params (dissoc (:params request) (name (:id-property-alias environment)))
mapping (apply-resource-argument-map params (:resource-map environment))
{id :id resource-id :uri} ((:id-gen-function environment) request environment)
triples-pre (conj (build-triples-from-resource-map id resource-id mapping) [resource-id rdf:type (:resource-type environment)])
triples (if (nil? id) triples-pre (conj triples-pre [resource-id (:id-property-alias environment) (d id)]))]
{id :id resource-uri :uri} ((:id-gen-function environment) request environment)
triples-pre (conj (build-triples-from-resource-map resource-uri mapping) [resource-uri rdf:type (:resource-type environment)])
triples (if (nil? id) triples-pre (conj triples-pre [resource-uri (:id-property-alias environment) (d id)]))]
(out (ts (:resource-ts environment)) triples)
{:body (render-triples triples :xml (:resource environment) request)
:headers {"Content-Type" "application/xml"}
Expand Down
10 changes: 5 additions & 5 deletions test/plaza/rest/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@
(str2/replace pattern ":name" (get (:params req) "name")))))

(spawn-rest-collection-resource! :foaf-agent "/CustomIds" :resource
:allowed-methods [:post]
:id-gen-fn (fn [req env]
(let [prefix (:resource-qname-prefix env)
local (:resource-qname-local env)]
(str prefix local "/" (get (:params req) "name"))))))
:allowed-methods [:post]
:id-gen-fn (fn [req env]
(let [prefix (:resource-qname-prefix env)
local (:resource-qname-local env)]
{:id (get (:params req) "name") :uri (str prefix local "/" (get (:params req) "name"))}))))

;; Runnin the application
(future (run-jetty (var example) {:port 8082}))
Expand Down

0 comments on commit de86a52

Please sign in to comment.