cancel
Showing results for 
Search instead for 
Did you mean: 

Assert a json value is NOT present in the json response's array?

SOLVED
Community Leader

Assert a json value is NOT present in the json response's array?

Hey!

 

I've been stuck doing strategy on my new job for the last 5 weeks and today is the first time back to looking at SoapUI and my brain has gone blank.

 

I need to assert that a particular json array value isnt present in the response

 

so if response is as follows:

 

{
"Permissions" : [
"ACME:CommodityCode.Read",
"ACME:Notification.Create",
"ACME:Org.Species.Read",
"ACME:CommodityCode.Read",
"ACME:Notification.Create",
"ACME:OrgInfo.Read",
"ACME:Species.Read",
"ACME:OrgInfo.Read",
"ACME:Species.Read"
]
}

a change is submitted altering the response and the new response is as follows:

{
"Permissions" : [
"ACME:CommodityCode.Read",
"ACME:Notification.Create",
"ACME:Org.Species.Read",
"ACME:CommodityCode.Read",
"ACME:Notification.Create",
"ACME:OrgInfo.Read",
"ACME:Species.Read"
]
}

As you can see the 2 array values of

 

 "ACME:OrgInfo.Read",
"ACME:Species.Read"

aren't present in the new response. 

 

I need an assertion that these array values are NOT present to prove the update worked.

 

Before now, I've used an existence check (set result to false) - but this won't work for this - the the json is an array (name of Permissions) with some comma separated values - there's no name value pairing - the Outline tab in the Response appears as follows:

 

 

 ArrayNonExistence.PNG

 

Because the values are in the array, rather than name/value pair - if the response is missing 2 values from the array - it just reduces the number of array values - using jsonpathfinder the jsonpath to any of the array items is as follows:

 

x.Permissions[x] - where x is the array position

 

Can anyone advise please?

 

Many thanks to all!

 

richie

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 title of the post to something more descriptive? This will help people when searching for problems.
2 REPLIES
Highlighted
Contributor

Re: Assert a json value is NOT present in the json response's array?

Hello richie,

 

The json response looks to be a nice list to begin with which allows some collection comparisons.

This groovy code snippet has a couple different assertions based on your exact example. Smiley Happy

 

Regards.

 

 

import groovy.json.*;

def jsonBeforetxt = '''
{
 "Permissions" : [
 "ACME:CommodityCode.Read",
 "ACME:Notification.Create",
 "ACME:Org.Species.Read",
 "ACME:CommodityCode.Read",
 "ACME:Notification.Create",
 "ACME:OrgInfo.Read",
 "ACME:Species.Read",
 "ACME:OrgInfo.Read",
 "ACME:Species.Read"
 ]
}
''';

def jsonAftertxt = '''
{
 "Permissions" : [
 "ACME:CommodityCode.Read",
 "ACME:Notification.Create",
 "ACME:Org.Species.Read",
 "ACME:CommodityCode.Read",
 "ACME:Notification.Create",
 "ACME:OrgInfo.Read",
 "ACME:Species.Read"
 ]
}
''';

def jsonBeforeobj = new JsonSlurper().parseText(jsonBeforetxt);
jsonBeforeobj.Permissions.each { item ->
   log.info 'item=' + item.toString();
};
log.info 'jsonBeforeobj.Permissions.size()= ' + jsonBeforeobj.Permissions.size();
log.info ' ';

def jsonAfterobj = new JsonSlurper().parseText(jsonAftertxt);
jsonAfterobj.Permissions.each { item ->
   log.info 'item=' + item.toString();
};
log.info 'jsonAfterobj.Permissions.size()= ' + jsonAfterobj.Permissions.size();
log.info ' ';

permissionsIntersectList = jsonBeforeobj.Permissions.intersect(jsonAfterobj.Permissions);
permissionsIntersectList.each { intr ->
   log.info 'intersect=' + intr;
};

assert jsonBeforeobj.Permissions.size() != jsonAfterobj.Permissions.size(), "Before and After should be different";
assert jsonAfterobj.Permissions.size() == permissionsIntersectList.size(), "The After result should be equal to the Intersection of the Before and After";

assert jsonBeforeobj.Permissions.count { it.startsWith('ACME:OrgInfo.Read') } == 2, "Original should have 2 entries";
assert jsonAfterobj.Permissions.count { it.startsWith('ACME:OrgInfo.Read') } == 1, "Subsequent should have 1 entry";
assert jsonBeforeobj.Permissions.count { it.startsWith('ACME:Species.Read') } == 2, "Original should have 2 entries";
assert jsonAfterobj.Permissions.count { it.startsWith('ACME:Species.Read') } == 1, "Subsequent should have 1 entry";

log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';

 

Community Leader

Re: Assert a json value is NOT present in the json response's array?

@Todd_Neuschwang

 

thanks so much - I wasn't asking you to do all my work for me - but I'm very, very grateful! Smiley Happy

 

richie

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 title of the post to something more descriptive? This will help people when searching for problems.
Share Your Stories
Read Latest News
Top Kudoed Authors