cancel
Showing results for 
Search instead for 
Did you mean: 

Cross field validation (if/then/else assertion) on repeating attributes in response

Highlighted
Community Hero

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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
6 REPLIES 6
Community Hero

Re: Cross field validation (if/then/else assertion) on repeating attributes in response

I believe that, actual date does not matter. What matters is that are all the values same?

 

Below does that

 

//Assumes json object is defined allread
assert 1 == json.data.findAll{it.'GeographicalIndicationStatusId_Name' == 'Registered'}.collect {it.'RegistrationFromDate'}.unique().size()

Here is the explanation

 

Find all StatusId_Name value is Registered, collect the Registration From Dates which is a list of dates, apply uniqueness on that list and that size should be always equals to one.



Regards,
Rao.
Highlighted
Community Hero

Re: Cross field validation (if/then/else assertion) on repeating attributes in response

Hey @rao

 

Ok -I don't know if my rubbish explanations have confused you - I need to think about your code a bit cos just when I think I can read your code, you introduce new stuff that I can't!

 

Anyway -  (in the meantime whilst I work out exactly how your code snippet works to see if it does what I need) I've tried to clarify the requirement I'm trying to test is to actually copy in the requirement statement below - I've copied this straight from the JIRA user story:

 

Registration From Date = If Geographical Indication Status is 'Registered' then '29 Mar 2019' else Null;

 

Aah! - hold it - my brain's catching up - so using your example I can use two assertion as follows to cover the 29 Mar 2019 and null scenario like as follows:

 

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

assert 1 == json.data.findAll{it.'GeographicalIndicationStatusId_Name' == 'Registered'}.collect {it.'RegistrationFromDate'}.unique().size()
assert 1 == json.data.findAll{it.'GeographicalIndicationStatusId_Name' != 'Registered'}.collect {it.'RegistrationFromDate'}.unique().size()

Just trying this to see if the second assertion works for nulls......

 

I hope I've been clear!

 

thanks!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Cross field validation (if/then/else assertion) on repeating attributes in response

@richie 

Actually there was a bit of confusion for me from below statements of yours.

 

[Statement 1]

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.

 

[Statement 2]

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

 

So, I thought date can be possible to change between responses and that's why I gave that "equals to one". But, that holds still true (irrespective of expected value i.e., null or a date)

 

If you still want to verify along with values as well, then use below snippet of code and pass the values.

 

Here there another advantage is that you change sibling property names / attributes or keys and their values i.e., works generic.

 

//assumes json object is defined already

def areAllSiblingValuesMatching = { key, value, condition -> 
	def actalValues = json.data.findAll(condition).collect {it."$key"}.unique()
	(1 == actalValues.size() && value == actalValues.first() ) ? true : false
}

//verify date value and status name is Registered
assert areAllSiblingValuesMatching('RegistrationFromDate', '2019-03-29 00:00:00.000'){ it.GeographicalIndicationStatusId_Name == 'Registered'}

//verify date value and status name is not Registered
assert areAllSiblingValuesMatching('RegistrationFromDate', null){ it.GeographicalIndicationStatusId_Name != 'Registered'}

 

 



Regards,
Rao.
Highlighted
Community Hero

Re: Cross field validation (if/then/else assertion) on repeating attributes in response

Hey @nmrao 

 

again my attempt at the code looked completely different to yours!

 

Thanks man - it works great - but I have a question - regarding the second assertion below

 

 

//Code courtesy of Rao
//assumes json object is defined already def areAllSiblingValuesMatching = { key, value, condition -> def actalValues = json.data.findAll(condition).collect {it."$key"}.unique() (1 == actalValues.size() && value == actalValues.first() ) ? true : false } //verify date value and status name is Registered assert areAllSiblingValuesMatching('RegistrationFromDate', '2019-03-29 00:00:00.000'){ it.GeographicalIndicationStatusId_Name == 'Registered'} //verify date value and status name is not Registered - THIS IS THE SECOND ASSERTION assert areAllSiblingValuesMatching('RegistrationFromDate', null){ it.GeographicalIndicationStatusId_Name != 'Registered'}

 

 

If there are no records in the dataset where GeographicalIndicationStatusId_Name != 'Registered - I get an assertion failure on this.

 

When I execute the step when the resultset DOES have >=1 row where GeographicalIndicationStatusId_Name != 'Registered' - the step PASSES

 

Is there something I can do to tweak this for scenarios where the resultset does NOT contain records where  GeographicalIndicationStatusId_Name != 'Registered' ???

 

Thanks man - do you fancy a job?  I need some help at my place! 🙂

 

cheers,

 

richie

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Cross field validation (if/then/else assertion) on repeating attributes in response

What is expected in below case? Is the response expected to meet that condition?

If there are no records in the dataset where GeographicalIndicationStatusId_Name != 'Registered - I get an assertion failure on this.

 

Otherwise, it is ok not to have additional assertions which are not expected.



Regards,
Rao.
Highlighted
Community Hero

Re: Cross field validation (if/then/else assertion) on repeating attributes in response

@richie,
Is there anything that is still required in this regard?


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Script Wanted
Let's start July with new challenges!

Create a script to change request body before sending it to the server for the TechCorner tag!


Scripts created in June:
- A script to generate a request body based on data from Datasource
- A script to go through all response nodes and check the value of the required field
- A Dispatching script to get responses based on the values in Request Body
- A script to add a new tag and assign it to all TestCases in a TestSuite
Top Kudoed Authors