0

I have the following code to update the key "runTimeDate" within my json file.

`final LocalDate plus180Days = LocalDate.now().plusDays(180);
 DateTimeFormatter dateTimeFormatter = 
     DateTimeFormatter.ofPattern("yyyy-MM-dd");
 String substituteDate = plus180Days.format(dateTimeFormatter);
 String jsonFile = "src/examples/sample.json";

 public String Body_ValueDate()
 {
  return jsonFile.replaceAll("\"runTimeDate\"", "\"" + substituteDate + 
               "\"");
 }
 public String Body_KeyDate()
 {
  return Body_ValueDate().replaceAll("\"keyDate\"", "\"" + 
      substituteDate + "\"");
 }`

I used the code above from here: https://stackoverflow.com/questions/7463414/what-s-the-best-way-to-load-a-jsonobject-from-a-json-text-file#:~:text=23-,With%20java%208,-you%20can%20try

Thing is, the date is not getting updated. plus180Days is basically a function adding 180 days from current date. Can anyone share what I am missing here?

final LocalDate plus180Days = LocalDate.now().plusDays(180);

Sample Json

{
  "city": {
  "details": {
  "a1": "AUS",
  "a2": "AUS",
  "country": "AUS"
  }
},
 "detail": {
    "getCountryDetail": {
        "b1": "SYD",
        "b2": "MEL",
        "country": {
            "keyDate|AUS|1234|SYD|MEL": {
                "date1": "runTimeDate",
                "time1": "15:38",
                "date2": "runTimeDate",
                "time2": "19:13"
                 },
            "keyDate|AUS|1234|ADL|MEL": {
                "date1": "runTimeDate",
                "time1": "15:38",
                "date2": "runTimeDate",
                "time2": "19:13"
                 }
               }
             }
           }
         }

I am using the updated json method "Body_KeyDate" as body parameter for my rest assured method as per here:

@Test
public void UA_Avail_Cascading_Request()
  throws IOException
{
 Response response = RestAssured.given()
    .header(readConfigFile())
    .contentType("application/json")
    .body(Body_KeyDate())
    .when()
    .post(readBaseUrl() + "samplePage")
    .then().statusCode(200)
    .log().all()
    .extract().response();
   }

Following solution worked: with ObjectMapper

public JsonNode RequestBody_DynamicDate() throws Exception
  {
  String requestBodyJson = loadJson("Sample.json");
  String removeKeyDate = requestBodyJson.replace("keyDate", NEW_DATE);
  String removeValueDate = removeKeyDate.replace("runTimeDate", NEW_DATE);
  JsonNode processedNewDates = OBJECT_MAPPER.readTree(removeValueDate);
  System.out.println("New JSON Body: "
    + OBJECT_MAPPER
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString(processedNewDates));
  return processedNewDates;
}

Here is the code for loadJson: BASEPATH is the folder location

String loadJson(String filePath)
  {
    try
    {
      return new String(Files.readAllBytes(Paths.get(BASEPATH + filePath)));
    }
    catch (Exception e)
    {
      Assertions.fail("unable to load test data, check file path, and format");
      throw new RuntimeException();
    }

Hope this helps someone. Also turns there was some restrictions and limitations on which library and packages can be used.

gg_man
  • 11
  • 3
  • It's helpful if you can provide code of `plus180Days` and a sample of json file to help us reproduce issue in our ends. – lucas-nguyen-17 Sep 27 '22 at 02:52
  • @lucas-nguyen-17 I have added the code for plus180Days and sample json file. Thank you! – gg_man Sep 27 '22 at 03:07
  • @gg_man It seems that "runTimeDate" in your JSON sample is a _Value_. But JsonObject **remove** and **put** are operate on _Key_. – Raymond Choi Sep 27 '22 at 03:13
  • Oh, do you want to update `"date1": "runTimeDate"` -->`"date1": "2023-03-27"` ? Look like you remove the wrong one. `JSONObject ` is a map actually, you have to remove key, not value. In this case, I think you can simple use String method `replaceAll()` to update from `runTimeDate` to `2023-03-27`. – lucas-nguyen-17 Sep 27 '22 at 03:14
  • @lucas-nguyen-17 I updated the method to this `return jsonFile.replaceAll("runTimeDate", substituteDate);` but getting 400 - I'm assuming that code is not reaching to the right element. – gg_man Sep 27 '22 at 03:26
  • You can use `RestAssured.given().log().all()...` to print request information to console, then check that was it updated correctly or not. – lucas-nguyen-17 Sep 27 '22 at 03:33
  • @lucas-nguyen-17 I already am using that but its not printing anything. Also does replaceAll's first parameter be regex, passing String within "" should work right? – gg_man Sep 27 '22 at 03:38
  • 1
    @gg_man You also replaced key 'runTimeDate|AUS|1234|SYD|MEL'. Try `replaceAll("\"runTimeDate\"", "\"" + substituteDate + "\"")`. – Raymond Choi Sep 27 '22 at 03:38
  • @RaymondChoi lol, I'd like to change the `runTimeDate` to another placeholder text to avoid complexity. Anw, good catch! – lucas-nguyen-17 Sep 27 '22 at 03:43
  • @RaymondChoi I tried that too, same result - 400. I thought that since one of the dates is a Key and also couple of it as a Value so wondering if that is causing it. Also, all the appearance of runTimeDate should be updated. – gg_man Sep 27 '22 at 03:50
  • @RaymondChoi @lucas-nguyen-17 I updated the key with `keyDate|AUS|1234|SYD|MEL` and trying to update with JSONObject while keeping the value to "runTimeDate" but it is still giving 400. Trying to get the ouput printed. – gg_man Sep 27 '22 at 04:05
  • @gg_man Good, keep going. BTW, do you still get 400 if not replacing anything? – Raymond Choi Sep 27 '22 at 05:27
  • @RaymondChoi sadly yes, still getting 400. – gg_man Sep 27 '22 at 06:13
  • @gg_man Could you provide the correct request body to make request successful? – lucas-nguyen-17 Sep 27 '22 at 07:38
  • @RaymondChoi I updated the main code in main question with the changes to code and sample json. – gg_man Sep 28 '22 at 01:08
  • @gg_man I mean that your final expected request body, not the sample json you want to edit. – lucas-nguyen-17 Sep 28 '22 at 05:06
  • @lucas-nguyen-17 btw: as from the json "keyDate" is part of a bigger String, do you reckon when I'm replacing it is replacing the whole thing and not just "keyDate"? – gg_man Sep 28 '22 at 22:59

0 Answers0