Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

Assert every instance of a json attribute Is UNIQUE

Hey,

 

It's more of almost the same from me - I apologise...I'm getting boring.  I need to assert a repeating json attribute always contains a unique value

 

nmrao provided the following code to assert if the repeating DataSourceId_Name attribute was null  (JSONPATH to DataSourceId_Name= $['data'][0]['DataSourceId_Name']

 

assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.DataSourceId_Name.every{null == it}

I know need to assert that the repeating attribute value is always unique.

 

tbh - I thought I'd already asked this a while ago - so apologise if I'm repeating myself - I can't find the post.

 

I did a bit of googling and found the unique() method - I tried messing around with the following:

assert context.response, 'Request parameter is correct'
def json = new groovy.json.JsonSlurper().parseText(context.response)
assert json.data.DataSourceId_Name.every{unique()== it}

Obviously this isn't working - and I haven't a clue how to fix it.

 

I also tried the following:

def json = new groovy.json.JsonSlurper().parseText(context.response)
json.data.Name.each {
assert it == it.unique()
}

but I got the following response for this 

No signature of method: java.lang.String.unique() is applicable for argument types: () values: [] Possible solutions: minus(java.lang.Object), minus(java.util.regex.Pattern), size(), size(), use([Ljava.lang.Object;), use(java.util.List, groovy.lang.Closure)

 

Would anyone be able to steer me what I'm doing wrong??

 

Cheers!

 

richie

  • One way I did this, to assert uniqueness across multiple paginated replies is to make use of a properties step. This way, as I assume you will page through the request multiple times, you can keep an accurate count of how many times a value appears.I had to do this to ensure we were paginating through every object in the cache, and had to keep a running total of how many times any one unique identifier came back. I had 20,000 objects and could only pull 200 at a time, so I kept a running properties step that contained the unique ID and how many times it appeared. If pagination worked as expected, I ended up with 20,000 objects with each having a count of 1. Should be similar to what you are trying. 

     

    Something similar to this hastily written psuedo code:

     
     // Set up a property step to contain our property names and counts.
     def propertiesStep = context.testCase.testSteps["NameCount"]
     
     // get the list of DataSourceId_Name values on current page of values
     DataSourceId_Name= $['data']['DataSourceId_Name']
     
     // Loop through the idNames
     for idName in DataSourecId_Name
     {
     
    	// get the property value from the properties step above.
    	def propertyValue = propertiesStep.getPropertyValue(idName);
    	
    	// if the property has a value, increment it by 1
    	if (propertyValue != null)
    	{
    		propertiesStep.setPropertyValue(idName, (propertyValue.toInteger() + 1).toString());
    	}
    	// if the property does not exist, assign it a count of 1
    	else
    	{
    		propertiesStep.setPropertyValue(idName, "1");
    	}
     }

     

     

  • richie ,

     

    There is no need to create properties step.

     

    Just verify with in Groovy script itself.

     

    def response = context.expand( '${REST Request#Response}' ) 
    def json = new groovy.json.JsonSlurper().parseText(response)
    def totalNamesCount = json.data.TransporterId_Name.size()
    //Below removes the duplicates and does the count
    def uniqueNamesCount = json.data.TransporterId_Name.unique().size()
    // If you want find which name and number of times it occurs, uncomment below block of code
    /*
    def list = json.data.TransporterId_Name.unique()
    list.each { element ->
    	log.info "$element : ${json.data.TransporterId_Name.findAll { it == element}.size()}"
    }
    */
    //Since you wanted all the names are unique, both the counts should be same
    assert totalNamesCount == uniqueNamesCount, 'There are duplicate Transporter Names' 

    Please see the above if you want to check the same?

  • nmrao's avatar
    nmrao
    Champion Level 3

    unique() is applicable to the list object.

    For example, there are duplicate elements in the list. After applying unique, dupes will go.

    def list = [ 'abc', 'def', 'abc']
    assert list.unique() == ['abc', 'def']
    • richie's avatar
      richie
      Community Hero

      Hey nmrao 

       

      ok - so if the unique() method is applicable to the list object - if I'm understanding you're code - I can't use the unique method if I don't specify the values?

       

      that's not what I wanted to hear - ok - do you know an alternative to the unique() method which I don't need to specify the list first off?

       

      I've done some googling - but I can't find an alternative....

       

      Cheers!

       

      richie

      • nmrao's avatar
        nmrao
        Champion Level 3
        Looks like context is missing and data as well. Mor details would help to understand better.