JSONPathΒΆ
JSONPath assertions can be evaluated against payloads of both request
and response
variables offered by the Touchstone Rules-Engine. Both XPath assertions and JSONPath assertions run significantly faster than FHIRPath assertions. Separate XPath and JSONPath expressions are needed though for XML and JSON content while only one FHIRPath expression is needed for both XML and JSON content.
assertJsonPathContains(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression contains the provided expectedValue.
Example:
// Asserts that the value of the family element of the second entry contains 'Chalmers' response.assertJsonPathContains("entry[1].resource.name.family", "Chalmers")
Equivalent to each of these:
assertJsonPathContains("entry[1].resource.name.family", "Chalmers", response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[1].resource.name.family", "Chalmers", "contains", response)
def family = response.getJsonPathValue("entry[1].resource.name.family") assert family.contains("Chalmers"): "The actual value \""+family+"\" did not contain the expected value \"Chalmers\" for \"entry[1].resource.name.family\" in response."
def family = response.jsonPath("entry[1].resource.name.family").value assert contains("Chalmers", family): "The actual value \""+family+"\" did not contain the expected value \"Chalmers\" for \"entry[1].resource.name.family\" in response."
Notice how the value of JsonPath evaluation can be stored in a variable. This is useful when you want to develop more complicated rule scripts where the assertions involve multiple comparisons.
assertJsonPathNotContains(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression does not contain the provided expectedValue.
Example:
// Asserts that the value of the family element of the second entry contains 'Chalmers' response.assertJsonPathNotContains("entry[1].resource.name.family", "Chalmers")
Equivalent to each of these:
assertJsonPathNotContains("entry[1].resource.name.family", "Chalmers", response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[1].resource.name.family", "Chalmers", "notContains", response)
def family = response.getJsonPathValue("entry[1].resource.name.family") assert !family.contains("Chalmers"): "The actual value \""+family+"\" contained the expected value \"Chalmers\" for \"entry[1].resource.name.family\" with operator 'notContains' in response."
def family = response.jsonPath("entry[1].resource.name.family").value assert notContains("Chalmers", family): "The actual value \""+family+"\" contained the expected value \"Chalmers\" for \"entry[1].resource.name.family\" with operator 'notContains' in response."
assertJsonPathEmpty(jsonpath)
Asserts that the evaluated value of the provided jsonpath expression is absent or empty.
Example:
response.assertJsonPathEmpty("entry[0].resource.photo.title")
Equivalent to these:
assertJsonPathEmpty("entry[0].resource.photo.title", response)
response.jsonPath("entry[0].resource.photo.title").empty();
def title = response.getJsonPathValue("entry[0].resource.photo.title") assert !title: "Expected title to be absent but was present in response"
def title = response.jsonPath("entry[0].resource.photo.title").value assert empty(title): "Expected title to be absent but was present in response"
assertJsonPathNotEmpty(jsonpath)
Asserts that the evaluated value of the provided jsonpath expression is present and not empty.
Example:
response.assertJsonPathNotEmpty("entry[0].resource.birthDate")
Equivalent to these:
assertJsonPathNotEmpty("entry[0].resource.birthDate", response)
response.jsonPath("entry[0].resource.birthDate").notEmpty();
def title = response.getJsonPathValue("entry[0].resource.birthDate") assert title: "Expected birthDate to be absent but was present in response"
def title = response.jsonPath("entry[0].resource.birthDate").value assert notEmpty(title): "Expected birthDate to be absent but was present in response"
assertJsonPathEquals(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression matches the provided expectedValue.
Example:
// Asserts that the value of the family element of the first entry is 'Chalmers' response.assertJsonPathEquals("entry[1]/resource/Patient/name/family", "Chalmers")
Equivalent to each of these:
assertJsonPathEquals("entry[1]/resource/Patient/name/family", "Chalmers", response)
// xPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[1]/resource/Patient/name/family", "Chalmers", "equals", response)
def family = response.getJsonPathValue("entry[1]/resource/Patient/name/family") assert family.equals("Chalmers"): "The actual value \""+family+"\" did not match the expected value \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" in response."
def family = response.jsonPath("entry[1]/resource/Patient/name/family").getValue() assert family == "Chalmers": "The actual value \""+family+"\" did not match the expected value \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" in response."
def family = response.xPath("entry[1]/resource/Patient/name/family").value assert equals("Chalmers", family): "The actual value \""+family+"\" did not match the expected value \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" in response."
assertJsonPathEquals(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression matches the provided expectedValue.
Example:
// Asserts that the value of the family element of the first entry is 'Chalmers' response.assertJsonPathEquals("entry[0].resource.name.family", "Chalmers")
Equivalent to each of these:
assertJsonPathEquals("entry[0].resource.name.family", "Chalmers", response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[0].resource.name.family", "Chalmers", "equals", response)
def family = response.getJsonPathValue("entry[0].resource.name.family") assert family.equals("Chalmers"): "The actual value \""+family+"\" did not match the expected value \"Chalmers\" for \"entry[0].resource.name.family\" in response."
def family = response.jsonPath("entry[0].resource.name.family").getValue() assert family == "Chalmers": "The actual value \""+family+"\" did not match the expected value \"Chalmers\" for \"entry[0].resource.name.family\" in response."
def family = response.jsonPath("entry[0].resource.name.family").value assert equals("Chalmers", family): "The actual value \""+family+"\" did not match the expected value \"Chalmers\" for \"entry[0].resource.name.family\" in response."
assertJsonPathNotEquals(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression does not match the provided expectedValue.
Example:
// Asserts that values of the family element of the first entry is not 'Chalmers' response.assertJsonPathNotEquals("entry[0].resource.name.family", "Chalmers")
Equivalent to each of these:
assertJsonPathNotEquals("entry[0].resource.name.family", "Chalmers", response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[0].resource.name.family", "Chalmers", "notEquals", response)
def family = response.getJsonPathValue("entry[0].resource.name.family") assert !family.equals("Chalmers"): "The actual value \""+family+"\" matched the expected value \"Chalmers\" for \"entry[0].resource.name.family\" with operator 'notEquals' in response."
def family = response.jsonPath("entry[0].resource.name.family").getValue() assert family != "Chalmers": "The actual value \""+family+"\" matched the expected value \"Chalmers\" for \"entry[0].resource.name.family\" with operator 'notEquals' in response."
def family = response.jsonPath("entry[0].resource.name.family").value assert notEquals("Chalmers", family): "The actual value \""+family+"\" matched the expected value \"Chalmers\" for \"entry[0].resource.name.family\" with operator 'notEquals' in response."
assertJsonPathGreaterThan(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression is greater than the provided expectedValue.
Example:
// Asserts that resource id of the second entry is greater than 1100 in the response response.assertJsonPathGreaterThan("entry[1].resource.id", 1100)
Equivalent to each of these:
assertJsonPathGreaterThan("entry[1].resource.id", 1100, response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[1].resource.id", "1100", "greaterThan", response)
def id = response.getJsonPathValue("entry[1].resource.id") assert id.toInteger() > 1100: "Expected \"entry[1].resource.id\" to be greater than 5000 but was "+id+" in response"
def id = response.jsonPath("entry[1].resource.id").getValue() assert (id as Integer) > 1100: "Expected \"entry[1].resource.id\" to be greater than 5000 but was "+id+" in response"
def id = response.jsonPath("entry[1].resource.id").value assert greaterThan(1100, id): "Expected \"entry[1].resource.id\" to be greater than 5000 but was "+id+" in response"
assertJsonPathLessThan(jsonpath, expectedValue)
Asserts that the evaluated value of the provided jsonpath expression is less than the provided expectedValue.
Example:
// Asserts that resource id of the first entry is less than 1100 in the response response.assertJsonPathLessThan("entry[0].resource.id", 1100)
Equivalent to each of these:
assertJsonPathLessThan("entry[0].resource.id", 1100, response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[0].resource.id", "1100", "lessThan", response)
def id = response.getJsonPathValue("entry[0].resource.id") assert id.toInteger() < 1100: "Expected \"entry[0].resource.id\" to be less than 5000 but was "+id+" in response"
def id = response.jsonPath("entry[0].resource.id").getValue() assert (id as Integer) < 1100: "Expected \"entry[0].resource.id\" to be less than 5000 but was "+id+" in response"
def id = response.jsonPath("entry[0].resource.id").value assert lessThan(1100, id): "Expected \"entry[0].resource.id\" to be less than 5000 but was "+id+" in response"
assertJsonPathIn(jsonpath, expectedValues)
Asserts that the evaluated value of the provided jsonpath expression is one of the provided expectedValues where each value is separated by a comma.
Example:
// Asserts that resource id of the second entry is either 1100 or 1101 or 1102 response.assertJsonPathIn("entry[1].resource.id", "1100,1101,1102")
Equivalent to each of these:
assertJsonPathIn("entry[1].resource.id", "1100,1101,1102", response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[1].resource.id", "1100,1101,1102", "in", response)
def id = response.getJsonPathValue("entry[1].resource.id") assert id in ["1100", "1101", "1102"]: "Expected one of the values in [1100, 1101, 1102] for \"entry[1].resource.id\" but encountered \""+id+"\" in response."
def id = response.jsonPath("entry[1].resource.id").value assert isIn("1100,1101,1102", id): "Expected one of the values in [1100, 1101, 1102] for \"entry[1].resource.id\" but encountered \""+id+"\" in response."
assertJsonPathNotIn(jsonpath, expectedValues)
Asserts that the evaluated value of the provided jsonpath expression is none of the provided expectedValues where each value is separated by a comma.
Example:
// Asserts that resource id of the second entry is either 1100 or 1101 or 1102 response.assertJsonPathNotIn("entry[1].resource.id", "1100,1101,1102")
Equivalent to each of these:
assertJsonPathNotIn("entry[1].resource.id", "1100,1101,1102", response)
// jsonPath, expectedValue, and operator can be passed as parameters from test script. assertJsonPath("entry[1].resource.id", "1100,1101,1102", "notIn", response)
def id = response.getJsonPathValue("entry[1].resource.id") assert !(id in ["1100", "1101", "1102"]): "Expected none of the values in [1100, 1101, 1102] for \"entry[1].resource.id\" but encountered \""+id+"\" with operator 'notIn' in response."
def id = response.jsonPath("entry[1].resource.id").value assert isNotIn("1100,1101,1102", id): "Expected none of the values in [1100, 1101, 1102] for \"entry[1].resource.id\" but encountered \""+id+"\" with operator 'notIn' in response."