Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

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:

 

 

 

 

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

  • 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. :)

     

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

     

2 Replies

  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 1

    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. :)

     

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

     

    • richie's avatar
      richie
      Community Hero

      TNeuschwanger

       

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

       

      richie