Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

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

  • nmrao's avatar
    nmrao
    Champion Level 3

    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.

    • richie's avatar
      richie
      Community Hero

      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

      • nmrao's avatar
        nmrao
        Champion Level 3

        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'}