Reusable assertions for REST error handling in SoapUI
I've been asked to come up with a way to provide reusable assertions for validating standard error responses (HTTP Status Codes 400, 403, etc.) so that those who develop other SoapUI TestCases that encounter errors don't have to code the same validations of HTTP status codes and JSON error response payload validation over and over again.
Since it does not appear to be possible to call one test case from another across projects, I thought the next best thing would be to be to come up with a shared Groovy script library to do these error validations.
Is there a Groovy technique that can be used to invoke the same function that is in the native SoapUI Json Schema Validator Assertion? I imagine something like that can be done using JsonSlurper functionality, but I am not familiar enough with Groovy scripting to code that by myself?
Any help would be much appreciated. Thanks in advance.
I came up with the following which works at the script assertion level.
// Logic for comparing response to JSON schema in Groovy import groovy.json.JsonSlurper def responseStatus = messageExchange.responseHeaders['#status#'][0] if ( responseStatus != 200 ) { // Error code, make sure we got the confirmation message. URL schemaUrl = new URL("https://developers.adp.com/static/Swagger/confirmmessage-schema.json") // Create URL object in Groovy. def jsonSchemaMap = new JsonSlurper().parseText(schemaUrl.text) // Slurp JSON schema data into a Groovy map. def ResponseMessage = messageExchange.response.responseContent // Fetch the JSON payload from the response message. def jsonResponseMap = new JsonSlurper().parseText(ResponseMessage) // Slurp JSON response data into a Groovy map. def schemaKeys = jsonResponseMap.keySet() // Get the list of keys from the JSON schema. def responseKeys = jsonResponseMap.keySet()// Get the list of keys from the JSON response. assert schemaKeys == responseKeys // Assert that the key lists from the JSON schema and the JSON response should match. // This is our indicator that we got a valid confirmation message. } else assert responseStatus == 200 // Otherwise, assert we got the normal expected response (HTTP Status Code = 200).
From here I think it is mainly a matter of packaging things with classes, etc. to prepare for reuse from a script library.