Skip to content

Commit

Permalink
Organise create-draftsets out of draftset-api into feature layout
Browse files Browse the repository at this point in the history
  • Loading branch information
RickMoynihan committed Jan 29, 2018
1 parent 294efba commit 5d491f1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 56 deletions.
4 changes: 2 additions & 2 deletions resources/system.edn
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
:wrap-auth #ig/ref :drafter.middleware/wrap-auth}


:drafter.routes.draftsets-api/create-draftsets-handler {:drafter/backend #ig/ref :drafter/backend
:drafter.feature.draftset.create/handler {:drafter/backend #ig/ref :drafter/backend
:wrap-auth #ig/ref :drafter.middleware/wrap-auth}

:drafter.feature.middleware/wrap-as-draftset-owner {:drafter/backend #ig/ref :drafter/backend
Expand Down Expand Up @@ -113,7 +113,7 @@

:drafter.routes/draftsets-api {:get-users-handler #ig/ref :drafter.feature.users.list/get-users-handler
:get-draftsets-handler #ig/ref :drafter.feature.draftset.list/get-draftsets-handler
:create-draftsets-handler #ig/ref :drafter.routes.draftsets-api/create-draftsets-handler
:create-draftsets-handler #ig/ref :drafter.feature.draftset.create/handler
:get-draftset-handler #ig/ref :drafter.routes.draftsets-api/get-draftset-handler
:delete-draftset-handler #ig/ref :drafter.routes.draftsets-api/delete-draftset-handler
:draftset-options-handler #ig/ref :drafter.routes.draftsets-api/draftset-options-handler
Expand Down
27 changes: 26 additions & 1 deletion src/drafter/feature/draftset/create.clj
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
(ns drafter.feature.draftset.create)
(ns drafter.feature.draftset.create
(:require [drafter.backend.draftset.operations :as dsops]
[drafter.feature.common :as feat-common]
[drafter.rdf.draftset-management.job-util :as jobutil]
[drafter.util :as util]
[ring.util.response :as ring]
[swirrl-server.responses :as response]))

(defn create-draftsets-handler [{wrap-authenticated :wrap-auth backend :drafter/backend}]
(let [version "/v1"]
(wrap-authenticated
(fn [{{:keys [display-name description]} :params user :identity :as request}]
(feat-common/run-sync #(dsops/create-draftset! backend user display-name description util/create-uuid util/get-current-time)
(fn [result]
(if (jobutil/failed-job-result? result)
(response/api-response 500 result)
(ring/redirect-after-post (str version "/draftset/"
(get-in result [:details :id]))))))))))

(s/def ::wrap-auth fn?)

(defmethod ig/pre-init-spec ::handler [_]
(s/keys :req [:drafter/backend] :req-un [::wrap-auth]))

(defmethod ig/init-key ::handler [_ opts]
(create-draftsets-handler opts))

9 changes: 0 additions & 9 deletions src/drafter/routes/draftsets_api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@
(ring/redirect-after-post (str version "/draftset/"
(get-in result [:details :id]))))))))))

(s/def ::wrap-auth fn?)

(defmethod ig/pre-init-spec ::create-draftsets-handler [_]
(s/keys :req [:drafter/backend] :req-un [::wrap-auth]))

(defmethod ig/init-key ::create-draftsets-handler [_ opts]
(create-draftsets-handler opts))


(defn get-draftset-handler [{wrap-authenticated :wrap-auth backend :drafter/backend}]
(wrap-authenticated
(feat-middleware/existing-draftset-handler
Expand Down
69 changes: 68 additions & 1 deletion test/drafter/feature/draftset/create_test.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
(ns drafter.feature.draftset.create-test
(:require [drafter.feature.draftset.create :as sut]
[clojure.test :as t]))
[clojure.test :as t])
(:require [clojure.java.io :as io]
[clojure.set :as set]
[clojure.test :refer :all :as t]
[drafter.middleware :as middleware]
[drafter.rdf.drafter-ontology
:refer
[drafter:DraftGraph drafter:modifiedAt]]
[drafter.rdf.draftset-management.job-util :as jobs]
[drafter.rdf.sparql :as sparql]
[drafter.swagger :as swagger]
[drafter.test-common :as tc]
[drafter.timeouts :as timeouts]
[drafter.user :as user]
[drafter.user-test :refer [test-editor test-manager test-password test-publisher]]
[drafter.user.memory-repository :as memrepo]
[drafter.util :as util]
[grafter.rdf :refer [add context statements]]
[grafter.rdf.protocols :refer [->Quad ->Triple map->Triple]]
[grafter.rdf4j.formats :as formats]
[grafter.rdf4j.io :refer [rdf-writer]]
[schema.core :as s]
[swirrl-server.async.jobs :refer [finished-jobs]]))

(def see-other-response-schema
(merge tc/ring-response-schema
{:status (s/eq 303)
:headers {(s/required-key "Location") s/Str}}))



(defn assert-is-see-other-response [response]
(tc/assert-schema see-other-response-schema response))



(defn valid-swagger-response?
"Applies handler to request and validates the response against the
swagger spec for the requested route.
Returns the response if valid, otherwise raises an error."
[handler request]
(let [swagger-spec (swagger/load-spec-and-resolve-refs)]
(swagger/validate-response-against-swagger-spec swagger-spec request (handler request))))

(defn create-draftset-request
"Build a HTTP request object that representing a request to create a
draftset."
([] (create-draftset-request test-editor))
([user] (create-draftset-request user nil))
([user display-name] (create-draftset-request user display-name nil))
([user display-name description]
(tc/with-identity user {:uri "/v1/draftsets" :request-method :post :params {:display-name display-name :description description}})))

(tc/deftest-system create-draftset-without-title-or-description
[{handler :drafter.feature.draftset.create/handler} "test-system.edn"]
(let [request (tc/with-identity test-editor {:uri "/v1/draftsets" :request-method :post})
response (valid-swagger-response? handler request)]
(assert-is-see-other-response response)))

(tc/deftest-system create-draftset-with-title-and-without-description
[{handler :drafter.feature.draftset.create/handler} "test-system.edn"]
(let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test Title!"))]
(assert-is-see-other-response response)))

(tc/deftest-system create-draftset-with-title-and-description
[{handler :drafter.feature.draftset.create/handler} "test-system.edn"]
(let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test title" "Test description"))]
(assert-is-see-other-response response)))
47 changes: 6 additions & 41 deletions test/drafter/routes/draftsets_api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:refer
[drafter:DraftGraph drafter:modifiedAt]]
[drafter.rdf.draftset-management.job-util :as jobs]
[drafter.feature.draftset.create-test :as create-test]
[drafter.rdf.sparql :as sparql]
[drafter.routes.draftsets-api :as sut :refer :all]
[drafter.swagger :as swagger]
Expand Down Expand Up @@ -66,13 +67,6 @@
:body data-stream
:headers {"content-type" content-type}}))

