Ask a Question

assertion json date check

SOLVED
msalvador
Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
TNeuschwanger
Community Leader

Re: assertion json date check

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

 

View solution in original post

4 REPLIES 4
mrarkapravo
Occasional Contributor

Re: assertion json date check

Use jsonslupper and then store in a variable ,and then nevigate till the object then use the condition using if else
richie
Community Hero

Re: assertion json date check

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
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
msalvador
Contributor

Re: assertion json date check

Thanks

TNeuschwanger
Community Leader

Re: assertion json date check

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

 

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: