lundi 30 mars 2020

How to verify the the list of keys are present in the Json?

In my API testing I am using Jcontainer to Convert response to Json. Ex:

[Test]
public void GetUsersList()
{
    var response = us.UserList();
    JContainer jsonresponse = rh.ConvertResponseToJson(response);
}

I am trying to the following validation against the Json Verify if all Keys are present (If all keys in json are present, like id, timestamp, type etc..) Here is my json

[
  {
    "id": "aa0db615-d4cb-4466-bc23-0e0083002330",
    "timestamp": "2020-02-11T19:00:00-05:00",
    "type": 33554432,
    "info": "Full Synchronization request for all endpoints",
    "schedule": "once",
    "lastRun": null,
    "flags": 6,
    "creator": null,
    "isEditable": true,
    "location": 0,
    "duration": null
  },
  {
    "id": "70baa28c-e270-447b-b88a-20d30a9542db",
    "timestamp": "2020-02-11T19:00:00-05:00",
    "type": 33554432,
    "info": "Full Synchronization request for all endpoints",
    "schedule": "once",
    "lastRun": null,
    "flags": 6,
    "creator": null,
    "isEditable": true,
    "location": 0,
    "duration": null
  }
]

Here is my Convert respone to Json for reference

 public JContainer ConvertResponseToJson(HttpWebResponse response)
        {
            string localString;

            if (response.ContentEncoding.Contains("application/xml"))
            {
                // Convert the escaped Stream into an XML document.
                ConfigXmlDocument xmlDocument = new ConfigXmlDocument();
                xmlDocument.LoadXml(ConvertResponseStreamToString(response));

                // Now convert the properly-escaped JSON for the response into a JContainer
                localString = JsonConvert.SerializeXmlNode(xmlDocument);
            }
            else
                localString = ConvertResponseStreamToString(response);

            return JToken.Parse(localString) as JContainer;
        }

For now I created a model of the Json to read it by array index. But I am doing mutiple assetions to vaidate all keys. I want to just loop through them. Here is what i have so far

var response = us.UserList();
    JContainer jsonresponse = rh.ConvertResponseToJson(response);
 var castedModel = Jsonresponse.ToObject<IList<Model>>();
            Assert.IsNotNull(castedModel[0].info);  //This is repeated I am trying to avoid this
          Assert.IsNotNull(castedModel[0].task);
           Assert.IsNotNull(castedModel[0].timestamp)

Aucun commentaire:

Enregistrer un commentaire