Service Virtualization

Expand all | Collapse all

Need to have UNIX epoch time in one of the response field. How can I achieve that?

Jump to Best Answer
  • 1.  Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-21-2019 12:04 PM

    Hi- I have a requirement where in one of the response field- “"issued_at" should be UNIX epoch time. How can I achieve that? Please suggest.

     

    Response should be-

    {

        "token": "abcde",

        "access": "999999999",

        "issued_at": {{UNIX epoch time}},

        "expires_in": "1234"

    }

     

    Regards,

    Ritu



  • 2.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?
    Best Answer

    Posted 02-21-2019 01:02 PM

    I believe doDateDelta and doDateDeltaFromCurrent use Simple Date Time formats. I do not know if there is a format that produces epoc time.

     

    In a JSR or Scripted Assertion before the Responder Step (or prior to any custom logic that might be performing a parseInState on the VSI response object), generate the epoc:

     

    long epocTime = System.currentTimeMillis() / 1000L;
    testExec.setStateValue("UNIX_epoch_time", Long.toString( epocTime ) );

     

    I don't care for spaces in property names, so in your VSI response example, I would do this: 

      "issued_at": {{UNIX_epoch_time}},



  • 3.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-21-2019 01:55 PM

    Thanks much Joel. I put the solution and validating. Looks good but not able to validate from https://currentmillis.com/ as it refreshes so fast.

     

    I will let you know in case further queries but for now its working. Thanks!!!



  • 4.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-21-2019 02:04 PM
      |   view attached

    Never heard of that site.

    Download this attached test case and run it in ITR mode. 

    If you add a looping dataset to count from 1 to 10 on the Do Nothing step, you can see the property value changing. 

    Copy the epoc property from the ITR panel and paste it into some converter like https://www.epochconverter.com/ 

    Attachment(s)

    zip
    Java_Epoc_Time.tst.zip   1K 1 version


  • 5.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-21-2019 02:28 PM

    Thanks Joel. It was nice. I could validate this. Big thanks!!!



  • 6.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-21-2019 04:25 PM

    And, if you need a 13 digit time field, remove the divide by 1,000.  The divide by 1,000 reduces the time to from 13 to 10 digits.  Was not certain which one you needed.



  • 7.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-21-2019 09:03 PM

    yes Joel. My requirement was to have time in ms and I removed divide 1,000 and its working as expected. Thanks much!!!



  • 8.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-22-2019 04:25 AM

    Hi,

     

    Just wanted to remind that in addition to running scripts in JSR step or scripted assertions it is also possible to add expressions directly in the responses inside the vsi.

     

    Haven't tested it but the below should also work:

    {

        "token": "abcde",

        "access": "999999999",

        "issued_at": {{=System.currentTimeMillis()/1000L;}},

        "expires_in": "1234"

    }

     

    Cheers,

    Danny



  • 9.  Re: Need to have UNIX epoch time in one of the response field. How can I achieve that?

    Posted 02-22-2019 07:56 AM

    +1 DannySaro an excellent point.