Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

Assert a date in format (e.g. MM-DD-CCYY) = date in a format (e.g. CCYY-MM-DDTHH24:MI:SSZ)

Hi,

 

I have a PATCH REST request (json type payload) which contains a requiredByDate attribute with a date value with format MM-DD-CCYY

 

The follow up GET REST json response contains the requiredByDate attribute, but the value is transformed from MM-DD-CCYY to CCYY-MM-DDTHH24:MI:SS format.

 

I need to verify the date is correct with the new date format, but  I've no idea how to do this - I'm guessing a small script assertion might do the job - but I really have no idea how to proceed - although i did have a go....

 

e.g. my example PATCH's payload is as follows:

{
"requiredByDate" : "07-29-2019",
"certifierId" : "123456"
}

my example GET's response payload is as follows:

{
"certifierdecision" : 814250000,
"certifierfullname" : "Robert Smith",
"certifierid" : "123456",
"reasonforrejecting" : null,
"statuscode" : 814250005,
"reasonnotes" : null,
"requiredByDate" : "2019-07-29T00:00:00Z",
"dateofinspection" : "2019-10-15T00:00:00Z",
"certificateitemid" : "65f73d0f-fdef-e911-a813-000d3a4aa8a9"
}


I tried putting a script assertion together against the GET request (after saving the PATCH request's date in a Properties step) knowing I wouldnt get absolutely right - but I wanted to give it a go anyway and this is what I got:

def USdate = context.expand('${Properties#requiredByDate}')
log.info(USdate)
def date = Date.parse("MM-dd-yyyy",USdate)
def UKdate = date.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
log.info(UKdate)

assert USdate == UKdate

Obviously the assertion fails - because I'm saying 07-29-2019 is the same as '2019-07-29T00:00:00'

the results of the logging in the snippet is as follows:

Tue Oct 22 20:01:58 BST 2019: INFO: 07-29-2019
Tue Oct 22 20:01:58 BST 2019: INFO: 2019-07-29T00:00:00Z

Would anyone know how I can assert a date in one format (e.g. MM-DD-CCYY) is equivalent to the date in a different format (e.g. CCYY-MM-DDTHH24:MI:SSZ)?

 

I hope I've been clear in my request

 

Thanks to all!

 

rich