SubmitListener.afterSubmit - gather all codes from http400 responses
Hi community.
I have a nice little script that needs some tweaking to get it working properly and would really appreciate your help.
Use Case: I want to verify which http400 Bad Request error codes my testcases cover. From developers I have a list of implemented codes, so it would be nice to see that my test project covers all of these. If not, then I know I miss some scenario's. An example of a bad request results typiacally in this response with one code and one or more errors.code values:
HTTP/1.1 400 Bad Request
{
"code": 1,
"message": "Technical failure.",
"errors": [
{
"code": 51,
"message": "This field is mandatory.",
"description": "Additional description.",
"field": "target[0].type"
},
{
"code": 51,
"message": "This field is mandatory.",
"description": "Additional description",
"field": "target[1].type"
},
{
"code": 83,
"message": "Other message.",
"description": "",
"field": ""
}
]
}
So I am interested in all above code values.
My piece of groovy script for SubmitListener.afterSubmit at event level works fine when I do a run at test step level, but when I run from test case level (or test suite or project) it goes wrong. The context.expand below does no longer returns the expected value 1, but "" skipping the step that pushes this value to the listArray. Same for those at errors.code level. So it seems that depending on the context where the test is run the context.expand is not the same.
The values for my status (responseheader) and for my stepName (submit.request.getName()) does always provide met the correct test tesult I can see in my logs, irregardles of run context. So I assume I have to find a way to replace the context.expand with a submit.getResponse() somehow? But how do I get my code and errors.code values?
Mighty thanks in advance for anyone who might guide me to help this issue!!!
import com.eviware.soapui.support.JsonUtil import groovy.json.JsonSlurper String endpoint = submit.request.getEndpoint() if (endpoint.contains("myEndPointUnderTest.be")) { // errorCodeList is defined as empty array [] in my project setup script def listProperty = context.expand('${#Project#errorCodeList}') def listArray = new JsonSlurper().parseText(listProperty) String status = submit.getResponse().getResponseHeaders() "#status#" //log.info "response for endpoint "+ endpoint + " from submitListener event handler " +status if (status.contains("400")) { def stepName = submit.request.getName() log.info ("stepName: " + stepName) def code = context.expand('${'+stepName+'#Response#$[\'code\']}') if (code != ""){ log.info ("Code to add :" + code) listArray.push(code)} def errors = context.expand('${'+stepName+'#Response#$[\'errors\'][*]}') if (errors != "") { def numberOfErrorCodeArray = JsonUtil.parseTrimmedText(errors) int numberOfErrorCodes = numberOfErrorCodeArray .size() for (i = 0; i < numberOfErrorCodes ; i++) { def errorCode = context.expand('${'+stepName+'#Response#$[\'errors\'][' + i + '][\'code\']}') listArray.push(errorCode) log.info "and for this step : " + stepNaam + " we add following error codes " + errorCode +" to the listArray" log.info "total= " + listArray } } } def step = submit.getRequest() log.info("final listArray = " + listArray) def testcase = step.getTestCase() def testsuite = testcase.getTestSuite() def project = testsuite.getProject() project.setPropertyValue("errorCodeList", listArray.toString())
FYI: In my project teardown I gather all these values, sort and get the unique values:
log.info "------ HTTP400 ERROR CODES covered ------" def listProperty = context.expand('${#Project#errorCodeList}') def listArray = new JsonSlurper().parseText(listProperty) // sort Array listArray.sort() int n = listArray.size() int res = 0 def uniqueList = [] for (i=0;i<n;i++){ while (i< n-1 && listArray[i] == listArray[i+1]){ i++ } res ++ uniqueList.push(listArray[i]) } log.info (res+ " unique (error)codes checked in my project run. This is the full list covered: "+ uniqueList.toString())