cancel
Showing results for 
Search instead for 
Did you mean: 

Assert every instance of a json attribute Is UNIQUE

SOLVED
Highlighted
Community Hero

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

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

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

 

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

@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?



Regards,
Rao.

View solution in original post

19 REPLIES 19
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

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']


Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

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

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

Looks like context is missing and data as well. Mor details would help to understand better.


Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

If you want to check every value is unique, then obviously you have the list of values. Isn't it?

And in that case, list should be the same even after applying unique() to the list.

For instance:

def list = [ 'abc', 'def', 'abcd']
assert list.unique() == ['abc', 'def', 'abcd']



Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

@nmrao 

 

I always forget this bit don't it?

 

Ok - I have a .json response (attached) with many possible 1000's of records (pagination is 500 at a time) - and I have a requirement that a certain repeating json attribute - call it TransporterId_Name (JSONPATH = $['data'][0]['TransporterId_Name']) is unique in the returned dataset.

 

I need to assert that the repeating attribute is unique in the returned dataset, so there is only 1 instance of each TransporterId_Name value in the returned records

 

Hence the reason I can't specify the possible values in an array - it would be time prohibitive to specify the possible values

 

Does that help?  Sorry if I'm a little confusing sometimes!

 

Thanks man,

 

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

Have you check the other message in the reply?


Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

Hey @nmrao 

 

Are you talking about your response as follows:

 

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']

Or this one?

If you want to check every value is unique, then obviously you have the list of values. Isn't it?

And in that case, list should be the same even after applying unique() to the list.

For instance:

def list = [ 'abc', 'def', 'abcd']
assert list.unique() == ['abc', 'def', 'abcd']

 

Am I being dumb?  I'm obviously misunderstanding something you stated that the unique() method was peculiar to the list object.

 

In your second response you state "obviously you have a list of values...right?"  but I don't.  That's the point - I don't have a list of values cos the list is hundreds of values as per my .json I attached on the previous message.

 

I was hoping I could use the unique() method to prove a certain repeating attribute contains only unique values WITHOUT actually specifying the list of values first - i.e. I could assert that every instance of a particular attribute that is repeated contains only unique values relative to the rest of the repeated attributes in the .json response.

 

From what you said - I cannot do this - have I understood correctly?

 

Thanks man,

 

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 Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

have I understood correctly? => I belive no.

"to prove a certain repeating attribute contains only unique values WITHOUT actually specifying the list of values first" => When attributes are repeating, so as the values. Repeating values means a list of values. You need not to specify the list of values in advance in the above case, but can collect those value and apply unique() and see the list size is equal before and after applying unique.



Regards,
Rao.
Highlighted
Community Hero

Re: Assert every instance of a json attribute Is UNIQUE

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

 

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors