Forum Discussion

msalvador's avatar
msalvador
Frequent Contributor
3 years ago
Solved

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
    TNeuschwanger
    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. 

     

    Regards,

    Todd

    import groovy.json.JsonSlurper;
    
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    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);  
    log.info "jsonObj=$jsonObj";
    
    def nowDate = new Date();
    
    jsonObj.each { row ->
       log.info "";
       log.info "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
       log.info "evalEndDate=$evalEndDate";
       log.info "nowDate=$nowDate";   
       assert evalEndDate > nowDate, "Expected EndDate to be greater than now";   // comment this line out to travel the whole json
    };
    
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';