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

    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");
    	}
     }

     

     

  • nmrao's avatar
    nmrao
    6 years ago

    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?