0

I'm performing load testing on my APIs using Jmeter. I'm making an Http Request and the response of that Http Request is passed as part of request body to my 2nd Http Request. There, I'm passing the dynamic values from a text file as follows :- Obj.txt

"Details": {
    "Start": "2019-08-28T11:39:57.153",
    "Expiry": "2020-08-28T11:39:57.153",
    "Accounts": [
      {
        "accType": "SAVINGS",
        "ReferenceNumber": "${RefNumber}",
        "AccNumber": "${AccNumber}"
      }
    ]
}

This json object is read in my java program. I'm calculating a signature for that json object as follows :-

  public static String signConsent() throws Exception{

        String objDetail = loadDetailsObject();

        String keysJson = "Random String";

        JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(keysJson);

        String signature = sign(objDetail, jsonWebKeySet);

        return signature;
    }

How do I read dynamic values from a text file. I'm loading the textfile using loadDetailsObject function as follows :-

public static String loadDetailsObject()  {
        String details = "";
        try {
            consentDetails =  FileUtils.readFileToString(new File("Obj.txt"), Charset.defaultCharset());
        } catch (IOException e) {
            log.error("Erorr\n", e);
        }
        return details;
    }

The RefNumber and AccNumber will change each time and the signature accordingly will change. How do I read these dynamic values in java from a .txt file?

cyberman123
  • 412
  • 5
  • 14
  • *"How do I read dynamic values from a text file."* - Are you asking how to read a JSON file? A non-JSON text file? Your question is unclear. – Stephen C Jan 17 '20 at 07:06
  • @StephenC I've updated the question. Please check now. – cyberman123 Jan 17 '20 at 07:09
  • You need to decide which format you are using. You are not using both. – Stephen C Jan 17 '20 at 07:18
  • @StephenC Please check now. – cyberman123 Jan 17 '20 at 07:24
  • And what is the **format** of the text file? You cannot read information from a file unless you know what the file format is. Show us an example of the text file you are intending to read. – Stephen C Jan 17 '20 at 07:25
  • @StephenC The text file is the same as mentioned above in `Obj.txt` – cyberman123 Jan 17 '20 at 07:28
  • 1) That is JSON. 2) It doesn't contain the values of "${RefNumber}" and "${AccNumber}". Those of placeholders of some kind. You are really not making a lot of sense here .... – Stephen C Jan 17 '20 at 07:29
  • Try reading this Q&A and see if it answers your question: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Stephen C Jan 17 '20 at 07:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206125/discussion-between-cyberman123-and-stephen-c). – cyberman123 Jan 17 '20 at 07:35

1 Answers1

0

Your "java program" doesn't know anything about JMeter Variables, I would recommend moving your signature code into JSR223 PreProcessor like:

  1. In the HTTP Request sampler's "Body data" use the __eval() and __FileToString() functions combination to read file and at the same time evaluate all nested variables:

    ${__eval(${__FileToString(Obj.txt,,)})}
    

    enter image description here

  2. In the JSR223 PreProcessor use the following code:

    String objDetail = sampler.getArguments().getArgument(0).getValue()
    

    enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133