1

In rest-assured test cases I am getting mentioned 'ValidatableResponse response' where I want to remove keys such as "updated_at", "deleted_at", "created_at" and "notice" and assert this object with expected string object with contains 'settings' using java 8 lambda expressions.

{ 
    "notice": "The Settings are updated successfully.",
    "settings": {
    "push_notification": {
        "enabled": true,
        "credentials": [{
            "key": "value"
        }],
        "service_name": "API Testing"
    },
    "created_at": "2019-05-04T14:52:32.773Z",
    "deleted_at": "false",
    "updated_at": "2019-05-07T11:23:22.781Z"
}

}

Please help me with creating a common method which can be reuse for all test cases.

3
  • Why specifically using a lambda?
    – anonygoose
    May 17, 2019 at 13:54
  • I would say this is a programming question that should not be voted off topic because managing validations while dealing with dynamic data is an essential skill to testers using automation.
    – Kate Paulk
    May 17, 2019 at 19:24
  • @anonygoose - I would guess it's because lambdas make searching for specific pattern matches much easier. Being able to call response.where(r => r.Data["deleted_at"] == false).single(); (no guarantees on syntax or casing, I'm doing this ad hoc) is much easier than trying to mess with regex or string manipulation.
    – Kate Paulk
    May 17, 2019 at 19:29

1 Answer 1

1

You can convert the JSON string into a JSON Object and then use <your_field>.remove() and get the updated JSON back by converting it back to string.

Below is a small example which demonstrates this:

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

class Scratch {
    public static void main(String[] args) throws JSONException {
        String json = "{\n" +
                "  \"notice\": \"The Settings are updated successfully.\",\n" +
                "  \"settings\": {\n" +
                "    \"push_notification\": {\n" +
                "      \"enabled\": true,\n" +
                "      \"credentials\": [\n" +
                "        {\n" +
                "          \"key\": \"value\"\n" +
                "        }\n" +
                "      ],\n" +
                "      \"service_name\": \"API Testing\"\n" +
                "    },\n" +
                "    \"created_at\": \"2019-05-04T14:52:32.773Z\",\n" +
                "    \"deleted_at\": \"false\",\n" +
                "    \"updated_at\": \"2019-05-07T11:23:22.781Z\"\n" +
                "  " +
                "}\n" +
                "}";

        // Create a JSON object from the json string
        JSONObject jsonObject = new JSONObject(json);

        // Get the object which needs to be manipulated
        JSONObject settings = jsonObject.getJSONObject("settings");

        // Add the attributes which needs to be removed as a list
        ArrayList<String> keysToRemove = new ArrayList<>();
        keysToRemove.add("created_at");
        keysToRemove.add("deleted_at");
        keysToRemove.add("updated_at");

        // Iterate for the all the keys to remove
        for (String key : keysToRemove) {
            // Remove the field from the JSON
            settings.remove(key);
        }

        System.out.println(jsonObject.toString());
    }
}

Output:

{"settings":{"push_notification":{"credentials":[{"key":"value"}],"service_name":"API Testing","enabled":true}},"notice":"The Settings are updated successfully."}

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .