Prabhu
Prabhu

Reputation: 927

How to test rest request and response are same using soap ui?

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?

  1. request and response are same (For Update)
  2. request and response are same except id (For Create)

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

Answers (1)

Rao
Rao

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

enter image description here

Upvotes: 2

Related Questions