Forum Discussion

msalvador's avatar
msalvador
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.

 

[
{
"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"
}
]

  • 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...';

     

  • Use jsonslupper and then store in a variable ,and then nevigate till the object then use the condition using if else
    • richie's avatar
      richie
      Community Hero
      Hey msalvador,

      I can probably work out the groovy for this so you can add a script assertion to do this, but it'll take me an hour or two (cos my groovy is rubbish!) whereas ChrisAdams or nmrao could do it in minutes instead.

      If no one's answered this by tomorrow night, i'll look to do it.

      Cheers,

      Rich