CSV to specific JSON format for API Testing using Rest Assured

27 Views Asked by At

I have this requirement of converting a CSV file with 100's of records to JSON to feed to the API for automation testing using RestAssured.

Code reads the CSV file, removes the header line. Then converting into a LinkedHashmap as the JSON file I have is a very specific format. Then using ObjectMapper to write the file out.

package CSVFileRead;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.LinkedHashMap;

public class HashMapTrial {

    public static void main(String[] args) throws IOException {
        String line = "";
        String splitBy = ",";
        String language = null;
        try {
            BufferedReader br = new BufferedReader(new FileReader("src\\test\\java\\CSVFileRead\\Apply CSV.csv"));
            String headerLine = br.readLine(); // This helps omit the header line
            while ((line = br.readLine()) != null)   //returns a Boolean value
            {
                String[] row = line.split(splitBy);    // use comma as separator
                language = row[0];
                System.out.println("Language is " + row[0] + ", Country is " + row[1]);


                //controls
                LinkedHashMap<String, Object> elementControls = new LinkedHashMap<>();
                elementControls.put("Language", language);
                elementControls.put("Country", "GB");
                LinkedHashMap<String, Object> channelIdentification = new LinkedHashMap<>();
                channelIdentification.put("Type", "Digital");
                channelIdentification.put("Source", "SOAPUI");
                channelIdentification.put("Detail", "ILTest");
                elementControls.put("ChannelIdentification", channelIdentification);
                elementControls.put("BusinessIdentifier", "000442");
                LinkedHashMap<String, Object> controls = new LinkedHashMap<>();
                controls.put("Controls", elementControls);


                //asset
                LinkedHashMap<String, Object> elementAsset = new LinkedHashMap<>();
                elementAsset.put("Type", "MV");
                elementAsset.put("SubType", "Car");
                LinkedHashMap<String, Object> vehicleSpecification = new LinkedHashMap<>();
                // LinkedHashMap<String, Object> elementAsset = new LinkedHashMap<>();
                elementAsset.put("DfoPricegross", "250");

                LinkedHashMap<String, Object> allElements = new LinkedHashMap<>();
                allElements.put("Controls", elementControls);
                allElements.put("Asset", elementAsset);
                System.out.println(allElements);

                ObjectMapper mapper = new ObjectMapper();
                try (FileOutputStream fileOutput = new FileOutputStream("File Path")              
                {
                    mapper.writeValue(fileOutput, allElements);
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

The issues I am facing are:

  1. After running the code I only get the latest JSON whereas what I need is each CSV line as a JSON payload and all in different files.
  2. How do I get the CSV to read multiple values from the same column as the JSON contains nested arrays?
0

There are 0 best solutions below