Cross field validation (if/then/else assertion) on repeating attributes in response
Hey Guys,
Ok - so I'm trying to create an assertion on (yet again) repeating attributes (1 instance per record) where 1 to many records are returned.
HOWEVER - this time I need to assert on some cross field validation - using say if/then/else
So if attribute1 = valueX, assert attribute2 = valueY, if attribute1 = valueZ, assert attribute2 = valueA - so I think the basic if/then/else isn't a problem - but it's getting it to work on repeating attributes.
I want to assert that every instance of 'RegistrationFromDate' is 29th March when 'GeographyIndicationStatusId_Name' is 'Registered' and 'RegistrationFromDate' is null for all other scenarios
the jsonpath to the repeating attributes are as follows:
JSONPATH to RegistrationFromDate = $[data][x][RegistrationFromDate] JSONPATH to GeographyIndicationStatusId_Name = $[data][x][GeographyIndicationStatusId_Name]
I have the following code:
def json = new groovy.json.JsonSlurper().parseText(context.response) if (json.data.GeographyIndicationStatusId_Name == 'Registered') assert json.data.RegistrationFromDate == '2019-03-29 00:00:00.000'; else assert json.data.RegistrationFromDate == null;
rao has introduced me to it and each methods but I'm still learning and the above wont work if there is more than 1 record (so more than 1 instance of those repeating attributes in my resultset) so I tried tweaking it to the following:
def json = new groovy.json.JsonSlurper().parseText(context.response) if (json.data.GeographyIndicationStatusId_Name == 'Registered') assert json.data.RegistrationFromDate.every{'2019-03-29 00:00:00.000' == it} else assert json.data.RegistrationFromDate.every{null == it}
I'm getting an assertion failure when I execute the above and it begins as follows:
assert json.data.RegistrationFromDate.every{null == it} | | | | | | | false | | [2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, null, 2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, null, 2019-03-29 00:00:00.000, null, 2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, 2019-03-29 00:00:00.000, 2019-03-29 00:0
Now I suppose it's good that I'm getting an assertion failure rather than a syntax failure! But obviously its not doing what I want it to do.
Can anyone spot what I'm doing wrong? As always can't thank everyone enough for their help
I hope I've been clear this time! :)
I've attached an example response
thanks to alll!
richie