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 update the CSV present in the Virtual Asset

In the 'Incoming Request -> Extension Tool' Groovy script, with the below statement I am able to update the CSV file which is present in my local machine.
File file = new File('C:/Users/filepath/Thirdparty.csv')
file.append(value+'\n')
But with the below statement CSV file which is present in the Virtual Asset is not getting updated.
File file = new File('${project_loc:VirtualAssets}/Thirdparty.csv'')
file.append(value+'\n')
Or
File file = new File('/Thirdparty.csv'')
file.append(value+'\n')
Can you please help me to identify and fix the issue.

Tagged:

Comments

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,
    Data sources are initialize once and changes to file after that will not be picked up until the virtual asset is redeployed. Some data sources, like the Data Repository, offer CRUD actions but most data sources do not.

    What use case are you trying to model? Are you adding a new row? Could you share an example of what you want your service to do?

  • srikanthmummadi
    srikanthmummadi Posts: 39
    edited January 2020

    I have a POST method, which will take ID as a parameter and body will have a JSON array. Based on the ID coming in the request, matching JSON object data needs to be inserted into the CSV file.
    And I have another GET method which will fetch the JSON data based on the ID in the request.
    If CSV file is in my local machine, I am able to write and read the data. I want to move my CSV file inside my VirtualAssets folder and perform the read/write operations.

    Attached the zip file. Need to add one more output, Incoming Request -> Extension Tool (missing in the attached zip file because of licensing restrictions on my personal laptop).

    Extension tool will read the data from incoming request and save it into the CSV file on my local machine.

  • Attached the 'Incoming Request -> Extension Tool' Groovy script.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    What you are looking for is supported using a Data Repository Data Source and the Data Repository CRUD tool. What you want to do won't work with the csv data source because it will be initialized and read only once by the data source. Changes to the file will not be picked up during runtime.

    See this documentation for information about CRUD
    https://docs.parasoft.com/display/SOAVIRT9107/Data+Repository+CRUD+Tool

  • Thanks for the details. I am checking the documentation. On the above example, I am not using the csv data source. I am trying to write the data to a csv file.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,
    If you are not using the data source and are using scripting to load/update the data then instead of a csv file you could just put the values into the context. Here is an example where I check the context for a value based on the incoming id and put new data if I don't find anything.

  • Thank you. I am not using csv data source but I am writing the data to csv so that I can retrieve the data using the GET method. If I put the values in context, can I access those values in the future using GET method?.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,
    I updated the example, to get the values to persist across responders there is an option on the put/get method to put/get the value at a project level. When that option is true then values put from the first responder will be available to the second responder.

  • The read method is always returning the last updated value. And I noticed that after the server restart context values are cleared out.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    Yes the values are stored in memory and accessed from memory. You could alter the example to persist to csv as you originally desired. I just wanted to show you the in memory option.

  • Got it. The actual issue with the example I attached above is, I am not able to write the data to CSV if it is inside my Server Virtual Assets folder.
    I am able to write the data to csv successfully if the file is in my local machine.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Is the issue is that on a remote machine you won't know the path to the virtual assets folder? You could try using a relative path for the file but I am not sure where that would write to.

  • All my .pva files are under Virtual Assets folder in the server. I placed the CSV in the same folder. But it did not read/write to the file, I tried using both relative and absolute paths.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    There is a method on the context that you can use to get a file path to files in Virtual Assets. Use the method context.getAbsolutePathFile("my.csv") and it will return a file object that is based in the VirtualAssets project.

  • Thanks for the details. I used the getAbsolutePathFile as below and it worked.
    File file = new File(context.getAbsolutePathFile("."),"my.csv")

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Glad to hear it worked!