From 6b25f8b69d78cff2d170ca1aad12614589cad7fa Mon Sep 17 00:00:00 2001 From: Alexander Kiel Date: Mon, 16 Sep 2024 10:04:22 +0200 Subject: [PATCH] Add delete-history and history-instance Functions We need that functions in #1382 for the Jepsen tests. --- modules/fhir-client/Makefile | 8 +++- modules/fhir-client/src/blaze/fhir_client.clj | 31 ++++++++++++- .../src/blaze/fhir_client_spec.clj | 19 ++++++++ .../test/blaze/fhir_client_test.clj | 45 +++++++++++++++---- 4 files changed, 93 insertions(+), 10 deletions(-) diff --git a/modules/fhir-client/Makefile b/modules/fhir-client/Makefile index d28ff329c..edadd5867 100644 --- a/modules/fhir-client/Makefile +++ b/modules/fhir-client/Makefile @@ -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 @@ -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 diff --git a/modules/fhir-client/src/blaze/fhir_client.clj b/modules/fhir-client/src/blaze/fhir_client.clj index b77fd0286..3df937e48 100644 --- a/modules/fhir-client/src/blaze/fhir_client.clj +++ b/modules/fhir-client/src/blaze/fhir_client.clj @@ -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." @@ -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." diff --git a/modules/fhir-client/src/blaze/fhir_client_spec.clj b/modules/fhir-client/src/blaze/fhir_client_spec.clj index 27f6f987c..745bfbf36 100644 --- a/modules/fhir-client/src/blaze/fhir_client_spec.clj +++ b/modules/fhir-client/src/blaze/fhir_client_spec.clj @@ -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)) @@ -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?) diff --git a/modules/fhir-client/test/blaze/fhir_client_test.clj b/modules/fhir-client/test/blaze/fhir_client_test.clj index 7729ddb3f..e44762fc8 100644 --- a/modules/fhir-client/test/blaze/fhir_client_test.clj +++ b/modules/fhir-client/test/blaze/fhir_client_test.clj @@ -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 @@ -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.)] @@ -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.)] @@ -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.)] @@ -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.)] @@ -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