0

I am new to coding and learning Rest Assured I have saved a JSON file "expectedResponse.json" with content as:

[
  {
    "id": 1,
    "name": "Resident Evil 4",
    "releaseDate": "2005-10-01",
    "reviewScore": 85,
    "category": "Shooter",
    "rating": "Universal"
  },
  {
    "id": 2,
    "name": "Gran Turismo 3",
    "releaseDate": "2001-03-10",
    "reviewScore": 91,
    "category": "Driving",
    "rating": "Universal"
  },
  {
    "id": 3,
    "name": "Tetris",
    "releaseDate": "1984-06-25",
    "reviewScore": 88,
    "category": "Puzzle",
    "rating": "Universal"
  },
  {
    "id": 4,
    "name": "Super Mario 64",
    "releaseDate": "1996-10-20",
    "reviewScore": 90,
    "category": "Platform",
    "rating": "Universal"
  },
  {
    "id": 5,
    "name": "The Legend of Zelda: Ocarina of Time",
    "releaseDate": "1998-12-10",
    "reviewScore": 93,
    "category": "Adventure",
    "rating": "PG-13"
  },
  {
    "id": 6,
    "name": "Doom",
    "releaseDate": "1993-02-18",
    "reviewScore": 81,
    "category": "Shooter",
    "rating": "Mature"
  },
  {
    "id": 7,
    "name": "Minecraft",
    "releaseDate": "2011-12-05",
    "reviewScore": 77,
    "category": "Puzzle",
    "rating": "Universal"
  },
  {
    "id": 8,
    "name": "SimCity 2000",
    "releaseDate": "1994-09-11",
    "reviewScore": 88,
    "category": "Strategy",
    "rating": "Universal"
  },
  {
    "id": 9,
    "name": "Final Fantasy VII",
    "releaseDate": "1997-08-20",
    "reviewScore": 97,
    "category": "RPG",
    "rating": "PG-13"
  },
  {
    "id": 10,
    "name": "Grand Theft Auto III",
    "releaseDate": "2001-04-23",
    "reviewScore": 90,
    "category": "Driving",
    "rating": "Mature"
  }
]

However I want to skip some keys like "releaseDate" from comparison with the response as the date field can sometimes have a different format like "2021-03-13T20:22:49.935Z" (just an example) in response.

Currently I am able to assert the response and Expected JSON file with help of following method:=

public static String readFile(String className, String fileName) throws IOException
    {
        String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\src\\test\\java\\TestData\\"+className+"\\data\\"+fileName)),StandardCharsets.UTF_8);
        return content;
    }


public static void responseAssertionWithFile(Response response, String className , String expResponseFile) throws ParseException, IOException
    {
        
        JSONParser jsonParser = new JSONParser();
        JSONArray respArray = (JSONArray) jsonParser.parse(response.asString());
        JSONArray expjsonArray = (JSONArray) jsonParser.parse(readFile(className,expResponseFile));
        Assert.assertEquals(respArray.size(), expjsonArray.size());
        @SuppressWarnings("unchecked")
        Iterator<JSONObject> respIterator = respArray.iterator();
        @SuppressWarnings("unchecked")
        Iterator<JSONObject> expIterator = expjsonArray.iterator();
        while(respIterator.hasNext() && expIterator.hasNext())
        {
            
            Assert.assertEquals(respIterator.next(), expIterator.next());
        }
    }
Purendra Agrawal
  • 558
  • 1
  • 6
  • 17

1 Answers1

0

Override your assertEquals method and exclude the keys you dont want or include only the keys you want, whatever makes more sense in your usecase.

Something like:

public Boolean asserEquals(JSONObject obj1, JSONObject obj2){
    String[] keysToExclude = {"releaseDate","whatever"};
    for(String key : obj1){
        if(!keysToExclude.contains(key) && obj1.get(key)!==obj2.get(key)){
            return false;
        }
    }
    return true;
}

This is probably not working Java code. I switched languages so much lately I am gettin them all mixed up. Lets call it pseudocode.

UPDATE:

Real Java code with json-simple

       public Boolean assertEquals(JSONObject obj1, JSONObject obj2){
            String[] keysToExclude = {"releaseDate","whatever"};
            Iterator<String> keyIter = obj1.keySet().iterator();
            while(keyIter.hasNext()) {
                String key = keyIter.next();
                if(!contains(keysToExclude,key) && !obj1.get(key).equals(obj2.get(key))) {
                    return false;
                }
            }
            retrun true;
        }

Oh and you will have to implement the contains function yourself. Here is a pointer: How do I determine whether an array contains a particular value in Java?.

  • First of all thank you for your help! If I'll create the next iterator as objects i.e. JSONObject obj1 = expIterator.next(); and similarly JSONObject obj2 = respIterator.next(); Then I get the error in the step: - for(String key : obj1) for the 'obj1' saying "Can only iterate over an array or an instance of java.lang.Iterable" – Aditya Rao Mar 14 '21 at 07:15
  • Yea as I said, thats pseudocode. You have to get the keyset and iterate over that. I dont know what JSON Library you use for Java. I have updated the answer with how it would work with json-simple. –  Mar 14 '21 at 11:30