Right now I am using the JSON path function to get the JSON response values like placeid, profileid and then am declaring that as static, because I need to use those variables in clickProfile() and clickPlace() functions. Going forward I will have a lot of values like placeid, profileid, contentid etc and I want to avoid those declaring as static String at the beginning. Instead I want to use the Maps concept here going forward and want to use it from framework Utility. 

I am new to HashMaps. Can someone help me here in implementing the Maps concept here?

public static String placeid;

public static String profileid;

 public void extractplaceId()
 {
  placeid = getJsonPath("results.place_id");
  System.out.println(placeid);
 }

 public void extractpleId()
 {
  profileid = getJsonPath("results.profile_id");
  System.out.println(profileid);
 }

 public Response clickProfile() throws IOException
 {
  config.setProperty("APIendPoints.properties");
  PROFILE_URL = config.getConfig().getProperty("CLICK_PROFILE") + profileid + ".json";
  response = requestSpec.when().get(PROFILE_URL);
  return response;
 }

 public Response clickPlace() throws IOException
 {
  config.setProperty("APIendPoints.properties");
  PLACE_URL = config.getConfig().getProperty("CLICK_PLACE") + placeid + ".json";
  response = requestSpec.when().get(PLACE_URL);
  return response;
 }

Framework Utility:

 public String getJsonPath(String key) 
 {
  String resp = response.asString();
  JsonPath js = new JsonPath(resp);
  return js.get(key).toString();
 }
1

There are 1 best solutions below

0
On BEST ANSWER

I assume that you want something like a pack of all variables that you want to save and retrieve later. My POC below just work correctly for single thread, not sure about multiple threads.

  1. A class to add/get/edit/remove variables. It wraps a Map to do the trick.
public class EnvironmentVariable {
    private static final Map<String, Object> variables  = new HashMap<>();

    public static void add(String key, Object value) {
        variables.put(key, value);
    }

    public static Object get(String key) {
        return variables.get(key);
    }

    public static void edit(String key, Object value) {
        variables.put(key, value);
    }

    public static Object remove(String key) {
        return variables.remove(key);
    }
}
  1. ExtractUtil class to provide functions extract or extractAndSave
public class ExtractUtils {

    public static Object extractFrom(Response res, String query){
        return res.jsonPath().get(query);
    }

    public static void extractAndSave(Response res, String query, String key) {
        Object value = res.jsonPath().get(query);
        EnvironmentVariable.add(key, value);
    }
}
  1. Test client
public class TestClient {

    public Response test() {
        return given().get("https://auto-test-challenges.herokuapp.com/challenge3restassured");
    }

    @Test
    void name() {
        Response res = test();
        ExtractUtils.extractAndSave(res, "data.key1.number", "key1_number");
        given().log().all().get("https://postman-echo.com/get?a=" + EnvironmentVariable.get("key1_number"));
    }

    @Test
    void name2() {
        Response res = test();
        int key2_number = (Integer) ExtractUtils.extractFrom(res, "data.key2.number");
        EnvironmentVariable.add("key2_number", key2_number);
        given().log().all().get("https://postman-echo.com/get?a=" + EnvironmentVariable.get("key2_number"));
    }
}

Because I want my map can save any object, so that I use Map<String,Object>. It requires casting to get value from the map, like int key2_number = (Integer) ExtractUtils.extractFrom(res, "data.key2.number");