Skip to content

Commit

Permalink
Merge pull request #1068 from NHSDigital/SPINEDEM-3403-remove-statefu…
Browse files Browse the repository at this point in the history
…l-behaviour-karate-sandbox

SPINEDEM-3403 - Removed stateful behaviour of Karate sandbox
  • Loading branch information
agelledi authored Nov 12, 2024
2 parents fc08f7d + d09e9c1 commit bc45675
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 58 deletions.
1 change: 0 additions & 1 deletion karate-tests/src/test/java/mocks/sandbox/patch-patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ if (request.pathMatches('/Patient/{nhsNumber}') && request.patch) {
const originalPatient = session.patients[nhsNumber]
const updatedPatient = patchPatient(originalPatient, request)
if (updatedPatient) {
session.patients[nhsNumber] = updatedPatient
response.headers = buildResponseHeaders(request, updatedPatient)
response.body = updatedPatient
response.status = 200
Expand Down
16 changes: 4 additions & 12 deletions karate-tests/src/test/java/mocks/sandbox/post-patient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* karate objects */
/* global context, request, response, session */
/* global context, request, response */

/* functions defined in supporting-functions.js */
/* global basicResponseHeaders */
Expand All @@ -9,14 +9,6 @@
* to pick from when we need to create a new patient. When the array is exhausted, the mock server
* should reset itself to its initial state, removing all creates and updates.
*/
const VALID_NHS_NUMBERS = [
'5182663366', '5306479413', '5865618296', '5596570559', '5061511085',
'5954974977', '5157195761', '5897052085', '5359513144', '5446520122',
'5103506718', '5468649624', '5002272274', '5941993854', '5574773538',
'5899264950', '5604719625', '5117676297', '5705279671', '5890418181'
]

session.nhsNumberIndex = session.nhsNumberIndex || 0

function generateObjectId () {
// generates a random ID for the name and address objects, e.g. 8F1A21BC
Expand Down Expand Up @@ -144,9 +136,9 @@ function initializePatientData (request) {
const patient = JSON.parse(JSON.stringify(NEW_PATIENT))

// set a new NHS number for the patient
patient.id = VALID_NHS_NUMBERS[session.nhsNumberIndex]
patient.identifier[0].value = VALID_NHS_NUMBERS[session.nhsNumberIndex]
session.nhsNumberIndex += 1

patient.id = '5182663366'
patient.identifier[0].value = '5182663366'

// name and address objects need an ID
patient.name[0] = request.body.name[0]
Expand Down
3 changes: 2 additions & 1 deletion karate-tests/src/test/java/mocks/sandbox/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ session.patients = session.patients || {
9000000025: context.read('classpath:mocks/stubs/patientResponses/patient_9000000025.json'),
9000000033: context.read('classpath:mocks/stubs/patientResponses/patient_9000000033.json'),
9693632109: context.read('classpath:mocks/stubs/patientResponses/patient_9693632109.json'),
9733162043: context.read('classpath:mocks/stubs/patientResponses/patient_9733162043.json')
9733162043: context.read('classpath:mocks/stubs/patientResponses/patient_9733162043.json'),
9733162051: context.read('classpath:mocks/stubs/patientResponses/patient_9733162051.json')
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,24 @@
"Mr"
],
"use": "usual"
}
},
{
"use": "old",
"period": {
"start": "2018-12-31"
},
"prefix": [
"Dr"
],
"given": [
"Roger"
],
"family": "Killen",
"suffix": [
"PhD"
],
"id": "4E1F33EF"
}
],
"resourceType": "Patient"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"address": [
{
"id": "OVvLE",
"line": [
"2 DALE FARM COTTAGE",
"RACE LANE",
"WOOTTON",
"ULCEBY",
"S HUMBERSIDE"
],
"period": {
"start": "2016-01-31"
},
"postalCode": "DN39 6RF",
"use": "home"
}
],
"birthDate": "1939-10-21",
"gender": "male",
"generalPractitioner": [
{
"id": "KsFNC",
"identifier": {
"period": {
"start": "2004-12-20"
},
"system": "https://fhir.nhs.uk/Id/ods-organization-code",
"value": "A20047"
},
"type": "Organization"
}
],
"id": "9733162051",
"identifier": [
{
"extension": [
{
"url": "https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-NHSNumberVerificationStatus",
"valueCodeableConcept": {
"coding": [
{
"code": "01",
"display": "Number present and verified",
"system": "https://fhir.hl7.org.uk/CodeSystem/UKCore-NHSNumberVerificationStatus",
"version": "1.0.0"
}
]
}
}
],
"system": "https://fhir.nhs.uk/Id/nhs-number",
"value": "9733162051"
}
],
"meta": {
"security": [
{
"code": "U",
"display": "unrestricted",
"system": "http://terminology.hl7.org/CodeSystem/v3-Confidentiality"
}
],
"versionId": "4"
},
"name": [
{
"family": "FRAZER",
"given": [
"Nigel"
],
"id": "vfynR",
"period": {
"start": "1983-10-04"
},
"prefix": [
"MR"
],
"suffix": [
"PhD",
"MBBS"
],
"use": "usual"
}
],
"resourceType": "Patient"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ Feature: Patch patient - Add and remove data
* configure headers = call read('classpath:auth/auth-headers.js')

* url baseURL

@sandbox
Scenario: Add and remove patient data
* def nhsNumber = '5900056449'
* path 'Patient', nhsNumber
* method get
* status 200
* def patientObject = response
* def originalVersion = parseInt(response.meta.versionId)
* def originalEtag = karate.response.header('etag')
* copy originalNameArray = response.name

@sandbox
Scenario: Add and remove patient data
* match response.name == "#[1]"

# 1. Add a new name to the array of patient names
# ===============================================
Expand Down Expand Up @@ -52,8 +48,9 @@ Feature: Patch patient - Add and remove data
* request {"patches": [{ "op": "add", "path": "/name/-", "value": "#(newName)" }]}
* method patch
* status 200
* match response.name == "#[2]"
* match response.name[1] == expectedName
* def addedName = response.name.find(x => x.family == "Bloggs")

* match addedName == expectedName
* match parseInt(response.meta.versionId) == originalVersion + 1

# 2. Remove the name we just added
Expand All @@ -62,17 +59,17 @@ Feature: Patch patient - Add and remove data
# Now remove the name. To illustrate the remove behaviour, we do two tests here:
# 1. We try to remove the name without first testing for it, which should fail
# 2. We test for the name and then remove it, which should succeed
* def originalVersion = parseInt(response.meta.versionId)
* def nameID = response.name[1].id
* def etag = karate.response.header('etag')
* path 'Patient', nhsNumber
* method get
* status 200

# 2.1. You can't call a "remove" operation without first calling a "test" operation
* def diagnostics = "Invalid update with error - removal '/name/1' is not immediately preceded by equivalent test - instead it is the first item"
* def expectedBody = read('classpath:mocks/stubs/errorResponses/INVALID_UPDATE.json')

* configure headers = call read('classpath:auth/auth-headers.js')
* header Content-Type = "application/json-patch+json"
* header If-Match = etag
* header If-Match = karate.response.header('etag')
* path 'Patient', nhsNumber
* request {"patches":[{"op":"remove","path":"/name/1"}]}
* method patch
Expand All @@ -81,6 +78,11 @@ Feature: Patch patient - Add and remove data

# 2.2. How to remove the name object correctly - define the id of the object
# you want to remove in the "test" operation
* path 'Patient', nhsNumber
* method get
* status 200
* def originalVersion = parseInt(response.meta.versionId)
* def nameID = response.name[1].id
* def patchBody =
"""
{"patches":[
Expand All @@ -90,17 +92,20 @@ Feature: Patch patient - Add and remove data
"""
* configure headers = call read('classpath:auth/auth-headers.js')
* header Content-Type = "application/json-patch+json"
* header If-Match = etag
* header If-Match = karate.response.header('etag')
* path 'Patient', nhsNumber
* request patchBody
* method patch
* status 200
* match response.name == "#[1]"
* match response.name == originalNameArray
* match response.name !contains { id: '#(nameID)' }
* match parseInt(response.meta.versionId) == originalVersion + 1

# 3. Add a suffix array
# =====================
* path 'Patient', nhsNumber
* method get
* status 200
* def patientObject = response
* def originalVersion = parseInt(response.meta.versionId)
* match response.name[0].suffix == "#notpresent"

Expand All @@ -121,69 +126,89 @@ Feature: Patch patient - Add and remove data
* status 200
* match response.name[0].suffix == suffixArray
* match parseInt(response.meta.versionId) == originalVersion + 1

# 4. Remove one of the suffixes we just added
# ===========================================
# We added an array of suffixes; now we're going to remove one of the suffixes in the
# array.
* def originalVersion = parseInt(response.meta.versionId)

# 4. Remove the whole suffix array
# ================================
# And you can get rid of the whole array of suffixes
* path 'Patient', nhsNumber
* method get
* status 200
* def originalVersion = parseInt(response.meta.versionId)
* def patientObject = response

* configure headers = call read('classpath:auth/auth-headers.js')
* header Content-Type = "application/json-patch+json"
* header If-Match = karate.response.header('etag')
* header If-Match = karate.response.header('etag')
* path 'Patient', nhsNumber
* request
"""
{"patches":[
{ "op": "test", "path": "/name/0/id", "value": "#(patientObject.name[0].id)" },
{ "op": "remove","path": "/name/0/suffix/0" }
{ "op": "remove", "path": "/name/0/suffix" }
]}
"""
"""
* method patch
* status 200
* match response.name[0].suffix == ["MBBS"]
* match response.name[0].suffix == '#notpresent'
* match parseInt(response.meta.versionId) == originalVersion + 1

# 5. Add new suffix to the array

@sandbox
Scenario: Add suffix to the existing array of suffixes and then remove the same
# 1. Add new suffix to the array
# ==============================
# You can also add a new suffix to an existing array of suffixes
* def nhsNumber = '9733162051'
* path 'Patient', nhsNumber
* method get
* status 200
* def originalVersion = parseInt(response.meta.versionId)

* def firstNameIndexWithSuffix = response.name.findIndex(x => x.suffix != null)
* def namePath = "/name/"+ firstNameIndexWithSuffix + "/id"
* def nameId = response.name.find(x => x.suffix != null).id
* def suffix = "Esquire"

* configure headers = call read('classpath:auth/auth-headers.js')
* header Content-Type = "application/json-patch+json"
* header If-Match = karate.response.header('etag')
* path 'Patient', nhsNumber
* request
"""
{"patches":[
{ "op": "add", "path": "/name/0/id", "value": "#(patientObject.name[0].id)" },
{ "op": "add", "path": "/name/0/suffix/0", "value": "Esquire" }
{ "op": "add", "path": "#(namePath)", "value": "#(nameId)" },
{ "op": "add", "path": "/name/0/suffix/0", "value": "#(suffix)" }
]}
"""
* method patch
* status 200
* match response.name[0].suffix == ["Esquire", "MBBS"]
* match response.name[0].suffix contains suffix
* match parseInt(response.meta.versionId) == originalVersion + 1

# 6. Remove the whole suffix array
# ================================
# And you can get rid of the whole array of suffixes

# 2. Remove one of the suffixes we just added
# ===========================================
# We added an array of suffixes; now we're going to remove one of the suffixes in the
# array.
* path 'Patient', nhsNumber
* method get
* status 200
* def originalVersion = parseInt(response.meta.versionId)

* def firstNameIndexWithSuffix = response.name.findIndex(x => x.suffix != null)
* def namePath = "/name/"+ firstNameIndexWithSuffix + "/id"
* def nameId = response.name.find(x => x.suffix != null).id
* def suffixPath = "/name/"+ firstNameIndexWithSuffix + "/suffix/0"

* configure headers = call read('classpath:auth/auth-headers.js')
* header Content-Type = "application/json-patch+json"
* header If-Match = karate.response.header('etag')
* header If-Match = karate.response.header('etag')
* path 'Patient', nhsNumber
* request
"""
{"patches":[
{ "op": "test", "path": "/name/0/id", "value": "#(patientObject.name[0].id)" },
{ "op": "remove", "path": "/name/0/suffix" }
{ "op": "test", "path": "#(namePath)", "value": "#(nameId)" },
{ "op": "remove","path": "#(suffixPath)" }
]}
"""
"""
* method patch
* status 200
* match response.name[0].suffix == '#notpresent'
* match parseInt(response.meta.versionId) == originalVersion + 1


Expand Down Expand Up @@ -240,4 +265,3 @@ Feature: Patch patient - Add and remove data
* status 200
* match parseInt(response.meta.versionId) == parseInt(idAftPod)+ 1
* match response.extension[1] == '#notpresent'

0 comments on commit bc45675

Please sign in to comment.