Skip to content

Commit

Permalink
Allow Metadata Requests in Batches
Browse files Browse the repository at this point in the history
Closes: #778
  • Loading branch information
alexanderkiel committed Aug 2, 2022
1 parent 82427ae commit 03fd97a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
57 changes: 57 additions & 0 deletions .github/scripts/batch-metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash -e

#
# This script fetches the CapabilityStatement through a batch request.
#

BASE="http://localhost:8080/fhir"

bundle() {
cat <<END
{
"resourceType": "Bundle",
"type": "batch",
"entry": [
{
"request": {
"method": "GET",
"url": "metadata"
}
}
]
}
END
}
RESULT=$(curl -sH "Content-Type: application/fhir+json" -d "$(bundle)" "$BASE")

RESOURCE_TYPE="$(echo "$RESULT" | jq -r .resourceType)"
if [ "$RESOURCE_TYPE" = "Bundle" ]; then
echo "OK: the resource type is Bundle"
else
echo "Fail: the resource type is $RESOURCE_TYPE, expected Bundle"
exit 1
fi

BUNDLE_TYPE="$(echo "$RESULT" | jq -r .type)"
if [ "$BUNDLE_TYPE" = "batch-response" ]; then
echo "OK: the bundle type is batch-response"
else
echo "Fail: the bundle type is $BUNDLE_TYPE, expected batch-response"
exit 1
fi

RESPONSE_STATUS="$(echo "$RESULT" | jq -r .entry[].response.status)"
if [ "$RESPONSE_STATUS" = "200" ]; then
echo "OK: the response status is 200"
else
echo "Fail: the response status is $RESPONSE_STATUS, expected 200"
exit 1
fi

RESPONSE_RESOURCE_TYPE="$(echo "$RESULT" | jq -r .entry[].resource.resourceType)"
if [ "$RESPONSE_RESOURCE_TYPE" = "CapabilityStatement" ]; then
echo "OK: resource type is CapabilityStatement"
else
echo "Fail: resource type was $RESPONSE_RESOURCE_TYPE but should be CapabilityStatement"
exit 1
fi
2 changes: 1 addition & 1 deletion .github/scripts/batch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ RESPONSE_PATIENT_ID="$(echo "$RESULT" | jq -r .entry[].resource.id)"
if [ "$RESPONSE_PATIENT_ID" = "$PATIENT_ID" ]; then
echo "OK: patient id's match"
else
echo "Fail: response patient id was $RESPONSE_PATIENT_ID but should be $RESPONSE_PATIENT_ID"
echo "Fail: response patient id was $RESPONSE_PATIENT_ID but should be $PATIENT_ID"
exit 1
fi
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ jobs:
- name: Batch
run: .github/scripts/batch.sh

- name: Batch Metadata
run: .github/scripts/batch-metadata.sh

- name: Transaction
run: .github/scripts/transaction.sh

Expand Down Expand Up @@ -853,6 +856,9 @@ jobs:
- name: Batch
run: .github/scripts/batch.sh

- name: Batch Metadata
run: .github/scripts/batch-metadata.sh

- name: Transaction
run: .github/scripts/transaction.sh

Expand Down
5 changes: 4 additions & 1 deletion modules/interaction/src/blaze/interaction/transaction.clj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
(not (#{"GET" "POST" "PUT" "DELETE"} method))
(unsupported-method-anom method idx)

(and (= "GET" method) (= "metadata" url))
entry

(nil? type)
(missing-type-anom url idx)

Expand All @@ -183,7 +186,7 @@
(id-mismatch-anom resource url idx)

:else
(assoc entry :blaze/type type :blaze/id id))))
entry)))


(def ^:private validate-entry-xf
Expand Down
35 changes: 34 additions & 1 deletion modules/interaction/test/blaze/interaction/transaction_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
https://www.hl7.org/fhir/operationoutcome.html
https://www.hl7.org/fhir/http.html#ops"
(:require
[blaze.async.comp :as ac]
[blaze.db.api-stub :refer [mem-node-system with-system-data]]
[blaze.executors :as ex]
[blaze.fhir.spec.type :as type]
Expand All @@ -30,6 +31,7 @@
[reitit.core :as reitit]
[reitit.ring]
[ring.middleware.params :refer [wrap-params]]
[ring.util.response :as ring]
[taoensso.timbre :as log])
(:import
[java.time Instant]
Expand Down Expand Up @@ -61,7 +63,12 @@
[_ {:keys [node create-handler search-type-handler
read-handler delete-handler update-handler]}]
(reitit.ring/router
[["/Observation"
[["/metadata"
{:get
(fn [_]
(ac/completed-future
(ring/response {:fhir/type :fhir/CapabilityStatement})))}]
["/Observation"
{:name :Observation/type
:fhir.resource/type "Observation"
:get {:middleware [[wrap-db node]]
Expand Down Expand Up @@ -1414,6 +1421,32 @@
[:issue 0 :diagnostics] := "Unsupported method `PATCH`."
[:issue 0 :expression 0] := "Bundle.entry[0].request.method"))))))

(testing "on metadata"
(with-handler [handler]
[]
(let [{:keys [status] {[{:keys [resource response]}] :entry} :body}
@(handler
{:body
{:fhir/type :fhir/Bundle
:type #fhir/code"batch"
:entry
[{:fhir/type :fhir.Bundle/entry
:request
{:fhir/type :fhir.Bundle.entry/request
:method #fhir/code"GET"
:url #fhir/uri"metadata"}}]}})]

(testing "response status"
(is (= 200 status)))

(testing "entry resource"
(given resource
:fhir/type := :fhir/CapabilityStatement))

(testing "entry response"
(given response
:status := "200")))))

(testing "and update interaction"
(testing "on invalid type-level URL"
(with-handler [handler]
Expand Down
21 changes: 21 additions & 0 deletions test/blaze/system_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@
[:body fhir-spec/parse-json :resourceType] := "OperationOutcome")))


(def metadata-bundle
{:fhir/type :fhir/Bundle
:type #fhir/code "batch"
:entry
[{:fhir/type :fhir.Bundle/entry
:request
{:fhir/type :fhir.Bundle.entry/request
:method #fhir/code"GET"
:url #fhir/uri"metadata"}}]})


(deftest batch-metadata-test
(with-system [{:blaze/keys [rest-api]} system]
(given (call rest-api {:request-method :post :uri ""
:headers {"content-type" "application/fhir+json"}
:body (input-stream (fhir-spec/unform-json metadata-bundle))})
:status := 200
[:body fhir-spec/parse-json :entry 0 :resource :resourceType] := "CapabilityStatement"
[:body fhir-spec/parse-json :entry 0 :response :status] := "200")))


(deftest delete-test
(with-system [{:blaze/keys [rest-api]} system]
(given (call rest-api {:request-method :delete :uri "/Patient/0"})
Expand Down

0 comments on commit 03fd97a

Please sign in to comment.