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.

How to store array to writable data source using Extension tool

tsworld
tsworld Posts: 14

I know if I have to retrieve a column from datasource I can do like this
ArrayList list = context.getValues("data","C");

but what to do if I want to store array into the writable datasource from extension tool script

Answers

  • jakubiak
    jakubiak Posts: 807 admin

    There is no way to store values into a writable data source from a script. You typically will do that from an XML Data Bank or JSON Data Bank.

  • benken_parasoft
    benken_parasoft Posts: 1,290 ✭✭✭

    Search the forum for "SOAPUtil.getXMLFromString". It is a static method in SOAtest's scripting API. It can produce an XML document from an array of Strings. You can then chain an XML Data Bank to your Extension tool. The XML Data Bank can be configured to extract a list of text nodes from an XML document and write them to a Writable Data Source.

  • goofy78270
    goofy78270 Posts: 133

    I am also looking for this ability. I would like to be able to capture response start and complete times, but be able to manipulate them afterwards, within a table rather than having to extract them from the console.

    void RequestDuratione( input,  context) {
        reqTime = input.get("XML Request Time").getTime()
        //Application.showMessage("Request Time was :" + reqTime)
        respTime = input.get("XML Response Time").getTime()
        //Application.showMessage("Response Time was :" + respTime)
        Application.showMessage("Execution Time (" + (respTime - reqTime).toString() + "ms)")
    }
    

    Since these fields are not available in a databank extraction, that I am aware of, what would you recommend to be able to capture and manipulate this data?

  • benken_parasoft
    benken_parasoft Posts: 1,290 ✭✭✭
    edited April 2020

    Since these fields are not available in a databank extraction, that I am aware of,

    Did my previous answer make sense? For example:

    import com.parasoft.api.*
    import soaptest.api.*
    
    Object RequestDuratione(Object input, ScriptingContext context) {
        String reqTime = input.get(SOAPUtil.REQUEST_TIME).getTime()
        //Application.showMessage("Request Time was :" + reqTime)
        String respTime = input.get(SOAPUtil.RESPONSE_TIME).getTime()
        //Application.showMessage("Response Time was :" + respTime)
        Application.showMessage("Execution Time (" + (respTime - reqTime).toString() + "ms)")
        return SOAPUtil.getXMLFromString([reqTime, respTime] as String[])
    }
    

    Your Extension tool will return XML that looks something like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
     <z0>1587060211718</z0>
     <z1>1587060211730</z1>
    </root>
    

    You can chain an XML Data Bank to extract each value, those you passed to SOAPUtil.getXMLFromString(),

  • goofy78270
    goofy78270 Posts: 133

    awesome...return it as an xml list, then do the databank from there.

    Sorry @benken_parasoft , I quickly read the responses and thought you were referring to the same fact that jakubiak made about it only working with the databank. I didn't realize you actually detailed how to make this happen. Thanks for pointing out my miss.

  • goofy78270
    goofy78270 Posts: 133

    I also added support for a JSON databank based on our preferences. Hopefully it will help others.

    import com.parasoft.api.*;
    import groovy.json.*;
    import soaptest.api.*;
    
    Object RequestDuratione(Object input, ScriptingContext context) {
        String jsonStr = '{'
    
        testStepName = context.getParentContext()
        //Application.showMessage("Test Step Name is :" + testStepName)
        jsonStr = jsonStr + '"testStepName":"' + testStepName + '",'
    
        reqTime = input.get("XML Request Time").getTime()
        //Application.showMessage("Request Time was :" + reqTime)
        jsonStr = jsonStr + '"reqTime":"' + reqTime + '",'
        respTime = input.get("XML Response Time").getTime()
        //Application.showMessage("Response Time was :" + respTime)
        jsonStr = jsonStr + '"respTime":"' + respTime + '"'
    
        //Application.showMessage("Execution Time (" + (respTime - reqTime).toString() + "ms)")
    
        //For use with JSON DataBank
        jsonStr = jsonStr + '}'
        Application.showMessage("jsonStr is :" + jsonStr)
        jsonOutput = new JsonBuilder(new JsonSlurper().parseText(jsonStr)).toPrettyString()
        return jsonOutput
    
        //For use with XML DataBank
        //return SOAPUtil.getXMLFromString([testStepName, reqTime, respTime] as String[])
    }
    
  • benken_parasoft
    benken_parasoft Posts: 1,290 ✭✭✭
    edited April 2020

    I would avoid string concatenation. Something closer to this:

    import com.parasoft.api.*
    import soaptest.api.*
    import groovy.json.*
    
    String RequestDuratione(Map input, ScriptingContext context) {
        Map data = [:]
        data["testStepName"] = context.getParentContext().toString()
        data["reqTime"] = input.get(SOAPUtil.REQUEST_TIME).getTime()
        //Application.showMessage("Request Time was :" + data["reqTime"])
        data["respTime"]  = input.get(SOAPUtil.RESPONSE_TIME).getTime()
        //Application.showMessage("Response Time was :" + data["respTime"])
        Application.showMessage("Execution Time (" + (data["respTime"] - data["reqTime"]).toString() + "ms)")
        return JsonOutput.toJson(data)
    }
    
  • tsworld
    tsworld Posts: 14

    To make my things work I saved the list to csv file instead of writable datasource. The only issue with csv is that soatest doesn't work when I use workspace path (relative) but works when I use a fixed file system path.