(defn- create-draftset-request
([] (create-draftset-request test-editor))
([user] (create-draftset-request user nil))
([user display-name] (create-draftset-request user display-name nil))
([user display-name description]
(tc/with-identity user {:uri "/v1/draftsets" :request-method :post :params {:display-name display-name :description description}})))

(defn- make-append-data-to-draftset-request [user draftset-location data-file-path]
(with-open [fs (io/input-stream data-file-path)]
(let [request (append-to-draftset-request user draftset-location fs "application/x-trig")]
Expand Down Expand Up @@ -100,14 +94,6 @@
response (route request)]
(tc/await-success finished-jobs (get-in response [:body :finished-job]))))

(def see-other-response-schema
(merge tc/ring-response-schema
{:status (s/eq 303)
:headers {(s/required-key "Location") s/Str}}))

(defn assert-is-see-other-response [response]
(tc/assert-schema see-other-response-schema response))

(def ^:private DraftsetWithoutTitleOrDescription
{:id s/Str
:changes {URI {:status (s/enum :created :updated :deleted)}}
Expand Down Expand Up @@ -144,9 +130,9 @@
([user] (create-draftset-through-api user nil))
([user display-name] (create-draftset-through-api user display-name nil))
([user display-name description]
(let [request (create-draftset-request user display-name description)
(let [request (create-test/create-draftset-request user display-name description)
{:keys [headers] :as response} (route request)]
(assert-is-see-other-response response)
(create-test/assert-is-see-other-response response)
(get headers "Location"))))

(defn- get-draftset-quads-accept-request [draftset-location user accept union-with-live?-str]
Expand Down Expand Up @@ -262,30 +248,9 @@
(tc/assert-is-ok-response response)))

;; define a local alternative to the route fixture wrapper
(defn valid-swagger-response?
"Applies handler to request and validates the response against the
swagger spec for the requested route.
Returns the response if valid, otherwise raises an error."
[handler request]
(let [swagger-spec (swagger/load-spec-and-resolve-refs)]
(swagger/validate-response-against-swagger-spec swagger-spec request (handler request))))

(tc/deftest-system create-draftset-without-title-or-description
[{handler :drafter.routes.draftsets-api/create-draftsets-handler} "test-system.edn"]
(let [request (tc/with-identity test-editor {:uri "/v1/draftsets" :request-method :post})
response (valid-swagger-response? handler request)]
(assert-is-see-other-response response)))

(tc/deftest-system create-draftset-with-title-and-without-description
[{handler :drafter.routes.draftsets-api/create-draftsets-handler} "test-system.edn"]
(let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test Title!"))]
(assert-is-see-other-response response)))

(tc/deftest-system create-draftset-with-title-and-description
[{handler :drafter.routes.draftsets-api/create-draftsets-handler} "test-system.edn"]
(let [response (valid-swagger-response? handler (create-draftset-request test-editor "Test title" "Test description"))]
(assert-is-see-other-response response)))




#_(defn get-draftsets-request [include user]
(tc/with-identity user
Expand Down
4 changes: 2 additions & 2 deletions test/resources/web.edn
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
:wrap-auth #ig/ref :drafter.middleware/wrap-auth}


:drafter.routes.draftsets-api/create-draftsets-handler {:drafter/backend #ig/ref :drafter/backend
:drafter.feature.draftset.create/handler {:drafter/backend #ig/ref :drafter/backend
:wrap-auth #ig/ref :drafter.middleware/wrap-auth}

:drafter.feature.middleware/wrap-as-draftset-owner {:drafter/backend #ig/ref :drafter/backend
Expand Down Expand Up @@ -82,7 +82,7 @@

:drafter.routes/draftsets-api {:get-users-handler #ig/ref :drafter.feature.users.list/get-users-handler
:get-draftsets-handler #ig/ref :drafter.feature.draftset.list/get-draftsets-handler
:create-draftsets-handler #ig/ref :drafter.routes.draftsets-api/create-draftsets-handler
:create-draftsets-handler #ig/ref :drafter.feature.draftset.create/handler
:get-draftset-handler #ig/ref :drafter.routes.draftsets-api/get-draftset-handler
:delete-draftset-handler #ig/ref :drafter.routes.draftsets-api/delete-draftset-handler
:draftset-options-handler #ig/ref :drafter.routes.draftsets-api/draftset-options-handler
Expand Down

0 comments on commit 5d491f1

Please sign in to comment.