Forum Discussion

madden1942's avatar
madden1942
Occasional Contributor
10 years ago

Variable assertions?

I have a rest service that returns inventory status for products.

The inventory status is determined by the code in the software and is not stored in the database and works of the following rules

if availableQuantity >=1 show "Available"
Or
if availableQuantity =0 show "Unavailable".


for reference this is a sample of the JSON response;

{
"InventoryAvailability": [
{
"availableQuantity": "0.0",
"inventoryStatus": "Unavailable",
"onlineStoreId": "10151",
"onlineStoreName": "Store",
"productId": "259008",
"unitOfMeasure": "C62"
},
{
"availableQuantity": "914.0",
"inventoryStatus": "Available",
"onlineStoreId": "10151",
"onlineStoreName": "Store",
"productId": "259010",
"unitOfMeasure": "C62"
}



The issue that i am having is being able to assert for the "inventoryStatus" value based on the "availableQuantity" value using the above rule.

Any help would be fantastic

Many Thanks
Tim (SoapUI Pro 5.1.2)

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    If you are a pro user try posting it in pro boards for faster response.
  • nmrao's avatar
    nmrao
    Champion Level 3
    If you want to assert it with the response, add script assertion and paste below assertion


    import groovy.json.*
    def jsonText = messageExchange.response.responseContent
    def jsonSlurper = new JsonSlurper()
    def object = jsonSlurper.parseText(jsonText)
    object.InventoryAvailability.each{element ->
    log.info element.availableQuantity+":"+element.inventoryStatus
    if (1 <= Float.parseFloat(element.availableQuantity)) {
    assert element.inventoryStatus == "Available" : "Incorrect available status when quantity greater than 1"
    } else {
    assert element.inventoryStatus == "Unavailable" : "Incorrect available status when quantity less than 1"
    }
    }