Forum Discussion

Steve-3PO's avatar
Steve-3PO
New Contributor
7 years ago

Transfering values from a REST API response into a list for later use

I’m trying to store a multiple values into a list that is sent back in the response each time I call a "Create Employee" REST API - POST.

 

Example:

I want to create X amount of employees and store the employee ID found in the API response for each employee that is created.

 

I am currently using a Datasource with a list of employees in an excel file and then calling the API for each employee using the DataSourceLoop until all employees are created.

 

Each time the API is called, I would like to take the EmployeeID from the response and store into a list for later user.

 

I can’t seem to find a way of storing multiple values without having to have a property predefined to store it against.

 

As always, any help is very much appreciated. Thank you

3 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    If I understand correctly, you have something that ends up being a map or an array of values, and need to store that. I ran into similar issues where when writing to a properties file or to a data source, the map/array of objects is treated as a string instead of a collection of values.

     

    Unfortunately, without going the route you mentioned where you create X properties for X values, there's no real easy way to do it. You will either have to do that storing the data, or do that in re-hydrating the data.

  • Steve-3PO's avatar
    Steve-3PO
    New Contributor

    You are correct, an array of values is what I am looking for. Every time I call the API and get a response, I would use the data transfer tool to take the employeeId from the response and add to the next empty row.

     

    EmployeeId  
    1  
    2  
    3  
     <- Add next employeeId

     

    I did see the Data Sink tool, where you can specify a target file to store the values in, which I managed to transfer the employeeId to. However, I couldn't see anywhere where I could specify to add the following values to the next empty row. It kept overwriting the previous value.

     

    Any options available I would be happy to look into. Could you explain how I would either store or re-hydrate the data?

     

    Many thanks.

     

     

    • JustinM89's avatar
      JustinM89
      Contributor

      The only way I've found to persist objects throughout the duration of a test is to serialize them to JSON, store that as a test case property, then deserialize back into an object when I need to access it again. Groovy has JSON classes for doing just this, example below:

       

       

      // Check if list already exists as a property, if not create it
      String idListStr = context.expand('${#TestCase#idListStr}')
      List idList
      
      if(!idListStr) {
         idList = new ArrayList()
      } else {
         idList = new JsonSlurper().parseText(listStr)
      }
      
      // Add an ID
      idList.add(someEmployeeId)
      
      // Serialize back to JSON
      idListStr = new JsonBuilder(idList).toString()
      
      // Set as a property again
      testRunner.testCase.setPropertyValue("idListStr", idListStr)