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 particula...
  • TNeuschwanger's avatar
    6 years ago

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