BruceyBandit
BruceyBandit

Reputation: 4324

How to compare a value with a list of values

I am trying to perform an assertion where every instance of a region id matches the region id that's put through in the request. As an example, if in the request put in the number '736', then for each instance of region id within the response they should all be '736'. However when i perform at the asset, it states it's false and i beleive it is because the output is within a list so the == does not work. How can i get the request and each item in the list form the response to match?

Below is the code:

def hotelregionid = json.regions.hotels.regionId
assert hotelregionid != null
def hotelregionid_request = messageExchange.modelItem.testStep.testCase.testSuite.getPropertyValue("regionid")
assert hotelregionid.every {it == hotelregionid_request} -- this is where the assertion fails.

log.info hotelregionid
log.info hotelregionid_request

The error message it outputs is:

assert hotelregionid.every {it == hotelregionid_request} | | | false [[xxx,xxxx,xxxx,xxxx,xxx,xxx,xxx,xxx,xxx]]

The log.info provided when i comment out the assert is the same number which I will value as xxx.:

[[xxx, xxx, xxx, xxx, xxx,xxx,xxx]] - this is for hotelregionid

xxx - this is for hotelregionid_request

RAW RESPONSE:

{
    "xxx": [{
        "regionId": xxx,
        "xxx": [{
                "xxx": [{

                    "regionId": xxx

                }],

                "xxx": xxx
            },

            {
                "xxx": [{
                    "regionId": xxx
                }],
                "xxx": xxx
            }
        ]

    }]

Thank you

Upvotes: 0

Views: 62

Answers (2)

Rao
Rao

Reputation: 21359

Here is the script assertion.

Note that define the expected value in the below variable.

EDIT: updated answer based on OP's full response here

//Change the value as needed
def expectedRegionId = 736
assert context.response, 'Response is null or empty'

def json = new groovy.json.JsonSlurper().parseText(context.response)

def sb = new StringBuffer()
json.regions.each { region ->
    assert region.regionId == expectedRegionId
    region.hotels.each { hotel ->
        assert hotel.regionId == expectedRegionId
    }
}

Upvotes: 1

tim_yates
tim_yates

Reputation: 171054

From your output, hotelregionid is a List of Lists

So you can use flatten to get it back to just a List of integers:

assert hotelregionid.flatten().every {it == hotelregionid_request}

Upvotes: 0

Related Questions