|
| 1 | +import http from "k6/http"; |
| 2 | +import { check } from "k6"; |
| 3 | +import exec from "k6/execution"; |
| 4 | +import { CATEGORY_TYPE_GROUPS } from "../type-category-mappings.js"; |
| 5 | + |
| 6 | +const csvPath = __ENV.DIST_PATH |
| 7 | + ? `../../../${__ENV.DIST_PATH}/producer_reference_data.csv` |
| 8 | + : "../producer_reference_data.csv"; |
| 9 | +const csv = open(csvPath); |
| 10 | +const lines = csv.trim().split("\n"); |
| 11 | +// Skip header |
| 12 | +const dataLines = lines.slice(1); |
| 13 | + |
| 14 | +function getNextPointer() { |
| 15 | + // pick the next line according to iteration in scenario |
| 16 | + const iter = exec.vu.iterationInScenario; |
| 17 | + const index = iter % dataLines.length; |
| 18 | + const line = dataLines[index]; |
| 19 | + const [count, pointer_id, pointer_type, custodian, nhs_number] = line |
| 20 | + .split(",") |
| 21 | + .map((field) => field.trim()); |
| 22 | + return { pointer_id, pointer_type, nhs_number }; |
| 23 | +} |
| 24 | + |
| 25 | +function getHeaders(odsCode) { |
| 26 | + return { |
| 27 | + "Content-Type": "application/fhir+json", |
| 28 | + "X-Request-Id": `K6perftest-consumer-${exec.scenario.name}-${exec.vu.idInTest}-${exec.vu.iterationInScenario}`, |
| 29 | + "NHSD-Correlation-Id": `K6perftest-consumer-${exec.scenario.name}-${exec.vu.idInTest}-${exec.vu.iterationInScenario}`, |
| 30 | + "NHSD-Connection-Metadata": JSON.stringify({ |
| 31 | + "nrl.ods-code": odsCode, |
| 32 | + "nrl.app-id": "K6PerformanceTest", |
| 33 | + }), |
| 34 | + "NHSD-Client-RP-Details": JSON.stringify({ |
| 35 | + "developer.app.name": "K6PerformanceTest", |
| 36 | + "developer.app.id": "K6PerformanceTest", |
| 37 | + }), |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function getCustodianFromPointerId(pointer_id) { |
| 42 | + // pointer_id format is "CUSTODIAN-XXXX" |
| 43 | + return pointer_id.split("-")[0]; |
| 44 | +} |
| 45 | + |
| 46 | +function checkResponse(res) { |
| 47 | + const is_success = check(res, { "status is 200": (r) => r.status === 200 }); |
| 48 | + if (!is_success) { |
| 49 | + console.warn(res.json()); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const pointerTypeToCategoryMap = new Map(); |
| 54 | +for (const group of CATEGORY_TYPE_GROUPS) { |
| 55 | + for (const type of group.types) { |
| 56 | + pointerTypeToCategoryMap.set(type.code, group.category.code); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export function countDocumentReference() { |
| 61 | + const { pointer_id, nhs_number } = getNextPointer(); |
| 62 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 63 | + const identifier = encodeURIComponent( |
| 64 | + `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}` |
| 65 | + ); |
| 66 | + |
| 67 | + const res = http.get( |
| 68 | + `https://${__ENV.HOST}/consumer/DocumentReference?_summary=count&subject:identifier=${identifier}`, |
| 69 | + { |
| 70 | + headers: getHeaders(custodian), |
| 71 | + } |
| 72 | + ); |
| 73 | + checkResponse(res); |
| 74 | +} |
| 75 | + |
| 76 | +export function readDocumentReference() { |
| 77 | + const { pointer_id } = getNextPointer(); |
| 78 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 79 | + |
| 80 | + const res = http.get( |
| 81 | + `https://${__ENV.HOST}/consumer/DocumentReference/${pointer_id}`, |
| 82 | + { |
| 83 | + headers: getHeaders(custodian), |
| 84 | + } |
| 85 | + ); |
| 86 | + |
| 87 | + checkResponse(res); |
| 88 | +} |
| 89 | + |
| 90 | +export function searchDocumentReference() { |
| 91 | + const { pointer_id, pointer_type, nhs_number } = getNextPointer(); |
| 92 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 93 | + |
| 94 | + const identifier = encodeURIComponent( |
| 95 | + `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}` |
| 96 | + ); |
| 97 | + const type = encodeURIComponent(`http://snomed.info/sct|${pointer_type}`); |
| 98 | + |
| 99 | + const res = http.get( |
| 100 | + `https://${__ENV.HOST}/consumer/DocumentReference?subject:identifier=${identifier}&type=${type}`, |
| 101 | + { |
| 102 | + headers: getHeaders(custodian), |
| 103 | + } |
| 104 | + ); |
| 105 | + checkResponse(res); |
| 106 | +} |
| 107 | + |
| 108 | +export function searchDocumentReferenceByCategory() { |
| 109 | + const { pointer_id, pointer_type, nhs_number } = getNextPointer(); |
| 110 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 111 | + const category_code = pointerTypeToCategoryMap.get(pointer_type); |
| 112 | + |
| 113 | + const identifier = encodeURIComponent( |
| 114 | + `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}` |
| 115 | + ); |
| 116 | + const category = encodeURIComponent( |
| 117 | + `http://snomed.info/sct|${category_code}` |
| 118 | + ); |
| 119 | + |
| 120 | + const res = http.get( |
| 121 | + `https://${__ENV.HOST}/consumer/DocumentReference?subject:identifier=${identifier}&category=${category}`, |
| 122 | + { |
| 123 | + headers: getHeaders(custodian), |
| 124 | + } |
| 125 | + ); |
| 126 | + checkResponse(res); |
| 127 | +} |
| 128 | + |
| 129 | +export function searchPostDocumentReference() { |
| 130 | + const { pointer_id, pointer_type, nhs_number } = getNextPointer(); |
| 131 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 132 | + |
| 133 | + const body = JSON.stringify({ |
| 134 | + "subject:identifier": `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}`, |
| 135 | + type: `http://snomed.info/sct|${pointer_type}`, |
| 136 | + }); |
| 137 | + |
| 138 | + const res = http.post( |
| 139 | + `https://${__ENV.HOST}/consumer/DocumentReference/_search`, |
| 140 | + body, |
| 141 | + { |
| 142 | + headers: getHeaders(custodian), |
| 143 | + } |
| 144 | + ); |
| 145 | + checkResponse(res); |
| 146 | +} |
| 147 | + |
| 148 | +export function searchPostDocumentReferenceByCategory() { |
| 149 | + const { pointer_id, pointer_type, nhs_number } = getNextPointer(); |
| 150 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 151 | + const category_code = pointerTypeToCategoryMap.get(pointer_type); |
| 152 | + |
| 153 | + const body = JSON.stringify({ |
| 154 | + "subject:identifier": `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}`, |
| 155 | + category: `http://snomed.info/sct|${category_code}`, |
| 156 | + }); |
| 157 | + |
| 158 | + const res = http.post( |
| 159 | + `https://${__ENV.HOST}/consumer/DocumentReference/_search`, |
| 160 | + body, |
| 161 | + { |
| 162 | + headers: getHeaders(custodian), |
| 163 | + } |
| 164 | + ); |
| 165 | + checkResponse(res); |
| 166 | +} |
| 167 | + |
| 168 | +export function countPostDocumentReference() { |
| 169 | + const { pointer_id, nhs_number } = getNextPointer(); |
| 170 | + const custodian = getCustodianFromPointerId(pointer_id); |
| 171 | + |
| 172 | + const body = JSON.stringify({ |
| 173 | + "subject:identifier": `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}`, |
| 174 | + }); |
| 175 | + |
| 176 | + const res = http.post( |
| 177 | + `https://${__ENV.HOST}/consumer/DocumentReference/_search?_summary=count`, |
| 178 | + body, |
| 179 | + { |
| 180 | + headers: getHeaders(custodian), |
| 181 | + } |
| 182 | + ); |
| 183 | + checkResponse(res); |
| 184 | +} |
| 185 | + |
| 186 | +export function searchPostDocumentReferenceAccessDenied() { |
| 187 | + const { nhs_number, pointer_type } = getNextPointer(); |
| 188 | + |
| 189 | + const body = JSON.stringify({ |
| 190 | + "subject:identifier": `https://fhir.nhs.uk/Id/nhs-number|${nhs_number}`, |
| 191 | + type: `http://snomed.info/sct|${pointer_type}`, |
| 192 | + }); |
| 193 | + |
| 194 | + // Use a custodian that should not have access (simulate denied) |
| 195 | + const deniedCustodian = "DENIED_ODS_CODE"; |
| 196 | + let headers = getHeaders(deniedCustodian); |
| 197 | + headers["NHSD-Connection-Metadata"] = JSON.stringify({ |
| 198 | + "nrl.ods-code": deniedCustodian, |
| 199 | + "nrl.app-id": "K6PerformanceTest", |
| 200 | + }); |
| 201 | + |
| 202 | + const res = http.post( |
| 203 | + `https://${__ENV.HOST}/consumer/DocumentReference/_search`, |
| 204 | + body, |
| 205 | + { |
| 206 | + headers: headers, |
| 207 | + } |
| 208 | + ); |
| 209 | + |
| 210 | + const is_denied = check(res, { "status is 403": (r) => r.status === 403 }); |
| 211 | + if (!is_denied) { |
| 212 | + console.warn(`Expected access denied but got: ${res.status}`); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +export function readDocumentReferenceNotFound() { |
| 217 | + const { custodian } = getNextPointer(); |
| 218 | + |
| 219 | + const res = http.get( |
| 220 | + `https://${__ENV.HOST}/consumer/DocumentReference/NonExistentID`, |
| 221 | + { |
| 222 | + headers: getHeaders(custodian), |
| 223 | + } |
| 224 | + ); |
| 225 | + |
| 226 | + // we expect a 404 here |
| 227 | + check(res, { "status is 404": (r) => r.status === 404 }); |
| 228 | +} |
0 commit comments