{
  "version": 13,
  "name": "22_fhir",
  "description": "Example: testing a FHIR R4 API (HL7 FHIR, JSON over REST). A full Patient lifecycle: read the server's CapabilityStatement, create a Patient (201, Location, ETag), read it back, update it (a new version), find it with a search by identifier (a searchset Bundle), check that an invalid resource is refused with an OperationOutcome, then delete it (Always run, so it is cleaned up even after a failure) and check that it is gone (410).\nAPI: the public HAPI FHIR R4 test server, hapi.fhir.org/baseR4 (test data only, purged regularly; never send real patient data to it).",
  "globals": [
    {
      "name": "$fhirBase",
      "value": "https://hapi.fhir.org/baseR4"
    },
    {
      "name": "$runTag",
      "value": ""
    },
    {
      "name": "$patientId",
      "value": ""
    }
  ],
  "globalFunctions": "// Assertion helpers, built on check(label, pass, details).\n// Each records a pass / fail line in the log and the report.\n// A failed one fails its step when the step is done;\n// the code keeps running. Change them or add your own.\n// r defaults to the last response.\n\nfunction assertTrue(value, label = \"is true\") {\n  return check(label, Boolean(value), { actual: value, expected: true });\n}\n\nfunction assertEqual(actual, expected, label = \"equals \" + JSON.stringify(expected)) {\n  return check(label, isEqual(actual, expected), { actual, expected });\n}\n\nfunction assertNotEqual(actual, unexpected, label = \"does not equal \" + JSON.stringify(unexpected)) {\n  return check(label, !isEqual(actual, unexpected), { actual, expected: \"not \" + JSON.stringify(unexpected) });\n}\n\n// text contains a part, or a list contains an item\nfunction assertContains(haystack, needle, label = \"contains \" + JSON.stringify(needle)) {\n  const pass = Array.isArray(haystack) ? haystack.some((x) => isEqual(x, needle)) : String(haystack).includes(needle);\n  return check(label, pass, { actual: haystack, expected: needle });\n}\n\nfunction assertMatch(text, regex, label = \"matches \" + regex) {\n  return check(label, new RegExp(regex).test(String(text)), { actual: text, expected: String(regex) });\n}\n\nfunction assertLessThan(actual, limit, label = \"< \" + limit) {\n  return check(label, Number(actual) < Number(limit), { actual, expected: \"< \" + limit });\n}\n\nfunction assertGreaterThan(actual, limit, label = \"> \" + limit) {\n  return check(label, Number(actual) > Number(limit), { actual, expected: \"> \" + limit });\n}\n\n// expected: 200, \"2xx\", or a list such as [200, 201]\nfunction assertStatus(expected, r = response) {\n  const list = [].concat(expected).map(String);\n  const status = String(r && r.status);\n  const pass = list.some((e) => e === status || (/^[1-5]xx$/i.test(e) && e[0] === status[0]));\n  return check(\"status is \" + list.join(\" or \"), pass, { actual: r && r.status, expected });\n}\n\nfunction assertTimeBelow(ms, r = response) {\n  return check(\"time < \" + ms + \" ms\", Boolean(r) && r.timeMs < ms, { actual: r && r.timeMs, expected: \"< \" + ms });\n}\n\n// without expected, the header only has to exist\nfunction assertHeader(name, expected, r = response) {\n  const value = r ? r.headers[String(name).toLowerCase()] : undefined;\n  if (expected === undefined) return check(\"header \" + name + \" exists\", value !== undefined, { actual: value });\n  return check(\"header \" + name + \" equals \" + expected, String(value) === String(expected), { actual: value, expected });\n}\n\n// e.g. assertJsonPath(\"$.items[0].id\", 42); without expected, the value only has to exist\nfunction assertJsonPath(path, expected, r = response) {\n  const value = jsonPath(path, r);\n  if (expected === undefined) return check(path + \" exists\", value !== undefined, { actual: value });\n  return check(path + \" equals \" + JSON.stringify(expected), isEqual(value, expected), { actual: value, expected });\n}\n\n// schema: a JSON Schema object\nfunction assertSchema(schema, r = response) {\n  const result = validateSchema(r, schema);\n  return check(\"matches the JSON Schema\", result.valid, { error: result.errors || undefined });\n}\n",
  "libraries": [],
  "activeEnvironment": "",
  "auth": {
    "type": "none"
  },
  "settings": {},
  "data": {
    "file": ""
  },
  "cookies": [],
  "tags": [
    "example",
    "fhir",
    "healthcare",
    "crud"
  ],
  "steps": [
    {
      "code": "// A unique identifier for this run, so the search below finds only our Patient\n// (the public server holds many test patients).\n$runTag = \"clem-\" + require(\"crypto\").randomUUID();\nlog(\"identifier for this run:\", $runTag);",
      "id": "run-tag",
      "name": "A unique identifier for this run",
      "type": "code"
    },
    {
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.resourceType",
          "target": "json",
          "value": "CapabilityStatement"
        },
        {
          "op": "matches",
          "path": "$.fhirVersion",
          "target": "json",
          "value": "^4\\.0"
        },
        {
          "op": "contains",
          "path": "$.format[*]",
          "target": "json",
          "value": "application/fhir+json"
        },
        {
          "op": "contains",
          "path": "$.rest[0].resource[*].type",
          "target": "json",
          "value": "Patient"
        }
      ],
      "headers": [
        {
          "name": "Accept",
          "value": "application/fhir+json"
        }
      ],
      "id": "capabilities",
      "method": "GET",
      "name": "CapabilityStatement: FHIR 4.0 (R4), Patient supported",
      "type": "request",
      "url": "$fhirBase/metadata"
    },
    {
      "body": "{\n  \"resourceType\": \"Patient\",\n  \"identifier\": [\n    {\n      \"system\": \"https://clemflows.com/examples\",\n      \"value\": \"$runTag\"\n    }\n  ],\n  \"name\": [\n    {\n      \"use\": \"official\",\n      \"family\": \"Example\",\n      \"given\": [\n        \"Clem\"\n      ]\n    }\n  ],\n  \"gender\": \"female\",\n  \"birthDate\": \"1990-05-17\"\n}",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "201"
        },
        {
          "op": "matches",
          "path": "Location",
          "target": "header",
          "value": "/Patient/[^/]+/_history/1$"
        },
        {
          "op": "equals",
          "path": "ETag",
          "target": "header",
          "value": "W/\"1\""
        },
        {
          "op": "equals",
          "path": "$.resourceType",
          "target": "json",
          "value": "Patient"
        },
        {
          "op": "exists",
          "path": "$.id",
          "target": "json",
          "value": ""
        },
        {
          "op": "equals",
          "path": "$.meta.versionId",
          "target": "json",
          "value": "1"
        },
        {
          "op": "equals",
          "path": "$.identifier[0].value",
          "target": "json",
          "value": "$runTag"
        }
      ],
      "extract": [
        {
          "after": "/_history",
          "before": "/Patient/",
          "from": "Headers",
          "name": "$patientId",
          "ordinal": 1
        }
      ],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/fhir+json"
        },
        {
          "name": "Accept",
          "value": "application/fhir+json"
        },
        {
          "name": "Prefer",
          "value": "return=representation"
        }
      ],
      "id": "create",
      "method": "POST",
      "name": "Create a Patient (201, Location, version 1)",
      "type": "request",
      "url": "$fhirBase/Patient"
    },
    {
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.id",
          "target": "json",
          "value": "$patientId"
        },
        {
          "op": "equals",
          "path": "$.name[0].family",
          "target": "json",
          "value": "Example"
        },
        {
          "op": "equals",
          "path": "$.name[0].given[0]",
          "target": "json",
          "value": "Clem"
        },
        {
          "op": "equals",
          "path": "$.birthDate",
          "target": "json",
          "value": "1990-05-17"
        }
      ],
      "headers": [
        {
          "name": "Accept",
          "value": "application/fhir+json"
        }
      ],
      "id": "read",
      "method": "GET",
      "name": "Read it back by id",
      "type": "request",
      "url": "$fhirBase/Patient/$patientId"
    },
    {
      "body": "{\n  \"resourceType\": \"Patient\",\n  \"id\": \"$patientId\",\n  \"telecom\": [\n    {\n      \"system\": \"phone\",\n      \"value\": \"+40 700 000 000\",\n      \"use\": \"mobile\"\n    }\n  ],\n  \"identifier\": [\n    {\n      \"system\": \"https://clemflows.com/examples\",\n      \"value\": \"$runTag\"\n    }\n  ],\n  \"name\": [\n    {\n      \"use\": \"official\",\n      \"family\": \"Example\",\n      \"given\": [\n        \"Clem\"\n      ]\n    }\n  ],\n  \"gender\": \"female\",\n  \"birthDate\": \"1990-05-17\"\n}",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "ETag",
          "target": "header",
          "value": "W/\"2\""
        },
        {
          "op": "equals",
          "path": "$.meta.versionId",
          "target": "json",
          "value": "2"
        },
        {
          "op": "equals",
          "path": "$.telecom[0].system",
          "target": "json",
          "value": "phone"
        }
      ],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/fhir+json"
        },
        {
          "name": "Accept",
          "value": "application/fhir+json"
        },
        {
          "name": "Prefer",
          "value": "return=representation"
        }
      ],
      "id": "update",
      "method": "PUT",
      "name": "Update it (PUT): add a phone, version 2",
      "type": "request",
      "url": "$fhirBase/Patient/$patientId"
    },
    {
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.resourceType",
          "target": "json",
          "value": "Bundle"
        },
        {
          "op": "equals",
          "path": "$.type",
          "target": "json",
          "value": "searchset"
        },
        {
          "op": "equals",
          "path": "$.total",
          "target": "json",
          "value": "1"
        },
        {
          "op": "equals",
          "path": "$.entry[0].resource.id",
          "target": "json",
          "value": "$patientId"
        },
        {
          "op": "equals",
          "path": "$.entry[0].resource.meta.versionId",
          "target": "json",
          "value": "2"
        }
      ],
      "headers": [
        {
          "name": "Accept",
          "value": "application/fhir+json"
        }
      ],
      "id": "search",
      "method": "GET",
      "name": "Search by identifier: a Bundle with exactly our Patient",
      "params": [
        {
          "name": "identifier",
          "value": "https://clemflows.com/examples|$runTag"
        }
      ],
      "type": "request",
      "url": "$fhirBase/Patient"
    },
    {
      "body": "{\n  \"resourceType\": \"Patient\",\n  \"gender\": \"robot\"\n}",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "400"
        },
        {
          "op": "equals",
          "path": "$.resourceType",
          "target": "json",
          "value": "OperationOutcome"
        },
        {
          "op": "equals",
          "path": "$.issue[0].severity",
          "target": "json",
          "value": "error"
        },
        {
          "op": "contains",
          "path": "$.issue[0].diagnostics",
          "target": "json",
          "value": "robot"
        }
      ],
      "headers": [
        {
          "name": "Content-Type",
          "value": "application/fhir+json"
        },
        {
          "name": "Accept",
          "value": "application/fhir+json"
        },
        {
          "name": "Prefer",
          "value": "return=representation"
        }
      ],
      "id": "invalid",
      "method": "POST",
      "name": "Negative: an invalid gender is refused (400, OperationOutcome)",
      "type": "request",
      "url": "$fhirBase/Patient"
    },
    {
      "alwaysRun": true,
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.resourceType",
          "target": "json",
          "value": "OperationOutcome"
        }
      ],
      "conditional": "$patientId !== ''",
      "headers": [
        {
          "name": "Accept",
          "value": "application/fhir+json"
        }
      ],
      "id": "delete",
      "method": "DELETE",
      "name": "Cleanup: delete the Patient (Always run)",
      "type": "request",
      "url": "$fhirBase/Patient/$patientId"
    },
    {
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "410"
        },
        {
          "op": "equals",
          "path": "$.resourceType",
          "target": "json",
          "value": "OperationOutcome"
        }
      ],
      "headers": [
        {
          "name": "Accept",
          "value": "application/fhir+json"
        }
      ],
      "id": "gone",
      "method": "GET",
      "name": "It is gone (410 Gone)",
      "type": "request",
      "url": "$fhirBase/Patient/$patientId"
    }
  ]
}
