Forum Discussion

msalvador's avatar
Frequent Contributor
3 years ago

assertion json date check

Hi guys, I have to check in my assertion if field EndDate is null or date is less then now. I have to put it in my assertions, any idea/suggestion?   here ther is the json sample.   [ { "crea...
  • TNeuschwanger's avatar
    3 years ago

    Hello  msalvador 


    Below is groovy code for your assert that can be dropped into a "Groovy Script" test step.  You can hack it up to meet your needs. 




    import groovy.json.JsonSlurper; 'Test Step "' + + '" start...'; "";
    def String jsonStr = """
        "createDate": "2004-01-01T00:00:00",
        "endDate": null,
        "lastUpdateDate": null,
        "lastUpdateUser": null,
        "instanceCode": "CLAIM",
        "roleCode": "ALL",
        "roleName": "All",
        "roleType": "DEVELOPER"
        "createDate": "2004-01-01T00:00:00",
        "endDate": "2021-01-12T00:00:00",
        "lastUpdateDate": null,
        "lastUpdateUser": null,
        "instanceCode": "CLAIM",
        "roleCode": "BACK_OFFICE",
        "roleName": "Back Office",
        "roleType": "WEB"
    def jsonSlurper = new JsonSlurper();
    def jsonObj = jsonSlurper.parseText(jsonStr); "jsonObj=$jsonObj";
    def nowDate = new Date();
    jsonObj.each { row -> ""; "row=${row}";
       def evalEndDate = new Date(0);   //  set a date way back from now as a proxy for null content
       if (row.endDate) {
          evalEndDate = Date.parse("yyy-MM-dd'T'hh:mm:ss", row.endDate);
       // at this point we have a low date (if it was null) or actual date to evaluate "evalEndDate=$evalEndDate"; "nowDate=$nowDate";   
       assert evalEndDate > nowDate, "Expected EndDate to be greater than now";   // comment this line out to travel the whole json
    }; ""; 'Test Step "' + + '" done...';