Skip to content

Commit

Permalink
Add delete-history and history-instance Functions
Browse files Browse the repository at this point in the history
We need that functions in #1382 for the Jepsen tests.
  • Loading branch information
alexanderkiel committed Sep 16, 2024
1 parent 3afb319 commit 7a8d388
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
8 changes: 7 additions & 1 deletion modules/fhir-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test: prep
test-coverage: prep
clojure -M:test:coverage

deps-tree:
clojure -X:deps tree

deps-list:
clojure -X:deps list

cloc-prod:
cloc src

Expand All @@ -22,4 +28,4 @@ cloc-test:
clean:
rm -rf .clj-kondo/.cache .cpcache target

.PHONY: fmt lint prep test test-coverage cloc-prod cloc-test clean
.PHONY: fmt lint prep test test-coverage deps-tree deps-list cloc-prod cloc-test clean
31 changes: 30 additions & 1 deletion modules/fhir-client/src/blaze/fhir_client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@
(impl/update (str base-uri "/" (name type) "/" id) resource opts))

(defn delete
"Returns a CompletableFuture that will complete with resource of `type` and
`id` deleted."
[base-uri type id & [opts]]
(impl/delete (str base-uri "/" type "/" id) opts))

(defn delete-history
"Returns a CompletableFuture that will complete with resource of `type` and
`id` deleted."
{:arglists '([base-uri type id & [opts]])}
[base-uri type id & [opts]]
(impl/delete (str base-uri "/" (name type) "/" id) opts))
(impl/delete (str base-uri "/" type "/" id "/_history") opts))

(defn transact
"Returns a CompletableFuture that will complete with `bundle` transacted."
Expand Down Expand Up @@ -113,6 +119,29 @@
(flow/subscribe! src pro)
dst))

(defn history-instance-publisher
"Returns a Publisher that produces a Bundle per page of versions of resource
with `type` and `id`.
Use `resource-processor` to transform the pages to individual resources. Use
`history-instance` if you simply want to fetch all resources."
[base-uri type id & [opts]]
(reify Flow$Publisher
(subscribe [_ subscriber]
(->> (impl/paging-subscription subscriber (str base-uri "/" type "/" id "/_history") opts)
(flow/on-subscribe! subscriber)))))

(defn history-instance
"Returns a CompletableFuture that will complete with all versions of resource
with `type` and `id` in case of success or will complete exceptionally with an
anomaly in case of an error."
[base-uri type id & [opts]]
(let [src (history-instance-publisher base-uri type id opts)
pro (resource-processor)
dst (flow/collect pro)]
(flow/subscribe! src pro)
dst))

(defn spit
"Returns a CompletableFuture that will complete with a vector of all filenames
written of all resources the `publisher` produces."
Expand Down
19 changes: 19 additions & 0 deletions modules/fhir-client/src/blaze/fhir_client_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
:opts (s/? :blaze.fhir-client/options))
:ret ac/completable-future?)

(s/fdef fhir-client/delete-history
:args (s/cat :base-uri string? :type :fhir.resource/type :id :blaze.resource/id
:opts (s/? :blaze.fhir-client/options))
:ret ac/completable-future?)

(s/fdef fhir-client/transact
:args (s/cat :base-uri string? :bundle :blaze/resource
:opts (s/? :blaze.fhir-client/options))
Expand All @@ -59,10 +64,24 @@
:opts (s/? :blaze.fhir-client/options))
:ret ac/completable-future?)

(s/fdef fhir-client/search-system-publisher
:args (s/cat :base-uri string? :opts (s/? :blaze.fhir-client/options))
:ret flow/publisher?)

(s/fdef fhir-client/search-system
:args (s/cat :base-uri string? :opts (s/? :blaze.fhir-client/options))
:ret ac/completable-future?)

(s/fdef fhir-client/history-instance-publisher
:args (s/cat :base-uri string? :type :fhir.resource/type :id :blaze.resource/id
:opts (s/? :blaze.fhir-client/options))
:ret flow/publisher?)

(s/fdef fhir-client/history-instance
:args (s/cat :base-uri string? :type :fhir.resource/type :id :blaze.resource/id
:opts (s/? :blaze.fhir-client/options))
:ret ac/completable-future?)

(s/fdef fhir-client/spit
:args (s/cat :dir #(instance? Path %) :publisher flow/publisher?)
:ret ac/completable-future?)
45 changes: 37 additions & 8 deletions modules/fhir-client/test/blaze/fhir_client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@
{:http-client http-client})
:fhir/type := :fhir/OperationOutcome))))

(deftest delete-history-test
(let [http-client (HttpClientMock.)]

(-> (.onDelete http-client "http://localhost:8080/fhir/Patient/0/_history")
(.doReturnStatus 204))

(is (nil? @(fhir-client/delete-history "http://localhost:8080/fhir"
"Patient" "0"
{:http-client http-client})))))

(deftest transact-test
(let [http-client (HttpClientMock.)
bundle {:fhir/type :fhir/Bundle
Expand Down Expand Up @@ -373,9 +383,9 @@

(given @(fhir-client/search-system "http://localhost:8080/fhir"
{:http-client http-client})
count := 1
[0 :fhir/type] := :fhir/Patient
[0 :id] := "0"
1 := nil)))
[0 :id] := "0")))

(testing "one bundle with two patients"
(let [http-client (HttpClientMock.)]
Expand All @@ -391,11 +401,11 @@

(given @(fhir-client/search-system "http://localhost:8080/fhir"
{:http-client http-client})
count := 2
[0 :fhir/type] := :fhir/Patient
[0 :id] := "0"
[1 :fhir/type] := :fhir/Patient
[1 :id] := "1"
2 := nil)))
[1 :id] := "1")))

(testing "two bundles with two patients"
(let [http-client (HttpClientMock.)]
Expand All @@ -421,11 +431,11 @@

(given @(fhir-client/search-system "http://localhost:8080/fhir"
{:http-client http-client})
count := 2
[0 :fhir/type] := :fhir/Patient
[0 :id] := "0"
[1 :fhir/type] := :fhir/Patient
[1 :id] := "1"
2 := nil)))
[1 :id] := "1")))

(testing "with query params"
(let [http-client (HttpClientMock.)]
Expand All @@ -441,9 +451,9 @@
(given @(fhir-client/search-system "http://localhost:8080/fhir"
{:http-client http-client
:query-params {"_id" "0"}})
count := 1
[0 :fhir/type] := :fhir/Patient
[0 :id] := "0"
1 := nil)))
[0 :id] := "0")))

(testing "Server Error without JSON response"
(let [http-client (HttpClientMock.)]
Expand All @@ -455,6 +465,25 @@
{:http-client http-client})
::anom/category := ::anom/fault))))

(deftest history-instance-test
(testing "one bundle with one patient"
(let [http-client (HttpClientMock.)]

(-> (.onGet http-client "http://localhost:8080/fhir/Patient/0/_history")
(.doReturn
(j/write-value-as-string
{:resourceType "Bundle"
:entry
[{:resource {:resourceType "Patient" :id "0"}}]}))
(.withHeader "content-type" "application/fhir+json"))

(given @(fhir-client/history-instance "http://localhost:8080/fhir"
"Patient" "0"
{:http-client http-client})
count := 1
[0 :fhir/type] := :fhir/Patient
[0 :id] := "0"))))

(def temp-dir (Files/createTempDirectory "blaze" (make-array FileAttribute 0)))

(deftest spit-test
Expand Down

0 comments on commit 7a8d388

Please sign in to comment.