Reputation: 927
I am having the following request for create employee
{
"name": "abc",
"dept": "mech",
"dob": "20-Feb-1994"
}
i get response same and additionally id of an employee. i am writing assertion to response for all properties like
"name":"${CreateEmp#Request#$.name}"
Is there any simple way to test the following conditions?
EDIT from Comments:
{
"empid":"4187",
"deptId":"4",
"branchId":"6",
"firstName":"Prabhu",
"lastName":"S",
"emailAddress":"[email protected]",
"contactNumber":"987654321",
"isEnabled":"Y",
"dob":"02/05/1994",
"doj":"03/04/2016",
"fatherName":"XXXX",
"motherName":"YYYY",
"activationCode":"ssp",
"spouseName":"ZZZZ",
"address":"MMMMMM",
"pincode":"123456",
"district":"16",
"dateCreated":"2017-02-21T13:00:24.317Z",
"dateModified":"2017-02-21T13:00:24.317Z",
"status":"0"
}
Upvotes: 1
Views: 662
Reputation: 21359
Here is the groovy script which does the response comparison against the request values
Groovy Script: Use this as Script Assertion
for the request step, so that it can work with dynamically for any request and response instead of fixed json.
//Provide the keys to be ignored
def ignoreKeys = ['empid', 'dateCreated', 'dateModified']
def jsonRequest = context.rawRequest
def jsonResponse = context.response
def reqParsed = new groovy.json.JsonSlurper().parseText(jsonRequest)
def resParsed = new groovy.json.JsonSlurper().parseText(jsonResponse)
def sb = new StringBuffer()
reqParsed.keySet().each { key ->
if (!ignoreKeys.contains(key)) {
reqParsed."$key" == resParsed."$key" ?: sb.append("${key} value does not match; request[${reqParsed."$key"}], response[${resParsed."$key"}]\n")
}
}
if(sb.toString()) {
throw new Error("There are differences between the request values and response values. Details below:\n${sb.toString()}")
}
Here you can try quickly online Demo with the fixed sample you have provided.
Output: Have used different values to domonstrate assert works
Upvotes: 2