Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Lightweight Scripting of Datetime Fields in JSON Responder

alex
alex Posts: 4

Is there a simple way to programmatically alter certain values in an otherwise static JSON response payload with Virtualize?

For example, let's say I've defined something like the following as a literal response in a JSON Message Responder:

{
    "foo": "some static value for foo",
    "timestamp": "1970-01-01T00:00:00Z"
}

I'd like to alter the timestamp property whenever the response is given to represent, for example, the present moment in time. In javascript, something like this could be achieved with the following logic:

response.timestamp = new Date().toISOString();

Is there any lightweight facility in Virtualize that provides such a hook?

By "lightweight", I'm especially looking to avoid calling into the Java Extensibility API as I'm having a hard time groking the documentation. However, if it turns out to be the only way to satisfy this use case, I'd certainly appreciate pointers towards using it.

Best Answer

  • Thomas Moore
    Thomas Moore Posts: 82 admin
    Answer ✓

    Hi Alex,

    If you are using version 9.10.2 of Virtualize, then we added a tool called the Data Generator Tool that you can use to generate timestamps in that manner without scripting.

    In prior versions of the tool, you will need to script the field to send the current date. If you are using javascript, then the following script should work for you:
    function time(){ return new Date().toISOString(); }

    Hope this helps!

Answers

  • jakubiak
    jakubiak Posts: 795 admin

    One thing to note about the script - if the performance is not sufficient, writing the script in Java will achieve much better performance. But the Data Generator Tool is the way to go.

  • alex
    alex Posts: 4

    @jakubiak I imagine you're right about the potential performance gain. But I find working with JSON (which we may need to do a bit more than my example in the original question suggests) is more comfortable than Java. We'll keep it in mind.

  • alex
    alex Posts: 4

    @Thomas Moore Ah. My problem is that I never used a named function. I see now that is what populates the "method" dropdown on the scripting interface. Awesome.

    For reference, here's what I tried before posting here:
    An anonymous function:

    function (){ return new Date().toISOString(); }
    

    Returning the value globally from the script (this prints an error making it obvious that there is an eval() going on somewhere.

    return new Date().toISOString();
    

    Since I guessed the script was being eval'd, I tried relying on Last Expression Evaluated

    var timestamp = new Date().toISOString();
    

    Obviously, none of these worked.

    Maybe I just missed it, but I felt like the documentation was lacking in how to accomplish this.

    Also, I saw this in the docs:

    The Virtualize JavaScript emulation is based on FESI. For details on FESI, visit http://home.worldcom.ch/~jmlugrin/fesi/

    1. http://home.worldcom.ch/~jmlugrin/fesi/ is a dead link
    2. Honestly, for 2017 this implementation seems outdated. In fact, toISOString() is not even available on Date. I will have to polyfill it to achieve my goal. Although, that's proving not to be straightforward. Maybe I'll use Java after all...

    Thanks for the heads up about the Data Generator Tool, but for now I'm on v9.9.4 per corporate policy : )

  • jakubiak
    jakubiak Posts: 795 admin

    Very true that FESI is outdated. In the current version of the product, the default Javascript engine is Oracle Nashorn and the dead link has been removed. :smile: Another option if you want to continue to use JavaScript is to add the Nashorn engine to Virtualize - see the docs about how to add JSR 223 scripting languages to Virtualize.

  • alex
    alex Posts: 4

    @jakubiak Ah! Oracle Nashorn was already available in the Language dropdown. I had never heard of Nashorn before, so I didn't pay any attention to it at first. It's working beautifully. Thank you!