Forum Discussion

KPP's avatar
KPP
New Contributor
4 years ago
Solved

Script assertion to check date time between min and max

I'll appreciate your time and expertise since I'm not sure what is wrong.

Following groovy script works if def expectedMinDateTime is exact as the epoch conversion. However, I was thinking it should pass because assert is given as >.

//imports
import groovy.json.JsonSlurper

// EXPECTED RESULTS - MODIFY HERE >>>>>>>>>>>>>>>>>>>>// 18:00-20:00 is PST in input, to get CST +2hr = 20:00 - 22:00 for server time//We want to test the time is CST because PST is origination
def expectedMinDateTime = '2020-11-03 18:00:00.000Z'
def expectedMaxDateTime = '2020-11-03 20:00:00.000Z'
def attemptNumber = 1
// RNF <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//grab the response
def ResponseMessage = messageExchange.response.responseContent
//defines a JSON Slurper
def jsonResponse = new JsonSlurper().parseText(ResponseMessage)
//verify the slurper is not empty
assert !(jsonResponse.isEmpty())

//get the epoch date
def epochScheduledDateTime = jsonResponse[0].ScheduledAttempt.ScheduledDateTime
log.info(epochScheduledDateTime) // epoch is 1604449835

//convert epoch date to milliseconds in long int else it will be truncated
Date date = new Date((epochScheduledDateTime) * 1000L);
log.info(date)

//format date
def formattedScheduledDateTime = date.format("yyyy-MM-dd HH.mm.ss.SSS Z")
log.info(formattedScheduledDateTime)

assert(formattedScheduledDateTime > expectedMinDateTime) ------------------(this fails as it is looking for exact match)
assert(formattedScheduledDateTime <= expectedMaxDateTime) ------------ (this passes as time is less than max)

I'm trying to get pass if epoch converted time is between 18:00 and 20:00.

  • KPP 

    Why can't you get Min(CST&/ Current Time) and Max(PST(adding 2 hrs)) times in Epoch and compare your response time(as per my understand it's in Epoch).

     

    If this scenarios works for you and I understood your requirement correctly, hope below code helps you.

     

    Code::

    import java.util.concurrent.TimeUnit
    
    long epoch = System.currentTimeMillis() //Gives current time in Epoch in milliseconds
    long epochin2hr = epoch + TimeUnit.MINUTES.toMillis(120) //Adds 120 mins to current time in milliseconds
    
    long responsetime = 1234567890 //get this from your response as like above you did and convert to Millis
    
    assert (responsetime <= epoch && responsetime < epochin2hr) // You can modify first condition(<=) depends on your requirement, like response time should be less than equal or greater than equal to current time.

2 Replies

  • PrathapR's avatar
    PrathapR
    Frequent Contributor

    KPP 

    Why can't you get Min(CST&/ Current Time) and Max(PST(adding 2 hrs)) times in Epoch and compare your response time(as per my understand it's in Epoch).

     

    If this scenarios works for you and I understood your requirement correctly, hope below code helps you.

     

    Code::

    import java.util.concurrent.TimeUnit
    
    long epoch = System.currentTimeMillis() //Gives current time in Epoch in milliseconds
    long epochin2hr = epoch + TimeUnit.MINUTES.toMillis(120) //Adds 120 mins to current time in milliseconds
    
    long responsetime = 1234567890 //get this from your response as like above you did and convert to Millis
    
    assert (responsetime <= epoch && responsetime < epochin2hr) // You can modify first condition(<=) depends on your requirement, like response time should be less than equal or greater than equal to current time.
    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      Thanks for your reply, PrathapR!

      KPP, does this suggestion help? please let us know if you still need help.