Forum Discussion

TheEnd's avatar
TheEnd
Occasional Contributor
6 years ago

Groovy script Assertion

Below is a sample from an XML file from my Response window of my project. I need to create a groovy assertion that will parse through the XML response file and compare the "Event" (looking for the same event name) and then compare the "EventTime" to make sure the time is 10 sec apart. I don't know groovy, so I piecing together a script from youtube and the internet, but I keep having issues. Help will be greatly appreciated...Thanks!

<Response>

<e>
<Tplus>8201.19</Tplus>
<SinceLast>8.29</SinceLast>
<EventTime>2019-12-16T19:35:37.189</EventTime>
<Event>trap:PATS-NE-MIB::patsConfigFileDownloadCompleteTrap"/test_lru_manifest.xml"XX.XX.XX.162</Event>
</e>
<e>
<Tplus>8211.24</Tplus>
<SinceLast>10.05</SinceLast>
<EventTime>2019-12-16T19:35:47.24</EventTime>
<Event>trap:PATS-NE-MIB::patsConfigFileDownloadCompleteTrap"/test_lru_manifest.xml"XX.XX.XX.162</Event>
</e>

<Response>

  • Hey TheEnd 

     

    Ok - firstly - I need to point out - I'm not the best person to comment on this - my groovy is nowhere near as good as the other peoples on here (like avidCodernmrao etc. - they'll be able to do this far quicker than I can. 

    I think I can probably work it out - I've already found a groovy time method that you might need - but the coders I mentioned above would probably do this in pure groovy - whereas what I'd do is extract the values from the response, save them to a properties step and use some groovy (that Rao gave me actually!) to compare the 2 values against each other - the complication with this is that time isn't decimal (I think the phrase is sexagesimal - but dont quote me) - so during the comparison I still need to treat the extracted values saved in the property files as time values and not strings (property values are stored as strings).

     

    I'll start working on this tomorrow - but by the time I've worked out an answer avidCoder or nmrao  will have likely worked out the answer between coffee breaks - for me it'll probably be a days work.

     

    Actually I've changed my mind - I think pure groovy is probably easier

     

    Couple more questions to help guide:

    Q1.  The response - are you saying for Event=x (where polling interval = 10secs) - the response will have numerous records contained - 1 record for each 10second interval since the initial call was made? - cos this makes it more awkward - you'll need to assert that EVERY record is 10 seconds greater than previous - I haven't got any bright ideas on how to do this at this point - maybe extract the lot into an array and parse that - but even then - this is gonna be a problem - every day has 8640 10second intervals - does this mean if you don't run your test for a day - you're gonna have a response with 8640 individual records (considring there's 86400 seconds in a day)?

    OR - are you content considering typical extrapolation rules (number of extrapolates to determine a pattern) maybe only 4 data points is enough to assert against?

     

    WOW! - you really didn't start with an easy one - did you? ;)

     

    As I mention above - I can probably work out how to assert that one eventtime is 10seconds greater than previous but depending on your answer to Q1 - this might exclude me from helping out - leaving it to the other fellas/ladies on here

     

    nice one,

     

    rich

  • richie's avatar
    richie
    Community Hero
    Hey TheEnd!

    You want to put a script assertion together to to do your validation but you're new to ReadyAPI!, right? (I've just seen your other post, so i'm assuming you've realised you can only assert on responses within ReadyAPI!, rather than in an external command window?)

    I'm not saying don't use groovyscript but there might be a way to utilise the embedded functionality rather than require groovyscript which would make it easier for you if you're new to ReadyAPI! an unfamiliar with scripting...

    Either way, can you just explain your use case a little more please?

    If i understand correctly you want some cross field validation so when <Event/> tag value is set to 'x', then you want to assert that <EventTime/> tag's datetime value has incremented by +10 seconds?

    If my previous question's correct, can you describe what's controlling the initiating API call to generate the response? Or put another way, isn't the datetime value populating your <EventTime/> tag determined by yourself when you're making your ReST call? If that's the case then asserting the value's +10 secs since the last call won't have any value as you control the time interval.....or am i misunderstanding something?

    The more info people have, the more likely they'll be able to help/provide you with a solution.

    Cheers!

    Rich
    • TheEnd's avatar
      TheEnd
      Occasional Contributor

      Ok, thanks Rich, let me try to explain this better:

      NOTE: a Test step before this Test step(which I explain below) sets the polling interval (in this example to 10 sec).

      1. Using an HTTP GET request call on a URL

      2. The URL’s response is a list of events (<Event>) that have time stamps (<EventTime>)

      3. These events, Event X, Event Y etc., are no more than polling events. So, if I send a polling interval for Event X to poll every 10 sec, then I want to verify that Event X is polling every 10 sec. Event Y could be polling at a different interval, no problem. I need to verify if I set the polling interval for Event X to be 10 sec, then when I send a GET on the URL, in the response I should see the time stamps for Event X to be 10 sec apart.

      4. So, my Assertion should compare Event X with time stamp X and the next time that Event X polls the time stamp should be 10 sec from the previous Event X time stamp. Verifying polling interval was set correctly.

      5. Example below shows there is a 10 sec diff between the Event X at 19:35:37.189 time and the same Event X at 19:35:47.24

      <Response>

      <e>
      <Tplus>8201.19</Tplus>
      <SinceLast>8.29</SinceLast>
      <EventTime>2019-12-16T19:35:37.189</EventTime>
      <Event>trapPATS-NE-MIB: patsConfigFileDownloadCompleteTrap"/test_lru_manifest.xml"XX.XX.XX.162</Event>
      </e>
      <e>
      <Tplus>8211.24</Tplus>
      <SinceLast>10.05</SinceLast>
      <EventTime>2019-12-16T19:35:47.24</EventTime>
      <Event>trapPATS-NE-MIB:patsConfigFileDownloadCompleteTrap"/test_lru_manifest.xml"XX.XX.XX.162</Event>
      </e>

      <Response>

      6. Trying to verify the polling request was set correctly. If there is a better way than a groovyscript, please point me into that direction. But if the groovyscript is the way please provide code on how to do this and an explanation into what the code is doing, this way I can learn, until I can get more experience with groovy.

      7. To start with, not sure if I should go to the end of the response file or parse through the entire response file?

      Thanks, again, all help is greatly appreciated!

      • richie's avatar
        richie
        Community Hero

        Hey TheEnd 

         

        Ok - firstly - I need to point out - I'm not the best person to comment on this - my groovy is nowhere near as good as the other peoples on here (like avidCodernmrao etc. - they'll be able to do this far quicker than I can. 

        I think I can probably work it out - I've already found a groovy time method that you might need - but the coders I mentioned above would probably do this in pure groovy - whereas what I'd do is extract the values from the response, save them to a properties step and use some groovy (that Rao gave me actually!) to compare the 2 values against each other - the complication with this is that time isn't decimal (I think the phrase is sexagesimal - but dont quote me) - so during the comparison I still need to treat the extracted values saved in the property files as time values and not strings (property values are stored as strings).

         

        I'll start working on this tomorrow - but by the time I've worked out an answer avidCoder or nmrao  will have likely worked out the answer between coffee breaks - for me it'll probably be a days work.

         

        Actually I've changed my mind - I think pure groovy is probably easier

         

        Couple more questions to help guide:

        Q1.  The response - are you saying for Event=x (where polling interval = 10secs) - the response will have numerous records contained - 1 record for each 10second interval since the initial call was made? - cos this makes it more awkward - you'll need to assert that EVERY record is 10 seconds greater than previous - I haven't got any bright ideas on how to do this at this point - maybe extract the lot into an array and parse that - but even then - this is gonna be a problem - every day has 8640 10second intervals - does this mean if you don't run your test for a day - you're gonna have a response with 8640 individual records (considring there's 86400 seconds in a day)?

        OR - are you content considering typical extrapolation rules (number of extrapolates to determine a pattern) maybe only 4 data points is enough to assert against?

         

        WOW! - you really didn't start with an easy one - did you? ;)

         

        As I mention above - I can probably work out how to assert that one eventtime is 10seconds greater than previous but depending on your answer to Q1 - this might exclude me from helping out - leaving it to the other fellas/ladies on here

         

        nice one,

         

        rich