Forum Discussion

martinh3345's avatar
martinh3345
New Contributor
4 years ago
Solved

SoapUI - How to create dynamic array mock responses for REST call

I currently have a REST mock service setup for a POST call that responds with different responses depending on the content of the call, this is all working fine but I need this to be able to handle arrays \ multiple requests and respond with the same sized responses accordingly, I'm wondering if this is possible?

 

My current service works as follows, we send a request as per below...

 

[{
"orderCorrelationId" : "85a64706-49c7-4013-a2d2-cb0502300e2d",
"customerNumber" : "1234",
"testing" : 123,
"lineItems" : [ {
"test_param_1" : "ABC"
}, {
"test_param_1" : "GHI"
} ],
"requestMetaData" : {
"correlationId" : "9333c85d-8bd7-425c-81c8-b6d45b40817a"
}
}]

 

This will respond as follows with the orderCorrelationId being echoed back as part of the response...

 

[{

"orderCorrelationId": "85a64706-49c7-4013-a2d2-cb0502300e2d",
"customerName": null,
"responseStatuses": [
{
"code": "",
"message": "OK",
"severity": "INFO"
}]
}]

 

This all works fine, however if there are multiple orders in the request then the response is singular. I'm looking for a way to make this dynamic so that if we get another order included in the request like...

 

[{
"orderCorrelationId" : "85a64706-49c7-4013-a2d2-cb0502300e2d",
"customerNumber" : "1234",
"testing" : 123,
"lineItems" : [ {
"test_param_1" : "ABC"
}, {
"test_param_1" : "GHI"
} ],
"requestMetaData" : {
"correlationId" : "9333c85d-8bd7-425c-81c8-b6d45b40817a"
}
}, {
"orderCorrelationId" : "a89d13da-064d-4fa2-ad7b-7dfbf8815bbc",
"customerNumber" : "1234",
"testing" : 456,
"lineItems" : [ {
"test_param_1" : "AAA"
}, {
"test_param_1" : "CCC"
} ],
"requestMetaData" : {
"correlationId" : "9333c85d-8bd7-425c-81c8-b6d45b40817a"
}
}]

 

...then the response would also be an array as per below...

 

[{
"orderCorrelationId": "85a64706-49c7-4013-a2d2-cb0502300e2d",
"customerName": null,
"responseStatuses": [
{
"code": "",
"message": "OK",
"severity": "INFO"
}]
},
{
"orderCorrelationId": "a89d13da-064d-4fa2-ad7b-7dfbf8815bbc",
"customerName": null,
"responseStatuses": [
{
"code": "1234",
"message": "Customer is inactive",
"severity": "ERROR"
}]
}]

 

I'm wondering if this is possible? I found some documentation on doing similar using SOAP but not REST. If this is not possible then I will try to build different responses with different array sizes and try using the incoming array size to determine which response to use

 

Any advice or pointers to useful articles would be appreciated, thanks!

  • Thanks

     

    I managed to get this working in the end. I picked up the incoming requests and set them as an array, managed to loop through them and build up a variable containing dynamic responses to each and output the variable as my response. 

     

    Here's my code for anyone interested, not the prettiest but did the job

     

    // Parse the JSON request.
    def jsonrequest = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())

     

    // Capture values from request
    requestContext.echoOrderCorrelationId = jsonrequest.orderCorrelationId

     

    //Define Incoming Array for CorrelationIds 
    def myarray = new ArrayList<String>(requestContext.echoOrderCorrelationId)
    def arraysize = myarray.size()

     

    //set variables for use in while loop
    def count = 0
    String Response = '['
    String Full = ''

    while(count < arraysize)
    {
    //take the correlation ID for item in the loop
    def CorrelationID = myarray.get(count)
    //write the body of the response for the item in the loop
    String Body = '{"orderCorrelationId": "' + CorrelationID + '","customerName": "MOCK Customer","responseStatuses": [{"code": "","message": "OK","severity": "INFO"}]}'

     

    //increment the count
    count++

    //check if last iteration
    if (count < arraysize)
    {
    //add comma to continue adding records
    Full = Response + Body + ','
    Response = Full
    }
    else
    {
    //add ] to finalise response
    Full = Response + Body + ']'
    Response = Full
    }
    log.info "Body = ${Response}"
    }

    //set variable to return for Response Body
    requestContext.ResponseBody = Response

     

    For my response I just default to an OK and use ${ResponseBody} to output my variable

2 Replies

  • martinh3345's avatar
    martinh3345
    New Contributor

    Thanks

     

    I managed to get this working in the end. I picked up the incoming requests and set them as an array, managed to loop through them and build up a variable containing dynamic responses to each and output the variable as my response. 

     

    Here's my code for anyone interested, not the prettiest but did the job

     

    // Parse the JSON request.
    def jsonrequest = new groovy.json.JsonSlurper().parseText(mockRequest.getRequestContent())

     

    // Capture values from request
    requestContext.echoOrderCorrelationId = jsonrequest.orderCorrelationId

     

    //Define Incoming Array for CorrelationIds 
    def myarray = new ArrayList<String>(requestContext.echoOrderCorrelationId)
    def arraysize = myarray.size()

     

    //set variables for use in while loop
    def count = 0
    String Response = '['
    String Full = ''

    while(count < arraysize)
    {
    //take the correlation ID for item in the loop
    def CorrelationID = myarray.get(count)
    //write the body of the response for the item in the loop
    String Body = '{"orderCorrelationId": "' + CorrelationID + '","customerName": "MOCK Customer","responseStatuses": [{"code": "","message": "OK","severity": "INFO"}]}'

     

    //increment the count
    count++

    //check if last iteration
    if (count < arraysize)
    {
    //add comma to continue adding records
    Full = Response + Body + ','
    Response = Full
    }
    else
    {
    //add ] to finalise response
    Full = Response + Body + ']'
    Response = Full
    }
    log.info "Body = ${Response}"
    }

    //set variable to return for Response Body
    requestContext.ResponseBody = Response

     

    For my response I just default to an OK and use ${ResponseBody} to output my variable